Projet

Général

Profil

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

Lauréline Guérin, 27 janvier 2022 16:45

Télécharger (5,1 ko)

Voir les différences:

Subject: [PATCH 5/5] api: subscription detail endpoint (#61161)

 chrono/api/urls.py             |  5 +++
 chrono/api/views.py            | 25 ++++++++++++-
 tests/api/test_subscription.py | 64 ++++++++++++++++++++++++++++++++++
 3 files changed, 93 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
......
1930 1930
        return Response({'err': 0, 'id': subscription.pk})
1931 1931

  
1932 1932

  
1933
subscriptions = SubscriptionsAPI.as_view()
1934

  
1935

  
1936
class SubscriptionAPI(APIView):
1937
    permission_classes = (permissions.IsAuthenticated,)
1938
    serializer_class = serializers.SubscriptionSerializer
1939

  
1940
    def initial(self, request, *args, **kwargs):
1941
        super().initial(request, *args, **kwargs)
1942
        self.subscription = get_object_or_404(
1943
            Subscription,
1944
            pk=kwargs.get('subscription_pk'),
1945
            agenda__kind='events',
1946
            agenda__slug=kwargs.get('agenda_identifier'),
1947
        )
1948

  
1949
    def get(self, request, *args, **kwargs):
1950
        serializer = self.serializer_class(self.subscription)
1951
        response = serializer.data
1952
        response.update({'err': 0})
1953
        return Response(response)
1954

  
1955

  
1933 1956
subscription = SubscriptionAPI.as_view()
1934 1957

  
1935 1958

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

  
213

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

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

  
237
    app.authorization = ('Basic', ('john.doe', 'password'))
238

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

  
255
    resp = app.get('/api/agenda/%s/subscription/%s/' % (other_agenda.slug, other_subscription.pk))
256
    assert resp.json == {
257
        'id': other_subscription.pk,
258
        'user_external_id': 'xxx',
259
        'user_first_name': '',
260
        'user_last_name': '',
261
        'user_email': '',
262
        'user_phone_number': '',
263
        'date_start': '2021-09-01',
264
        'date_end': '2021-10-01',
265
        'extra_data': None,
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)
212
-