def test_authenticated_existing_user_match(self): """Tests the workflow when the invitation is connected to a user and that same user is authenticated and opens the invitation""" invite = GroupInviteFactory.create(group=self.group, email=self.user.email, invitor=self.invitor) # Login self.client.login(email=self.user.email, password='******') # Try accessing the group page, it should redirect to the login page response = self.client.get(reverse('group-posts', kwargs={'group': self.group.slug})) self.assertEqual(response.status_code, 302) response = self.client.get(invite.get_response_url()) invite = GroupInvite.objects.get(id=invite.id) # Refresh self.assertTemplateUsed(response, 'groups/group_invite_accept.html') self.assertContains(response, invite.get_response_url() + "y/") self.assertNotContains(response, reverse("account_logout") + "?next=" + invite.get_response_url()) self.assertFalse(invite.responded) # Accept the invitation response = self.client.get(invite.get_response_url() + "y/") invite = GroupInvite.objects.get(id=invite.id) # Refresh self.assertRedirects(response, reverse('group-posts', kwargs={'group': self.group.slug})) self.assertTrue(invite.responded) self.assertEqual(invite.user, self.user) # Invitor has to be notified self.check_invitor_notification() # Now the user should be logged in and able to view the posts from the group response = self.client.get(reverse('group-posts', kwargs={'group': self.group.slug})) self.assertEqual(response.status_code, 200, "User should now be able to access group page")
def test_already_used(self): """Test the case where an invitiation is already used""" invite = GroupInviteFactory.create(group=self.group) invite.accept(self.user) response = self.client.get(invite.get_response_url()) self.assertTemplateUsed(response, 'groups/group_invite_already_used.html') self.assertNotIn('form', response.context.keys())
def test_authenticated_no_user(self): """Tests the workflow when the invitiation isn't connected to a user yet but a authenticated user opens the invitation""" invite = GroupInviteFactory.create(group=self.group, invitor=self.invitor) # Login self.client.login(email=self.user.email, password='******') response = self.client.get(invite.get_response_url()) # Response should almost be the same as previous testcase, # only show an extra link where the user can logout and create a new account self.assertTemplateUsed(response, 'groups/group_invite_accept.html') self.assertContains(response, reverse("account_logout") + "?next=" + invite.get_response_url()) self.assertContains(response, invite.get_response_url() + "y/") # Accept the invitation response = self.client.get(invite.get_response_url() + "y/") invite = GroupInvite.objects.get(id=invite.id) # Refresh self.assertRedirects(response, reverse('group-posts', kwargs={'group': self.group.slug})) self.assertTrue(invite.responded) self.assertEqual(invite.user, self.user) self.assertNotEqual(invite.email, self.user.email) # The email in the invitation was different, it shouldn't be reset # Invitor has to be notified self.check_invitor_notification() # Now the user should be logged in and able to view the posts from the group response = self.client.get(reverse('group-posts', kwargs={'group': self.group.slug})) self.assertEqual(response.status_code, 200, "User should now be able to access group page")
def test_double_invitation(self): """Create two invitations and let it be accepted by the same user. It should not result in two memberships, only one""" invites = GroupInviteFactory.create_batch(2, group=self.group) for invite in invites: invite.accept(self.user) for invite in invites: self.assertTrue(invite.responded) self.assertEquals(self.user.membership_set.count(), 1)
def test_anonymous_existing_user(self): """Tests the workflow when a user is connected to the invite already but the user accepting it is not logged in""" user2 = UserFactory.create() invite = GroupInviteFactory.create(group=self.group, email=user2.email, invitor=self.invitor) self.assertEqual(invite.user, user2) response = self.client.get(invite.get_response_url()) invite = GroupInvite.objects.get(id=invite.id) # Refresh self.assertTemplateUsed(response, 'groups/group_invite_already_has_user.html') self.assertContains(response, reverse("account_login") + "?next=" + invite.get_response_url()) self.assertFalse(invite.responded)
def test_anonymous_new_user(self): """Tests the workflow when a new user is invited""" invite = GroupInviteFactory.create(group=self.group, invitor=self.invitor) # Try accessing the group page, it should redirect to the login page response = self.client.get(reverse('group-posts', kwargs={'group': self.group.slug})) self.assertEqual(response.status_code, 302) # Open the invitation response = self.client.get(invite.get_response_url()) invite = GroupInvite.objects.get(id=invite.id) # Refresh # It should show the form, no action yet self.assertTemplateUsed(response, 'groups/group_invite_accept_form.html') self.assertIn('form', response.context.keys()) self.assertFalse(invite.responded) self.assertEqual(response.context['form'].initial['email'], invite.email) self.assertEqual(response.context['form'].initial['name'], invite.name) data = {'email': invite.email, 'name': invite.name} # Try posting without a password, form should return an error response = self.client.post(invite.get_response_url(), data) self.assertFalse(response.context['form'].is_valid()) data['password1'] = 'pass12345' data['password2'] = 'pass12345' # Try posting with correct data, form should be saved, user account should be created # and invitation should be accepted response = self.client.post(invite.get_response_url(), data) self.assertEqual(response.status_code, 302, "User should be redirected to the group overview page") invite = GroupInvite.objects.get(id=invite.id) # Refresh self.assertTrue(invite.responded) self.assertIsNotNone(invite.user, "User attribute should be set") self.assertEquals(invite.user.email, invite.email) self.assertEquals(invite.user.name, invite.name) # Invitor has to be notified self.check_invitor_notification() # Now the user should be logged in and able to view the posts from the group response = self.client.get(reverse('group-posts', kwargs={'group': self.group.slug})) self.assertEqual(response.status_code, 200, "User should now be able to access group page")
def test_authenticated_existing_user_mismatch(self): """Tests the workflow when the invitation is connected to a user but another user is authenticated and opens the invitation""" user2 = UserFactory.create() invite = GroupInviteFactory.create(group=self.group, email=user2.email, invitor=self.invitor) self.assertEqual(invite.user, user2) # Login self.client.login(email=self.user.email, password='******') response = self.client.get(invite.get_response_url()) invite = GroupInvite.objects.get(id=invite.id) # Refresh self.assertTemplateUsed(response, 'groups/group_invite_already_has_user.html') self.assertContains(response, reverse("account_logout") + "?next=" + reverse("account_login")) self.assertFalse(invite.responded)
def test_already_invited_but_out_of_group(self): """Tests the case where the user has been invited previously but has left the group. In this case, it should be able to invite the person again""" invite = GroupInviteFactory.create(email=self.user.email, group=self.group) invite.accept(self.user) self.assertIn(self.user, self.group.users.all()) data = { 'email': self.user.email, 'first_name': 'John', 'last_name': 'Doe', } form = GroupInviteForm(group=self.group, invitor=self.invitor, data=data) self.assertFalse(form.is_valid()) self.group.membership_set.get(user=self.user).delete() form = GroupInviteForm(group=self.group, invitor=self.invitor, data=data) self.assertTrue(form.is_valid())