Projet

Général

Profil

0005-api-subscription-detail-endpoint-61161.patch

Lauréline Guérin, 27 janvier 2022 11:05

Télécharger (5,09 ko)

Voir les différences:

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(-)
chrono/api/urls.py
90 90
    ),
91 91
    url(
92 92
        r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/$',
93
        views.subscriptions,
94
        name='api-agenda-subscriptions',
95
    ),
96
    url(
97
        r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/(?P<subscription_pk>\d+)/$',
93 98
        views.subscription,
94 99
        name='api-agenda-subscription',
95 100
    ),
chrono/api/views.py
1893 1893
        ]
1894 1894

  
1895 1895

  
1896
class SubscriptionAPI(ListAPIView):
1896
class SubscriptionsAPI(ListAPIView):
1897 1897
    filter_backends = (filters.DjangoFilterBackend,)
1898 1898
    serializer_class = serializers.SubscriptionSerializer
1899 1899
    filterset_class = SubscriptionFilter
......
1935 1935
        return Response({'err': 0, 'id': subscription.pk})
1936 1936

  
1937 1937

  
1938
subscriptions = SubscriptionsAPI.as_view()
1939

  
1940

  
1941
class SubscriptionAPI(APIView):
1942
    permission_classes = (permissions.IsAuthenticated,)
1943
    serializer_class = serializers.SubscriptionSerializer
1944

  
1945
    def initial(self, request, *args, **kwargs):
1946
        super().initial(request, *args, **kwargs)
1947
        self.subscription = get_object_or_404(
1948
            Subscription,
1949
            pk=kwargs.get('subscription_pk'),
1950
            agenda__kind='events',
1951
            agenda__slug=kwargs.get('agenda_identifier'),
1952
        )
1953

  
1954
    def get(self, request, *args, **kwargs):
1955
        serializer = self.serializer_class(self.subscription)
1956
        response = serializer.data
1957
        response.update(self.subscription.extra_data or {})
1958
        response.update({'err': 0})
1959
        return Response(response)
1960

  
1961

  
1938 1962
subscription = SubscriptionAPI.as_view()
1939 1963

  
1940 1964

  
tests/api/test_subscription.py
212 212
    }
213 213
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
214 214
    assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
215

  
216

  
217
def test_api_get_subscription(app, user):
218
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
219
    subscription = Subscription.objects.create(
220
        agenda=agenda,
221
        user_external_id='xxx',
222
        user_first_name='Foo',
223
        user_last_name='BAR',
224
        user_email='foo@bar.com',
225
        user_phone_number='06',
226
        extra_data={'foo': 'bar'},
227
        date_start=datetime.date(year=2021, month=9, day=1),
228
        date_end=datetime.date(year=2021, month=10, day=1),
229
    )
230
    other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
231
    other_subscription = Subscription.objects.create(
232
        agenda=other_agenda,
233
        user_external_id='xxx',
234
        date_start=datetime.date(year=2021, month=9, day=1),
235
        date_end=datetime.date(year=2021, month=10, day=1),
236
    )
237

  
238
    resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=401)
239

  
240
    app.authorization = ('Basic', ('john.doe', 'password'))
241

  
242
    resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
243
    assert resp.json == {
244
        'id': subscription.pk,
245
        'user_external_id': 'xxx',
246
        'user_first_name': 'Foo',
247
        'user_last_name': 'BAR',
248
        'user_email': 'foo@bar.com',
249
        'user_phone_number': '06',
250
        'date_start': '2021-09-01',
251
        'date_end': '2021-10-01',
252
        'foo': 'bar',
253
        'err': 0,
254
    }
255

  
256
    resp = app.get('/api/agenda/%s/subscription/%s/' % (other_agenda.slug, other_subscription.pk))
257
    assert resp.json == {
258
        'id': other_subscription.pk,
259
        'user_external_id': 'xxx',
260
        'user_first_name': '',
261
        'user_last_name': '',
262
        'user_email': '',
263
        'user_phone_number': '',
264
        'date_start': '2021-09-01',
265
        'date_end': '2021-10-01',
266
        'err': 0,
267
    }
268

  
269
    app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, other_subscription.pk), status=404)
270
    app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, 0), status=404)
271
    app.get('/api/agenda/%s/subscription/%s/' % ('unknown', subscription.pk), status=404)
272
    for kind in ['meetings', 'virtual']:
273
        agenda.kind = kind
274
        agenda.save()
275
        app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=404)
215
-