From 44a0cbb22e57bed011895429030d7d879841db42 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 27 Jan 2022 10:36:05 +0100
Subject: [PATCH 5/5] api: subscription detail endpoint (#61161)

---
 chrono/api/urls.py             |  5 +++
 chrono/api/views.py            | 26 ++++++++++++++-
 tests/api/test_subscription.py | 61 ++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/chrono/api/urls.py b/chrono/api/urls.py
index 73f3280..3bf5309 100644
--- a/chrono/api/urls.py
+++ b/chrono/api/urls.py
@@ -90,6 +90,11 @@ urlpatterns = [
     ),
     url(
         r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/$',
+        views.subscriptions,
+        name='api-agenda-subscriptions',
+    ),
+    url(
+        r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/(?P<subscription_pk>\d+)/$',
         views.subscription,
         name='api-agenda-subscription',
     ),
diff --git a/chrono/api/views.py b/chrono/api/views.py
index c3ae4f2..794e73b 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -1893,7 +1893,7 @@ class SubscriptionFilter(filters.FilterSet):
         ]
 
 
-class SubscriptionAPI(ListAPIView):
+class SubscriptionsAPI(ListAPIView):
     filter_backends = (filters.DjangoFilterBackend,)
     serializer_class = serializers.SubscriptionSerializer
     filterset_class = SubscriptionFilter
@@ -1935,6 +1935,30 @@ class SubscriptionAPI(ListAPIView):
         return Response({'err': 0, 'id': subscription.pk})
 
 
+subscriptions = SubscriptionsAPI.as_view()
+
+
+class SubscriptionAPI(APIView):
+    permission_classes = (permissions.IsAuthenticated,)
+    serializer_class = serializers.SubscriptionSerializer
+
+    def initial(self, request, *args, **kwargs):
+        super().initial(request, *args, **kwargs)
+        self.subscription = get_object_or_404(
+            Subscription,
+            pk=kwargs.get('subscription_pk'),
+            agenda__kind='events',
+            agenda__slug=kwargs.get('agenda_identifier'),
+        )
+
+    def get(self, request, *args, **kwargs):
+        serializer = self.serializer_class(self.subscription)
+        response = serializer.data
+        response.update(self.subscription.extra_data or {})
+        response.update({'err': 0})
+        return Response(response)
+
+
 subscription = SubscriptionAPI.as_view()
 
 
diff --git a/tests/api/test_subscription.py b/tests/api/test_subscription.py
index 39c7dfa..907abe8 100644
--- a/tests/api/test_subscription.py
+++ b/tests/api/test_subscription.py
@@ -212,3 +212,64 @@ def test_api_create_subscription(app, user):
     }
     resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
     assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
+
+
+def test_api_get_subscription(app, user):
+    agenda = Agenda.objects.create(label='Foo bar', kind='events')
+    subscription = Subscription.objects.create(
+        agenda=agenda,
+        user_external_id='xxx',
+        user_first_name='Foo',
+        user_last_name='BAR',
+        user_email='foo@bar.com',
+        user_phone_number='06',
+        extra_data={'foo': 'bar'},
+        date_start=datetime.date(year=2021, month=9, day=1),
+        date_end=datetime.date(year=2021, month=10, day=1),
+    )
+    other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
+    other_subscription = Subscription.objects.create(
+        agenda=other_agenda,
+        user_external_id='xxx',
+        date_start=datetime.date(year=2021, month=9, day=1),
+        date_end=datetime.date(year=2021, month=10, day=1),
+    )
+
+    resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=401)
+
+    app.authorization = ('Basic', ('john.doe', 'password'))
+
+    resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
+    assert resp.json == {
+        'id': subscription.pk,
+        'user_external_id': 'xxx',
+        'user_first_name': 'Foo',
+        'user_last_name': 'BAR',
+        'user_email': 'foo@bar.com',
+        'user_phone_number': '06',
+        'date_start': '2021-09-01',
+        'date_end': '2021-10-01',
+        'foo': 'bar',
+        'err': 0,
+    }
+
+    resp = app.get('/api/agenda/%s/subscription/%s/' % (other_agenda.slug, other_subscription.pk))
+    assert resp.json == {
+        'id': other_subscription.pk,
+        'user_external_id': 'xxx',
+        'user_first_name': '',
+        'user_last_name': '',
+        'user_email': '',
+        'user_phone_number': '',
+        'date_start': '2021-09-01',
+        'date_end': '2021-10-01',
+        'err': 0,
+    }
+
+    app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, other_subscription.pk), status=404)
+    app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, 0), status=404)
+    app.get('/api/agenda/%s/subscription/%s/' % ('unknown', subscription.pk), status=404)
+    for kind in ['meetings', 'virtual']:
+        agenda.kind = kind
+        agenda.save()
+        app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=404)
-- 
2.34.1

