Projet

Général

Profil

0001-api-can-not-cancel-accept-or-suspend-a-secondary-boo.patch

Lauréline Guérin, 05 mars 2020 14:04

Télécharger (9,52 ko)

Voir les différences:

Subject: [PATCH 1/2] api: can not cancel, accept or suspend a secondary
 booking (#40039)

 chrono/api/views.py | 38 +++++++++++++++----
 tests/test_api.py   | 90 +++++++++++++++++++++++++++++++++------------
 2 files changed, 97 insertions(+), 31 deletions(-)
chrono/api/views.py
728 728
    '''
729 729
    Cancel a booking.
730 730

  
731
    It will return an error (code 1) if the booking was already cancelled.
731
    It will return error codes if the booking was cancelled before (code 1) or
732
    if the booking is not primary (code 2).
732 733
    '''
733 734

  
734 735
    permission_classes = (permissions.IsAuthenticated,)
......
742 743
                'err_desc': _('already cancelled'),
743 744
            }
744 745
            return Response(response)
746
        if booking.primary_booking is not None:
747
            response = {
748
                'err': 2,
749
                'err_class': 'secondary booking',
750
                'err_desc': _('secondary booking'),
751
            }
752
            return Response(response)
745 753
        booking.cancel()
746 754
        response = {'err': 0, 'booking_id': booking.id}
747 755
        return Response(response)
......
754 762
    '''
755 763
    Accept a booking currently in the waiting list.
756 764

  
757
    It will return error codes if the booking was cancelled before (code 1) and
758
    if the booking was not in waiting list (code 2).
765
    It will return error codes if the booking was cancelled before (code 1),
766
    if the booking is not primary (code 2) or
767
    if the booking was not in waiting list (code 3).
759 768
    '''
760 769

  
761 770
    permission_classes = (permissions.IsAuthenticated,)
......
769 778
                'err_desc': _('booking is cancelled'),
770 779
            }
771 780
            return Response(response)
772
        if not booking.in_waiting_list:
781
        if booking.primary_booking is not None:
773 782
            response = {
774 783
                'err': 2,
784
                'err_class': 'secondary booking',
785
                'err_desc': _('secondary booking'),
786
            }
787
            return Response(response)
788
        if not booking.in_waiting_list:
789
            response = {
790
                'err': 3,
775 791
                'err_class': 'booking is not in waiting list',
776 792
                'err_desc': _('booking is not in waiting list'),
777 793
            }
......
788 804
    '''
789 805
    Suspend a accepted booking.
790 806

  
791
    It will return error codes if the booking was cancelled before (code 1) and
792
    if the bookingis already in waiting list (code 2).
807
    It will return error codes if the booking was cancelled before (code 1)
808
    if the booking is not primary (code 2) or
809
    if the booking is already in waiting list (code 3).
793 810
    '''
794 811

  
795 812
    permission_classes = (permissions.IsAuthenticated,)
......
803 820
                'err_desc': _('booking is cancelled'),
804 821
            }
805 822
            return Response(response)
806
        if booking.in_waiting_list:
823
        if booking.primary_booking is not None:
807 824
            response = {
808 825
                'err': 2,
826
                'err_class': 'secondary booking',
827
                'err_desc': _('secondary booking'),
828
            }
829
            return Response(response)
830
        if booking.in_waiting_list:
831
            response = {
832
                'err': 3,
809 833
                'err_class': 'booking is already in waiting list',
810 834
                'err_desc': _('booking is already in waiting list'),
811 835
            }
tests/test_api.py
1298 1298
    assert resp.json['err_desc'] == 'sold out'
1299 1299

  
1300 1300

  
1301
def test_cancel_booking(app, some_data, user):
1302
    agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].pk
1303
    event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]
1304
    primary = Booking.objects.create(event=event)
1305
    secondary = Booking.objects.create(event=event, primary_booking=primary)
1306
    app.authorization = ('Basic', ('john.doe', 'password'))
1307

  
1308
    resp = app.post('/api/booking/%s/cancel/' % secondary.pk)
1309
    assert resp.json['err'] == 2
1310
    assert resp.json['reason'] == 'secondary booking'  # legacy
1311
    assert resp.json['err_class'] == 'secondary booking'
1312
    assert resp.json['err_desc'] == 'secondary booking'
1313

  
1314
    resp = app.post('/api/booking/%s/cancel/' % primary.pk)
1315
    assert resp.json['err'] == 0
1316
    primary.refresh_from_db()
1317
    secondary.refresh_from_db()
1318
    assert primary.cancellation_datetime is not None
1319
    assert secondary.cancellation_datetime is not None
1320

  
1321

  
1301 1322
def test_accept_booking(app, some_data, user):
1302 1323
    agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
1303 1324
    event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]
......
1305 1326
    event.save()
1306 1327

  
1307 1328
    # create a booking on the waiting list
1308
    booking = Booking(event=event, in_waiting_list=True)
1309
    booking.save()
1329
    primary = Booking.objects.create(event=event, in_waiting_list=True)
1330
    secondary = Booking.objects.create(event=event, in_waiting_list=True, primary_booking=primary)
