2022-07-26 10:16:11 +02:00
|
|
|
import datetime
|
|
|
|
|
2022-04-04 15:14:39 +02:00
|
|
|
import pytest
|
2022-04-26 10:59:33 +02:00
|
|
|
from django.core.files.base import ContentFile
|
2022-06-29 17:13:46 +02:00
|
|
|
from django.utils.timezone import now
|
2022-04-04 15:14:39 +02:00
|
|
|
|
2022-06-28 15:17:57 +02:00
|
|
|
from chrono.agendas.models import Person, SharedCustodyAgenda, SharedCustodySettings, UnavailabilityCalendar
|
2022-04-04 15:14:39 +02:00
|
|
|
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
|
|
|
2022-04-26 10:59:33 +02:00
|
|
|
with open('tests/data/holidays.ics') as f:
|
|
|
|
ICS_HOLIDAYS = f.read()
|
|
|
|
|
|
|
|
|
2022-04-04 15:14:39 +02:00
|
|
|
def test_add_shared_custody_agenda(app, user, settings):
|
|
|
|
app.authorization = ('Basic', ('john.doe', 'password'))
|
|
|
|
|
|
|
|
params = {
|
2022-04-26 10:59:33 +02:00
|
|
|
'guardian_first_name': 'John',
|
|
|
|
'guardian_last_name': 'Doe',
|
|
|
|
'guardian_id': 'xxx',
|
|
|
|
'other_guardian_first_name': 'Jane',
|
|
|
|
'other_guardian_last_name': 'Doe',
|
|
|
|
'other_guardian_id': 'yyy',
|
2022-11-28 14:49:26 +01:00
|
|
|
'child_first_name': 'James',
|
|
|
|
'child_last_name': 'Doe',
|
|
|
|
'child_id': 'zzz',
|
2022-06-29 17:13:46 +02:00
|
|
|
'date_start': '2020-10-20',
|
2022-04-04 15:14:39 +02:00
|
|
|
}
|
|
|
|
resp = app.post_json('/api/shared-custody/', params=params)
|
|
|
|
|
2022-04-26 10:59:33 +02:00
|
|
|
first_guardian = Person.objects.get(user_external_id='xxx', first_name='John', last_name='Doe')
|
|
|
|
second_guardian = Person.objects.get(user_external_id='yyy', first_name='Jane', last_name='Doe')
|
2022-11-28 14:49:26 +01:00
|
|
|
child = Person.objects.get(user_external_id='zzz', first_name='James', last_name='Doe')
|
2022-04-04 15:14:39 +02:00
|
|
|
|
|
|
|
agenda = SharedCustodyAgenda.objects.get()
|
2022-04-26 10:59:33 +02:00
|
|
|
assert agenda.first_guardian == first_guardian
|
|
|
|
assert agenda.second_guardian == second_guardian
|
2022-11-28 14:49:26 +01:00
|
|
|
assert agenda.child == child
|
2022-04-04 15:14:39 +02:00
|
|
|
|
|
|
|
assert resp.json['data'] == {
|
|
|
|
'id': agenda.pk,
|
|
|
|
'settings_url': 'http://testserver/manage/shared-custody/%s/settings/' % agenda.pk,
|
2022-06-27 17:08:16 +02:00
|
|
|
'backoffice_url': 'http://testserver/manage/shared-custody/%s/' % agenda.pk,
|
2022-04-04 15:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
params = {
|
2022-04-26 10:59:33 +02:00
|
|
|
'guardian_first_name': 'John',
|
|
|
|
'guardian_last_name': 'Doe',
|
|
|
|
'guardian_id': 'xxx',
|
|
|
|
'other_guardian_first_name': 'Other',
|
|
|
|
'other_guardian_last_name': 'Doe',
|
|
|
|
'other_guardian_id': 'other',
|
2022-11-28 14:49:26 +01:00
|
|
|
'child_first_name': 'Bruce',
|
|
|
|
'child_last_name': 'Doe',
|
|
|
|
'child_id': 'bruce',
|
2022-06-29 17:13:46 +02:00
|
|
|
'date_start': '2020-10-20',
|
2022-04-04 15:14:39 +02:00
|
|
|
}
|
|
|
|
resp = app.post_json('/api/shared-custody/', params=params)
|
|
|
|
assert resp.json['data']['id'] != agenda.pk
|
2022-04-26 10:59:33 +02:00
|
|
|
assert SharedCustodyAgenda.objects.filter(first_guardian=first_guardian).count() == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_shared_custody_agenda_with_rules(app, user, settings):
|
|
|
|
app.authorization = ('Basic', ('john.doe', 'password'))
|
|
|
|
|
|
|
|
# configure holidays
|
2022-06-28 15:17:57 +02:00
|
|
|
unavailability_calendar = UnavailabilityCalendar.objects.create(label='Calendar')
|
|
|
|
SharedCustodySettings.objects.create(holidays_calendar=unavailability_calendar)
|
2022-04-26 10:59:33 +02:00
|
|
|
source = unavailability_calendar.timeperiodexceptionsource_set.create(
|
|
|
|
ics_filename='holidays.ics', ics_file=ContentFile(ICS_HOLIDAYS, name='holidays.ics')
|
|
|
|
)
|
|
|
|
source.refresh_timeperiod_exceptions_from_ics()
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'guardian_first_name': 'John',
|
|
|
|
'guardian_last_name': 'Doe',
|
|
|
|
'guardian_id': 'xxx',
|
|
|
|
'other_guardian_first_name': 'Jane',
|
|
|
|
'other_guardian_last_name': 'Doe',
|
|
|
|
'other_guardian_id': 'yyy',
|
2022-11-28 14:49:26 +01:00
|
|
|
'child_first_name': 'James',
|
|
|
|
'child_last_name': 'Doe',
|
|
|
|
'child_id': 'zzz',
|
2022-06-29 17:13:46 +02:00
|
|
|
'date_start': '2020-10-20',
|
2022-04-26 10:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp = app.post_json('/api/shared-custody/', params={'weeks': '', **params})
|
|
|
|
agenda = SharedCustodyAgenda.objects.get(pk=resp.json['data']['id'])
|
|
|
|
assert not agenda.is_complete()
|
|
|
|
assert not agenda.rules.exists()
|
|
|
|
|
|
|
|
resp = app.post_json('/api/shared-custody/', params={'weeks': 'even', **params})
|
|
|
|
agenda = SharedCustodyAgenda.objects.get(pk=resp.json['data']['id'])
|
|
|
|
assert agenda.is_complete()
|
|
|
|
assert agenda.rules.filter(guardian__first_name='John', weeks='even').exists()
|
|
|
|
assert agenda.rules.filter(guardian__first_name='Jane', weeks='odd').exists()
|
|
|
|
|
|
|
|
resp = app.post_json('/api/shared-custody/', params={'weeks': 'odd', **params})
|
|
|
|
agenda = SharedCustodyAgenda.objects.get(pk=resp.json['data']['id'])
|
|
|
|
assert agenda.is_complete()
|
|
|
|
assert agenda.rules.filter(guardian__first_name='John', weeks='odd').exists()
|
|
|
|
assert agenda.rules.filter(guardian__first_name='Jane', weeks='even').exists()
|
|
|
|
|
|
|
|
resp = app.post_json(
|
|
|
|
'/api/shared-custody/', params={'christmas_holidays:periodicity': 'first-half', **params}
|
|
|
|
)
|
|
|
|
agenda = SharedCustodyAgenda.objects.get(pk=resp.json['data']['id'])
|
|
|
|
assert agenda.holiday_rules.filter(
|
|
|
|
guardian__first_name='John', holiday__slug='christmas_holidays', periodicity='first-half', years=''
|
|
|
|
).exists()
|
|
|
|
assert agenda.holiday_rules.filter(
|
|
|
|
guardian__first_name='Jane', holiday__slug='christmas_holidays', periodicity='second-half', years=''
|
|
|
|
).exists()
|
|
|
|
assert agenda.periods.count() == 12
|
|
|
|
|
|
|
|
resp = app.post_json(
|
|
|
|
'/api/shared-custody/',
|
|
|
|
params={
|
|
|
|
'summer_holidays:periodicity': 'first-and-third-quarters',
|
|
|
|
'summer_holidays:years': 'odd',
|
|
|
|
**params,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
agenda = SharedCustodyAgenda.objects.get(pk=resp.json['data']['id'])
|
|
|
|
assert agenda.holiday_rules.filter(
|
|
|
|
guardian__first_name='John',
|
|
|
|
holiday__slug='summer_holidays',
|
|
|
|
periodicity='first-and-third-quarters',
|
|
|
|
years='odd',
|
|
|
|
).exists()
|
|
|
|
assert agenda.holiday_rules.filter(
|
|
|
|
guardian__first_name='John',
|
|
|
|
holiday__slug='summer_holidays',
|
|
|
|
periodicity='second-and-fourth-quarters',
|
|
|
|
years='even',
|
|
|
|
).exists()
|
|
|
|
assert agenda.holiday_rules.filter(
|
|
|
|
guardian__first_name='Jane',
|
|
|
|
holiday__slug='summer_holidays',
|
|
|
|
periodicity='first-and-third-quarters',
|
|
|
|
years='even',
|
|
|
|
).exists()
|
|
|
|
assert agenda.holiday_rules.filter(
|
|
|
|
guardian__first_name='Jane',
|
|
|
|
holiday__slug='summer_holidays',
|
|
|
|
periodicity='second-and-fourth-quarters',
|
|
|
|
years='odd',
|
|
|
|
).exists()
|
|
|
|
assert agenda.periods.count() == 20
|
|
|
|
|
|
|
|
# unknown holiday
|
|
|
|
resp = app.post_json(
|
|
|
|
'/api/shared-custody/', params={'unknown:periodicity': 'first-half', **params}, status=400
|
|
|
|
)
|
|
|
|
assert resp.json['errors']['non_field_errors'][0] == 'Unknown holiday: unknown'
|
|
|
|
|
|
|
|
# unknown holiday param
|
|
|
|
resp = app.post_json(
|
|
|
|
'/api/shared-custody/', params={'summer_holidays:unknown': 'first-half', **params}, status=400
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
resp.json['errors']['non_field_errors'][0] == 'Unknown parameter for holiday summer_holidays: unknown'
|
|
|
|
)
|
|
|
|
|
|
|
|
# years without periodicity
|
|
|
|
resp = app.post_json(
|
|
|
|
'/api/shared-custody/', params={'summer_holidays:years': 'even', **params}, status=400
|
|
|
|
)
|
|
|
|
assert resp.json['errors']['non_field_errors'][0] == 'Missing periodicity for holiday: summer_holidays'
|
|
|
|
|
|
|
|
# quarters with short holiday
|
|
|
|
resp = app.post_json(
|
|
|
|
'/api/shared-custody/',
|
|
|
|
params={'christmas_holidays:periodicity': 'first-and-third-quarters', **params},
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
assert resp.json['errors']['non_field_errors'][0] == 'Short holidays cannot be cut into quarters.'
|
2022-04-04 15:14:39 +02:00
|
|
|
|
|
|
|
|
2022-07-26 10:16:11 +02:00
|
|
|
def test_shared_custody_agenda_update_date_start(app, user, settings):
|
|
|
|
father = Person.objects.create(user_external_id='father_id', first_name='John', last_name='Doe')
|
|
|
|
mother = Person.objects.create(user_external_id='mother_id', first_name='Jane', last_name='Doe')
|
2022-11-28 14:49:26 +01:00
|
|
|
child = Person.objects.create(user_external_id='child_id', first_name='James', last_name='Doe')
|
2022-07-26 10:16:11 +02:00
|
|
|
agenda = SharedCustodyAgenda.objects.create(
|
2022-11-28 14:49:26 +01:00
|
|
|
first_guardian=father, second_guardian=mother, child=child, date_start=now()
|
2022-07-26 10:16:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
app.authorization = ('Basic', ('john.doe', 'password'))
|
|
|
|
resp = app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={'date_start': '2020-10-20'})
|
|
|
|
assert resp.json['err'] == 0
|
|
|
|
|
|
|
|
agenda.refresh_from_db()
|
|
|
|
assert agenda.date_start == datetime.date(year=2020, month=10, day=20)
|
|
|
|
|
|
|
|
resp = app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={'first_guardian': 'xxx'}, status=400)
|
|
|
|
app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={}, status=400)
|
|
|
|
|
|
|
|
agenda.delete()
|
|
|
|
app.patch_json('/api/shared-custody/1/', params={'date_start': '2020-10-20'}, status=404)
|