314 |
314 |
resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
|
315 |
315 |
assert resp.json['err'] == 0
|
316 |
316 |
assert Subscription.objects.filter(pk=subscription.pk).exists() is False
|
|
317 |
|
|
318 |
|
|
319 |
def test_api_patch_subscription(app, user):
|
|
320 |
agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
|
321 |
subscription = Subscription.objects.create(
|
|
322 |
agenda=agenda,
|
|
323 |
user_external_id='xxx',
|
|
324 |
user_first_name='Foo',
|
|
325 |
user_last_name='BAR',
|
|
326 |
user_email='foo@bar.com',
|
|
327 |
user_phone_number='06',
|
|
328 |
date_start=datetime.date(year=2021, month=9, day=1),
|
|
329 |
date_end=datetime.date(year=2021, month=10, day=1),
|
|
330 |
)
|
|
331 |
other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
|
332 |
other_subscription = Subscription.objects.create(
|
|
333 |
agenda=other_agenda,
|
|
334 |
user_external_id='xxx',
|
|
335 |
date_start=datetime.date(year=2021, month=9, day=1),
|
|
336 |
date_end=datetime.date(year=2021, month=10, day=1),
|
|
337 |
)
|
|
338 |
|
|
339 |
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=401)
|
|
340 |
|
|
341 |
app.authorization = ('Basic', ('john.doe', 'password'))
|
|
342 |
|
|
343 |
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
|
|
344 |
assert resp.json == {
|
|
345 |
'id': subscription.pk,
|
|
346 |
'user_external_id': 'xxx',
|
|
347 |
'user_first_name': 'Foo',
|
|
348 |
'user_last_name': 'BAR',
|
|
349 |
'user_email': 'foo@bar.com',
|
|
350 |
'user_phone_number': '06',
|
|
351 |
'date_start': '2021-09-01',
|
|
352 |
'date_end': '2021-10-01',
|
|
353 |
'extra_data': None,
|
|
354 |
'err': 0,
|
|
355 |
}
|
|
356 |
|
|
357 |
to_test = [
|
|
358 |
('user_external_id', 'yyy', 'yyy'),
|
|
359 |
('user_first_name', 'fooo', 'fooo'),
|
|
360 |
('user_last_name', 'baaaar', 'baaaar'),
|
|
361 |
('user_email', '', ''),
|
|
362 |
('user_email', 'fooo@baaaar.com', 'fooo@baaaar.com'),
|
|
363 |
('user_phone_number', '', ''),
|
|
364 |
('user_phone_number', '0606', '0606'),
|
|
365 |
('date_start', '2021-08-31', datetime.date(2021, 8, 31)),
|
|
366 |
('date_end', '2022-07-31', datetime.date(2022, 7, 31)),
|
|
367 |
]
|
|
368 |
for key, value, db_value in to_test:
|
|
369 |
params = {
|
|
370 |
key: value,
|
|
371 |
}
|
|
372 |
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
|
373 |
assert resp.json[key] == value
|
|
374 |
subscription.refresh_from_db()
|
|
375 |
assert getattr(subscription, key) == db_value
|
|
376 |
|
|
377 |
params = {
|
|
378 |
'foo': 'bar',
|
|
379 |
}
|
|
380 |
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
|
381 |
assert resp.json['extra_data'] == {'foo': 'bar'}
|
|
382 |
subscription.refresh_from_db()
|
|
383 |
assert subscription.extra_data == {'foo': 'bar'}
|
|
384 |
params = {
|
|
385 |
'foo': 'bar2',
|
|
386 |
}
|
|
387 |
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
|
388 |
assert resp.json['extra_data'] == {'foo': 'bar2'}
|
|
389 |
subscription.refresh_from_db()
|
|
390 |
assert subscription.extra_data == {'foo': 'bar2'}
|
|
391 |
params = {
|
|
392 |
'user_phone_number': '060606', # mix normal attribute and extra_data
|
|
393 |
'bar': 'baz',
|
|
394 |
}
|
|
395 |
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
|
|
396 |
assert resp.json['user_phone_number'] == '060606'
|
|
397 |
assert resp.json['extra_data'] == {'foo': 'bar2', 'bar': 'baz'}
|
|
398 |
subscription.refresh_from_db()
|
|
399 |
assert subscription.user_phone_number == '060606'
|
|
400 |
assert subscription.extra_data == {'foo': 'bar2', 'bar': 'baz'}
|
|
401 |
|
|
402 |
params = {
|
|
403 |
'date_start': 'foobar',
|
|
404 |
'date_end': 'foobar',
|
|
405 |
}
|
|
406 |
resp = app.patch(
|
|
407 |
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
|
|
408 |
)
|
|
409 |
assert resp.json['err_class'] == 'invalid payload'
|
|
410 |
assert 'wrong format' in resp.json['errors']['date_start'][0]
|
|
411 |
assert 'wrong format' in resp.json['errors']['date_end'][0]
|
|
412 |
|
|
413 |
params = {
|
|
414 |
'date_start': '2021-10-01',
|
|
415 |
'date_end': '2021-09-01',
|
|
416 |
}
|
|
417 |
resp = app.patch(
|
|
418 |
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
|
|
419 |
)
|
|
420 |
assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
|
|
421 |
|
|
422 |
app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, other_subscription.pk), status=404)
|
|
423 |
app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, 0), status=404)
|
|
424 |
app.patch('/api/agenda/%s/subscription/%s/' % ('unknown', subscription.pk), status=404)
|
|
425 |
for kind in ['meetings', 'virtual']:
|
|
426 |
agenda.kind = kind
|
|
427 |
agenda.save()
|
|
428 |
app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=404)
|
317 |
|
-
|