Ejemplo n.º 1
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'
Ejemplo n.º 2
0
def test_team_add_player():
    player = mixer.blend('users.User')
    team_manager = mixer.blend('users.User')
    team = mixer.blend('teams.Team', managers=[team_manager])

    team_url = reverse('team-detail', (team.id, ))
    team_players_url = reverse('team-role-list', (team.id, ))

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

    res = client.post(team_players_url, {
        'id': player.id,
        'role': Role.INVITED,
    })
    assert res.status_code == status.HTTP_201_CREATED, \
        'Team manager can invite players to his teams'

    role_id = res.data['role_id']
    role_url = reverse('team-role-detail', (team.id, role_id))

    client.force_authenticate(user=player)
    res = client.put(role_url, {'role': Role.FIELD})
    assert res.status_code == status.HTTP_200_OK, \
        'Invited player can accept an invite'

    assert player in team.players.all(), 'Player should be in the team now'
Ejemplo n.º 3
0
def test_team_retrieve():
    user = mixer.blend('users.User')
    client = APIClient()
    client.force_authenticate(user=user)

    team = mixer.blend('teams.Team')
    url = reverse('team-detail', (team.id, ))

    res = client.get(url)
    assert res.status_code == status.HTTP_200_OK, \
        'Should be able to retrieve team info'
Ejemplo n.º 4
0
def test_my_teams():
    user = mixer.blend('users.User')
    client = APIClient()
    client.force_authenticate(user=user)

    team = mixer.blend('teams.Team', managers=[user])
    url = reverse('team-managed')
    res = client.get(url)

    assert res.status_code == status.HTTP_200_OK, \
        'Should be able to retrieve managed teams'

    assert res.data['count'] == 1, \
        'Should be exactly one'
Ejemplo n.º 5
0
def test_user_has_game_invites():
    user = mixer.blend('users.User')
    rsvp = mixer.blend('games.RsvpStatus',
                       player=user,
                       status=RsvpStatus.INVITED)
    role = mixer.blend('teams.Role', player=user, status=Role.INVITED)

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

    res = client.get(reverse('current-user'))

    assert 'invites' in res.data, 'Sould have invites'

    assert res.data['invites'] == {'total': 2, 'teams': 1, 'games': 1}, \
        'Sould have two invites total, one for games and one for teams'
Ejemplo n.º 6
0
def test_rsvps_annotation():
    user = mixer.blend('users.User')
    game = mixer.blend('games.Game')
    RsvpStatus(player=user, game=game, status=RsvpStatus.GOING).save()
    RsvpStatus(player=mixer.blend('users.User'),
               game=game,
               status=RsvpStatus.GOING).save()

    annotated_game = Game.objects.with_rsvps(user)[0]
    assert annotated_game.rsvp == RsvpStatus.GOING

    client = APIClient()
    client.force_authenticate(user)

    res = client.get(reverse('game-list'))
    assert res.data['results'][0]['rsvp'] == RsvpStatus.GOING
Ejemplo n.º 7
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'
Ejemplo n.º 8
0
def test_team_create():
    user = mixer.blend('users.User')
    url = reverse('team-list')
    client = APIClient()

    res = client.post(url, {'name': 'Test team'})
    assert res.status_code == status.HTTP_401_UNAUTHORIZED, \
        'Unauthenticated user can not create teams'

    client.force_authenticate(user=user)

    res = client.post(url, {'name': 'Test team'})
    assert res.status_code == status.HTTP_201_CREATED, \
        'User can create a team'

    res = client.post(url, {})
    assert res.status_code == status.HTTP_400_BAD_REQUEST, \
        'Should not create a team without a name'
Ejemplo n.º 9
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'
Ejemplo n.º 10
0
def test_team_creator_added_as_player():
    """
    User that creates a team should be added to that teams as a player
    automatically
    """
    user = mixer.blend('users.User')
    client = APIClient()
    client.force_authenticate(user=user)

    create_url = reverse('team-list')
    res = client.post(create_url, {'name': 'Test team'})

    team = Team.objects.get(id=res.data['id'])

    assert user in team.players.all(), 'Creator should be a player'

    user2 = mixer.blend('users.User')
    team.managers.add(user2)
    assert user2 not in team.players.all(), \
        'Any extra managers are not added automatically'