Projet

Général

Profil

0003-api-remove-user-bookings-on-subscription-deletion-61.patch

Lauréline Guérin, 10 février 2022 12:06

Télécharger (6,18 ko)

Voir les différences:

Subject: [PATCH 3/5] api: remove user bookings on subscription deletion
 (#61065)

 chrono/api/views.py            |  12 ++++
 tests/api/test_subscription.py | 103 ++++++++++++++++++++++++++++++++-
 2 files changed, 114 insertions(+), 1 deletion(-)
chrono/api/views.py
2009 2009
        return self.get(request, *args, **kwargs)
2010 2010

  
2011 2011
    def delete(self, request, *args, **kwargs):
2012
        booking_qs = Booking.objects.filter(
2013
            # remove user bookings for this agenda
2014
            event__agenda=self.subscription.agenda,
2015
            user_external_id=self.subscription.user_external_id,
2016
            # in the period of the deleted subscription
2017
            event__start_datetime__gt=self.subscription.date_start,
2018
            event__start_datetime__lt=self.subscription.date_end + datetime.timedelta(days=1),
2019
        ).filter(
2020
            # but only in the future
2021
            event__start_datetime__gt=now(),
2022
        )
2023
        booking_qs.delete()
2012 2024
        self.subscription.delete()
2013 2025
        response = {'err': 0}
2014 2026
        return Response(response)
tests/api/test_subscription.py
1 1
import datetime
2 2

  
3 3
import pytest
4
from django.utils.timezone import make_aware, now
4 5

  
5
from chrono.agendas.models import Agenda, Subscription
6
from chrono.agendas.models import Agenda, Booking, Event, Subscription
6 7

  
7 8
pytestmark = pytest.mark.django_db
8 9

  
......
397 398
    assert Subscription.objects.filter(pk=subscription.pk).exists() is False
398 399

  
399 400

  
401
@pytest.mark.parametrize(
402
    'date_now, event_date, user_id, in_waiting_list, cancelled, deleted',
403
    [
404
        # event in the future, but no booking for the user
405
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'yyy', False, False, False),
406
        # event in the future
407
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', False, False, True),
408
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', True, False, True),
409
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', False, True, True),
410
        # event in the past
411
        ('2021-09-10 10:00', (2021, 9, 10, 12, 0), 'xxx', False, False, False),
412
        # event in the future, before the period
413
        ('2021-08-01 10:00', (2021, 8, 14, 12, 0), 'xxx', False, False, False),
414
        ('2021-08-01 10:00', (2021, 8, 31, 12, 0), 'xxx', False, False, False),
415
        # event in the future, after the period
416
        ('2021-08-01 10:00', (2021, 10, 1, 12, 0), 'xxx', False, False, False),
417
        ('2021-08-01 10:00', (2021, 10, 16, 12, 0), 'xxx', False, False, False),
418
    ],
419
)
420
def test_api_delete_subscription_and_bookings(
421
    app, user, freezer, date_now, event_date, user_id, in_waiting_list, cancelled, deleted
422
):
423
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
424
    subscription = Subscription.objects.create(
425
        agenda=agenda,
426
        user_external_id='xxx',
427
        date_start=datetime.date(year=2021, month=9, day=1),
428
        date_end=datetime.date(year=2021, month=9, day=30),
429
    )
430
    Subscription.objects.create(
431
        agenda=agenda,
432
        user_external_id='zzz',  # another user
433
        date_start=datetime.date(year=2021, month=9, day=1),
434
        date_end=datetime.date(year=2021, month=9, day=30),
435
    )
436

  
437
    other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
438
    Subscription.objects.create(
439
        agenda=other_agenda,
440
        user_external_id='xxx',
441
        date_start=datetime.date(year=2021, month=9, day=1),
442
        date_end=datetime.date(year=2021, month=10, day=1),
443
    )
444

  
445
    app.authorization = ('Basic', ('john.doe', 'password'))
446

  
447
    freezer.move_to(date_now)
448
    event = Event.objects.create(
449
        agenda=agenda, start_datetime=make_aware(datetime.datetime(*event_date)), places=10
450
    )
451
    booking = Booking.objects.create(
452
        event=event,
453
        user_external_id=user_id,
454
        in_waiting_list=in_waiting_list,
455
        cancellation_datetime=(now() if cancelled else None),
456
    )
457
    resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
458
    assert resp.json['err'] == 0
459
    assert Subscription.objects.filter(pk=subscription.pk).exists() is False
460
    assert Booking.objects.filter(pk=booking.pk).exists() is not deleted
461

  
462

  
463
@pytest.mark.parametrize(
464
    'event_date, deleted',
465
    [
466
        # just before first day
467
        ((2021, 8, 31, 12, 00), False),
468
        # first day
469
        ((2021, 9, 1, 12, 00), True),
470
        # last day
471
        ((2021, 9, 30, 12, 00), True),
472
        # just after last day
473
        ((2021, 10, 1, 12, 00), False),
474
    ],
475
)
476
def test_api_delete_subscription_and_bookings_on_limit(app, user, freezer, event_date, deleted):
477
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
478
    subscription = Subscription.objects.create(
479
        agenda=agenda,
480
        user_external_id='xxx',
481
        date_start=datetime.date(year=2021, month=9, day=1),
482
        date_end=datetime.date(year=2021, month=9, day=30),
483
    )
484

  
485
    app.authorization = ('Basic', ('john.doe', 'password'))
486

  
487
    freezer.move_to('2021-08-01 10:00')
488
    event = Event.objects.create(
489
        agenda=agenda, start_datetime=make_aware(datetime.datetime(*event_date)), places=10
490
    )
491
    booking = Booking.objects.create(
492
        event=event,
493
        user_external_id='xxx',
494
    )
495
    resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
496
    assert resp.json['err'] == 0
497
    assert Subscription.objects.filter(pk=subscription.pk).exists() is False
498
    assert Booking.objects.filter(pk=booking.pk).exists() is not deleted
499

  
500

  
400 501
def test_api_patch_subscription(app, user):
401 502
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
402 503
    subscription = Subscription.objects.create(
403
-