Projet

Général

Profil

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

Lauréline Guérin, 09 mars 2020 12:14

Télécharger (9,52 ko)

Voir les différences:

Subject: [PATCH 2/3] 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
732 732
    '''
733 733
    Cancel a booking.
734 734

  
735
    It will return an error (code 1) if the booking was already cancelled.
735
    It will return error codes if the booking was cancelled before (code 1) or
736
    if the booking is not primary (code 2).
736 737
    '''
737 738

  
738 739
    permission_classes = (permissions.IsAuthenticated,)
......
746 747
                'err_desc': _('already cancelled'),
747 748
            }
748 749
            return Response(response)
750
        if booking.primary_booking is not None:
751
            response = {
752
                'err': 2,
753
                'err_class': 'secondary booking',
754
                'err_desc': _('secondary booking'),
755
            }
756
            return Response(response)
749 757
        booking.cancel()
750 758
        response = {'err': 0, 'booking_id': booking.id}
751 759
        return Response(response)
......
758 766
    '''
759 767
    Accept a booking currently in the waiting list.
760 768

  
761
    It will return error codes if the booking was cancelled before (code 1) and
762
    if the booking was not in waiting list (code 2).
769
    It will return error codes if the booking was cancelled before (code 1),
770
    if the booking is not primary (code 2) or
771
    if the booking was not in waiting list (code 3).
763 772
    '''
764 773

  
765 774
    permission_classes = (permissions.IsAuthenticated,)
......
773 782
                'err_desc': _('booking is cancelled'),
774 783
            }
775 784
            return Response(response)
776
        if not booking.in_waiting_list:
785
        if booking.primary_booking is not None:
777 786
            response = {
778 787
                'err': 2,
788
                'err_class': 'secondary booking',
789
                'err_desc': _('secondary booking'),
790
            }
791
            return Response(response)
792
        if not booking.in_waiting_list:
793
            response = {
794
                'err': 3,
779 795
                'err_class': 'booking is not in waiting list',
780 796
                'err_desc': _('booking is not in waiting list'),
781 797
            }
......
792 808
    '''
793 809
    Suspend a accepted booking.
794 810

  
795
    It will return error codes if the booking was cancelled before (code 1) and
796
    if the bookingis already in waiting list (code 2).
811
    It will return error codes if the booking was cancelled before (code 1)
812
    if the booking is not primary (code 2) or
813
    if the booking is already in waiting list (code 3).
797 814
    '''
798 815

  
799 816
    permission_classes = (permissions.IsAuthenticated,)
......
807 824
                'err_desc': _('booking is cancelled'),
808 825
            }
809 826
            return Response(response)
810
        if booking.in_waiting_list:
827
        if booking.primary_booking is not None:
811 828
            response = {
812 829
                'err': 2,
830
                'err_class': 'secondary booking',
831
                'err_desc': _('secondary booking'),
832
            }
833
            return Response(response)
834
        if booking.in_waiting_list:
835
            response = {
836
                'err': 3,
813 837
                'err_class': 'booking is already in waiting list',
814 838
                'err_desc': _('booking is already in waiting list'),
815 839
            }
tests/test_api.py
1306 1306
    assert resp.json['err_desc'] == 'sold out'
1307 1307

  
1308 1308

  
1309
def test_cancel_booking(app, some_data, user):
1310
    agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].pk
1311
    event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]
1312
    primary = Booking.objects.create(event=event)
1313
    secondary = Booking.objects.create(event=event, primary_booking=primary)
1314
    app.authorization = ('Basic', ('john.doe', 'password'))
1315

  
1316
    resp = app.post('/api/booking/%s/cancel/' % secondary.pk)
1317
    assert resp.json['err'] == 2
1318
    assert resp.json['reason'] == 'secondary booking'  # legacy
1319
    assert resp.json['err_class'] == 'secondary booking'
1320
    assert resp.json['err_desc'] == 'secondary booking'
1321

  
1322
    resp = app.post('/api/booking/%s/cancel/' % primary.pk)
1323
    assert resp.json['err'] == 0
1324
    primary.refresh_from_db()
1325
    secondary.refresh_from_db()
1326
    assert primary.cancellation_datetime is not None
1327
    assert secondary.cancellation_datetime is not None
1328

  
1329

  
1309 1330
def test_accept_booking(app, some_data, user):
1310 1331
    agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
1311 1332
    event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]
......
1313 1334
    event.save()
1314 1335

  
1315 1336
    # create a booking on the waiting list
1316
    booking = Booking(event=event, in_waiting_list=True)
1317
    booking.save()
1337
    primary = Booking.objects.create(event=event, in_waiting_list=True)
1338
    secondary = Booking.objects.create(event=event, in_waiting_list=True, primary_booking=primary)
1318 1339

  
1319
    assert Booking.objects.filter(in_waiting_list=True).count() == 1
1340
    assert Booking.objects.filter(in_waiting_list=True).count() == 2
