class EventSolveDropdownTestCase(APITestCase): """ New event with descriptions for solve with dropdown. """ fixtures = ['event-solve-dropdown.json'] GAME_PK = 1 def setUp(self): self.client = JClient() def tearDown(self): self.client = None def authenticate(self, username, pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) def test_convert_challengue_description(self): self.authenticate('*****@*****.**') response = self.client.get('/api/clue/my-clues/{0}/'.format(self.GAME_PK), {}) self.assertEqual(response.status_code, 200) qregex = re.compile("#\[[\d]+\]\[([^#]*)\]") for res in response.json(): desc = res.get('challenge').get('desc') self.assertFalse(qregex.search(desc)) def test_convert_game_description(self): self.authenticate('*****@*****.**') response = self.client.get('/api/event/my-events/', {}) self.assertEqual(response.status_code, 200) qregex = re.compile("#\[[\d]+\]\[(?:option|text)\]\[([^#]*)\]") for res in response.json(): desc = res.get('game').get('desc') self.assertFalse(qregex.search(desc)) def test_get_possible_solutions(self): self.authenticate('*****@*****.**') response = self.client.get('/api/event/{}/'.format(self.GAME_PK), {}) self.assertEqual(response.status_code, 200) solution = response.json().get('solution') self.assertTrue(isinstance(solution, list)) self.assertTrue(isinstance(solution[0], dict)) self.assertEqual(sorted(solution[0].keys()), ["answers", "question", "type"])
class EventTasksTestCase(APITestCase): """ Test tasks for check taht work well """ fixtures = ['player-test.json', 'event.json'] def setUp(self): self.client = JClient() self.event2 = Event.objects.get(pk=2) self.event2.start_date = timezone.now() - timezone.timedelta(hours=2) self.event2.end_date = timezone.now() + timezone.timedelta(hours=2) self.event3 = Event.objects.get(pk=3) self.event3.start_date = timezone.now() - timezone.timedelta(hours=2) self.event3.end_date = timezone.now() + timezone.timedelta(hours=2) self.ini_players = Player.objects.count() def authenticate(self, username, pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) def get_member_players(self, event): return Membership.objects.filter(event=event).count() def get_playing_players(self, event): return PlayingEvent.objects.filter(event=event).count() def get_need_players(self, event): return event.game.challenges.count() - event.players.count() def test_manage_ais_not_change(self): """ test manage_ais fails: * event is None * event have date out of current date * event haven't place """ event1 = Event.objects.get(pk=1) ini_member_players = self.get_member_players(event1) ini_playing_players = self.get_playing_players(event1) manage_ais(None) manage_ais(event1) event1.start_date = timezone.now() - timezone.timedelta(hours=2) event1.end_date = timezone.now() + timezone.timedelta(hours=2) manage_ais(event1) end_players = Player.objects.count() end_member_players = self.get_member_players(event1) end_playing_players = self.get_playing_players(event1) self.assertEqual(self.ini_players, end_players) self.assertEqual(ini_member_players, end_member_players) self.assertEqual(ini_playing_players, end_playing_players) def test_manage_ais_fill_event(self): """ Check that add players and these are add like member and like playing in event """ ini_member_players = self.get_member_players(self.event2) ini_playing_players = self.get_playing_players(self.event2) need_players = self.get_need_players(self.event2) manage_ais(self.event2) end_players = Player.objects.count() end_member_players = self.get_member_players(self.event2) end_playing_players = self.get_playing_players(self.event2) self.assertEqual(self.ini_players + need_players, end_players) self.assertEqual(ini_member_players + need_players, end_member_players) self.assertEqual(ini_playing_players + need_players, end_playing_players) def test_join_player_sustitute_ia(self): """ Fill event with ai players, and join new player that sustitute onw ai. """ available = self.event3.max_players - self.event3.players.count() manage_ais(self.event3, amount=available) self.assertEqual(self.event3.max_players, self.event3.players.count()) self.authenticate('test1') response = self.client.post('/api/event/join/{0}/'.format(self.event3.pk), {}) self.assertEqual(response.status_code, 201) def test_manage_ais_amount(self): """ Check that add players and these are add like member and like playing in event. And You can't create more players than max_players in event. """ ini_member_players = self.get_member_players(self.event2) ini_playing_players = self.get_playing_players(self.event2) need_players = 2 manage_ais(self.event2, amount=need_players) end_players = Player.objects.count() end_member_players = self.get_member_players(self.event2) end_playing_players = self.get_playing_players(self.event2) self.assertEqual(self.ini_players + need_players, end_players) self.assertEqual(ini_member_players + need_players, end_member_players) self.assertEqual(ini_playing_players + need_players, end_playing_players) def test_manage_ais_amount_max_players(self): """ Check that add players and these are add like member and like playing in event """ ini_member_players = self.get_member_players(self.event2) ini_playing_players = self.get_playing_players(self.event2) need_players = 20 available = self.event2.max_players - self.event2.players.count() players_are_created = need_players if available >= need_players else available manage_ais(self.event2, amount=need_players) end_players = Player.objects.count() end_member_players = self.get_member_players(self.event2) end_playing_players = self.get_playing_players(self.event2) self.assertEqual(self.ini_players + players_are_created, end_players) self.assertEqual(ini_member_players + players_are_created, end_member_players) self.assertEqual(ini_playing_players + players_are_created, end_playing_players)
class PlayerEventTestCase(APITestCase): """ Inside event.json fixture have: - 3 event, 3 game and 17 challenges: - general event (pk=1) with general game (pk=1) with general challenge (pk=1). Max_player = 0 - event 2 (pk=2) with game 2 (pk=2) with challenges (pk=[2,3,4,5,6,7,8]). Event have solution, and Challenges no have solutions. Max_player = 3 - event 3 (pk=3) with game 3 (pk=3) with general challenge (pk=[9,10,11,12,13]). Event and Challenges have solution. Max_player = 10 - membership: player 3 ('test3') in event 3 player 5 ('test5') in event 1 and 3 all player are in event 4: 4 inside and 1 outside place In event 3: player 1 is near to player 2 and player 3 player 1 is near to player 5 but player 5 is outside place player 1 is far player 4 """ fixtures = ['player-test.json', 'event.json'] def setUp(self): self.client = JClient() def tearDown(self): self.client = None def authenticate(self, username, pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) def test_players_near_in_event2(self): event_pk = 4 near_player4_ev4 = 0 self.authenticate('test4') response = self.client.get('/api/player/near/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 200) players = [d for d in response.json() if not d.get('ptype') is 'ai'] self.assertEqual(len(players), near_player4_ev4) def test_players_near_in_event3(self): event_pk = 4 self.authenticate('test5') response = self.client.get('/api/player/near/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), 'Your player is outside of place.') def test_players_near_in_unauth_event(self): event_pk = 2 self.authenticate('test5') response = self.client.get('/api/player/near/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 401) def test_players_meeting_in_event(self): event_pk = 4 player_pk = 2 self.authenticate('test1') response = self.client.post('/api/player/meeting/{0}/{1}/'.format(player_pk, event_pk), {}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {'status': 'step1'}) def test_players_meeting_in_event_unauth_event(self): event_pk = 2 player_pk = 3 self.authenticate('test5') response = self.client.post('/api/player/meeting/{0}/{1}/'.format(player_pk, event_pk), {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), 'Unauthorized event') def test_players_meeting_in_event_with_player2_outside_event(self): event_pk = 1 player_pk = 3 self.authenticate('test5') response = self.client.post('/api/player/meeting/{0}/{1}/'.format(player_pk, event_pk), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'Other player not join at this event') def test_players_meeting_in_event_with_himself(self): event_pk = 3 player_pk = 5 self.authenticate('test5') response = self.client.post('/api/player/meeting/{0}/{1}/'.format(player_pk, event_pk), {}) self.assertEqual(response.json(), 'narcissistic: you cannot connect with yourself') self.assertEqual(response.status_code, 400)
class PlayingEventTestCase(APITestCase): """ Two events and general events player 1 and 2 in None event player 3 and 4 in event 1 player 5 in event 2 Players in the same current event, can be see it. Players in the different current event, can't be see it. """ fixtures = ['player-test.json', 'event.json', 'playing_event.json'] def setUp(self): self.event1 = 1 self.event2 = 2 self.client = JClient() def tearDown(self): self.client = None def authenticate(self, username, pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) def test_players_playing_event_with_event_none(self): """ Player 1 and 2 in event None: visible Player 1 and 3 in event None: no visible """ username = '******' self.authenticate('test1') response = self.client.get('/api/player/near/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0].get('username'), username) def test_players_playing_event_with_event_id(self): """ Player 3 and 4 in event 1: visible. Player 3 is far to player 4 Player 3 and 5 in event 1: no visible """ event = Event.objects.get(pk=self.event1) prev_vd = event.vision_distance event.vision_distance = 9999 event.save() self.authenticate('test3') response = self.client.get('/api/player/near/{0}/'.format(self.event1), {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) username = '******' self.assertEqual(response.json()[0].get('username'), username) event.vision_distance = prev_vd event.save() def test_playing_event_not_exits(self): self.authenticate('test1') response = self.client.post('/api/event/current/{0}/'.format(self.event1), {}) self.assertEqual(response.status_code, 200) response = self.client.post('/api/event/current/', {}) self.assertEqual(response.status_code, 200) self.authenticate('test2') response = self.client.post('/api/event/current/1/', {}) self.assertEqual(response.status_code, 201) def test_playing_event_exits(self): self.authenticate('test5') response = self.client.post('/api/event/current/{0}/'.format(self.event1), {}) self.assertEqual(response.status_code, 200) response = self.client.post('/api/event/current/', {}) self.assertEqual(response.status_code, 200)
class MeetingTestCase(APITestCase): fixtures = ['meeting-test.json'] PLAYER2_PK = 2 def setUp(self): self.pwd = 'qweqweqwe' self.client = JClient() def tearDown(self): self.client = None def authenticate(self, username, pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) @classmethod def get_username(cls, pk): return Player.objects.get(pk=pk).user.username @classmethod def get_challenge_from_player(cls, pk, event=1): clue = Clue.objects.get(player=pk, main=True, event=event) return ClueSerializer(clue).data def test_meeting_player1_not_in_event(self): """ player1 not in event """ player1 = 6 player2 = 1 event = 1 response = self.client.authenticate(self.get_username(player1), self.pwd) self.assertEqual(response.status_code, 200) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), "Unauthorized event") def test_meeting_player2_not_in_event(self): """ player2 not in event """ player1 = 1 player2 = 6 event = 1 self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), "Other player not join at this event") def test_meeting_near_step1_ai(self): """ player1 near player2 (AI) """ player1 = 1 player2 = 2 event = 1 self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 201) res = self.get_challenge_from_player(player2) self.assertEqual(response.json()['clues'][0]['challenge']['name'], res['challenge']['name']) def test_meeting_near_step1_no_ai(self): """ no meeting between player1 and player3. player1 near player3. """ player1 = 1 player2 = 3 event = 1 self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {'status': 'step1'}) def test_meeting_near_step2(self): """ meeting between player3 and player4 with status step1. player3 near player4. """ player1 = 4 player2 = 3 event = 1 self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 200) secret = Meeting.objects.get(player1=player2, player2=player1, event_id=event).secret self.assertEqual(response.json(), { 'status': 'step2', 'secret': secret }) def test_meeting_near_invalid_code(self): """ meeting between player5 and player4 with status step2. player4 near player5. """ player1 = 4 player2 = 5 event = 1 secret = 'INCORRECT' self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/captured/{2}/'.format( player2, event, secret), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), {"error": "Invalid secret"}) def test_meeting_near_valid_code(self): """ meeting between player5 and player4 with status step2. player4 near player5. """ player1 = 4 player2 = 5 event = 1 secret = '0123456789ABCDEF' self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/captured/{2}/'.format( player2, event, secret), {}) self.assertEqual(response.status_code, 200) res = self.get_challenge_from_player(player2) self.assertEqual(response.json()['clues'][0]['challenge']['name'], res['challenge']['name']) def test_meeting_near_polling_waiting(self): """ meeting between player4 and player5 with status step2. player4 near player5. """ player1 = 5 player2 = 4 event = 1 self.authenticate(self.get_username(player1), self.pwd) response = self.client.get( '/api/player/meeting/{0}/{1}/qrclue/'.format(player2, event), {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': 'waiting'}) def test_meeting_near_polling_correct(self): """ meeting between player4 and player5 with status step2. player4 near player5. """ player1 = 5 player2 = 4 event = 1 meeting = Meeting.objects.filter(player1=player2, player2=player1, event_id=event).first() prev_status = meeting.status meeting.status = 'connected' meeting.save() self.authenticate(self.get_username(player1), self.pwd) response = self.client.get( '/api/player/meeting/{0}/{1}/qrclue/'.format(player2, event), {}) self.assertEqual(response.status_code, 200) res = self.get_challenge_from_player(player2) self.assertEqual(response.json()['clues'][0]['challenge']['name'], res['challenge']['name']) meeting.status = prev_status meeting.save() def test_meeting_between_players_without_challenge(self): user_1 = User.objects.create_user('user_1', '*****@*****.**', 'qweqweqwe') user_2 = User.objects.create_user('user_2', '*****@*****.**', 'qweqweqwe') pos = GEOSGeometry('POINT(37.00000 -5.00000)') player_1 = Player(user=user_1, pos=pos) player_1.save() player_2 = Player(user=user_2, pos=pos) player_2.save() self.authenticate(self.get_username(player_1.pk), self.pwd) # step 1 response = self.client.post( '/api/player/meeting/{0}/'.format(player_2.pk), {}) self.assertEqual(response.status_code, 201) self.authenticate(self.get_username(player_2.pk), self.pwd) # step 2 response = self.client.post( '/api/player/meeting/{0}/'.format(player_1.pk), {}) self.assertEqual(response.status_code, 200) secret = Meeting.objects.get(player1=player_1, player2=player_2).secret self.authenticate(self.get_username(player_1.pk), self.pwd) # step 3 response = self.client.post( '/api/player/meeting/{0}/captured/{1}/'.format( player_2.pk, secret), {}) self.assertEqual(response.status_code, 200) self.authenticate(self.get_username(player_2.pk), self.pwd) # step 4 response = self.client.get( '/api/player/meeting/{0}/qrclue/'.format(player_1.pk), {}) self.assertEqual(response.status_code, 200) def test_meeting_with_challenge_dependencies(self): player1 = 1 player2 = 7 event = 1 # without the depends clues self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {'clues': [], 'status': 'connected'}) # with one of the depends, but without the other player1 = 5 self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {'clues': [], 'status': 'connected'}) # getting the second depends player2 = 2 response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 201) # with all dependencies player2 = 7 self.authenticate(self.get_username(player1), self.pwd) response = self.client.post( '/api/player/meeting/{0}/{1}/'.format(player2, event), {}) self.assertEqual(response.status_code, 201) res = self.get_challenge_from_player(player2) self.assertEqual(response.json()['clues'][0]['challenge']['name'], res['challenge']['name'])
class EventTestCase(APITestCase): """ Inside event.json fixture have: - 3 event, 3 game and 17 challenges: - general event (pk=1) with general game (pk=1) with general challenge (pk=1). Max_player = 0 - event 2 (pk=2) with game 2 (pk=2) with challenges (pk=[2,3,4,5,6,7,8]). Event have solution, and Challenges no have solutions. Max_player = 3 - event 3 (pk=3) with game 3 (pk=3) with general challenge (pk=[9,10,11,12,13]). Event and Challenges have solution. Max_player = 10 - membership: player 3 ('test3') in event 3 player 5 ('test5') in event 1 and 3 """ fixtures = ['player-test.json', 'event.json'] def setUp(self): self.client = JClient() event_pk = 2 self.event = Event.objects.get(pk=event_pk) def tearDown(self): self.client = None self.event.players.clear() def authenticate(self, username, pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) @classmethod def get_username_by_player(cls, pk): return Player.objects.get(pk=pk).user.username def test_join_an_event_unauthorized(self): event_pk = 2 response = self.client.post('/api/event/join/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 401) def test_join_an_event_not_exist(self): self.authenticate('test1') response = self.client.post('/api/event/join/{0}/'.format(999), {}) self.assertEqual(response.status_code, 403) self.assertEqual(response.json(), {'detail': "Event doesn't exists"}) def test_join_an_event(self): event_pk = 2 response = self.client.post('/api/event/join/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 401) self.authenticate('test1') response = self.client.post('/api/event/join/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 201) def test_join_an_event_repeat(self): event_pk = 2 self.authenticate('test1') response = self.client.post('/api/event/join/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 201) response = self.client.post('/api/event/join/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'This player already is join at this event') def test_join_an_event_max(self): event_pk = 2 repeat = 1 max_player_ev2 = Event.objects.get(pk=2).max_players while repeat <= max_player_ev2: username = '******'.format(repeat) self.authenticate(username) response = self.client.post('/api/event/join/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 201) repeat += 1 response = self.client.post('/api/event/join/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'Maximum number of player in this event') def test_unjoin_event_anonymous(self): event_pk = 3 response = self.client.delete('/api/event/unjoin/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), anonymous_user) def test_unjoin_event(self): event_pk = 3 self.authenticate('test3') response = self.client.delete('/api/event/unjoin/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 200) def test_unjoin_event_no_join(self): event_pk = 3 self.authenticate('test1') response = self.client.delete('/api/event/unjoin/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), "You aren't joined to this event.") def test_unjoin_event_no_exist(self): event_pk = 5 self.authenticate('test1') response = self.client.delete('/api/event/unjoin/{0}/'.format(event_pk), {}) self.assertEqual(response.status_code, 403) self.assertEqual(response.json(), {'detail': "Event doesn't exists"}) def test_get_my_events_unauth(self): response = self.client.get('/api/event/my-events/', {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), anonymous_user) def test_get_my_events(self): player5_joined_event = Player.objects.get(pk=5).membership_set.count() self.authenticate('test5') response = self.client.get('/api/event/my-events/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), player5_joined_event) def test_get_all_events_unauth(self): response = self.client.get('/api/event/all/', {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), anonymous_user) @override_settings( task_eager_propagates=True, task_always_eager=True, broker_url='memory://', backend='memory' ) def test_get_all_events(self): self.authenticate('test5') response = self.client.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) # Edit an event for tomorrow self.event.start_date = timezone.now() + timezone.timedelta(days=1) self.event.end_date = timezone.now() + timezone.timedelta(days=2) self.event.save() response = self.client.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) @override_settings( task_eager_propagates=True, task_always_eager=True, broker_url='memory://', backend='memory' ) def test_get_all_events_paginated(self): self.authenticate('test5') response = self.client.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) # Adding a lot of events evs = [] for i in range(35): ev = Event(name="test-%s" % i, game=self.event.game) ev.start_date = timezone.now() + timezone.timedelta(days=1 + i) ev.end_date = timezone.now() + timezone.timedelta(days=2 + i) ev.save() evs.append(ev.pk) response = self.client.get('/api/event/all/?page=a', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 20) self.assertEqual(response.json()[0]['name'], 'test-34') response = self.client.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 20) self.assertEqual(response.json()[0]['name'], 'test-34') response = self.client.get('/api/event/all/?page=1', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 15) self.assertEqual(response.json()[0]['name'], 'test-14') response = self.client.get('/api/event/all/?q=test-0', {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()[0]['name'], 'test-0') response = self.client.get('/api/event/all/?q=test-38', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) @override_settings( task_eager_propagates=True, task_always_eager=True, broker_url='memory://', backend='memory' ) def test_get_events_filtered(self): self.authenticate('test5') response = self.client.get('/api/event/all/', {'filter': 'mine'}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) response = self.client.get('/api/event/all/?q=test-38', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) p = Player.objects.get(pk='5') # Adding a lot of events evs = [] from django.db import transaction for i in range(35): ev = Event(name="test-%s" % i, game=self.event.game) ev.start_date = timezone.now() + timezone.timedelta(days=1+i) ev.end_date = timezone.now() + timezone.timedelta(days=2+i) ev.save() evs.append(ev) with transaction.atomic(): for ev in evs[0:5]: ev.owners.add(p.user) with transaction.atomic(): for ev in evs[5:11]: m = Membership(event=ev, player=p) m.save() response = self.client.get('/api/event/all/', {'filter': 'admin'}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 5) response = self.client.get('/api/event/all/', {'filter': 'mine'}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 6) @override_settings( task_eager_propagates=True, task_always_eager=True, broker_url='memory://', backend='memory' ) def test_get_event_detail(self): self.authenticate('test5') # Edit an event for tomorrow self.event.start_date = timezone.now() + timezone.timedelta(days=1) self.event.end_date = timezone.now() + timezone.timedelta(days=2) self.event.save() pk = self.event.pk response = self.client.get('/api/event/%s/' % pk, {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['pk'], pk) # event that doesn't exist response = self.client.get('/api/event/523/', {}) self.assertEqual(response.status_code, 403) def test_solve_event_unauthorized(self): """ User try solve event with event_id unauthorized. """ event_id = 1 player = 3 data = {'solution': 'solution'} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 403) def test_solve_event_no_data(self): """ Event's solution is incorrect. """ event_id = 3 player = 3 data = {} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 400) def test_solve_event_empty(self): """ Event's solution is incorrect. """ event_id = 3 player = 3 data = {'solution': ''} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 400) def test_solve_event_incorrect(self): """ Event's solution is incorrect. """ event_id = 3 player = 3 data = {'solution': 'solution'} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "incorrect"}) def test_solve_event_correct(self): """ Event's solution is correct. """ event_id = 3 player = 3 event = Event.objects.get(pk=event_id) solution = event.game.solution data = {'solution': solution} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "correct"}) player = Player.objects.get(pk=player) membership_status = Membership.objects.filter(event=event, player=player).first().status self.assertEqual(membership_status, 'solved')
class PlayerTestCase(APITestCase): """ Inside fixture have 5 players: - admin -> pk=1; playerX -> pk=X+1 - player1 and player2: distance < 10 m. IS NEAR - player1 and player3: distance < 1 km. IS NEAR - player1 and player4: distance < 5 km. IS FAR - player5: distance < 1 km, but outside event.place. IS NEAR - applications: facebook, twitter """ fixtures = ['player-test.json'] PLAYER2_PK = 2 PLAYER5_PK = 5 PLAYERS_NEAR_PLAYER1 = 3 def setUp(self): self.username = '******' self.pwd = 'qweqweqwe' self.client = JClient() def tearDown(self): self.client = None def authenticate(self, username='******', pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) def test_players_near_without_login(self): response = self.client.get('/api/player/near/', {}) self.assertEqual(response.status_code, 401) def test_meeting_without_login(self): response = self.client.post( '/api/player/meeting/{0}/'.format(self.PLAYER2_PK), {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), 'Anonymous user') def test_meeting_with_login_far(self): self.authenticate() response = self.client.post( '/api/player/meeting/{0}/'.format(self.PLAYER5_PK), {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), 'User is far for meeting') def test_change_position_unauthorized(self): response = self.client.post('/api/player/set-pos/', { 'lat': '-6', 'lon': '37' }) self.assertEqual(response.status_code, 401) def test_change_position(self): self.authenticate() response = self.client.post('/api/player/set-pos/', { 'lat': '-6', 'lon': '37' }) self.assertEqual(response.status_code, 200) def test_delete_position_unauthorized(self): response = self.client.delete('/api/player/set-pos/', {}) self.assertEqual(response.status_code, 401) def test_delete_position(self): self.authenticate() response = self.client.delete('/api/player/set-pos/', {}) self.assertEqual(response.status_code, 200) def test_change_position_invalid(self): self.authenticate() response = self.client.post('/api/player/set-pos/', { 'lat': 'bad', 'lon': '37' }) self.assertEqual(response.status_code, 400) response = self.client.post('/api/player/set-pos/', { 'lat': '', 'lon': '' }) self.assertEqual(response.status_code, 400) response = self.client.post('/api/player/set-pos/', { 'lat': '', 'lon': '37' }) self.assertEqual(response.status_code, 400) def test_profile(self): self.authenticate() interests = ['sports', 'books', 'music'] interests2 = ['music'] data1 = {'about': 'about me', 'interests': interests} response = self.client.post('/api/player/profile/', data1) self.assertEqual(response.status_code, 200) response = self.client.get('/api/player/profile/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['about'], 'about me') self.assertEqual(response.json()['interests'], interests) response = self.client.post('/api/player/profile/', {'interests': interests2}) self.assertEqual(response.status_code, 200) response = self.client.get('/api/player/profile/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['about'], 'about me') self.assertEqual(response.json()['interests'], interests2)
class ClueTestCase(APITestCase): """ There are 2 events: - 1: Einstein Game: with 4 challenge (without solution in challenge) - 2: Easy Game: with 1 challeng (with solution in challenge) Players: - 1: belong to event 1 and 2. have 2 clue in event 1 with pk 1 and 2 - 1: belong to event 2. have 1 clue in event 2 with pk 3 """ fixtures = ['player-test.json', 'clue.json'] EVENT_PK = 1 GAME_PK = 1 USER_CLUES = 2 EVENT3_PK = 3 def setUp(self): self.pwd = 'qweqweqwe' # USER_1 self.c = JClient() self.event = Event.objects.get(pk=self.EVENT_PK) def tearDown(self): self.c = None self.event.players.clear() @classmethod def get_username_by_player(cls, pk): return Player.objects.get(pk=pk).user.username def test_get_my_clues(self): player = 1 response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.get('/api/clue/my-clues/{0}/'.format(self.GAME_PK), {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), self.USER_CLUES) def test_join_event_create_clue(self): player = 2 start_clues = Clue.objects.count() response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/join/{0}/'.format(self.EVENT_PK), {}) self.assertEqual(response.status_code, 201) end_clues = Clue.objects.count() self.assertEqual(start_clues + 1, end_clues) def test_join_event_create_clue_two_players(self): player1 = 1 player2 = 2 response = self.c.authenticate(self.get_username_by_player(player1), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/join/{0}/'.format(self.EVENT3_PK), {}) self.assertEqual(response.status_code, 201) response = self.c.authenticate(self.get_username_by_player(player2), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/join/{0}/'.format(self.EVENT3_PK), {}) self.assertEqual(response.status_code, 201) event = Event.objects.get(pk=self.EVENT3_PK) clue1 = Clue.objects.filter(player=player1, main=True, event=event).first() clue2 = Clue.objects.filter(player=player2, main=True, event=event).first() self.assertNotEqual(clue1.challenge, clue2.challenge) def test_unjoin_event_delete_clue(self): player = 1 start_clues = Clue.objects.count() response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.delete( '/api/event/unjoin/{0}/'.format(self.EVENT_PK), {}) self.assertEqual(response.status_code, 200) end_clues = Clue.objects.count() self.assertEqual(start_clues - 1, end_clues) def test_solve_clue_not_exist(self): """ User try solve clue with clue_id not exist. """ player = 1 clue_id = 4 data = {'solution': 'solution'} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 401) def test_solve_clue_unauthorized2(self): """ User try solve clue with clue_id unauthorized. """ player = 1 clue_id = 3 data = {'solution': 'solution'} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 401) def test_solve_clue_invalid(self): """ Clue can't solve because clue haven't solution. """ player = 1 clue_id = 1 data = {'solution': 'solution'} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 400) def test_solve_clue_no_data(self): """ Clue's solution not in data. """ player = 2 clue_id = 3 data = {} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 400) def test_solve_clue_empty(self): """ Clue's solution is empty. """ player = 2 clue_id = 3 data = {'solution': ''} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 400) def test_solve_clue_incorrect(self): """ Clue's solution is incorrect. """ player = 2 clue_id = 3 data = {'solution': 'no solution'} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "incorrect"}) def test_solve_clue_correct(self): """ Clue's solution is correct. """ player = 2 clue_id = 3 data = {'solution': 'solution'} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "correct"}) clue_status = Clue.objects.get(pk=clue_id).status self.assertEqual(clue_status, 'solved')
class EventTestCase(APITestCase): """ Inside event.json fixture have: - 3 event, 3 game and 17 challenges: - general event (pk=1) with general game (pk=1) with general challenge (pk=1). Max_player = 0 - event 2 (pk=2) with game 2 (pk=2) with challenges (pk=[2,3,4,5,6,7,8]). Event have solution, and Challenges no have solutions. Max_player = 3 - event 3 (pk=3) with game 3 (pk=3) with general challenge (pk=[9,10,11,12,13]). Event and Challenges have solution. Max_player = 10 - membership: player 3 ('test3') in event 3 player 5 ('test5') in event 1 and 3 """ fixtures = ['player-test.json', 'event.json'] EVENT_PK_2 = 2 EVENT_PK_3 = 3 EVENT_PK_5 = 5 MAX_PLAYER_EVENT_2 = 3 PLAYER5_JOINED_EVENT = 3 def setUp(self): self.username = '******' self.pwd = 'qweqweqwe' self.c = JClient() self.event = Event.objects.get(pk=self.EVENT_PK_2) def tearDown(self): self.c = None self.event.players.clear() @classmethod def get_username_by_player(cls, pk): return Player.objects.get(pk=pk).user.username def test_join_an_event_unauthorized(self): response = self.c.post('/api/event/join/{0}/'.format(self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 401) def test_join_an_event_not_exist(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/join/{0}/'.format(999), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'Event not exist') def test_join_an_event(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/join/{0}/'.format(self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 201) def test_join_an_event_repeat(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/join/{0}/'.format(self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 201) response = self.c.post('/api/event/join/{0}/'.format(self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'This player already is join at this event') def test_join_an_event_max(self): repeat = 1 while repeat <= self.MAX_PLAYER_EVENT_2: username = '******'.format(repeat) response = self.c.authenticate(username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/join/{0}/'.format(self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 201) repeat += 1 response = self.c.post('/api/event/join/{0}/'.format(self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'Maximum number of player in this event') def test_unjoin_event_anonymous(self): response = self.c.delete('/api/event/unjoin/{0}/'.format(self.EVENT_PK_3), {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), 'Anonymous user') def test_unjoin_event(self): response = self.c.authenticate('test3', self.pwd) self.assertEqual(response.status_code, 200) response = self.c.delete('/api/event/unjoin/{0}/'.format(self.EVENT_PK_3), {}) self.assertEqual(response.status_code, 200) def test_unjoin_event_no_join(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.delete('/api/event/unjoin/{0}/'.format(self.EVENT_PK_3), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'You not join in this event.') def test_unjoin_event_no_exist(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.delete('/api/event/unjoin/{0}/'.format(self.EVENT_PK_5), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'Event not exist') def test_get_my_events_unauth(self): response = self.c.get('/api/event/my-events/', {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), 'Anonymous user') def test_get_my_events(self): response = self.c.authenticate('test5', self.pwd) self.assertEqual(response.status_code, 200) response = self.c.get('/api/event/my-events/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), self.PLAYER5_JOINED_EVENT) def test_get_all_events_unauth(self): response = self.c.get('/api/event/all/', {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), 'Anonymous user') def test_get_all_events(self): response = self.c.authenticate('test5', self.pwd) self.assertEqual(response.status_code, 200) response = self.c.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) # Edit an event for tomorrow self.event.start_date = timezone.now() + timezone.timedelta(days=1) self.event.end_date = timezone.now() + timezone.timedelta(days=2) self.event.save() response = self.c.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) def test_get_all_events_paginated(self): response = self.c.authenticate('test5', self.pwd) self.assertEqual(response.status_code, 200) response = self.c.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) # Adding a lot of events evs = [] for i in range(35): ev = Event(name="test-%s" % i, game=self.event.game) ev.start_date = timezone.now() + timezone.timedelta(days=1+i) ev.end_date = timezone.now() + timezone.timedelta(days=2+i) ev.save() evs.append(ev.pk) response = self.c.get('/api/event/all/', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 20) self.assertEqual(response.json()[0]['name'], 'test-34') response = self.c.get('/api/event/all/?page=1', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 15) self.assertEqual(response.json()[0]['name'], 'test-14') response = self.c.get('/api/event/all/?q=test-0', {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()[0]['name'], 'test-0') response = self.c.get('/api/event/all/?q=test-38', {}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) def test_get_event_detail(self): response = self.c.authenticate('test5', self.pwd) self.assertEqual(response.status_code, 200) # Edit an event for tomorrow self.event.start_date = timezone.now() + timezone.timedelta(days=1) self.event.end_date = timezone.now() + timezone.timedelta(days=2) self.event.save() pk = self.event.pk response = self.c.get('/api/event/%s/' % pk, {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['pk'], pk) # event that doesn't exist response = self.c.get('/api/event/523/', {}) self.assertEqual(response.status_code, 400) def test_solve_event_unauthorized(self): """ User try solve event with event_id unauthorized. """ event_id = 1 player = 3 data = {'solution': 'solution'} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 401) def test_solve_event_no_data(self): """ Event's solution is incorrect. """ event_id = 3 player = 3 data = {} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 400) def test_solve_event_empty(self): """ Event's solution is incorrect. """ event_id = 3 player = 3 data = {'solution': ''} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 400) def test_solve_event_incorrect(self): """ Event's solution is incorrect. """ event_id = 3 player = 3 data = {'solution': 'solution'} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "incorrect"}) def test_solve_event_correct(self): """ Event's solution is correct. """ event_id = 3 player = 3 event = Event.objects.get(pk=event_id) solution = event.game.solution data = {'solution': solution} response = self.c.authenticate(self.get_username_by_player(player), self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/event/solve/{0}/'.format(event_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "correct"}) player = Player.objects.get(pk=player) membership_status = Membership.objects.filter(event=event, player=player).first().status self.assertEqual(membership_status, 'solved')
class PlayerEventTestCase(APITestCase): """ Inside event.json fixture have: - 3 event, 3 game and 17 challenges: - general event (pk=1) with general game (pk=1) with general challenge (pk=1). Max_player = 0 - event 2 (pk=2) with game 2 (pk=2) with challenges (pk=[2,3,4,5,6,7,8]). Event have solution, and Challenges no have solutions. Max_player = 3 - event 3 (pk=3) with game 3 (pk=3) with general challenge (pk=[9,10,11,12,13]). Event and Challenges have solution. Max_player = 10 - membership: player 3 ('test3') in event 3 player 5 ('test5') in event 1 and 3 all player are in event 4: 4 inside and 1 outside place In event 3: player 1 is near to player 2 and player 3 player 1 is near to player 5 but player 5 is outside place player 1 is far player 4 """ fixtures = ['player-test.json', 'event.json'] EVENT_PK_1 = 1 EVENT_PK_2 = 2 EVENT_PK_3 = 3 EVENT_PK_4 = 4 PLAYER_PK_2 = 2 PLAYER_PK_3 = 3 PLAYER_PK_5 = 5 NEAR_PLAYER_1_EVENT_4 = 2 NEAR_PLAYER_4_EVENT_4 = 0 NEAR_PLAYER_5_EVENT_4 = 0 # outside event def setUp(self): self.username1 = 'test1' self.username4 = 'test4' self.username = '******' self.pwd = 'qweqweqwe' self.c = JClient() def tearDown(self): self.c = None def test_players_near_in_event2(self): response = self.c.authenticate(self.username4, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.get('/api/player/near/{0}/'.format(self.EVENT_PK_4), {}) self.assertEqual(response.status_code, 200) players = [d for d in response.json() if d.get('ia') is False] self.assertEqual(len(players), self.NEAR_PLAYER_4_EVENT_4) def test_players_near_in_event3(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.get('/api/player/near/{0}/'.format(self.EVENT_PK_4), {}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), 'Your player is outside of place.') def test_players_near_in_unauth_event(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.get('/api/player/near/{0}/'.format(self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 401) def test_players_meeting_in_event(self): response = self.c.authenticate(self.username1, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/player/meeting/{0}/{1}/'.format(self.PLAYER_PK_2, self.EVENT_PK_4), {}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {'status': 'step1'}) def test_players_meeting_in_event_unauth_event(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/player/meeting/{0}/{1}/'.format(self.PLAYER_PK_3, self.EVENT_PK_2), {}) self.assertEqual(response.status_code, 401) self.assertEqual(response.json(), 'Unauthorized event') def test_players_meeting_in_event_with_player2_outside_event(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/player/meeting/{0}/{1}/'.format(self.PLAYER_PK_3, self.EVENT_PK_1), {}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'Other player not join at this event') def test_players_meeting_in_event_with_himself(self): response = self.c.authenticate(self.username, self.pwd) self.assertEqual(response.status_code, 200) response = self.c.post('/api/player/meeting/{0}/{1}/'.format(self.PLAYER_PK_5, self.EVENT_PK_3), {}) self.assertEqual(response.json(), 'narcissistic: you cannot connect with yourself') self.assertEqual(response.status_code, 400)
class ClueTestCase(APITestCase): """ There are 2 events: - 1: Einstein Game: with 4 challenge (without solution in challenge) - 2: Easy Game: with 1 challeng (with solution in challenge) Players: - 1: belong to event 1 and 2. have 3 clue in event 1 with pk 1, 2 and 3 - 1: belong to event 2. have 1 clue in event 2 with pk 3 """ fixtures = ['player-test.json', 'clue.json'] EVENT_PK = 1 GAME_PK = 1 ORDERER_CLUES_BY_PK = [1, 2, 4] EVENT3_PK = 3 def setUp(self): self.pwd = 'qweqweqwe' # USER_1 self.client = JClient() self.event = Event.objects.get(pk=self.EVENT_PK) def tearDown(self): self.client = None self.event.players.clear() def authenticate(self, username, pwd='qweqweqwe'): response = self.client.authenticate(username, pwd) self.assertEqual(response.status_code, 200) @classmethod def get_username_by_player(cls, pk): return Player.objects.get(pk=pk).user.username def get_my_clues(self): player = 1 self.authenticate(self.get_username_by_player(player)) response = self.client.get('/api/clue/my-clues/{0}/'.format(self.GAME_PK), {}) self.assertEqual(response.status_code, 200) return response def test_get_my_clues(self): response = self.get_my_clues() self.assertEqual(len(response.json()), len(self.ORDERER_CLUES_BY_PK)) def test_get_my_clues_ordered(self): response = self.get_my_clues() self.assertEqual([cha.get('pk') for cha in response.json()], self.ORDERER_CLUES_BY_PK) def test_join_event_create_clue(self): player = 2 start_clues = Clue.objects.count() self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/event/join/{0}/'.format(self.EVENT_PK), {}) self.assertEqual(response.status_code, 201) end_clues = Clue.objects.count() self.assertEqual(start_clues + 1, end_clues) def test_join_event_create_clue_without_assign_auto(self): player = 2 game = Event.objects.get(pk=self.EVENT_PK).game game.auto_assign_clue = False game.save() start_clues = Clue.objects.count() self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/event/join/{0}/'.format(self.EVENT_PK), {}) self.assertEqual(response.status_code, 201) end_clues = Clue.objects.count() self.assertEqual(start_clues, end_clues) game.auto_assign_clue = True game.save() def test_join_event_create_clue_two_players(self): player1 = 1 player2 = 2 self.authenticate(self.get_username_by_player(player1)) response = self.client.post('/api/event/join/{0}/'.format(self.EVENT3_PK), {}) self.assertEqual(response.status_code, 201) self.authenticate(self.get_username_by_player(player2)) response = self.client.post('/api/event/join/{0}/'.format(self.EVENT3_PK), {}) self.assertEqual(response.status_code, 201) event = Event.objects.get(pk=self.EVENT3_PK) clue1 = Clue.objects.filter(player=player1, main=True, event=event).first() clue2 = Clue.objects.filter(player=player2, main=True, event=event).first() self.assertNotEqual(clue1.challenge, clue2.challenge) def test_unjoin_event_delete_clue(self): player = 1 start_clues_player = Clue.objects.filter(player__pk=player).count() start_clues_main = Clue.objects.filter(main=True).count() self.assertTrue(start_clues_player > 0) self.authenticate(self.get_username_by_player(player)) response = self.client.delete('/api/event/unjoin/{0}/'.format(self.EVENT_PK), {}) self.assertEqual(response.status_code, 200) end_clues_player = Clue.objects.filter(player__pk=player).count() end_clues_main = Clue.objects.filter(main=True).count() self.assertEqual(end_clues_player, 0) self.assertEqual(start_clues_main, end_clues_main) def test_solve_clue_not_exist(self): """ User try solve clue with clue_id not exist. """ player = 1 clue_id = 5 data = {'solution': 'solution'} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 403) def test_solve_clue_unauthorized2(self): """ User try solve clue with clue_id unauthorized. """ player = 1 clue_id = 3 data = {'solution': 'solution'} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 403) def test_solve_clue_invalid(self): """ Clue can't solve because clue haven't solution. """ player = 1 clue_id = 1 data = {'solution': 'solution'} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 400) def test_solve_clue_no_data(self): """ Clue's solution not in data. """ player = 2 clue_id = 3 data = {} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 400) def test_solve_clue_empty(self): """ Clue's solution is empty. """ player = 2 clue_id = 3 data = {'solution': ''} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 400) def test_solve_clue_incorrect(self): """ Clue's solution is incorrect. """ player = 2 clue_id = 3 data = {'solution': 'no solution'} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "incorrect"}) def test_solve_clue_correct(self): """ Clue's solution is correct. """ player = 2 clue_id = 3 data = {'solution': 'solution'} self.authenticate(self.get_username_by_player(player)) response = self.client.post('/api/clue/solve/{0}/'.format(clue_id), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'status': "correct", 'clues': []}) clue_status = Clue.objects.get(pk=clue_id).status self.assertEqual(clue_status, 'solved')