from django.test import TestCase from accounts.models import CustomUser from animations.models import PuzzlePointsFactor, PuzzlePointsValue from events.models import Event from games.models import EventGame, Game from .api import OPUS_MAGNUM_APP_ID from .models import PuzzleResponse, SteamCollection, SteamCollectionItem, Submission URL = "/api/events/event/opus-magnum/points.csv" class PointsCsvTests(TestCase): def setUp(self): self.event = Event.objects.create(name="Event", path="event") game = Game.objects.create(name="Opus Magnum", steam_app_id=OPUS_MAGNUM_APP_ID) EventGame.objects.create(event=self.event, game=game) collection = SteamCollection.objects.create( event=self.event, steam_id="1", url="https://example.com", is_active=True ) factor = PuzzlePointsFactor.objects.create(cost=1, cycles=1, area=1) value = PuzzlePointsValue.objects.create(points=[10, 5, 1]) self.p1, self.p2 = ( SteamCollectionItem.objects.create( collection=collection, steam_item_id=str(i), title=f"Puzzle {i}", order_index=i, points_factor=factor, points_value=value, ) for i in (1, 2) ) self.alice = CustomUser.objects.create_user(username="alice", password="x") self.bob = CustomUser.objects.create_user(username="bob", password="x") self.admin = CustomUser.objects.create_user( username="admin", password="x", is_staff=True ) def respond(self, user, puzzle, score): submission = Submission.objects.create(event=self.event, user=user) PuzzleResponse.objects.create( submission=submission, puzzle=puzzle, puzzle_name=puzzle.title, validated_cost=score, validated_cycles=0, validated_area=0, ) def test_requires_staff(self): self.client.force_login(self.alice) response = self.client.get(URL) self.assertEqual(response.status_code, 401) def test_points_per_puzzle(self): self.respond(self.alice, self.p1, 10) self.respond(self.bob, self.p1, 20) self.respond(self.bob, self.p2, 30) self.client.force_login(self.admin) response = self.client.get(URL) self.assertEqual( response.content.decode().splitlines(), [ "username;Puzzle 1;Puzzle 2", "bob;5;10", "alice;10;0", ], )