summaryrefslogtreecommitdiffstats
path: root/tests/test_pages.py
blob: 5f45670e539f4480192f900aed57b5ff26d40a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import datetime
import os
import pytest
import sys

from django.conf import settings
from django.contrib.auth.models import User, Group
from django.test import override_settings
from django.test.client import RequestFactory
from django.utils.six import StringIO
from django.utils.timezone import now
from combo.data.models import Page, PageSnapshot, CellBase, TextCell, LinkCell
from combo.data.management.commands.import_site import Command as ImportSiteCommand
from combo.data.management.commands.export_site import Command as ExportSiteCommand
from combo.manager.forms import PageVisibilityForm

pytestmark = pytest.mark.django_db

def test_page_order():
    page = Page()
    page.title = u'foo'
    page.save()
    page2 = Page()
    page2.title = u'bar'
    page2.save()

    assert Page.objects.get(id=page.id).order < Page.objects.get(id=page2.id).order

def test_page_url():
    page = Page()
    page.slug = u'foo'
    page.save()
    assert page.get_online_url() == '/foo/'
    page2 = Page()
    page2.slug = 'bar'
    page2.parent = page
    assert page2.get_online_url() == '/foo/bar/'

def test_page_of_level():
    parent_page = None
    for i in range(10):
        page = Page()
        page.slug = 'level%d' % i
        if parent_page:
            page.parent = parent_page
        page.save()
        parent_page = page

    assert page.get_page_of_level(2).slug == 'level2'
    assert page.get_page_of_level(4).slug == 'level4'
    assert page.get_page_of_level(14) is None

def test_page_siblings():
    Page.objects.all().delete()
    page = Page()
    page.slug = 'foo'
    page.save()
    page2 = Page()
    page2.slug = 'bar'
    page2.parent = page
    page2.save()
    page3 = Page()
    page3.slug = 'baz'
    page3.parent = page
    page3.save()
    assert [x.slug for x in page3.get_siblings()] == ['bar', 'baz']

    assert page.has_children()
    assert not page2.has_children()

def test_flat_hierarchy():
    Page.objects.all().delete()
    page = Page()
    page.slug = 'foo'
    page.save()
    page2 = Page()
    page2.slug = 'bar'
    page2.parent = page
    page2.save()
    page3 = Page()
    page3.slug = 'baz'
    page3.parent = page
    page3.save()

    pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all())
    assert [x.slug for x in pages] == ['foo', 'bar', 'baz']

    page.order = 17
    page.save()
    pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all())
    assert [x.slug for x in pages] == ['foo', 'bar', 'baz']

    page2.parent = None
    page2.save()
    pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all())
    assert [x.slug for x in pages] == ['bar', 'foo', 'baz']

    page.parent = page2
    page.save()
    pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all())
    assert [x.slug for x in pages] == ['bar', 'foo', 'baz']

def test_page_visibility():
    page = Page()
    assert page.is_visible()

    page.public = False
    page.save()
    assert not page.is_visible()

    group = Group(name='foobar')
    group.save()
    user1 = User(username='foo')
    user1.save()
    user1.groups.set([group])
    user2 = User(username='bar')
    user2.save()

    assert page.is_visible(user1)
    assert page.is_visible(user2)

    page.groups.set([group])
    assert not page.is_visible()
    assert page.is_visible(user1)
    assert not page.is_visible(user2)


def test_page_visibility_groups_init():
    page = Page.objects.create()
    group1 = Group.objects.create(name='foobar')
    group2 = Group.objects.create(name='another group')

    form = PageVisibilityForm(instance=page)
    assert form['groups'].initial == []

    page.groups.set([group1])
    form = PageVisibilityForm(instance=page)
    assert form['groups'].initial == [group1]

    # test sorting
    assert list(form.fields['groups'].queryset) == [group2, group1]