1310 1331

  
1311
    assert Booking.objects.filter(in_waiting_list=True).count() == 1
1332
    assert Booking.objects.filter(in_waiting_list=True).count() == 2
1312 1333

  
1313 1334
    app.authorization = ('Basic', ('john.doe', 'password'))
1314
    resp = app.post('/api/booking/%s/accept/' % booking.id)
1335

  
1336
    resp = app.post('/api/booking/%s/accept/' % secondary.id)
1337
    assert resp.json['err'] == 2
1338
    assert resp.json['reason'] == 'secondary booking'  # legacy
1339
    assert resp.json['err_class'] == 'secondary booking'
1340
    assert resp.json['err_desc'] == 'secondary booking'
1341

  
1342
    resp = app.post('/api/booking/%s/accept/' % primary.id)
1315 1343
    assert Booking.objects.filter(in_waiting_list=True).count() == 0
1316
    assert Booking.objects.filter(in_waiting_list=False).count() == 1
1344
    assert Booking.objects.filter(in_waiting_list=False).count() == 2
1345
    primary.refresh_from_db()
1346
    secondary.refresh_from_db()
1347
    assert primary.in_waiting_list is False
1348
    assert secondary.in_waiting_list is False
1317 1349

  
1318 1350
    # accept a booking that doesn't exist
1319
    resp = app.post('/api/booking/%s/accept/' % 9999, status=404)
1351
    resp = app.post('/api/booking/0/accept/', status=404)
1320 1352

  
1321 1353
    # accept a booking that was not in the waiting list
1322
    resp = app.post('/api/booking/%s/accept/' % booking.id, status=200)
1323
    assert resp.json['err'] == 2
1354
    resp = app.post('/api/booking/%s/accept/' % primary.id, status=200)
1355
    assert resp.json['err'] == 3
1324 1356

  
1325 1357
    # accept a booking that was cancelled before
1326
    booking = Booking.objects.get(id=booking.id)
1327
    booking.in_waiting_list = True
1328
    booking.cancel()
1329
    resp = app.post('/api/booking/%s/accept/' % booking.id, status=200)
1358
    primary.suspend()
1359
    primary.cancel()
1360
    resp = app.post('/api/booking/%s/accept/' % primary.id, status=200)
1330 1361
    assert resp.json['err'] == 1
1331
    assert Booking.objects.filter(in_waiting_list=True).count() == 1
1362
    assert Booking.objects.filter(in_waiting_list=True).count() == 2
1332 1363
    assert Booking.objects.filter(in_waiting_list=False).count() == 0
1333 1364

  
1334 1365

  
......
1339 1370
    event.save()
1340 1371

  
1341 1372
    # create a booking not on the waiting list
1342
    booking = Booking.objects.create(event=event, in_waiting_list=False)
1343
    assert booking.in_waiting_list is False
1373
    primary = Booking.objects.create(event=event, in_waiting_list=False)
1374
    secondary = Booking.objects.create(event=event, in_waiting_list=False, primary_booking=primary)
1375

  
1376
    assert Booking.objects.filter(in_waiting_list=False).count() == 2
1344 1377

  
1345 1378
    app.authorization = ('Basic', ('john.doe', 'password'))
1346
    resp = app.post('/api/booking/%s/suspend/' % booking.pk)
1347
    booking.refresh_from_db()
1348
    assert booking.in_waiting_list is True
1379

  
1380
    resp = app.post('/api/booking/%s/suspend/' % secondary.id)
1381
    assert resp.json['err'] == 2
1382
    assert resp.json['reason'] == 'secondary booking'  # legacy
1383
    assert resp.json['err_class'] == 'secondary booking'
1384
    assert resp.json['err_desc'] == 'secondary booking'
1385

  
1386
    resp = app.post('/api/booking/%s/suspend/' % primary.pk)
1387
    primary.refresh_from_db()
1388
    secondary.refresh_from_db()
1389
    assert primary.in_waiting_list is True
1390
    assert secondary.in_waiting_list is True
1349 1391

  
1350 1392
    # suspend a booking that doesn't exist
1351 1393
    resp = app.post('/api/booking/0/suspend/', status=404)
1352 1394

  
1353 1395
    # suspend a booking that is in the waiting list
1354
    resp = app.post('/api/booking/%s/suspend/' % booking.pk, status=200)
1355
    assert resp.json['err'] == 2
1396
    resp = app.post('/api/booking/%s/suspend/' % primary.pk, status=200)
1397
    assert resp.json['err'] == 3
1356 1398

  
1357 1399
    # suspend a booking that was cancelled before
1358
    booking.in_waiting_list = False
1359
    booking.cancel()
1360
    resp = app.post('/api/booking/%s/suspend/' % booking.pk, status=200)
1400
    primary.accept()
1401
    primary.cancel()
1402
    resp = app.post('/api/booking/%s/suspend/' % primary.pk, status=200)
1361 1403
    assert resp.json['err'] == 1
1362
    assert booking.in_waiting_list is False
1404
    assert primary.in_waiting_list is False
1363 1405

  
1364 1406

  
1365 1407
def test_multiple_booking_api(app, some_data, user):
1366
-