Example #1
0
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')
Example #2
0
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)
Example #3
0
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')
Example #4
0
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')
Example #5
0
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')