def test_import_export_pages():
    page = Page(title=u'foo', slug='foo', order=0, description="Foo's page")
    page.save()

    cell = TextCell(page=page, text='foo', order=0, placeholder='content')
    cell.save()

    page2 = Page(title=u'bar', slug='bar', order=1, parent=page, description="Bar's page")
    page2.save()

    cell = TextCell(page=page2, text='bar', order=0, placeholder='content')
    cell.save()

    site_export = [x.get_serialized_page() for x in Page.objects.all()]
    Page.objects.all().delete()

    Page.load_serialized_pages(site_export)

    new_page_1 = Page.objects.all().order_by('order')[0]
    new_page_2 = Page.objects.all().order_by('order')[1]
    assert new_page_1.title == 'foo'
    assert new_page_2.title == 'bar'
    assert new_page_1.description == "Foo's page"
    assert new_page_2.description == "Bar's page"
    assert len(CellBase.get_cells(page_id=new_page_1.id)) == 1
    assert isinstance(CellBase.get_cells(page_id=new_page_1.id)[0], TextCell)
    assert CellBase.get_cells(page_id=new_page_1.id)[0].text == 'foo'

def test_import_export_pages_with_links():
    page = Page(title=u'foo', slug='foo', order=0)
    page.save()

    page2 = Page(title=u'bar', slug='bar', order=1)
    page2.save()

    cell = LinkCell(page=page, title='bar', placeholder='content', link_page=page2, order=1)
    cell.save()

    cell2 = LinkCell(page=page2, title='foo', placeholder='content', link_page=page, order=1)
    cell2.save()

    site_export = [x.get_serialized_page() for x in Page.objects.all()]
    Page.objects.all().delete()

    Page.load_serialized_pages(site_export)

    new_page_1 = Page.objects.all().order_by('order')[0]
    new_page_2 = Page.objects.all().order_by('order')[1]
    assert CellBase.get_cells(page_id=new_page_1.id)[0].link_page_id == new_page_2.id
    assert CellBase.get_cells(page_id=new_page_2.id)[0].link_page_id == new_page_1.id


def test_duplicate_page():
    group1 = Group.objects.create(name='foobar')
    group2 = Group.objects.create(name='fooblah')

    Page.objects.create()  # page without snapshot, with slug 'index'
    page = Page.objects.create(
        title='foo',
        slug='foo',
        description="Foo's page")
    page.groups.set([group1, group2])
    snapshot = PageSnapshot.objects.create(page=page)
    page.snapshot = snapshot
    page.save()

    cell1 = TextCell.objects.create(page=page, text='foo1', order=0, placeholder='content')
    cell1.groups.set([group1, group2])
    cell2 = TextCell.objects.create(page=page, text='foo2', order=1, placeholder='content')

    new_page = page.duplicate()
    assert new_page.pk != page.pk
    assert new_page.title == 'Copy of foo'
    assert new_page.slug == 'copy-of-foo'
    assert new_page.description == page.description
    assert new_page.parent is None
    assert new_page.snapshot is None
    assert list(new_page.groups.all()) == [group1, group2]
    assert len(new_page.get_cells()) == 2

    new_cell1 = TextCell.objects.get(page=new_page, text='foo1')
    new_cell2 = TextCell.objects.get(page=new_page, text='foo2')
    assert new_cell1.pk != cell1.pk
    assert new_cell1.text == cell1.text
    assert new_cell1.placeholder == cell1.placeholder
    assert list(new_cell1.groups.all()) == [group1, group2]
    assert new_cell2.pk != cell2.pk
    assert new_cell2.text == cell2.text
    assert new_cell2.placeholder == cell2.placeholder
    assert list(new_cell2.groups.all()) == []

    # duplicate again !
    new_page = page.duplicate()
    assert new_page.slug == 'copy-of-foo-2'

    parent = Page.objects.create()
    page.parent = parent
    page.save()
    new_page = page.duplicate()
    assert new_page.parent == parent


def test_next_previous():
    Page.objects.all().delete()
    page = Page()
    page.slug = 'foo'
    page.save()
    page2 = Page()
    page2.slug = 'bar'
    page2.parent = page
    page2.order = 1
    page2.save()
    page3 = Page()
    page3.slug = 'baz'
    page3.parent = page
    page3.order = 2
    page3.save()

    assert page.get_next_page(None).id == page2.id
    assert page.get_previous_page(None) is None

    assert page2.get_next_page(None).id == page3.id
    assert page2.get_previous_page(None).id == page.id

    page2.public = False
    page2.save()
    assert page.get_next_page(None).id == page3.id
    assert page3.get_previous_page(None).id == page.id

    user1 = User(username='foo')
    user1.save()
    assert page.get_next_page(user1).id == page2.id
    assert page3.get_previous_page(user1).id == page2.id

    page3.public = False
    page3.save()
    assert page.get_next_page(None) is None

    assert page.get_next_page(check_visibility=False).id == page2.pk
    assert page2.get_next_page(check_visibility=False).id == page3.pk
    assert page3.get_previous_page(check_visibility=False).id == page2.pk


