Skip to content

Commit

Permalink
made pennkeys lowercase and changed test case (#680)
Browse files Browse the repository at this point in the history
* made pennkeys lowercase and changed test case

* Added handling for edge cases

* fixed whitespace

* Fixed more whitespace. All stylizing

* changed the response schema

* made http404 message more succinct

* imports in alphabetical order I think

* removed one res = {}
  • Loading branch information
nruia-penn authored Nov 14, 2024
1 parent c7289c1 commit 765d0c3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
22 changes: 17 additions & 5 deletions backend/courses/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.auth import get_user_model
from django.db.models import Prefetch, Q
from django.http import JsonResponse
from django.http import Http404, JsonResponse
from django.shortcuts import get_object_or_404
from django_auto_prefetching import AutoPrefetchViewSetMixin
from rest_framework import generics, status
Expand Down Expand Up @@ -473,11 +473,12 @@ class FriendshipView(generics.ListAPIView):
"POST": {
201: "Friendship request created successfully.",
200: "Friendship request accepted successfully.",
404: "Username was None/ Username did not exist.",
409: "Friendship request already exists",
},
"DELETE": {
200: "Friendship rejected/deleted/cancelled successfully.",
404: "Friendship does not exist.",
404: "Friendship does not exist or Username does not exist.",
409: "Friendship request already rejected.",
},
}
Expand Down Expand Up @@ -526,7 +527,12 @@ def get_all_friendships(self):

def post(self, request):
sender = request.user
recipient = get_object_or_404(User, username=request.data.get("pennkey"))

username = request.data.get("pennkey")
if not username:
raise Http404("User not found.")

recipient = get_object_or_404(User, username=username.lower())

existing_friendship = (
self.get_all_friendships().filter(Q(recipient=recipient) | Q(sender=recipient)).first()
Expand Down Expand Up @@ -560,9 +566,15 @@ def post(self, request):
def delete(self, request):
# either deletes a friendship or cancels/rejects a friendship request
# (depends on who sends the request)
res = {}

sender = request.user
recipient = get_object_or_404(User, username=request.data.get("pennkey"))
res = {}

username = request.data.get("pennkey")
if not username:
raise Http404("User not found.")

recipient = get_object_or_404(User, username=username.lower())

existing_friendship = (
self.get_all_friendships().filter(Q(recipient=recipient) | Q(sender=recipient)).first()
Expand Down
29 changes: 28 additions & 1 deletion backend/tests/courses/test_friendship_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_basic_friendship(self):
self.assertTrue(UserProfile.objects.filter(user=u2).exists())
self.assertTrue(UserProfile.objects.filter(user=u1).exists())

make_friends = self.client1.post(reverse("friendship"), {"pennkey": u2.username})
make_friends = self.client1.post(reverse("friendship"), {"pennkey": u2.username.upper()})

self.assertEquals(make_friends.status_code, 201)
self.assertTrue(
Expand All @@ -37,6 +37,33 @@ def test_basic_friendship(self):
)
self.assertFalse(Friendship.objects.filter(sender=u2, recipient=u1).exists())

def test_none_username(self):
u1 = self.u1
u2 = self.u2

self.assertTrue(UserProfile.objects.filter(user=u2).exists())
self.assertTrue(UserProfile.objects.filter(user=u1).exists())

make_friends = self.client1.post(reverse("friendship"), {"pennkey": ""})

self.assertEquals(make_friends.status_code, 404)

def test_none_delete(self):
u1 = self.u1
u2 = self.u2

self.assertTrue(UserProfile.objects.filter(user=u2).exists())
self.assertTrue(UserProfile.objects.filter(user=u1).exists())

make_friends = self.client1.post(reverse("friendship"), {"pennkey": u2.username.upper()})
self.assertEquals(make_friends.status_code, 201)

delete_none = self.client1.delete(reverse("friendship"), {"pennkey": ""})
self.assertEquals(delete_none.status_code, 404)

delete_friends = self.client2.delete(reverse("friendship"), {"pennkey": u1.username})
self.assertEquals(delete_friends.status_code, 200)

def test_basic_friendship_accept(self):
u1 = self.u1
u2 = self.u2
Expand Down

0 comments on commit 765d0c3

Please sign in to comment.