示例#1
0
def test_email_blank_or_unique():
    alice = mixer.blend('users.User', email='*****@*****.**')

    with pytest.raises(ValidationError):
        eve = mixer.blend('users.User', email='*****@*****.**')

    mixer.cycle(2).blend('users.User', email='')
示例#2
0
def test_location_model():
    location = mixer.blend('games.Location')
    assert isinstance(eval(str(location)), Location), 'Should cover __str__'

    with pytest.raises(IntegrityError):
        "Should not allow two instances with same name + address"
        mixer.cycle(2).blend('games.Location', name='foo', address='bar')
示例#3
0
def test_pickup_games(client):
    mixer.cycle(5).blend('games.Game')

    url = reverse('game-list')
    res = client.get(url)

    assert res.status_code == status.HTTP_200_OK, 'Request should succeed'
    assert res.data['count'] == 5, 'Should be 5 games'
示例#4
0
def test_team_games(client):
    team = mixer.blend('teams.Team')
    mixer.cycle(5).blend('games.Game', teams=[team])

    url = reverse('team-games-list', (team.id, ))
    res = client.get(url)

    assert res.status_code == status.HTTP_200_OK, 'Request should succeed'
    assert res.data['count'] == 5, 'User should have 5 invites'
示例#5
0
def test_game_invites(client):
    mixer.cycle(5).blend('games.RsvpStatus',
                         status=RsvpStatus.INVITED,
                         player=client.user)

    url = reverse('game-invites')
    res = client.get(url)

    assert res.status_code == status.HTTP_200_OK, 'Request should succeed'
    assert res.data['count'] == 5, 'User should have 5 invites'
示例#6
0
def test_pickup_organizer_can_invite():
    organizer = mixer.blend('users.User')
    players = mixer.cycle(6).blend('users.User')
    pickup_game = mixer.blend('games.Game', organizer=organizer)
    other_game = mixer.blend('games.Game')

    pickup_url = reverse('rsvp-list', (pickup_game.id, ))
    other_url = reverse('rsvp-list', (other_game.id, ))

    client = APIClient()
    client.force_authenticate(organizer)

    res = client.post(pickup_url, {
        'id': players[0].id,
        'rsvp': RsvpStatus.INVITED,
    })

    assert res.status_code == status.HTTP_201_CREATED, \
        'Organizer can invite a player to his game'
    assert players[0].rsvps.count() == 1
    assert players[0].rsvps.get().status == RsvpStatus.INVITED

    for i, (rsvp, _) in enumerate(RsvpStatus.RSVP_CHOICES):
        if rsvp == RsvpStatus.INVITED:
            continue

        res = client.post(pickup_url, {'id': players[i + 1].id, 'rsvp': rsvp})
        assert res.status_code == status.HTTP_403_FORBIDDEN, \
            'Organizer can not do anything other than inviting'

    for i, (rsvp, _) in enumerate(RsvpStatus.RSVP_CHOICES):
        res = client.post(other_url, {'id': players[i + 1].id, 'rsvp': rsvp})
        assert res.status_code == status.HTTP_403_FORBIDDEN, \
            'Organizer can not do anything in another game'
示例#7
0
def test_my_games():
    user = mixer.blend('users.User')
    mixer.cycle(5).blend('games.RsvpStatus',
                         status=RsvpStatus.GOING,
                         player=user)

    client = APIClient()
    url = reverse('game-my')

    res = client.get(url)
    assert res.status_code == status.HTTP_401_UNAUTHORIZED, \
        'Should not be accessable when not authenticated'

    client.force_authenticate(user=user)

    res = client.get(url)

    assert res.status_code == status.HTTP_200_OK, 'Request should succeed'
    assert res.data['count'] == 5, 'User should have 5 games'
示例#8
0
def test_rsvp_update_validation(client):
    invalid_payloads = [{'rsvp': 'foo'}, {}]

    rsvps = mixer.cycle(len(invalid_payloads)).blend('games.RsvpStatus',
                                                     player=client.user,
                                                     status=RsvpStatus.INVITED)

    for payload, rsvp in zip(invalid_payloads, rsvps):
        rsvp_url = reverse('rsvp-detail', (rsvp.game.id, rsvp.id))
        res = client.put(rsvp_url, payload)
        assert res.status_code == status.HTTP_400_BAD_REQUEST, \
            'Player should be able to change his status'
示例#9
0
def test_game_create():
    player1, player2 = mixer.cycle(2).blend('users.User')
    team_manager = mixer.blend('users.User')
    team = mixer.blend('teams.Team', managers=[team_manager])

    player1_role = Role.objects.create(
        player=player1,
        team=team,
        role=Role.FIELD,
    )

    client = APIClient()
    client.force_authenticate(user=team_manager)

    games_url = reverse('game-list')
    address = mixer.faker.address()
    location_name = ' '.join(address.split()[1:3])
    res = client.post(
        games_url, {
            'datetime': datetime.utcnow() + timedelta(1),
            'location': {
                'address': address,
                'name': location_name,
            },
            'teams': [team.id],
        })

    assert res.status_code == status.HTTP_201_CREATED, \
        'Team manager should be able to create a game for his team'

    game = Game.objects.get(id=res.data['id'])

    assert player1 in game.players.all(), \
        'Player that was in the team should be automatically invited'

    # Invite another player to the team
    team_players_url = reverse('team-role-list', (team.id, ))
    res = client.post(team_players_url, {
        'id': player2.id,
        'role': Role.INVITED,
    })
    assert res.status_code == status.HTTP_201_CREATED, \
        'Team manager can invite players to his teams'

    assert player2 in game.players.all(), \
        'Player joining a team should be invited to the future team games'
示例#10
0
def test_can_join_pickup(client):
    pickup_games = mixer.cycle(5).blend('games.Game')

    for game, (rsvp, _) in zip(pickup_games, RsvpStatus.RSVP_CHOICES):
        res = client.post(
            reverse('rsvp-list', (game.id, )),
            {
                'id': client.user.id,
                'rsvp': rsvp
            },
        )
        if rsvp > RsvpStatus.INVITED:
            assert res.status_code == status.HTTP_201_CREATED, \
                'User can join any pickup game he likes'
        else:
            assert res.status_code == status.HTTP_403_FORBIDDEN, \
                'Inviting yourself to a game you can join does not make sense'
示例#11
0
def test_rsvp_create_validation(client):
    invalid_payloads = [
        {
            'id': client.user.id,
            'rsvp': 'foo'
        },
        {
            'id': 'bar',
            'rsvp': RsvpStatus.GOING
        },
        {
            'id': client.user.id
        },
        {
            'rsvp': RsvpStatus.GOING
        },  # this one in theory might be valid if
        {},  # we implicitly use authenticated user
    ]
    pickup_games = mixer.cycle(len(invalid_payloads)).blend('games.Game')

    for payload, game in zip(invalid_payloads, pickup_games):
        res = client.post(reverse('rsvp-list', (game.id, )), payload)
        assert res.status_code == status.HTTP_400_BAD_REQUEST, \
            'Should rise validation error'
示例#12
0
def test_rsvp_list(client):
    game = mixer.blend('games.Game')
    rsvps = mixer.cycle(5).blend('games.RsvpStatus', game=game)

    res = client.get(reverse('rsvp-list', (game.id, )))
    assert res.data['count'] == 5, 'Should have 5 rsvps'