1320 1341

  
1321 1342
    app.authorization = ('Basic', ('john.doe', 'password'))
1322
    resp = app.post('/api/booking/%s/accept/' % booking.id)
1343

  
1344
    resp = app.post('/api/booking/%s/accept/' % secondary.id)
1345
    assert resp.json['err'] == 2
1346
    assert resp.json['reason'] == 'secondary booking'  # legacy
1347
    assert resp.json['err_class'] == 'secondary booking'
1348
    assert resp.json['err_desc'] == 'secondary booking'
1349

  
1350
    resp = app.post('/api/booking/%s/accept/' % primary.id)
1323 1351
    assert Booking.objects.filter(in_waiting_list=True).count() == 0
1324
    assert Booking.objects.filter(in_waiting_list=False).count() == 1
1352
    assert Booking.objects.filter(in_waiting_list=False).count() == 2
1353
    primary.refresh_from_db()
1354
    secondary.refresh_from_db()
1355
    assert primary.in_waiting_list is False
1356
    assert secondary.in_waiting_list is False
1325 1357

  
1326 1358
    # accept a booking that doesn't exist
1327
    resp = app.post('/api/booking/%s/accept/' % 9999, status=404)
1359
    resp = app.post('/api/booking/0/accept/', status=404)
1328 1360

  
1329 1361
    # accept a booking that was not in the waiting list
1330
    resp = app.post('/api/booking/%s/accept/' % booking.id, status=200)
1331
    assert resp.json['err'] == 2
1362
    resp = app.post('/api/booking/%s/accept/' % primary.id, status=200)
1363
    assert resp.json['err'] == 3
1332 1364

  
1333 1365
    # accept a booking that was cancelled before
1334
    booking = Booking.objects.get(id=booking.id)
1335
    booking.in_waiting_list = True
1336
    booking.cancel()
1337
    resp = app.post('/api/booking/%s/accept/' % booking.id, status=200)
1366
    primary.suspend()
1367
    primary.cancel()
1368
    resp = app.post('/api/booking/%s/accept/' % primary.id, status=200)
1338 1369
    assert resp.json['err'] == 1
1339
    assert Booking.objects.filter(in_waiting_list=True).count() == 1
1370
    assert Booking.objects.filter(in_waiting_list=True).count() == 2
1340 1371
    assert Booking.objects.filter(in_waiting_list=False).count() == 0
1341 1372

  
1342 1373

  
......
1347 1378
    event.save()
1348 1379

  
1349 1380
    # create a booking not on the waiting list
1350
    booking = Booking.objects.create(event=event, in_waiting_list=False)
1351
    assert booking.in_waiting_list is False
1381
    primary = Booking.objects.create(event=event, in_waiting_list=False)
1382
    secondary = Booking.objects.create(event=event, in_waiting_list=False, primary_booking=primary)
1383

  
1384
    assert Booking.objects.filter(in_waiting_list=False).count() == 2
1352 1385

  
1353 1386
    app.authorization = ('Basic', ('john.doe', 'password'))
1354
    resp = app.post('/api/booking/%s/suspend/' % booking.pk)
1355
    booking.refresh_from_db()
1356
    assert booking.in_waiting_list is True
1387

  
1388
    resp = app.post('/api/booking/%s/suspend/' % secondary.id)
1389
    assert resp.json['err'] == 2
1390
    assert resp.json['reason'] == 'secondary booking'  # legacy
1391
    assert resp.json['err_class'] == 'secondary booking'
1392
    assert resp.json['err_desc'] == 'secondary booking'
1393

  
1394
    resp = app.post('/api/booking/%s/suspend/' % primary.pk)
1395
    primary.refresh_from_db()
1396
    secondary.refresh_from_db()
1397
    assert primary.in_waiting_list is True
1398
    assert secondary.in_waiting_list is True
1357 1399

  
1358 1400
    # suspend a booking that doesn't exist
1359 1401
    resp = app.post('/api/booking/0/suspend/', status=404)
1360 1402

  
1361 1403
    # suspend a booking that is in the waiting list
1362
    resp = app.post('/api/booking/%s/suspend/' % booking.pk, status=200)
1363
    assert resp.json['err'] == 2
1404
    resp = app.post('/api/booking/%s/suspend/' % primary.pk, status=200)
1405
    assert resp.json['err'] == 3
1364 1406

  
1365 1407
    # suspend a booking that was cancelled before
1366
    booking.in_waiting_list = False
1367
    booking.cancel()
1368
    resp = app.post('/api/booking/%s/suspend/' % booking.pk, status=200)
1408
    primary.accept()
1409
    primary.cancel()
1410
    resp = app.post('/api/booking/%s/suspend/' % primary.pk, status=200)
1369 1411
    assert resp.json['err'] == 1
1370
    assert booking.in_waiting_list is False
1412
    assert primary.in_waiting_list is False
1371 1413

  
1372 1414

  
1373 1415
def test_multiple_booking_api(app, some_data, user):
1374
-