Skip to content

Commit

Permalink
Change old-requests to all-requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeweng committed Nov 22, 2024
1 parent 6315065 commit bf105d4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
16 changes: 7 additions & 9 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3954,12 +3954,14 @@ class OwnershipRequestManagementViewSet(viewsets.ModelViewSet):
lookup_field = "requester__username"

def get_queryset(self):
if self.action != "old_requests":
if self.action != "all_requests":
return OwnershipRequest.objects.filter(
club__code=self.kwargs["club_code"], withdrawn=False
)
else:
return OwnershipRequest.objects.filter(withdrawn=False)
return OwnershipRequest.objects.filter(withdrawn=False).order_by(
"created_at"
)

@action(detail=True, methods=["post"])
def accept(self, request, *args, **kwargs):
Expand Down Expand Up @@ -3995,9 +3997,9 @@ def accept(self, request, *args, **kwargs):
return Response({"success": True})

@action(detail=False, methods=["get"], permission_classes=[IsSuperuser])
def old_requests(self, request, *args, **kwargs):
def all_requests(self, request, *args, **kwargs):
"""
View unaddressed ownership requests that are older than a week.
View unaddressed ownership requests, sorted by date.
---
requestBody: {}
responses:
Expand Down Expand Up @@ -4045,11 +4047,7 @@ def old_requests(self, request, *args, **kwargs):
---
"""

queryset = OwnershipRequest.objects.filter(
withdrawn=False, created_at__lte=timezone.now() - datetime.timedelta(days=7)
)

serializer = self.get_serializer(queryset, many=True)
serializer = self.get_serializer(self.get_queryset(), many=True)

return Response(serializer.data)

Expand Down
79 changes: 51 additions & 28 deletions backend/tests/clubs/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3003,25 +3003,6 @@ def test_ownership_requests_create_and_view(self):
self.assertEqual(resp.status_code, 200, resp.content)
self.assertEqual(resp.json()["club"], self.club1.code, resp.content)

# Superuser should only see old requests through old-requests
self.client.login(username=self.user5.username, password="test")
resp = self.client.get(
reverse("club-ownership-requests-old-requests", args=("anystring",))
)
self.assertEqual(resp.status_code, 200, resp.content)
self.assertEqual(len(resp.json()), 0, resp.content)

OwnershipRequest.objects.filter(club=self.club1, requester=self.user2).update(
created_at=timezone.now() - timezone.timedelta(days=8)
)

self.client.login(username=self.user5.username, password="test")
resp = self.client.get(
reverse("club-ownership-requests-old-requests", args=("anystring",))
)
self.assertEqual(resp.status_code, 200, resp.content)
self.assertEqual(len(resp.json()), 1, resp.content)

def test_ownership_requests_withdraw(self):
"""
Test the ownership requests withdraw feature
Expand Down Expand Up @@ -3098,15 +3079,6 @@ def test_ownership_requests_withdraw(self):
self.assertEqual(resp.status_code, 200, resp.content)
self.assertEqual(len(resp.json()), 1, resp.content)

# Recreated ownership requests are not old

self.client.login(username=self.user5.username, password="test")
resp = self.client.get(
reverse("club-ownership-requests-old-requests", args=("anystring",))
)
self.assertEqual(resp.status_code, 200, resp.content)
self.assertEqual(len(resp.json()), 0, resp.content)

def test_ownership_requests_accept(self):
"""
Test the ownership requests accept feature
Expand Down Expand Up @@ -3221,6 +3193,57 @@ def test_ownership_requests_destroy(self):
0,
)

def test_ownership_requests_list_all_requests(self):
"""
Test the ownership requests list all requests feature
"""

self.client.login(username=self.user5.username, password="test")
resp = self.client.get(
reverse("club-ownership-requests-all-requests", args=("anystring",))
)
self.assertEqual(resp.status_code, 200, resp.content)
self.assertEqual(len(resp.json()), 0, resp.content)

OwnershipRequest.objects.create(
club=self.club1,
requester=self.user2,
)
OwnershipRequest.objects.filter(
club=self.club1,
requester=self.user2,
).update(created_at=timezone.now() - timezone.timedelta(days=1))

OwnershipRequest.objects.create(
club=self.club1,
requester=self.user3,
)
OwnershipRequest.objects.filter(
club=self.club1,
requester=self.user3,
).update(created_at=timezone.now() - timezone.timedelta(days=100))

OwnershipRequest.objects.create(
club=self.club1,
requester=self.user4,
)
OwnershipRequest.objects.filter(
club=self.club1,
requester=self.user4,
).update(created_at=timezone.now() - timezone.timedelta(days=10))

self.client.login(username=self.user5.username, password="test")
resp = self.client.get(
reverse("club-ownership-requests-all-requests", args=("anystring",))
)
self.assertEqual(resp.status_code, 200, resp.content)
self.assertEqual(len(resp.json()), 3, resp.content)

# Check oldest requests first
self.assertEqual(resp.json()[0]["username"], self.user3.username, resp.content)
self.assertEqual(resp.json()[1]["username"], self.user4.username, resp.content)
self.assertEqual(resp.json()[2]["username"], self.user2.username, resp.content)

def test_membership_requests_create_and_view(self):
"""
Test the membership requests creation and viewing permissions
Expand Down

0 comments on commit bf105d4

Please sign in to comment.