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'
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'
def test_game_name(client): address = mixer.faker.address() location_name = ' '.join(address.split()[1:3]) game_name = f'The game at {location_name}' res = client.post( reverse('game-list'), { 'datetimes': [datetime.utcnow() + timedelta(i) for i in range(1, 5)], 'location': { 'address': address, 'name': location_name, }, 'name': game_name, }) game = Game.objects.get(id=res.data['id']) assert game.name == game_name, 'Should be able to create game with a name' games = Game.objects.filter(name__startswith=game_name) names = [item['name'] for item in games.values('name')] assert names == [ game_name, game_name + ' (2)', game_name + ' (3)', game_name + ' (4)', ], 'Games created at once with a name should get a (n) suffix'
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'
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'
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'
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'
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'