def test_leave_teams_on_accept(self):
     """Accepting invite causes accepter to leave other teams"""
     # Register Carl to compete
     RegistrationFactory(user=self.carl, competition=self.galapagos)
     # Put him on a couple of teams
     tg = TeamFactory.create(competition=self.galapagos, num_members=1)
     tg.add_team_member(self.carl)
     ts = TeamFactory.create(competition=self.space, num_members=1)
     ts.add_team_member(self.carl)
     # And send him an invitation to another Galapagos team
     inv = InvitationFactory.create(receiver=self.carl,
                                    team=self.alice_team)
     with self.loggedInAs("carl", "123"):
         resp = self.client.rpost('invitation_accept', follow=True,
                                  kwargs={'pk': inv.pk},
                                  data={'confirmed': True})
     # Carl is only on one Galapagos team
     carl_teams = self.carl.team_set.filter(competition=self.galapagos)
     self.assertEqual(1, carl_teams.count())
     # ... he's on Alice's team
     self.assertTrue(self.alice_team.is_user_on_team(self.carl))
     # ... off the other galapagos team
     self.assertFalse(tg.is_user_on_team(self.carl))
     # ... but still on his space team
     self.assertTrue(ts.is_user_on_team(self.carl))
    def test_list_teams(self):
        """List teams for a competition"""
        # Create some more teams
        for _i in range(10):
            TeamFactory.create(competition=self.space, num_members=0)

        with self.loggedInAs("alice", "123"):
            response = self.client.rget('team_list',
                                        kwargs={'comp_slug': self.space.slug})

        objects = response.context['teams']

        for i in range(len(objects) - 1):
            self.assertLess(objects[i].name, objects[i + 1].name)

        # Paginator sanity check
        self.assertTrue(response.context['is_paginated'])
    def setUp(self):
        self.alice = UserFactory.create(username="******")
        self.bob = UserFactory.create(username="******")
        self.carl = UserFactory.create(username="******")

        self.space = CompetitionFactory.create(name="Space",
                                               is_open=False)
        self.galapagos = CompetitionFactory.create(name="Galapagos",
                                                   is_open=True)

        self.alice_team = TeamFactory.create(competition=self.galapagos,
                                             num_members=0)
        self.alice_team.add_team_member(self.alice)

        self.bob_team = TeamFactory.create(competition=self.galapagos,
                                           num_members=0)
        self.bob_team.add_team_member(self.bob)
    def test_members_clear(self):
        """team.members.clear() removes all members"""
        t = TeamFactory.create(name="Team Awesome", competition=self.space,
                               num_members=2)
        members = t.members.all()
        t.members.clear()

        for member in members:
            self.assertFalse(t.is_user_on_team(member))
    def setUp(self):
        self.space = CompetitionFactory.create(name="Space")
        self.galapagos = CompetitionFactory.create(name="Galapagos")

        self.space_teams = [TeamFactory.create(competition=self.space,
                                               num_members=1)
                            for _ in range(3)]
        self.galapagos_teams = [TeamFactory.create(competition=self.galapagos,
                                                   num_members=1)
                                for _ in range(5)]
        self.alice = UserFactory.create(username="******")
        self.bob = UserFactory.create(username="******")

        # Register Alice and Bob for Space
        self.alice_reg = RegistrationFactory.create(user=self.alice,
                                                    competition=self.space)
        self.bob_reg = RegistrationFactory.create(user=self.bob,
                                                  competition=self.space)
    def test_leave_team_decline(self):
        """Declining leaving a team does nothing"""
        url = reverse('team_leave', kwargs={'comp_slug': self.space.slug})
        t = TeamFactory.create(competition=self.space, num_members=1)
        t.members.add(self.alice)

        self.assertEqual(2, t.members.count())   # Sanity check
        self.assertInGroup(self.alice, t.get_group())

        with self.loggedInAs("alice", "123"):
            resp = self.client.post(url, data={'confirmed': False}, follow=True)
            self.assertRedirects(resp, t.get_absolute_url())
            self.assertEqual(2, t.members.count())
            self.assertInGroup(self.alice, t.get_group())
    def test_remove_team_member(self):
        """remove_team_member deletes empty teams"""
        t = TeamFactory.create(name="Team Awesome", competition=self.space,
                               num_members=2)
        member1, member2 = t.members.all()[0], t.members.all()[1]

        # Remove member1, team still exists
        t.remove_team_member(member1)
        self.assertTrue(Team.objects.filter(pk=t.pk).exists())
        self.assertFalse(t.members.filter(pk=member1.pk).exists())

        # Remove member 2, team doesn't exist anymore
        t.remove_team_member(member2)
        self.assertFalse(Team.objects.filter(pk=t.pk).exists())
    def test_left_teams(self):
        """deactivation causes users to leave teams"""
        t = TeamFactory.create(competition=self.space, num_members=0)
        t.members.add(self.alice)

        # Alice is definitely on team t
        self.assertEqual(t, self.alice.team_set.get(competition=self.space))
        self.assertEqual(1, self.space.team_set.count())

        self.alice_reg.deactivate()

        # Alice doesn't have a team for space anymore
        alice_space_teams = self.alice.team_set.filter(competition=self.space)
        self.assertFalse(alice_space_teams.exists())
        # And for good measure, space doesn't have any teams anymore
        self.assertEqual(0, self.space.team_set.count())
    def test_team_deleted(self):
        """Teams get deleted when everyone leaves"""
        url = reverse('team_leave', kwargs={'comp_slug': self.space.slug})
        t = TeamFactory.create(competition=self.space, num_members=0)
        t.members.add(self.alice)

        # Sanity checks
        self.assertTrue(Team.objects.filter(pk=t.pk).exists())
        self.assertEqual(1, t.members.count())
        self.assertInGroup(self.alice, t.get_group())
        self.assertEqual(1, self.alice.groups.count())

        with self.loggedInAs("alice", "123"):
            resp = self.client.post(url, data={'confirmed': True}, follow=True)
            self.assertRedirects(resp, self.space.get_absolute_url())
            self.assertFalse(Team.objects.filter(pk=t.pk).exists())
            self.assertEqual(0, self.alice.groups.count())
            self.assertEqual(0, Group.objects.filter(name=t.group_name).count())
    def test_leave_team(self):
        """Confiming leaving a team causes user to leave team"""
        url = reverse('team_leave', kwargs={'comp_slug': self.space.slug})
        t = TeamFactory.create(competition=self.space, num_members=1)
        t.members.add(self.alice)

        self.assertEqual(2, t.members.count())   # Sanity check
        self.assertInGroup(self.alice, t.get_group())
        self.assertEqual(1, self.alice.groups.count())

        with self.loggedInAs("alice", "123"):
            resp = self.client.get(url)
            self.assertEqual(200, resp.status_code)
            self.assertIn("Are you sure", resp.context['question'])

            resp = self.client.post(url, data={'confirmed': True}, follow=True)
            self.assertRedirects(resp, self.space.get_absolute_url())
            self.assertEqual(1, t.members.count())
            self.assertEqual(0, self.alice.groups.count())
    def test_same_team(self):
        """Rejecting an invitation causes no team change"""
        # Register Carl to compete
        RegistrationFactory(user=self.carl, competition=self.galapagos)
        # Put him on a team
        carl_team = TeamFactory.create(competition=self.galapagos,
                                       num_members=1)
        carl_team.add_team_member(self.carl)

        # Alice sends him an invitation
        inv = InvitationFactory.create(receiver=self.carl,
                                       team=self.alice_team)
        with self.loggedInAs("carl", "123"):
            # Carl declines invitation
            resp = self.client.rpost('invitation_decline', follow=True,
                                     kwargs={'pk': inv.pk},
                                     data={'confirmed': True})
        # Carl isn't on Alice's team, and he's still on his old team
        self.assertFalse(self.alice_team.is_user_on_team(self.carl))
        self.assertTrue(carl_team.is_user_on_team(self.carl))
    def setUp(self):
        self.space = CompetitionFactory.create(name="Space",
                                               is_running=True)

        self.space_teams = [TeamFactory.create(competition=self.space,
                                               num_members=1)
                            for _ in range(5)]
        self.alice = UserFactory.create(username="******")
        self.bob = UserFactory.create(username="******")
        self.carl = UserFactory.create(username="******")

        # Register Alice and Bob for Space
        self.alice_reg = RegistrationFactory.create(user=self.alice,
                                                    competition=self.space)
        self.bob_reg = RegistrationFactory.create(user=self.bob,
                                                  competition=self.space)

        # Add users to teams
        self.alice_team = self.space_teams[0]
        self.alice_team.members.add(self.alice)
        self.bob_team = self.space_teams[1]
        self.bob_team.members.add(self.bob)

        # Some other team
        self.other_team = self.space_teams[2]

        # Add a game between bob and alice
        g = GameFactory.create(competition=self.space)
        GameScoreFactory.create(game=g, team=self.alice_team)
        GameScoreFactory.create(game=g, team=self.bob_team)

        # Add a game between alice and not-bob
        g = GameFactory.create(competition=self.space)
        GameScoreFactory.create(game=g, team=self.alice_team)
        GameScoreFactory.create(game=g, team=self.other_team)

        for _ in range(20):
            team1, team2 = random.sample(self.space_teams, 2)
            g = GameFactory.create(competition=self.space)
            GameScoreFactory.create(game=g, team=team1)
            GameScoreFactory.create(game=g, team=team2)
 def test_add_team_member_max_team_size(self):
     """add_team_member throws an exception if team is full"""
     t = TeamFactory.create(name="Team Awesome", competition=self.space,
                            num_members=3)
     with self.assertRaises(TeamException):
         t.add_team_member(self.alice)
 def test_slug_set(self):
     """Teams should have their slugs set automatically"""
     t = TeamFactory.create(name="Team Awesome", competition=self.space)
     self.assertEqual("team-awesome", t.slug)
 def test_duplicate_names(self):
     """Teams with duplicate names are not ok"""
     TeamFactory.create(name="Team Awesome", competition=self.space)
     with self.assertRaises(IntegrityError):
         TeamFactory.create(name="Team Awesome", competition=self.space)