def test_active_users_can_deactivate(self):
        """If a user has an active registration, they should be able
        to deactivate."""
        RegistrationFactory.create(user=self.alice, competition=self.galapagos)

        # Try viewing the unregister page
        with self.loggedInAs("alice", "123"):
            resp = self.client.rget('unregister_for',
                                    kwargs={'comp_slug': self.galapagos.slug})

        # Make sure we got the confirmation form
        self.assertIn('question', resp.context)
        self.assertIn('form', resp.context)

        with self.loggedInAs("alice", "123"):
            resp = self.client.rpost('unregister_for', follow=True,
                                     kwargs={'comp_slug': self.galapagos.slug},
                                     data={'confirmed': True})

        # No active registrations, but one inactive one.
        self.assertEqual(0, Registration.objects.filter(active=True).count())
        self.assertEqual(1, Registration.objects.filter(active=False).count())

        messages = list(resp.context['messages'])
        self.assertEqual(1, len(messages))
        self.assertIn("unregistered", messages[0].message)
        self.assertRedirects(resp, self.galapagos.get_absolute_url())
 def test_registrated_for_user(self):
     """List competition where a user is registered"""
     c1 = CompetitionFactory.create(name="MegaMinerAI1")
     c2 = CompetitionFactory.create(name="MegaMinerAI2")
     c3 = CompetitionFactory.create(name="MegaMinerAI3")
     alice = UserFactory.create()
     RegistrationFactory.create(user=alice, competition=c1)
     RegistrationFactory.create(user=alice, competition=c3)
     l = list(Competition.objects.user_registered(alice))
     self.assertEqual(2, len(l))
     self.assertIn(c1, l)
     self.assertIn(c3, l)
    def setUp(self):
        self.space = CompetitionFactory.create(name="Space")
        self.galapagos = CompetitionFactory.create(name="Galapagos")

        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_registered_users_cannot_view_register_page(self):
        """Users shouldn't be able to view the registration page once
        they have an active registration"""
        # Register Alice
        RegistrationFactory.create(user=self.alice, competition=self.galapagos)
        with self.loggedInAs("alice", "123"):
            kwargs = {'comp_slug': self.galapagos.slug}
            response = self.client.rget("register_for", follow=True,
                                        kwargs=kwargs)

        self.assertEqual(1, Registration.objects.all().count()) # still only 1
        messages = list(response.context['messages'])
        self.assertEqual(1, len(messages))
        self.assertIn("already", messages[0].message)
        self.assertRedirects(response, self.galapagos.get_absolute_url())
    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 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_inactive_users_can_register(self):
        """If a user has an inactive registration, they should be able
        to register again."""
        registration = RegistrationFactory.create(user=self.alice,
                                                  competition=self.galapagos)
        registration.deactivate()

        kwds = {'comp_slug': self.galapagos.slug}
        register_url = reverse("register_for", kwargs=kwds)
        with self.loggedInAs("alice", "123"):
            resp = self.client.get(register_url)
            data = self.fill_in_forms(resp)
            resp = self.client.post(register_url, follow=True, data=data)

        # Two registrations, one active, one inactive.
        self.assertEqual(1, Registration.objects.filter(active=True).count())
        self.assertEqual(1, Registration.objects.filter(active=False).count())