Projet

Général

Profil

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

Lauréline Guérin, 17 mars 2020 15:01

Télécharger (9,51 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
731 731
    '''
732 732
    Cancel a booking.
733 733

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

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

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

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

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

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

  
1315 1315

  
1316
def test_cancel_booking(app, some_data, user):
1317
    agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].pk
1318
    event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]
1319
    primary = Booking.objects.create(event=event)
1320
    secondary = Booking.objects.create(event=event, primary_booking=primary)
1321
    app.authorization = ('Basic', ('john.doe', 'password'))
1322

  
1323
    resp = app.post('/api/booking/%s/cancel/' % secondary.pk)
1324
    assert resp.json['err'] == 2
1325
    assert resp.json['reason'] == 'secondary booking'  # legacy
1326
    assert resp.json['err_class'] == 'secondary booking'
1327
    assert resp.json['err_desc'] == 'secondary booking'
1328

  
1329
    resp = app.post('/api/booking/%s/cancel/' % primary.pk)
1330
    assert resp.json['err'] == 0
1331
    primary.refresh_from_db()
1332
    secondary.refresh_from_db()
1333
    assert primary.cancellation_datetime is not None
1334
    assert secondary.cancellation_datetime is not None
1335

  
1336

  
1316 1337
def test_accept_booking(app, some_data, user):
1317 1338
    agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
1318 1339
    event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]
......
1320 1341
    event.save()
1321 1342

  
1322 1343
    # create a booking on the waiting list
1323
    booking = Booking(event=event, in_waiting_list=True)
1324
    booking.save()
1344
    primary = Booking.objects.create(event=event, in_waiting_list=True)
1345
    secondary = Booking.objects.create(event=event, in_waiting_list=True, primary_booking=primary)
1325 1346

  
1326
    assert Booking.objects.filter(in_waiting_list=True).count() == 1
1347
    assert Booking.objects.filter(in_waiting_list=True).count() == 2
1327 1348

  
1328 1349
    app.authorization = ('Basic', ('john.doe', 'password'))
1329
    resp = app.post('/api/booking/%s/accept/' % booking.id)
1350

  
1351
    resp = app.post('/api/booking/%s/accept/' % secondary.id)
1352
    assert resp.json['err'] == 2
1353
    assert resp.json['reason'] == 'secondary booking'  # legacy
1354
    assert resp.json['err_class'] == 'secondary booking'
1355
    assert resp.json['err_desc'] == 'secondary booking'
1356

  
1357
    resp = app.post('/api/booking/%s/accept/' % primary.id)
1330 1358
    assert Booking.objects.filter(in_waiting_list=True).count() == 0
1331
    assert Booking.objects.filter(in_waiting_list=False).count() == 1
1359
    assert Booking.objects.filter(in_waiting_list=False).count() == 2
1360
    primary.refresh_from_db()
1361
    secondary.refresh_from_db()
1362
    assert primary.in_waiting_list is False
1363
    assert secondary.in_waiting_list is False
1332 1364

  
1333 1365
    # accept a booking that doesn't exist
1334
    resp = app.post('/api/booking/%s/accept/' % 0, status=404)
1366
    resp = app.post('/api/booking/0/accept/', status=404)
1335 1367

  
1336 1368
    # accept a booking that was not in the waiting list
1337
    resp = app.post('/api/booking/%s/accept/' % booking.id, status=200)
1338
    assert resp.json['err'] == 2
1369
    resp = app.post('/api/booking/%s/accept/' % primary.id, status=200)
1370
    assert resp.json['err'] == 3
1339 1371

  
1340 1372
    # accept a booking that was cancelled before
1341
    booking = Booking.objects.get(id=booking.id)
1342
    booking.in_waiting_list = True
1343
    booking.cancel()
1344
    resp = app.post('/api/booking/%s/accept/' % booking.id, status=200)
1373
    primary.suspend()
1374
    primary.cancel()
1375
    resp = app.post('/api/booking/%s/accept/' % primary.id, status=200)
1345 1376
    assert resp.json['err'] == 1
1346
    assert Booking.objects.filter(in_waiting_list=True).count() == 1
1377
    assert Booking.objects.filter(in_waiting_list=True).count() == 2
1347 1378
    assert Booking.objects.filter(in_waiting_list=False).count() == 0
1348 1379

  
1349 1380

  
......
1354 1385
    event.save()
1355 1386

  
1356 1387
    # create a booking not on the waiting list
1357
    booking = Booking.objects.create(event=event, in_waiting_list=False)
1358
    assert booking.in_waiting_list is False
1388
    primary = Booking.objects.create(event=event, in_waiting_list=False)
1389
    secondary = Booking.objects.create(event=event, in_waiting_list=False, primary_booking=primary)
1390

  
1391
    assert Booking.objects.filter(in_waiting_list=False).count() == 2
1359 1392

  
1360 1393
    app.authorization = ('Basic', ('john.doe', 'password'))
1361
    resp = app.post('/api/booking/%s/suspend/' % booking.pk)
1362
    booking.refresh_from_db()
1363
    assert booking.in_waiting_list is True
1394

  
1395
    resp = app.post('/api/booking/%s/suspend/' % secondary.id)
1396
    assert resp.json['err'] == 2
1397
    assert resp.json['reason'] == 'secondary booking'  # legacy
1398
    assert resp.json['err_class'] == 'secondary booking'
1399
    assert resp.json['err_desc'] == 'secondary booking'
1400

  
1401
    resp = app.post('/api/booking/%s/suspend/' % primary.pk)
1402
    primary.refresh_from_db()
1403
    secondary.refresh_from_db()
1404
    assert primary.in_waiting_list is True
1405
    assert secondary.in_waiting_list is True
1364 1406

  
1365 1407
    # suspend a booking that doesn't exist
1366 1408
    resp = app.post('/api/booking/0/suspend/', status=404)
1367 1409

  
1368 1410
    # suspend a booking that is in the waiting list
1369
    resp = app.post('/api/booking/%s/suspend/' % booking.pk, status=200)
1370
    assert resp.json['err'] == 2
1411
    resp = app.post('/api/booking/%s/suspend/' % primary.pk, status=200)
1412
    assert resp.json['err'] == 3
1371 1413

  
1372 1414
    # suspend a booking that was cancelled before
1373
    booking.in_waiting_list = False
1374
    booking.cancel()
1375
    resp = app.post('/api/booking/%s/suspend/' % booking.pk, status=200)
1415
    primary.accept()
1416
    primary.cancel()
1417
    resp = app.post('/api/booking/%s/suspend/' % primary.pk, status=200)
1376 1418
    assert resp.json['err'] == 1
1377
    assert booking.in_waiting_list is False
1419
    assert primary.in_waiting_list is False
1378 1420

  
1379 1421

  
1380 1422
def test_multiple_booking_api(app, some_data, user):
1381
-