def test_accept_twice(self): """If a user follows an 'accept' link twice from an email, we want to deal with the case where they didn't make a prediction. So we want to allow accepting twice. """ # create a team as test user 1 and invite testie1 return self._login() url = reverse('add_league') post_data = {'name':'asdf1', 'form-0-email':'*****@*****.**', 'form-1-email': '*****@*****.**', 'form-2-email': self.user2.email, 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 3} response = self.client.post(url, post_data) league = League.objects.get(name='asdf1') user = CustomUser.objects.get(email='*****@*****.**') self.assertFalse(bool(getCurrentPrediction(user))) self.assertEquals(len(league.members()), 1) self.assertFalse(user.is_active) self.client.logout() # this user accepts the invite; their account becomes active key = RegistrationProfile.objects.get(email='*****@*****.**')\ .activation_key confirm_url = reverse('accept_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(confirm_url, follow=True) self.assertRedirects(response, reverse('make_prediction')) # try to accept the invite again response = self.client.get(confirm_url, follow=True) self.assertRedirects(response, reverse('make_prediction'))
def json_get_history(request, email=""): if not request.user.is_anonymous(): if email: user = CustomUser.objects.get(email=email) else: user = request.user prediction = getCurrentPrediction(user) history = prediction.positions\ .order_by('date')\ .all() data = { 'count': history.count(), 'positions': [x.position for x in history], 'email': user.email } else: data = {'count': 0, 'positions': [], 'email': ''} return HttpResponse(simplejson.dumps(data), mimetype='application/json')
def test_accept_twice(self): """If a user follows an 'accept' link twice from an email, we want to deal with the case where they didn't make a prediction. So we want to allow accepting twice. """ # create a team as test user 1 and invite testie1 return self._login() url = reverse('add_league') post_data = { 'name': 'asdf1', 'form-0-email': '*****@*****.**', 'form-1-email': '*****@*****.**', 'form-2-email': self.user2.email, 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 3 } response = self.client.post(url, post_data) league = League.objects.get(name='asdf1') user = CustomUser.objects.get(email='*****@*****.**') self.assertFalse(bool(getCurrentPrediction(user))) self.assertEquals(len(league.members()), 1) self.assertFalse(user.is_active) self.client.logout() # this user accepts the invite; their account becomes active key = RegistrationProfile.objects.get(email='*****@*****.**')\ .activation_key confirm_url = reverse('accept_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(confirm_url, follow=True) self.assertRedirects(response, reverse('make_prediction')) # try to accept the invite again response = self.client.get(confirm_url, follow=True) self.assertRedirects(response, reverse('make_prediction'))
def test_join_workflow(self): """When users apply or are invited to join a league, they must go through a simple approve-type workflow before they're really in it. New users get to verify their account at the same time. """ # create a team as test user 1 and invite testie1 self._login() url = reverse('add_league') post_data = {'name':'asdf1', 'form-0-email':'*****@*****.**', 'form-1-email': '*****@*****.**', 'form-2-email': self.user2.email, 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 3} response = self.client.post(url, post_data) league = League.objects.get(name='asdf1') user = CustomUser.objects.get(email='*****@*****.**') self.assertFalse(bool(getCurrentPrediction(user))) self.assertEquals(len(league.members()), 1) self.assertFalse(user.is_active) self.client.logout() # this user accepts the invite; their account becomes active key = RegistrationProfile.objects.get(email='*****@*****.**')\ .activation_key confirm_url = reverse('accept_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(confirm_url, follow=True) self.assertRedirects(response, reverse('make_prediction')) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) user = CustomUser.objects.get(email='*****@*****.**') self.assertTrue(user.is_active) # this user tries to accept an invite that's never been issued key = RegistrationProfile.objects.get(email=self.user3.email)\ .activation_key confirm_url = reverse('accept_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(confirm_url) self.assertContains(response, "Sorry") league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) # and this user declines the invite key = RegistrationProfile.objects.get(email=self.user2.email)\ .activation_key decline_url = reverse('decline_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(decline_url, follow=True) self.assertRedirects(response, reverse('home')) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) declined = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_DECLINED) self.assertEquals(len(declined.all()), 1) # now another user wants to join this team... self.client.logout() self.client.login(username=self.user3.username, password=PASSWORD) key = RegistrationProfile.objects.get(email=self.user3.email)\ .activation_key accept_url = reverse('approve_league_application', kwargs=dict(email=self.user3.email, league=league.slug)) # ...but you can't accept someone who's not applied response = self.client.get(accept_url) self.assertContains(response, "Sorry") post_data = {'name':'asdf2', 'join_leagues':[league.pk], 'form-0-email':'*****@*****.**', 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 1} response = self.client.post(url, post_data, follow=True) self.assertRedirects(response, reverse('make_prediction')) self.assertEquals(mail.outbox[-1].subject, 'Application to join asdf1') applied = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_APPLIED) self.assertEquals(len(applied.all()), 1) # now try to join again; shouldn't be able to post_data = {'name':'asdf2', 'join_leagues':[league.pk], 'form-0-email':'*****@*****.**', 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 1} self.assertRaises(LeagueJoinStates.InvalidStateTransition, self.client.post, url, post_data) # now they've applied, we can accept them; but only if we're # logged in as the league owner response = self.client.get(accept_url) self.assertContains(response, "Sorry") self.client.logout() # try again as owner self._login() response = self.client.get(accept_url, follow=True) self.assertRedirects(response, reverse('make_prediction')) applied = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_APPLIED) self.assertEquals(len(applied.all()), 0) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 3) # but we can reject people who are in our own team, too reject_url = reverse('reject_league_application', kwargs=dict(email=self.user3.email, league=league.slug)) response = self.client.get(reject_url, follow=True) rejected = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_REJECTED) self.assertEquals(len(rejected.all()), 1) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) # and now that person can no longer apply to join self.client.logout() self.client.login(username=self.user3.username, password=PASSWORD) post_data = {'name':'asdf2', 'join_leagues':[league.pk], 'form-0-email':'*****@*****.**', 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 1} self.assertRaises(LeagueJoinStates.InvalidStateTransition, self.client.post, url, post_data) applied = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_APPLIED) self.assertEquals(len(applied.all()), 0)
def test_join_workflow(self): """When users apply or are invited to join a league, they must go through a simple approve-type workflow before they're really in it. New users get to verify their account at the same time. """ # create a team as test user 1 and invite testie1 self._login() url = reverse('add_league') post_data = { 'name': 'asdf1', 'form-0-email': '*****@*****.**', 'form-1-email': '*****@*****.**', 'form-2-email': self.user2.email, 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 3 } response = self.client.post(url, post_data) league = League.objects.get(name='asdf1') user = CustomUser.objects.get(email='*****@*****.**') self.assertFalse(bool(getCurrentPrediction(user))) self.assertEquals(len(league.members()), 1) self.assertFalse(user.is_active) self.client.logout() # this user accepts the invite; their account becomes active key = RegistrationProfile.objects.get(email='*****@*****.**')\ .activation_key confirm_url = reverse('accept_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(confirm_url, follow=True) self.assertRedirects(response, reverse('make_prediction')) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) user = CustomUser.objects.get(email='*****@*****.**') self.assertTrue(user.is_active) # this user tries to accept an invite that's never been issued key = RegistrationProfile.objects.get(email=self.user3.email)\ .activation_key confirm_url = reverse('accept_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(confirm_url) self.assertContains(response, "Sorry") league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) # and this user declines the invite key = RegistrationProfile.objects.get(email=self.user2.email)\ .activation_key decline_url = reverse('decline_league_invite', kwargs=dict(league=league.slug, key=key)) response = self.client.get(decline_url, follow=True) self.assertRedirects(response, reverse('home')) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) declined = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_DECLINED) self.assertEquals(len(declined.all()), 1) # now another user wants to join this team... self.client.logout() self.client.login(username=self.user3.username, password=PASSWORD) key = RegistrationProfile.objects.get(email=self.user3.email)\ .activation_key accept_url = reverse('approve_league_application', kwargs=dict(email=self.user3.email, league=league.slug)) # ...but you can't accept someone who's not applied response = self.client.get(accept_url) self.assertContains(response, "Sorry") post_data = { 'name': 'asdf2', 'join_leagues': [league.pk], 'form-0-email': '*****@*****.**', 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 1 } response = self.client.post(url, post_data, follow=True) self.assertRedirects(response, reverse('make_prediction')) self.assertEquals(mail.outbox[-1].subject, 'Application to join asdf1') applied = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_APPLIED) self.assertEquals(len(applied.all()), 1) # now try to join again; shouldn't be able to post_data = { 'name': 'asdf2', 'join_leagues': [league.pk], 'form-0-email': '*****@*****.**', 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 1 } self.assertRaises(LeagueJoinStates.InvalidStateTransition, self.client.post, url, post_data) # now they've applied, we can accept them; but only if we're # logged in as the league owner response = self.client.get(accept_url) self.assertContains(response, "Sorry") self.client.logout() # try again as owner self._login() response = self.client.get(accept_url, follow=True) self.assertRedirects(response, reverse('make_prediction')) applied = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_APPLIED) self.assertEquals(len(applied.all()), 0) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 3) # but we can reject people who are in our own team, too reject_url = reverse('reject_league_application', kwargs=dict(email=self.user3.email, league=league.slug)) response = self.client.get(reject_url, follow=True) rejected = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_REJECTED) self.assertEquals(len(rejected.all()), 1) league = League.objects.get(name='asdf1') self.assertEquals(len(league.members()), 2) # and now that person can no longer apply to join self.client.logout() self.client.login(username=self.user3.username, password=PASSWORD) post_data = { 'name': 'asdf2', 'join_leagues': [league.pk], 'form-0-email': '*****@*****.**', 'form-INITIAL_FORMS': 0, 'form-TOTAL_FORMS': 1 } self.assertRaises(LeagueJoinStates.InvalidStateTransition, self.client.post, url, post_data) applied = CustomUser.objects\ .filter(leaguejoinstates__state=STATE_APPLIED) self.assertEquals(len(applied.all()), 0)