api: subscription patch endpoint (#61077)
This commit is contained in:
parent
a6cd31a24a
commit
fcd6dce3e2
|
@ -360,6 +360,6 @@ class SubscriptionSerializer(serializers.ModelSerializer):
|
|||
|
||||
def validate(self, attrs):
|
||||
super().validate(attrs)
|
||||
if attrs['date_start'] > attrs['date_end']:
|
||||
if attrs.get('date_start') and attrs.get('date_end') and attrs['date_start'] > attrs['date_end']:
|
||||
raise ValidationError(_('start_datetime must be before end_datetime'))
|
||||
return attrs
|
||||
|
|
|
@ -1952,6 +1952,21 @@ class SubscriptionAPI(APIView):
|
|||
response.update({'err': 0})
|
||||
return Response(response)
|
||||
|
||||
def patch(self, request, *args, **kwargs):
|
||||
serializer = self.serializer_class(self.subscription, data=request.data, partial=True)
|
||||
|
||||
if not serializer.is_valid():
|
||||
raise APIErrorBadRequest(N_('invalid payload'), errors=serializer.errors, err=4)
|
||||
|
||||
serializer.save()
|
||||
extra_data = {k: v for k, v in request.data.items() if k not in serializer.validated_data}
|
||||
if extra_data:
|
||||
self.subscription.extra_data = self.subscription.extra_data or {}
|
||||
self.subscription.extra_data.update(extra_data)
|
||||
self.subscription.save()
|
||||
|
||||
return self.get(request, *args, **kwargs)
|
||||
|
||||
def delete(self, request, *args, **kwargs):
|
||||
self.subscription.delete()
|
||||
response = {'err': 0}
|
||||
|
|
|
@ -314,3 +314,115 @@ def test_api_delete_subscription(app, user):
|
|||
resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
|
||||
assert resp.json['err'] == 0
|
||||
assert Subscription.objects.filter(pk=subscription.pk).exists() is False
|
||||
|
||||
|
||||
def test_api_patch_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',
|
||||
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.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=401)
|
||||
|
||||
app.authorization = ('Basic', ('john.doe', 'password'))
|
||||
|
||||
resp = app.patch('/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',
|
||||
'extra_data': None,
|
||||
'err': 0,
|
||||
}
|
||||
|
||||
to_test = [
|
||||
('user_external_id', 'yyy', 'yyy'),
|
||||
('user_first_name', 'fooo', 'fooo'),
|
||||
('user_last_name', 'baaaar', 'baaaar'),
|
||||
('user_email', '', ''),
|
||||
('user_email', 'fooo@baaaar.com', 'fooo@baaaar.com'),
|
||||
('user_phone_number', '', ''),
|
||||
('user_phone_number', '0606', '0606'),
|
||||
('date_start', '2021-08-31', datetime.date(2021, 8, 31)),
|
||||
('date_end', '2022-07-31', datetime.date(2022, 7, 31)),
|
||||
]
|
||||
for key, value, db_value in to_test:
|
||||
params = {
|
||||
key: value,
|
||||
}
|
||||
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
||||
assert resp.json[key] == value
|
||||
subscription.refresh_from_db()
|
||||
assert getattr(subscription, key) == db_value
|
||||
|
||||
params = {
|
||||
'foo': 'bar',
|
||||
}
|
||||
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
||||
assert resp.json['extra_data'] == {'foo': 'bar'}
|
||||
subscription.refresh_from_db()
|
||||
assert subscription.extra_data == {'foo': 'bar'}
|
||||
params = {
|
||||
'foo': 'bar2',
|
||||
}
|
||||
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
||||
assert resp.json['extra_data'] == {'foo': 'bar2'}
|
||||
subscription.refresh_from_db()
|
||||
assert subscription.extra_data == {'foo': 'bar2'}
|
||||
params = {
|
||||
'user_phone_number': '060606', # mix normal attribute and extra_data
|
||||
'bar': 'baz',
|
||||
}
|
||||
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
||||
assert resp.json['user_phone_number'] == '060606'
|
||||
assert resp.json['extra_data'] == {'foo': 'bar2', 'bar': 'baz'}
|
||||
subscription.refresh_from_db()
|
||||
assert subscription.user_phone_number == '060606'
|
||||
assert subscription.extra_data == {'foo': 'bar2', 'bar': 'baz'}
|
||||
|
||||
params = {
|
||||
'date_start': 'foobar',
|
||||
'date_end': 'foobar',
|
||||
}
|
||||
resp = app.patch(
|
||||
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
|
||||
)
|
||||
assert resp.json['err_class'] == 'invalid payload'
|
||||
assert 'wrong format' in resp.json['errors']['date_start'][0]
|
||||
assert 'wrong format' in resp.json['errors']['date_end'][0]
|
||||
|
||||
params = {
|
||||
'date_start': '2021-10-01',
|
||||
'date_end': '2021-09-01',
|
||||
}
|
||||
resp = app.patch(
|
||||
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
|
||||
)
|
||||
assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
|
||||
|
||||
app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, other_subscription.pk), status=404)
|
||||
app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, 0), status=404)
|
||||
app.patch('/api/agenda/%s/subscription/%s/' % ('unknown', subscription.pk), status=404)
|
||||
for kind in ['meetings', 'virtual']:
|
||||
agenda.kind = kind
|
||||
agenda.save()
|
||||
app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=404)
|
||||
|
|
Loading…
Reference in New Issue