From ecab5b2926947c12b67200a2bcab6591cd53ab5b Mon Sep 17 00:00:00 2001 From: Ilia Peterov Date: Mon, 20 Jan 2025 03:29:05 +0400 Subject: [PATCH] chore: implement API test examples for the UpdateBot mutation --- aiarena/graphql/tests/test_mutations.py | 160 ++++++++++++------------ 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/aiarena/graphql/tests/test_mutations.py b/aiarena/graphql/tests/test_mutations.py index 2f72d6ae..96394e9e 100644 --- a/aiarena/graphql/tests/test_mutations.py +++ b/aiarena/graphql/tests/test_mutations.py @@ -1,82 +1,82 @@ -from aiarena.core.models import Bot, WebsiteUser -from aiarena.core.models.bot_race import BotRace - - -""" -Example test cases for mutations. -Please update as needed. -""" - - -def test_update_bot_success(): - """ - Test updating a bot with all fields set to True. - """ - # Create required objects - user = WebsiteUser.objects.create_user(username="test_user", password="test123") - BotRace.create_all_races() - bot = Bot.objects.create( - user=user, - name="My Bot", - bot_zip_publicly_downloadable=False, # Should change False -> True in this test - bot_data_enabled=False, # Should change False -> True in this test - bot_data_publicly_downloadable=False, # Should change False -> True in this test - plays_race=BotRace.terran(), - ) - - # TODO: Prepare and submit mutation under user's credentials - - # TODO: assert no errors occurred - - # Verify bot was updated correctly - bot.refresh_from_db() - assert bot.bot_zip_publicly_downloadable is True - assert bot.bot_data_enabled is True - assert bot.bot_data_publicly_downloadable is True - - -def test_update_bot_unauthorized(): +from aiarena.core.tests.base import GraphQLTest +from aiarena.graphql import BotType + + +class TestUpdateBot(GraphQLTest): + mutation = """ + mutation($input: UpdateBotInput!) { + updateBot(input: $input) { + bot { + id + } + } + } """ - Test updating a bot that does not belong to the user. - """ - # Create required objects - owner = WebsiteUser.objects.create_user(username="owner", password="test123") - other_user = WebsiteUser.objects.create_user(username="other", password="test123") - BotRace.create_all_races() - bot = Bot.objects.create( - user=owner, - name="My Bot", - bot_zip_publicly_downloadable=False, # Should NOT change False -> True in this test - plays_race=BotRace.terran(), - ) - - # TODO: Prepare and submit mutation under other_user's credentials with bot.bot_zip_publicly_downloadable == True - - # todo: Assert error was returned - "This is not your bot" - - # Optionally, verify bot was not updated - bot.refresh_from_db() - assert bot.bot_zip_publicly_downloadable is False - - -def test_update_bot_unauthenticated(): - """ - Test updating a bot without being authenticated. - """ - # Create test user and bot - user = WebsiteUser.objects.create_user(username="test_user", password="test123") - BotRace.create_all_races() - bot = Bot.objects.create( - user=user, - name="My Bot", - bot_zip_publicly_downloadable=False, # Should NOT change False -> True in this test - plays_race=BotRace.terran(), - ) - - # TODO: Prepare and submit unauthenticated mutation with bot.bot_zip_publicly_downloadable == True - - # todo: Assert error was returned - "You are not signed in" - # Optionally, verify bot was not updated - bot.refresh_from_db() - assert bot.bot_zip_publicly_downloadable is False + def test_update_bot_success(self, user, bot): + """ + Test updating a bot with all fields set to True. + """ + + # We expect the bot fixture to be created with those values + assert bot.user == user + assert bot.bot_zip_publicly_downloadable is False + assert bot.bot_data_enabled is False + assert bot.bot_data_publicly_downloadable is False + + self.mutate( + login_user=user, + expected_status=200, + input={ + "id": self.to_global_id(BotType, bot.id), + "botZipPubliclyDownloadable": True, + "botDataEnabled": True, + "botDataPubliclyDownloadable": True, + }, + ) + + # Verify bot was updated correctly + bot.refresh_from_db() + assert bot.bot_zip_publicly_downloadable is True + assert bot.bot_data_enabled is True + assert bot.bot_data_publicly_downloadable is True + + def test_update_bot_unauthorized(self, user, other_user, bot): + """ + Test updating a bot that does not belong to the user. + """ + # We expect the bot fixture to be created with those values + assert bot.user == user + assert bot.bot_zip_publicly_downloadable is False + + response = self.mutate( + login_user=other_user, + input={ + "id": self.to_global_id(BotType, bot.id), + "botZipPubliclyDownloadable": True, + }, + ) + self.assert_graphql_error_like(response, "This is not your bot") + + # Verify bot was not updated + bot.refresh_from_db() + assert bot.bot_zip_publicly_downloadable is False + + def test_update_bot_unauthenticated(self, bot): + """ + Test updating a bot without being authenticated. + """ + # We expect the bot fixture to be created with those values + assert bot.bot_zip_publicly_downloadable is False + + response = self.mutate( + input={ + "id": self.to_global_id(BotType, bot.id), + "botZipPubliclyDownloadable": True, + }, + ) + self.assert_graphql_error_like(response, "You are not signed in") + + # Optionally, verify bot was not updated + bot.refresh_from_db() + assert bot.bot_zip_publicly_downloadable is False