diff --git a/billing/tests.py b/billing/tests.py index 7be55fe..ce550dd 100644 --- a/billing/tests.py +++ b/billing/tests.py @@ -190,3 +190,11 @@ class TopupTests(TestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.json()["data"]), 1) self.assertEqual(response.json()["data"][0]["amount"], 100.00) + + def test_retrieve_single_topup(self): + topup = Topup.objects.create(amount=50.00, user=self.real_user) + url = reverse("retrieve-topup", kwargs={"pk": topup.pk}) + response = self.client.get(url, format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.json()["amount"], 50.00) + self.assertEqual(response.json()["user"]["id"], getattr(self.real_user, "id")) diff --git a/billing/urls.py b/billing/urls.py index 6d57ce4..143ae4e 100644 --- a/billing/urls.py +++ b/billing/urls.py @@ -8,6 +8,7 @@ from .views import ( DeletePaymentView, ListCreateTopupView, VerifyTopupPaymentAPIView, + TopupDetailAPIView, ) urlpatterns = [ @@ -28,7 +29,7 @@ urlpatterns = [ ), # Topups path("topup/", ListCreateTopupView.as_view(), name="create-list-topups"), - # path("topup//", TopupDetailAPIView.as_view(), name="retrieve-topup"), + path("topup//", TopupDetailAPIView.as_view(), name="retrieve-topup"), path( "topup//verify/", VerifyTopupPaymentAPIView.as_view(), diff --git a/billing/views.py b/billing/views.py index 9b4c4ae..2ccb5e7 100644 --- a/billing/views.py +++ b/billing/views.py @@ -288,6 +288,18 @@ class ListCreateTopupView(StaffEditorPermissionMixin, generics.ListCreateAPIView return queryset.filter(user=self.request.user) +class TopupDetailAPIView(StaffEditorPermissionMixin, generics.RetrieveAPIView): + queryset = Topup.objects.all() + serializer_class = TopupSerializer + lookup_field = "pk" + + def get_queryset(self): + queryset = super().get_queryset() + if getattr(self.request.user, "is_admin") or self.request.user.is_superuser: + return queryset + return queryset.filter(user=self.request.user) + + class VerifyTopupPaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView): queryset = Topup.objects.all() serializer_class = TopupSerializer