315 lines
13 KiB
Python
315 lines
13 KiB
Python
import json
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
from django.test.client import RequestFactory
|
|
from publik_django_templatetags.wcs.context_processors import Cards
|
|
|
|
from combo.apps.family.models import WeeklyAgendaCell
|
|
from combo.data.models import Page
|
|
|
|
from .test_manager import login
|
|
from .utils import manager_submit_cell
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
@pytest.fixture
|
|
def context():
|
|
ctx = {'cards': Cards(), 'request': RequestFactory().get('/')}
|
|
ctx['request'].user = None
|
|
ctx['request'].session = {}
|
|
return ctx
|
|
|
|
|
|
class MockUser:
|
|
email = 'foo@example.net'
|
|
is_authenticated = True
|
|
is_anonymous = False
|
|
|
|
def get_name_id(self):
|
|
return None
|
|
|
|
|
|
class MockUserWithNameId:
|
|
email = 'foo@example.net'
|
|
is_authenticated = True
|
|
is_anonymous = False
|
|
|
|
def get_name_id(self):
|
|
return 'xyz'
|
|
|
|
|
|
class MockedRequestResponse(mock.Mock):
|
|
status_code = 200
|
|
|
|
def json(self):
|
|
return json.loads(self.content)
|
|
|
|
|
|
def test_manager_weeklyagenda_cell(app, admin_user):
|
|
page = Page.objects.create(title='Family', slug='family', template_name='standard')
|
|
cell = WeeklyAgendaCell.objects.create(page=page, placeholder='content', order=0)
|
|
|
|
app = login(app)
|
|
resp = app.get('/manage/pages/%s/' % page.pk)
|
|
assert ('data-cell-reference="%s"' % cell.get_reference()) in resp
|
|
assert cell.agenda_type == 'manual'
|
|
assert cell.agenda_references_template == ''
|
|
assert cell.agenda_categories == ''
|
|
assert cell.start_date_filter == ''
|
|
assert cell.end_date_filter == ''
|
|
resp.forms[0]['c%s-agenda_type' % cell.get_reference()].value = 'subscribed'
|
|
resp.forms[0]['c%s-agenda_references_template' % cell.get_reference()].value = 'foo,bar'
|
|
resp.forms[0]['c%s-agenda_categories' % cell.get_reference()].value = 'bar,foo'
|
|
resp.forms[0]['c%s-start_date_filter' % cell.get_reference()].value = '{{ start_date }}'
|
|
resp.forms[0]['c%s-end_date_filter' % cell.get_reference()].value = '{{ end_date }}'
|
|
manager_submit_cell(resp.form)
|
|
cell.refresh_from_db()
|
|
assert cell.agenda_type == 'subscribed'
|
|
assert cell.agenda_references_template == ''
|
|
assert cell.agenda_categories == 'bar,foo'
|
|
assert cell.start_date_filter == '{{ start_date }}'
|
|
assert cell.end_date_filter == '{{ end_date }}'
|
|
|
|
resp.forms[0]['c%s-agenda_type' % cell.get_reference()].value = 'manual'
|
|
resp.forms[0]['c%s-agenda_references_template' % cell.get_reference()].value = 'foo,bar'
|
|
resp.forms[0]['c%s-agenda_categories' % cell.get_reference()].value = 'bar,foo'
|
|
manager_submit_cell(resp.form)
|
|
cell.refresh_from_db()
|
|
assert cell.agenda_type == 'manual'
|
|
assert cell.agenda_references_template == 'foo,bar'
|
|
assert cell.agenda_categories == ''
|
|
assert cell.start_date_filter == '{{ start_date }}'
|
|
assert cell.end_date_filter == '{{ end_date }}'
|
|
|
|
|
|
def test_manager_weeklyagenda_cell_tabs(app, admin_user):
|
|
page = Page.objects.create(title='Family', slug='family', template_name='standard')
|
|
WeeklyAgendaCell.objects.create(page=page, placeholder='content', order=0)
|
|
login(app)
|
|
resp = app.get('/manage/pages/%s/' % page.pk)
|
|
|
|
assert not resp.pyquery('[data-tab-slug="general"] input[name$="title"]')
|
|
assert resp.pyquery('[data-tab-slug="appearance"] input[name$="title"]')
|
|
|
|
|
|
def test_weeklyagenda_cell_user_external_id(context):
|
|
page = Page.objects.create(title='Family', slug='index', template_name='standard')
|
|
cell = WeeklyAgendaCell.objects.create(page=page, placeholder='content', order=0, agenda_type='manual')
|
|
|
|
context['request'].user = MockUser()
|
|
context['synchronous'] = True # to get fresh content
|
|
|
|
data = {'data': []}
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?agendas=&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
context['request'].user = MockUserWithNameId()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?agendas=&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
cell.user_external_template = 'some-key:{{ user_nameid }}' # check that templating is ok
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?agendas=&date_start=&date_end=&user_external_id=some-key:xyz&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
|
|
def test_weeklyagenda_cell_agenda_references_template(context):
|
|
page = Page.objects.create(title='Family', slug='index', template_name='standard')
|
|
cell = WeeklyAgendaCell.objects.create(page=page, placeholder='content', order=0, agenda_type='manual')
|
|
|
|
context['request'].user = MockUser()
|
|
context['synchronous'] = True # to get fresh content
|
|
|
|
data = {'data': []}
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?agendas=&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
cell.agenda_references_template = 'some-agenda,other-agenda'
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?agendas=some-agenda,other-agenda&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
context['request'].user = MockUserWithNameId()
|
|
cell.agenda_references_template = (
|
|
'{{ cards|objects:"foo"|get_full|first|get:"fields"|get:"bar"|default:"" }}'
|
|
',some-agenda,other-agenda,{{ user_nameid }}' # check that templating is ok
|
|
)
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[1][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?agendas=,some-agenda,other-agenda,xyz&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
|
|
def test_weeklyagenda_cell_agenda_subscribed(context):
|
|
page = Page.objects.create(title='Family', slug='index', template_name='standard')
|
|
cell = WeeklyAgendaCell.objects.create(
|
|
page=page, placeholder='content', order=0, agenda_type='subscribed'
|
|
)
|
|
|
|
context['request'].user = MockUser()
|
|
context['synchronous'] = True # to get fresh content
|
|
|
|
data = {'data': []}
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?subscribed=all&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
|
|
def test_weeklyagenda_cell_agenda_agenda_categories(context):
|
|
page = Page.objects.create(title='Family', slug='index', template_name='standard')
|
|
cell = WeeklyAgendaCell.objects.create(
|
|
page=page,
|
|
placeholder='content',
|
|
order=0,
|
|
agenda_type='subscribed',
|
|
agenda_categories='foo,bar,foobar',
|
|
)
|
|
|
|
context['request'].user = MockUser()
|
|
context['synchronous'] = True # to get fresh content
|
|
|
|
data = {'data': []}
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?subscribed=foo,bar,foobar&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
context['request'].user = MockUserWithNameId()
|
|
cell.agenda_categories = 'foo,bar,foobar,{{ user_nameid }}' # check that templating is ok
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?subscribed=foo,bar,foobar,xyz&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'agenda_type,agenda_param', [('manual', 'agendas='), ('subscribed', 'subscribed=all')]
|
|
)
|
|
def test_weeklyagenda_cell_filter_dates(context, agenda_type, agenda_param):
|
|
page = Page.objects.create(title='Family', slug='index', template_name='standard')
|
|
cell = WeeklyAgendaCell.objects.create(page=page, placeholder='content', order=0, agenda_type=agenda_type)
|
|
|
|
context['request'].user = MockUser()
|
|
context['synchronous'] = True # to get fresh content
|
|
|
|
data = {'data': []}
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?%s&date_start=&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
% agenda_param
|
|
)
|
|
|
|
cell.start_date_filter = '2021-09-01'
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?%s&date_start=2021-09-01&date_end=&user_external_id=&show_past_events=true&with_status=true'
|
|
% agenda_param
|
|
)
|
|
|
|
cell.end_date_filter = '2022-08-31'
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?%s&date_start=2021-09-01&date_end=2022-08-31&user_external_id=&show_past_events=true&with_status=true'
|
|
% agenda_param
|
|
)
|
|
|
|
context['request'].user = MockUserWithNameId()
|
|
cell.start_date_filter = ''
|
|
cell.end_date_filter = '{{ user_nameid }}' # check that templating is ok
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
cell.render(context)
|
|
assert requests_get.call_args_list[0][0][0] == (
|
|
'http://chrono.example.org/api/agendas/datetimes/'
|
|
'?%s&date_start=&date_end=xyz&user_external_id=&show_past_events=true&with_status=true'
|
|
% agenda_param
|
|
)
|
|
|
|
|
|
def test_weeklyagenda_cell_booking_form_url(context):
|
|
page = Page.objects.create(title='Family', slug='index', template_name='standard')
|
|
cell = WeeklyAgendaCell.objects.create(page=page, placeholder='content', order=0)
|
|
data = {
|
|
'data': [
|
|
{'text': 'Foo', 'date': '2022-02-28'},
|
|
]
|
|
}
|
|
|
|
context['synchronous'] = True # to get fresh content
|
|
context['request'].user = MockUserWithNameId()
|
|
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
result = cell.render(context)
|
|
assert 'booking-btn' not in result
|
|
|
|
cell.booking_form_url = 'http://example.com/foobar/'
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
result = cell.render(context)
|
|
assert 'booking-btn' in result
|
|
assert 'http://example.com/foobar/?current=2022-02-28' in result
|
|
|
|
cell.booking_form_url = 'http://example.com/foobar/?user={{ user_nameid }}'
|
|
cell.save()
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
result = cell.render(context)
|
|
assert 'booking-btn' in result
|
|
assert 'http://example.com/foobar/?user=xyz¤t=2022-02-28' in result
|