Projet

Général

Profil

0001-api-proper-error-if-datetime-part-is-wrong-in-a-slot.patch

Lauréline Guérin, 09 novembre 2020 09:24

Télécharger (4,06 ko)

Voir les différences:

Subject: [PATCH] api: proper error if datetime part is wrong in a slot
 (#48132)

 chrono/api/views.py |  9 ++++++++-
 tests/test_api.py   | 18 +++++++++++++++---
 2 files changed, 23 insertions(+), 4 deletions(-)
chrono/api/views.py
832 832
                        err_class='all slots must have the same meeting type id (%s)' % meeting_type_id,
833 833
                        http_status=status.HTTP_400_BAD_REQUEST,
834 834
                    )
835
                datetimes.add(make_aware(datetime.datetime.strptime(datetime_str, '%Y-%m-%d-%H%M')))
835
                try:
836
                    datetimes.add(make_aware(datetime.datetime.strptime(datetime_str, '%Y-%m-%d-%H%M')))
837
                except ValueError:
838
                    raise APIError(
839
                        _('bad datetime format: %s') % datetime_str,
840
                        err_class='bad datetime format: %s' % datetime_str,
841
                        http_status=status.HTTP_400_BAD_REQUEST,
842
                    )
836 843

  
837 844
            try:
838 845
                resources = get_resources_from_request(request, agenda)
tests/test_api.py
1426 1426
    assert resp_cancel.json['err'] == 0
1427 1427
    assert Booking.objects.filter(cancellation_datetime__isnull=False).count() == 2
1428 1428

  
1429

  
1430
def test_booking_api_meeting_fillslots_wrong_slot(app, user):
1431
    agenda = Agenda.objects.create(label='Foo', kind='meetings')
1432
    app.authorization = ('Basic', ('john.doe', 'password'))
1433

  
1429 1434
    impossible_slots = ['1:2017-05-22-1130', '2:2017-05-22-1100']
1430
    resp = app.post('/api/agenda/%s/fillslots/' % agenda_id, params={'slots': impossible_slots}, status=400)
1435
    resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': impossible_slots}, status=400)
1431 1436
    assert resp.json['err'] == 1
1432 1437
    assert resp.json['reason'] == 'all slots must have the same meeting type id (1)'  # legacy
1433 1438
    assert resp.json['err_class'] == 'all slots must have the same meeting type id (1)'
1434 1439
    assert resp.json['err_desc'] == 'all slots must have the same meeting type id (1)'
1435 1440

  
1436 1441
    unknown_slots = ['0:2017-05-22-1130']
1437
    resp = app.post('/api/agenda/%s/fillslots/' % agenda_id, params={'slots': unknown_slots}, status=400)
1442
    resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': unknown_slots}, status=400)
1438 1443
    assert resp.json['err'] == 1
1439 1444
    assert resp.json['reason'] == 'invalid meeting type id: 0'  # legacy
1440 1445
    assert resp.json['err_class'] == 'invalid meeting type id: 0'
1441 1446
    assert resp.json['err_desc'] == 'invalid meeting type id: 0'
1442 1447
    unknown_slots = ['foobar:2017-05-22-1130']
1443
    resp = app.post('/api/agenda/%s/fillslots/' % agenda_id, params={'slots': unknown_slots}, status=400)
1448
    resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': unknown_slots}, status=400)
1444 1449
    assert resp.json['err'] == 1
1445 1450
    assert resp.json['reason'] == 'invalid meeting type id: foobar'  # legacy
1446 1451
    assert resp.json['err_class'] == 'invalid meeting type id: foobar'
1447 1452
    assert resp.json['err_desc'] == 'invalid meeting type id: foobar'
1448 1453

  
1454
    badformat_slots = ['foo:2020-10-28-14h00']
1455
    resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': badformat_slots}, status=400)
1456
    assert resp.json['err'] == 1
1457
    assert resp.json['reason'] == 'bad datetime format: 2020-10-28-14h00'  # legacy
1458
    assert resp.json['err_class'] == 'bad datetime format: 2020-10-28-14h00'
1459
    assert resp.json['err_desc'] == 'bad datetime format: 2020-10-28-14h00'
1460

  
1449 1461

  
1450 1462
def test_booking_api_meeting_across_daylight_saving_time(app, meetings_agenda, user):
1451 1463
    meetings_agenda.maximal_booking_delay = 365
1452
-