def invite(request, user_id): # Cannot invite ourself if request.user.pk == int(user_id): raise Http404 # Find the friend to request new_friend = get_object_or_404(Profile, user__pk=user_id) # Create the friend request friend_request = FriendRequest(user=request.user, to=new_friend.user) friend_request.save() # Alert the requester and the requested send_localized_mail( new_friend.user, _('[R.A.S.] Friend request'), 'RandoAmisSecours/friends/request_email.html', { 'from': friend_request.user.get_full_name(), 'to': friend_request.to.get_full_name(), 'accept': request.build_absolute_uri( reverse('friends.accept', args=[friend_request.pk])), 'refuse': request.build_absolute_uri( reverse('friends.refuse', args=[friend_request.pk])) }) messages.success( request, _("Friend request sent to «%(name)s»") % ({ 'name': new_friend.user.get_full_name() })) return HttpResponseRedirect(reverse('friends.search'))
def test_friends(self): self.helper_test_login(reverse('friends.search')) self.helper_test_login(reverse('friends.invite', args=[self.user2.pk]), redirect=reverse('friends.search')) FriendRequest.objects.all().delete() FR = FriendRequest(user=self.user2, to=self.user) FR.save() self.helper_test_login(reverse('friends.accept', args=[FR.pk]), redirect=reverse('accounts.profile')) self.helper_test_login(reverse('friends.delete', args=[self.user2.pk]), redirect=reverse('accounts.profile')) FR = FriendRequest(user=self.user2, to=self.user) FR.save() self.helper_test_login(reverse('friends.refuse', args=[FR.pk]), redirect=reverse('accounts.profile')) self.helper_test_login(reverse('friends.invite', args=[self.user2.pk]), redirect=reverse('friends.search')) self.helper_test_login(reverse('friends.cancel', args=[FriendRequest.objects.all()[0].pk]), redirect=reverse('accounts.profile'))
def test_accept_simple(self): # Simple accept session FR = FriendRequest(user=self.user2, to=self.user1) FR.save() self.assertEqual(FriendRequest.objects.all().count(), 1) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.accept', args=[FR.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 0) self.assertEqual(self.user1.profile.friends.all().count(), 1) self.assertEqual(self.user1.profile.friends.all()[0].user.pk, self.user2.pk) self.assertEqual(self.user2.profile.friends.all().count(), 1) self.assertEqual(self.user2.profile.friends.all()[0].user.pk, self.user1.pk) self.assertEqual(self.user3.profile.friends.all().count(), 0)
def test_accept_simple(self): # Simple accept session FR = FriendRequest(user=self.user2, to=self.user1) FR.save() self.assertEqual(FriendRequest.objects.all().count(), 1) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.accept', args=[FR.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 0) self.assertEqual(self.user1.profile.friends.all().count(), 1) self.assertEqual(self.user1.profile.friends.all()[0].user.pk, self.user2.pk) self.assertEqual(self.user2.profile.friends.all().count(), 1) self.assertEqual(self.user2.profile.friends.all()[0].user.pk, self.user1.pk) self.assertEqual(self.user3.profile.friends.all().count(), 0)
def test_cancel(self): FR1 = FriendRequest(user=self.user1, to=self.user3) FR1.save() FR2 = FriendRequest(user=self.user2, to=self.user1) FR2.save() self.assertEqual(FriendRequest.objects.all().count(), 2) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.cancel', args=[FR1.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 1) self.assertEqual(FriendRequest.objects.all()[0].pk, FR2.pk) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.cancel', args=[FR1.pk])) self.assertEqual(response.status_code, 404)
def test_cancel(self): FR1 = FriendRequest(user=self.user1, to=self.user3) FR1.save() FR2 = FriendRequest(user=self.user2, to=self.user1) FR2.save() self.assertEqual(FriendRequest.objects.all().count(), 2) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.cancel', args=[FR1.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 1) self.assertEqual(FriendRequest.objects.all()[0].pk, FR2.pk) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.cancel', args=[FR1.pk])) self.assertEqual(response.status_code, 404)
def test_friends(self): self.helper_test_login(reverse('friends.search')) self.helper_test_login(reverse('friends.invite', args=[self.user2.pk]), redirect=reverse('friends.search')) FriendRequest.objects.all().delete() FR = FriendRequest(user=self.user2, to=self.user) FR.save() self.helper_test_login(reverse('friends.accept', args=[FR.pk]), redirect=reverse('accounts.profile')) self.helper_test_login(reverse('friends.delete', args=[self.user2.pk]), redirect=reverse('accounts.profile')) FR = FriendRequest(user=self.user2, to=self.user) FR.save() self.helper_test_login(reverse('friends.refuse', args=[FR.pk]), redirect=reverse('accounts.profile')) self.helper_test_login(reverse('friends.invite', args=[self.user2.pk]), redirect=reverse('friends.search')) self.helper_test_login(reverse( 'friends.cancel', args=[FriendRequest.objects.all()[0].pk]), redirect=reverse('accounts.profile'))
def invite(request, user_id): # Cannot invite ourself if request.user.pk == int(user_id): raise Http404 # Find the friend to request new_friend = get_object_or_404(Profile, user__pk=user_id) # Create the friend request friend_request = FriendRequest(user=request.user, to=new_friend.user) friend_request.save() # Alert the requester and the requested send_localized_mail(new_friend.user, _('[R.A.S.] Friend request'), 'RandoAmisSecours/friends/request_email.html', {'from': friend_request.user.get_full_name(), 'to': friend_request.to.get_full_name(), 'accept': request.build_absolute_uri(reverse('friends.accept', args=[friend_request.pk])), 'refuse': request.build_absolute_uri(reverse('friends.refuse', args=[friend_request.pk]))}) messages.success(request, _("Friend request sent to «%(name)s»") % ({'name': new_friend.user.get_full_name()})) return HttpResponseRedirect(reverse('friends.search'))
def test_accept_advance(self): # Two at a time FR1 = FriendRequest(user=self.user3, to=self.user1) FR1.save() FR2 = FriendRequest(user=self.user2, to=self.user1) FR2.save() self.assertEqual(FriendRequest.objects.all().count(), 2) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.accept', args=[FR2.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 1) self.assertEqual(self.user1.profile.friends.all().count(), 1) self.assertEqual(self.user1.profile.friends.all()[0].user.pk, self.user2.pk) self.assertEqual(self.user2.profile.friends.all().count(), 1) self.assertEqual(self.user2.profile.friends.all()[0].user.pk, self.user1.pk) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.accept', args=[FR2.pk])) self.assertEqual(response.status_code, 404) response = self.client.get(reverse('friends.accept', args=[FR1.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 0) self.assertEqual(self.user1.profile.friends.all().count(), 2) self.assertEqual(self.user1.profile.friends.all()[0].user.pk, self.user2.pk) self.assertEqual(self.user1.profile.friends.all()[1].user.pk, self.user3.pk) self.assertEqual(self.user2.profile.friends.all().count(), 1) self.assertEqual(self.user2.profile.friends.all()[0].user.pk, self.user1.pk) self.assertEqual(self.user3.profile.friends.all().count(), 1) self.assertEqual(self.user3.profile.friends.all()[0].user.pk, self.user1.pk) response = self.client.get(reverse('friends.accept', args=[FR1.pk])) self.assertEqual(response.status_code, 404) # Only the 'to' can accept it FR3 = FriendRequest(user=self.user3, to=self.user2) FR3.save() response = self.client.get(reverse('friends.accept', args=[FR3.pk])) self.assertEqual(response.status_code, 404)
def test_accept_advance(self): # Two at a time FR1 = FriendRequest(user=self.user3, to=self.user1) FR1.save() FR2 = FriendRequest(user=self.user2, to=self.user1) FR2.save() self.assertEqual(FriendRequest.objects.all().count(), 2) self.assertEqual(self.user1.profile.friends.all().count(), 0) self.assertEqual(self.user2.profile.friends.all().count(), 0) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.accept', args=[FR2.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 1) self.assertEqual(self.user1.profile.friends.all().count(), 1) self.assertEqual(self.user1.profile.friends.all()[0].user.pk, self.user2.pk) self.assertEqual(self.user2.profile.friends.all().count(), 1) self.assertEqual(self.user2.profile.friends.all()[0].user.pk, self.user1.pk) self.assertEqual(self.user3.profile.friends.all().count(), 0) response = self.client.get(reverse('friends.accept', args=[FR2.pk])) self.assertEqual(response.status_code, 404) response = self.client.get(reverse('friends.accept', args=[FR1.pk])) self.assertRedirects(response, reverse('accounts.profile')) self.assertEqual(FriendRequest.objects.all().count(), 0) self.assertEqual(self.user1.profile.friends.all().count(), 2) self.assertEqual(self.user1.profile.friends.all()[0].user.pk, self.user2.pk) self.assertEqual(self.user1.profile.friends.all()[1].user.pk, self.user3.pk) self.assertEqual(self.user2.profile.friends.all().count(), 1) self.assertEqual(self.user2.profile.friends.all()[0].user.pk, self.user1.pk) self.assertEqual(self.user3.profile.friends.all().count(), 1) self.assertEqual(self.user3.profile.friends.all()[0].user.pk, self.user1.pk) response = self.client.get(reverse('friends.accept', args=[FR1.pk])) self.assertEqual(response.status_code, 404) # Only the 'to' can accept it FR3 = FriendRequest(user=self.user3, to=self.user2) FR3.save() response = self.client.get(reverse('friends.accept', args=[FR3.pk])) self.assertEqual(response.status_code, 404)
def test_friend_request(self): self.user2 = User.objects.create_user('tester', '*****@*****.**', 'ertyfjnbfvfceqsryuj') self.user3 = User.objects.create_user('Sophocle', '*****@*****.**', 'gzgvaryurvyyjchrvyhubtr') FR = FriendRequest(user=self.user1, to=self.user2) FR.save() FR2 = FriendRequest(user=self.user3, to=self.user1) FR2.save() FR3 = FriendRequest(user=self.user3, to=self.user2) FR3.save() response = self.client.get(reverse('accounts.profile')) self.assertEqual(response.status_code, 200) ctx = response.context self.assertEqual(len(ctx['friend_requests']), 1) self.assertEqual(ctx['friend_requests'][0].user, self.user3) self.assertEqual(ctx['friend_requests'][0].to, self.user1) self.assertEqual(len(ctx['friend_requests_sent']), 1) self.assertEqual(ctx['friend_requests_sent'][0].user, self.user1) self.assertEqual(ctx['friend_requests_sent'][0].to, self.user2)
def test_friend_request(self): self.user2 = User.objects.create_user('tester', '*****@*****.**', 'ertyfjnbfvfceqsryuj') self.user3 = User.objects.create_user('Sophocle', '*****@*****.**', 'gzgvaryurvyyjchrvyhubtr') FR = FriendRequest(user=self.user1, to=self.user2) FR.save() FR2 = FriendRequest(user=self.user3, to=self.user1) FR2.save() FR3 = FriendRequest(user=self.user3, to=self.user2) FR3.save() response = self.client.get(reverse('accounts.profile')) self.assertEqual(response.status_code, 200) ctx = response.context self.assertEqual(len(ctx['friend_requests']), 1) self.assertEqual(ctx['friend_requests'][0].user, self.user3) self.assertEqual(ctx['friend_requests'][0].to, self.user1) self.assertEqual(len(ctx['friend_requests_sent']), 1) self.assertEqual(ctx['friend_requests_sent'][0].user, self.user1) self.assertEqual(ctx['friend_requests_sent'][0].to, self.user2)