def test_import_export_management_commands():
    page = Page(title=u'foo', slug='foo', order=0)
    page.save()

    cell = TextCell(page=page, text='foo', order=0, placeholder='content')
    cell.save()

    page2 = Page(title=u'bar', slug='bar', order=1, parent=page)
    page2.save()

    cell = TextCell(page=page2, text='bar', order=0, placeholder='content')
    cell.save()

    export_filename = os.path.join(settings.MEDIA_ROOT, 'site-export.json')
    if os.path.exists(export_filename):
        os.unlink(export_filename)

    cmd = ExportSiteCommand()
    cmd.handle(output=export_filename)
    assert os.path.exists(export_filename)

    stdout = sys.stdout
    try:
        sys.stdout = StringIO()
        cmd.handle(output='-')
        assert sys.stdout.getvalue() == open(export_filename).read()
    finally:
        sys.stdout = stdout

    Page.objects.all().delete()

    cmd = ImportSiteCommand()
    cmd.handle(filename=export_filename, if_empty=False, clean=False)

    new_page_1 = Page.objects.all().order_by('order')[0]
    new_page_2 = Page.objects.all().order_by('order')[1]
    assert new_page_1.title == 'foo'
    assert new_page_2.title == 'bar'
    assert len(CellBase.get_cells(page_id=new_page_1.id)) == 1
    assert isinstance(CellBase.get_cells(page_id=new_page_1.id)[0], TextCell)
    assert CellBase.get_cells(page_id=new_page_1.id)[0].text == 'foo'

def test_last_update_time():
    page = Page(title=u'foo', slug='foo', order=0)
    page.save()

    cell = TextCell(page=page, text='foo', order=0)
    cell.save()

    cell = TextCell(page=page, text='bar', order=0)
    cell.save()

    future_time = now() + datetime.timedelta(days=2)
    TextCell.objects.filter(pk=cell.id).update(last_update_timestamp=future_time)

    assert page.get_last_update_time() == future_time

def test_get_placeholders():
    page = Page(title=u'foo', slug='foo', template_name='standard-sidebar', order=0)
    request = RequestFactory().get('/')
    placeholders = page.get_placeholders(request=request)
    assert [x.key for x in placeholders] == ['content', 'sidebar', 'footer']
    assert placeholders[0].acquired is False
    assert placeholders[-1].acquired is True

    with override_settings(COMBO_PUBLIC_TEMPLATES={
            'standard-sidebar': {
                'name': 'Test',
                'template': 'combo/page_template_sidebar.html',
                'placeholders': {
                    'content': {'name': 'Content'},
                    'content2': {'name': 'Second Content'},
                    'sidebar': {'name': 'Sidebar'},
                    'footer': {'name': 'Footer', 'acquired': False},
                    }}}):
        placeholders = page.get_placeholders(request=request)
        assert set([x.key for x in placeholders]) == set(['content', 'content2', 'sidebar', 'footer'])
        footer_placeholder = [x for x in placeholders if x.key == 'footer'][0]
        assert footer_placeholder.acquired is False

def test_render(app):
    page = Page(title=u'foo', slug='foo', template_name='standard-sidebar', order=0, description="page description")
    page.save()
    response = app.get(page.get_online_url())
    assert '<meta name="description" content="page description" />' in response.text
    assert '<title>Combo - foo</title>' in response.text

def test_render_cell_having_href_template_error(app):
    page = Page(title=u'foo', slug='foo', template_name='standard-sidebar', order=0, description="page description")
    page.save()
    cell = TextCell(page=page,
                    text='<a href="{{e-service_url}}backoffice/...">link</a>',
                    order=0, placeholder='content')
    cell.save()
    response = app.get(page.get_online_url())
    assert "{{e-service_url}}backoffice/..." in response.text  # href not rendered