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 |
|
... | ... | |
316 |
317 |
assert Subscription.objects.filter(pk=subscription.pk).exists() is False
|
317 |
318 |
|
318 |
319 |
|
|
320 |
@pytest.mark.parametrize(
|
|
321 |
'date_now, event_date, user_id, in_waiting_list, cancelled, deleted',
|
|
322 |
[
|
|
323 |
# event in the future, but no booking for the user
|
|
324 |
('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'yyy', False, False, False),
|
|
325 |
# event in the future
|
|
326 |
('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', False, False, True),
|
|
327 |
('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', True, False, True),
|
|
328 |
('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', False, True, True),
|
|
329 |
# event in the past
|
|
330 |
('2021-09-10 10:00', (2021, 9, 10, 12, 0), 'xxx', False, False, False),
|
|
331 |
# event in the future and not covered by another subscription
|
|
332 |
('2021-08-01 10:00', (2021, 9, 6, 12, 0), 'xxx', False, False, True),
|
|
333 |
('2021-08-01 10:00', (2021, 9, 24, 12, 0), 'xxx', False, False, True),
|
|
334 |
# event in the future but covered by another subscription
|
|
335 |
('2021-08-01 10:00', (2021, 9, 1, 12, 0), 'xxx', False, False, False),
|
|
336 |
('2021-08-01 10:00', (2021, 9, 5, 12, 0), 'xxx', False, False, False),
|
|
337 |
('2021-08-01 10:00', (2021, 9, 25, 12, 0), 'xxx', False, False, False),
|
|
338 |
('2021-08-01 10:00', (2021, 9, 30, 12, 0), 'xxx', False, False, False),
|
|
339 |
# event in the future, before the period
|
|
340 |
('2021-08-01 10:00', (2021, 8, 14, 12, 0), 'xxx', False, False, False),
|
|
341 |
('2021-08-01 10:00', (2021, 8, 31, 12, 0), 'xxx', False, False, False),
|
|
342 |
# event in the future, after the period
|
|
343 |
('2021-08-01 10:00', (2021, 10, 1, 12, 0), 'xxx', False, False, False),
|
|
344 |
('2021-08-01 10:00', (2021, 10, 16, 12, 0), 'xxx', False, False, False),
|
|
345 |
],
|
|
346 |
)
|
|
347 |
def test_api_delete_subscription_and_bookings(
|
|
348 |
app, user, freezer, date_now, event_date, user_id, in_waiting_list, cancelled, deleted
|
|
349 |
):
|
|
350 |
agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
|
351 |
subscription = Subscription.objects.create(
|
|
352 |
agenda=agenda,
|
|
353 |
user_external_id='xxx',
|
|
354 |
date_start=datetime.date(year=2021, month=9, day=1),
|
|
355 |
date_end=datetime.date(year=2021, month=9, day=30),
|
|
356 |
)
|
|
357 |
Subscription.objects.create(
|
|
358 |
agenda=agenda,
|
|
359 |
user_external_id='xxx',
|
|
360 |
date_start=datetime.date(year=2021, month=8, day=15),
|
|
361 |
date_end=datetime.date(year=2021, month=9, day=5), # overlaps
|
|
362 |
)
|
|
363 |
Subscription.objects.create(
|
|
364 |
agenda=agenda,
|
|
365 |
user_external_id='xxx',
|
|
366 |
date_start=datetime.date(year=2021, month=9, day=25), # overlaps
|
|
367 |
date_end=datetime.date(year=2021, month=10, day=15),
|
|
368 |
)
|
|
369 |
Subscription.objects.create(
|
|
370 |
agenda=agenda,
|
|
371 |
user_external_id='zzz', # another user
|
|
372 |
date_start=datetime.date(year=2021, month=9, day=1),
|
|
373 |
date_end=datetime.date(year=2021, month=9, day=30),
|
|
374 |
)
|
|
375 |
|
|
376 |
other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
|
377 |
Subscription.objects.create(
|
|
378 |
agenda=other_agenda,
|
|
379 |
user_external_id='xxx',
|
|
380 |
date_start=datetime.date(year=2021, month=9, day=1),
|
|
381 |
date_end=datetime.date(year=2021, month=10, day=1),
|
|
382 |
)
|
|
383 |
|
|
384 |
app.authorization = ('Basic', ('john.doe', 'password'))
|
|
385 |
|
|
386 |
freezer.move_to(date_now)
|
|
387 |
event = Event.objects.create(
|
|
388 |
agenda=agenda, start_datetime=make_aware(datetime.datetime(*event_date)), places=10
|
|
389 |
)
|
|
390 |
booking = Booking.objects.create(
|
|
391 |
event=event,
|
|
392 |
user_external_id=user_id,
|
|
393 |
in_waiting_list=in_waiting_list,
|
|
394 |
cancellation_datetime=(now() if cancelled else None),
|
|
395 |
)
|
|
396 |
resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
|
|
397 |
assert resp.json['err'] == 0
|
|
398 |
assert Subscription.objects.filter(pk=subscription.pk).exists() is False
|
|
399 |
assert Booking.objects.filter(pk=booking.pk).exists() is not deleted
|
|
400 |
|
|
401 |
|
|
402 |
@pytest.mark.parametrize(
|
|
403 |
'event_date, deleted',
|
|
404 |
[
|
|
405 |
# just before first day
|
|
406 |
((2021, 8, 31, 12, 00), False),
|
|
407 |
# first day
|
|
408 |
((2021, 9, 1, 12, 00), True),
|
|
409 |
# last day
|
|
410 |
((2021, 9, 30, 12, 00), True),
|
|
411 |
# just after last day
|
|
412 |
((2021, 10, 1, 12, 00), False),
|
|
413 |
],
|
|
414 |
)
|
|
415 |
def test_api_delete_subscription_and_bookings_on_limit(app, user, freezer, event_date, deleted):
|
|
416 |
agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
|
417 |
subscription = Subscription.objects.create(
|
|
418 |
agenda=agenda,
|
|
419 |
user_external_id='xxx',
|
|
420 |
date_start=datetime.date(year=2021, month=9, day=1),
|
|
421 |
date_end=datetime.date(year=2021, month=9, day=30),
|
|
422 |
)
|
|
423 |
|
|
424 |
app.authorization = ('Basic', ('john.doe', 'password'))
|
|
425 |
|
|
426 |
freezer.move_to('2021-08-01 10:00')
|
|
427 |
event = Event.objects.create(
|
|
428 |
agenda=agenda, start_datetime=make_aware(datetime.datetime(*event_date)), places=10
|
|
429 |
)
|
|
430 |
booking = Booking.objects.create(
|
|
431 |
event=event,
|
|
432 |
user_external_id='xxx',
|
|
433 |
)
|
|
434 |
resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
|
|
435 |
assert resp.json['err'] == 0
|
|
436 |
assert Subscription.objects.filter(pk=subscription.pk).exists() is False
|
|
437 |
assert Booking.objects.filter(pk=booking.pk).exists() is not deleted
|
|
438 |
|
|
439 |
|
319 |
440 |
def test_api_patch_subscription(app, user):
|
320 |
441 |
agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
321 |
442 |
subscription = Subscription.objects.create(
|
322 |
|
-
|