summaryrefslogtreecommitdiffstats
path: root/tests/test_manager.py
blob: 3cb0ecd1047afdab0c81ae7867a26050314c4d69 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
import base64
import json
import os
import re
import shutil

import mock

from django.core.files.storage import default_storage
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.auth.models import User, Group
from django.template import TemplateSyntaxError
from django.test import override_settings
from django.utils.http import urlencode
from django.utils.six import BytesIO
from django.utils.six.moves.urllib import parse as urlparse

import pytest
from webtest import TestApp
from webtest import Upload

from combo.wsgi import application
from combo.data.models import Page, CellBase, TextCell, LinkCell, ConfigJsonCell, JsonCell, PageSnapshot
from combo.apps.assets.models import Asset
from combo.apps.family.models import FamilyInfosCell
from combo.apps.search.models import SearchCell

pytestmark = pytest.mark.django_db

TESTS_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')


def login(app, username='admin', password='admin'):
    login_page = app.get('/login/')
    login_form = login_page.forms[0]
    login_form['username'] = username
    login_form['password'] = password
    resp = login_form.submit()
    assert resp.status_int == 302
    return app

def test_unlogged_access(app):
    # connect while not being logged in
    assert app.get('/manage/', status=302).location.endswith('/login/?next=/manage/')

def test_access(app, admin_user):
    app = login(app)
    resp = app.get('/manage/', status=200)
    assert 'Pages' in resp.text
    assert "This site doesn't have any page yet." in resp.text

def test_add_page(app, admin_user):
    app = login(app)
    resp = app.get('/manage/', status=200)
    resp = resp.click('New')
    assert resp.forms[0]['title'].value == 'Home' # default title for first page
    resp = resp.forms[0].submit()
    assert resp.location.endswith('/manage/pages/1/')
    assert Page.objects.get(slug='index').title == 'Home'
    assert Page.objects.get(slug='index').template_name == 'standard' # first one was taken

def test_add_second_page(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one')
    page.save()
    app = login(app)
    resp = app.get('/manage/', status=200)
    resp = resp.click('New')
    # assert there's no defaul title for the second page
    assert resp.forms[0]['title'].value == ''
    resp.forms[0]['title'].value = 'Foobar'
    resp = resp.forms[0].submit()
    assert Page.objects.get(slug='foobar').title == 'Foobar'


def test_add_child_page(app, admin_user):
    page = Page(title='One', slug='one')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.pk, status=200)
    resp = resp.click(href='.*/add/')
    resp.forms[0]['title'].value = 'Two'
    resp = resp.forms[0].submit()
    child = Page.objects.latest('pk')
    assert resp.location.endswith('/manage/pages/%s/' % child.pk)
    assert child.title == 'Two'
    assert child.parent == page

    app.get('/manage/pages/%s/add/' % child.pk, status=200)
    app.get('/manage/pages/42/add/', status=404)


def test_edit_page(app, admin_user):
    Page.objects.all().delete()
    app = login(app)
    resp = app.get('/manage/', status=200)
    resp = resp.click('New')
    resp = resp.forms[0].submit()
    resp = resp.follow()
    # title
    resp = resp.click(href='.*/title')
    resp.form['title'].value = 'Home 2'
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.all()[0].title == 'Home 2'
    # slug
    resp = resp.click(href='.*/slug')
    resp.form['slug'].value = 'foobar'
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.all()[0].slug == 'foobar'
    # description
    resp = resp.click(href='.*/description')
    resp.form['description'].value = 'second home page'
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.all()[0].description == 'second home page'
    # template name
    resp = resp.click(href='.*/template')
    assert resp.form['template_name'].value == 'standard'
    assert len(resp.form['template_name'].options) == 2 # by default there are 2 templates
    resp.form['template_name'].value = 'standard-sidebar'
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.all()[0].template_name == 'standard-sidebar'
    assert resp.click(href='.*/template').form['template_name'].value == 'standard-sidebar'
    # redirection
    resp = resp.click(href='.*/redirection')
    resp.form['redirect_url'].value = 'http://www.example.net'
    resp = resp.form.submit()
    resp = resp.follow()
    assert 'http://www.example.net' in resp.text
    assert Page.objects.all()[0].redirect_url == 'http://www.example.net'
    # exclude from nav
    resp = resp.click(href='.*/exclude-from-navigation')
    resp.form['exclude_from_navigation'].checked = True
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.all()[0].exclude_from_navigation is True

def test_edit_page_unknown_template(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='broken')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    assert 'Unknown (broken)' in resp.text

def test_page_edit_slug(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    page = Page(title='Two', slug='two', template_name='standard')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='.*/slug')
    resp.form['slug'].value = 'two' # no change
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.get(id=page.id).slug == 'two'

    resp = resp.click(href='.*/slug')
    resp.form['slug'].value = 'two2' # valid change
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.get(id=page.id).slug == 'two2'

    resp = resp.click(href='.*/slug')
    resp.form['slug'].value = 'one' # duplicate
    resp = resp.form.submit()
    assert 'Slug must be unique' in resp.text
    assert Page.objects.get(id=page.id).slug == 'two2'

def test_page_edit_picture(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='.*/picture/')

    resp.form['picture'] = Upload('black.jpeg',
            open(os.path.join(TESTS_DATA_DIR, 'black.jpeg'), mode='rb').read(), 'image/jpeg')
    resp = resp.form.submit()
    assert resp.location.endswith('/manage/pages/%s/' % page.id)
    resp = resp.follow()
    assert '<h2>Page - One</h2>' in resp.text
    assert '<img' in resp.text
    assert 'src="/media/cache/' in resp.text
    resp = resp.click(href='.*/remove-picture/')
    resp = resp.follow()
    assert '<h2>Page - One</h2>' in resp.text
    assert not '<img' in resp.text

    resp = resp.click(href='.*/picture/')
    resp.form['picture'] = Upload('black.svg',
            b'<svg xmlns="http://www.w3.org/2000/svg"/>', 'image/svg+xml')
    resp = resp.form.submit()
    assert resp.location.endswith('/manage/pages/%s/' % page.id)
    resp = resp.follow()
    assert '<h2>Page - One</h2>' in resp.text
    assert '<img' in resp.text
    assert Page.objects.get(id=page.id).picture.url in resp.text


def test_page_placeholder_restricted_visibility(app, admin_user):
    app = login(app)

    page = Page.objects.create()
    group = Group.objects.create(name='foobar')

    resp = app.get(reverse('combo-manager-homepage'))
    assert '<span class="visibility-summary"' not in resp.text

    page.public = False
    page.save()
    resp = app.get(reverse('combo-manager-homepage'))
    assert '<span class="visibility-summary" title="Restricted visibility">' in resp.text
    assert 'foobar' not in resp.text

    page.groups.set([group])
    resp = app.get(reverse('combo-manager-homepage'))
    assert '<span class="visibility-summary" title="Restricted visibility">' in resp.text
    assert 'foobar' in resp.text


def test_delete_page(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click('delete')
    assert '<button class="delete-button">Delete</button>' in resp.text
    resp = resp.forms[0].submit()
    assert resp.location.endswith('/manage/')
    assert Page.objects.count() == 0

def test_delete_page_keep_child(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    page2 = Page(title='Two', slug='two', parent=page, template_name='standard')
    page2.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click('delete')
    assert '<button class="delete-button">Delete</button>' in resp.text
    assert 'Delete only this page' in resp.text
    assert 'Delete this page and all subpages' in resp.text

    resp.form['choice'].value = 'delete-one'
    resp = resp.forms[0].submit()
    assert resp.location.endswith('/manage/')
    assert Page.objects.count() == 1
    assert Page.objects.get(id=page2.id) == page2

def test_delete_page_and_subpage(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    page2 = Page(title='Two', slug='two', parent=page, template_name='standard')
    page2.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click('delete')
    assert '<button class="delete-button">Delete</button>' in resp.text
    assert 'Delete only this page' in resp.text
    assert 'Delete this page and all subpages' in resp.text

    resp.form['choice'].value = 'delete-all'
    resp = resp.forms[0].submit()
    assert resp.location.endswith('/manage/')
    assert Page.objects.count() == 0

def test_page_reorder(app, admin_user):
    Page.objects.all().delete()
    page1 = Page(title='One', slug='one', parent=None, order=0, template_name='standard')
    page1.save()
    page2 = Page(title='Two', slug='two', parent=None, order=1, template_name='standard')
    page2.save()
    page3 = Page(title='Three', slug='three', parent=page2, order=2, template_name='standard')
    page3.save()
    page4 = Page(title='Four', slug='four', parent=page2, order=3, template_name='standard')
    page4.save()

    ordered_ids = [x.id for x in Page.get_as_reordered_flat_hierarchy(Page.objects.all())]
    ordered_ids = [page1.id, page2.id, page3.id, page4.id]

    # move page4 before page3
    app = login(app)
    resp = app.get('/manage/pages/order', params={
        'moved-page-id': page4.id,
        'moved-page-new-parent': page2.id,
        'new-order': ','.join([str(x) for x in [page1.id, page2.id, page4.id, page3.id]])})

    ordered_ids = [x.id for x in Page.get_as_reordered_flat_hierarchy(Page.objects.all())]
    ordered_ids = [page1.id, page2.id, page4.id, page3.id]

    # move page4 to level0
    resp = app.get('/manage/pages/order', params={
        'moved-page-id': page4.id,
        'moved-page-new-parent': '',
        'new-order': ','.join([str(x) for x in [page1.id, page4.id, page2.id, page3.id]])})
    ordered_ids = [x.id for x in Page.get_as_reordered_flat_hierarchy(Page.objects.all())]
    ordered_ids = [page1.id, page4.id, page2.id, page3.id]

    # change slug to check for autochange on duplicate
    page4.slug = 'three'
    page4.save()
    # move it as a sibling of page3
    resp = app.get('/manage/pages/order', params={
        'moved-page-id': page4.id,
        'moved-page-new-parent': page2.id,
        'new-order': ','.join([str(x) for x in [page1.id, page2.id, page4.id, page3.id]])})
    assert Page.objects.get(id=page4.id).slug.startswith('three-')

    # move a parent page as a child of its own child
    for page in (page1, page2, page3, page4):
        page.parent = None
        page.save()
    page2.parent = page1
    page2.save()
    page3.parent = page2
    page3.save()
    resp = app.get('/manage/pages/order', params={
        'moved-page-id': page1.id,
        'moved-page-new-parent': page3.id,
        'new-order': ','.join([str(x) for x in [page2.id, page3.id, page1.id, page4.id]])})
    assert Page.objects.get(id=page2.id).parent_id is None

    for page in (page1, page2, page3, page4):
        page.parent = None
        page.save()
    page2.parent = page1
    page2.save()
    page3.parent = page2
    page3.save()
    page4.parent = page3
    page4.save()
    resp = app.get('/manage/pages/order', params={
        'moved-page-id': page2.id,
        'moved-page-new-parent': page4.id,
        'new-order': ','.join([str(x) for x in [page1.id, page3.id, page4.id, page2.id]])})
    assert Page.objects.get(id=page1.id).parent_id is None
    assert Page.objects.get(id=page3.id).parent_id == page1.id


def test_export_page(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click('export')
    assert resp.headers['content-type'] == 'application/json'
    assert resp.json['pages'][0].get('fields').get('slug') == 'one'

def test_export_page_order():
    Page.objects.all().delete()
    page1 = Page(title='One', slug='one', template_name='standard')
    page2 = Page(title='Two', slug='two', parent=page1, template_name='standard')
    page3 = Page(title='Three', slug='three', parent=page2, template_name='standard')
    page4 = Page(title='Four', slug='four', parent=page1, template_name='standard')
    random_list = [page3, page4, page1, page2]
    ordered_list = Page.get_as_reordered_flat_hierarchy(random_list)
    assert ordered_list[0] == page1
    assert ordered_list[1] == page4
    assert ordered_list[2] == page2
    assert ordered_list[3] == page3

def test_site_export_import(app, admin_user):
    Page.objects.all().delete()
    page1 = Page(title='One', slug='one', template_name='standard')
    page1.save()
    page2 = Page(title='Two', slug='two', parent=page1, template_name='standard')
    page2.save()
    page3 = Page(title='Three', slug='three', parent=page2, template_name='standard')
    page3.save()
    page4 = Page(title='Four', slug='four', parent=page1, template_name='standard')
    page4.save()

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

    cell = LinkCell(page=page1, placeholder='content', link_page=page2, order=0)
    cell.save()

    cell = LinkCell(page=page2, placeholder='content', link_page=page1, order=0)
    cell.save()

    app = login(app)
    resp = app.get('/manage/')
    resp = resp.click('Export Site')
    assert resp.headers['content-type'] == 'application/json'
    site_export = resp.body

    Page.objects.all().delete()
    assert LinkCell.objects.count() == 0
    app = login(app)
    resp = app.get('/manage/')
    resp = resp.click('Import Site')
    resp.form['site_json'] = Upload('site-export.json', site_export, 'application/json')
    resp = resp.form.submit()
    assert Page.objects.count() == 4
    assert LinkCell.objects.count() == 2
    assert LinkCell.objects.get(page__slug='one').link_page.slug == 'two'
    assert LinkCell.objects.get(page__slug='two').link_page.slug == 'one'

    # check with invalid file
    resp = app.get('/manage/')
    resp = resp.click('Import Site')
    resp.form['site_json'] = Upload('site-export.json', b'invalid content', 'application/json')
    resp = resp.form.submit()
    assert 'File is not in the expected JSON format.' in resp.text

def test_add_edit_cell(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    # click on first option link, this should add a text cell
    resp = app.get(resp.html.find('option').get('data-add-url'))

    cells = CellBase.get_cells(page_id=page.id)
    assert len(cells) == 1
    assert isinstance(cells[0], TextCell)
    assert resp.location.endswith('/manage/pages/1/#cell-%s' % cells[0].get_reference())

    resp = app.get('/manage/pages/%s/' % page.id)
    assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.text
    resp.forms[0]['c%s-text' % cells[0].get_reference()].value = 'Hello world'
    resp = resp.forms[0].submit()
    assert resp.status_int == 302
    assert resp.location.endswith('/manage/pages/1/#cell-%s' % cells[0].get_reference())

    resp = app.get('/manage/pages/%s/' % page.id)
    assert resp.forms[0]['c%s-text' % cells[0].get_reference()].value == 'Hello world'

    resp = app.get('/manage/pages/%s/' % page.id)
    assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.text
    resp.forms[0]['c%s-text' % cells[0].get_reference()].value = 'World Hello'
    resp = resp.forms[0].submit(xhr=True)
    assert resp.status_int == 200
    assert resp.text.strip().startswith('<p><label')

    resp = app.get('/manage/pages/%s/' % page.id)
    assert resp.forms[0]['c%s-text' % cells[0].get_reference()].value == 'World Hello'

def test_add_all_basic_cell_types(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    for option in resp.html.find_all('option'):
        if not '/data_' in option.get('data-add-url'):
            continue
        resp = app.get(option.get('data-add-url'))
        resp = resp.follow()

SEARCH_SERVICES = {
    'search1': {
        'label': 'Search 1',
        'url': 'http://www.example.net/search/?q=%(q)s',
    }
}


class SearchServices(object):
    def __init__(self, search_services):
        self.search_services = search_services
    def __enter__(self):
        settings.COMBO_SEARCH_SERVICES = self.search_services
    def __exit__(self, *args, **kwargs):
        settings.COMBO_SEARCH_SERVICES = {}

def test_search_cell(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()

    # add
    app = login(app)
    assert SearchCell.objects.count() == 0
    with SearchServices(SEARCH_SERVICES):
        resp = app.get('/manage/pages/%s/' % page.id)
        for option in resp.html.find_all('option'):
            if 'content/search_' in option.get('data-add-url'):
                resp = app.get(option.get('data-add-url'))
                resp = resp.follow()
        assert SearchCell.objects.count() == 1
        cell = SearchCell.objects.first()

    # delete
    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/search_searchcell-%s/delete' % cell.id)
    resp = resp.form.submit()
    assert SearchCell.objects.count() == 0


def test_delete_cell(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    cell = TextCell(page=page, placeholder='content', text='Foobar', order=0)
    cell.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/delete' % cell.id)
    resp = resp.form.submit()
    assert TextCell.objects.count() == 0

def test_edit_cell_visibility(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    cell = TextCell(page=page, placeholder='content', text='Foobar', order=0)
    cell.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/visibility' % cell.id)
    assert resp.form['cdata_textcell-%s-visibility' % cell.id].value == 'all'
    resp.form['cdata_textcell-%s-visibility' % cell.id] = 'logged'
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).public is False
    assert TextCell.objects.get(id=cell.id).restricted_to_unlogged is False
    assert TextCell.objects.get(id=cell.id).groups.count() == 0

    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/visibility' % cell.id)
    assert resp.form['cdata_textcell-%s-visibility' % cell.id].value == 'logged'
    resp.form['cdata_textcell-%s-visibility' % cell.id] = 'unlogged'
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).restricted_to_unlogged is True
    assert TextCell.objects.get(id=cell.id).public is True
    assert TextCell.objects.get(id=cell.id).groups.count() == 0

    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/visibility' % cell.id)
    assert resp.form['cdata_textcell-%s-visibility' % cell.id].value == 'unlogged'
    resp.form['cdata_textcell-%s-visibility' % cell.id] = 'all'
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).restricted_to_unlogged is False
    assert TextCell.objects.get(id=cell.id).public is True
    assert TextCell.objects.get(id=cell.id).groups.count() == 0

    group1 = Group(name='A group')
    group1.save()
    group2 = Group(name='Another group')
    group2.save()

    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/visibility' % cell.id)
    resp.form['cdata_textcell-%s-visibility' % cell.id] = 'groups-any'
    resp.form['cdata_textcell-%s-groups' % cell.id] = [group1.id]
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).restricted_to_unlogged is False
    assert TextCell.objects.get(id=cell.id).public is False
    assert TextCell.objects.get(id=cell.id).groups.count() == 1
    assert TextCell.objects.get(id=cell.id).groups.all()[0].name == 'A group'

    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/visibility' % cell.id)
    resp.form['cdata_textcell-%s-visibility' % cell.id] = 'groups-none'
    resp.form['cdata_textcell-%s-groups' % cell.id] = [group2.id]
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).restricted_to_unlogged is True
    assert TextCell.objects.get(id=cell.id).public is False
    assert TextCell.objects.get(id=cell.id).groups.count() == 1
    assert TextCell.objects.get(id=cell.id).groups.all()[0].name == 'Another group'


def test_edit_cell_options(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    cell = TextCell(page=page, placeholder='content', text='Foobar', order=0)
    cell.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/options' % cell.id)
    assert resp.form['cdata_textcell-%s-slug' % cell.id].value == ''
    assert resp.form['cdata_textcell-%s-extra_css_class' % cell.id].value == ''
    resp.form['cdata_textcell-%s-slug' % cell.id] = 'SLUG'
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).slug == 'SLUG'
    assert 'SLUG' in app.get('/manage/pages/%s/' % page.id)

    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/options' % cell.id)
    resp.form['cdata_textcell-%s-slug' % cell.id] = ''
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).slug == ''
    assert not 'SLUG' in app.get('/manage/pages/%s/' % page.id)

    resp = app.get('/manage/pages/%s/' % page.id)
    resp = resp.click(href='/data_textcell-%s/options' % cell.id)
    resp.form['cdata_textcell-%s-extra_css_class' % cell.id] = 'CSS'
    resp = resp.form.submit('submit')
    assert TextCell.objects.get(id=cell.id).extra_css_class == 'CSS'
    assert '[CSS]' in app.get('/manage/pages/%s/' % page.id)

def test_edit_cell_order(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    cells = []
    for i in range(5):
        cells.append(TextCell(page=page, placeholder='content', text='Foobar%s' % i, order=i))
        cells[-1].save()

    params = []
    new_order = [2, 3, 1, 4, 5]
    for i, (cell, new_pos) in enumerate(zip(cells, new_order)):
        params.append(('ph_data_textcell-%s' % cell.id, 'content')) # no placeholder change
        params.append(('pos_data_textcell-%s' % cell.id, str(new_order[i])))

    app = login(app)
    resp = app.get('/manage/pages/%s/order?%s' % (page.id, urlencode(params)))
    assert resp.status_code == 204
    for i, cell in enumerate(cells):
        assert TextCell.objects.get(id=cell.id).order == new_order[i]


def test_edit_text_cell(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    data_add_url = [x for x in resp.html.find_all('option') if x.text == 'Text'][0].get('data-add-url')
    resp = app.get(data_add_url)

    cells = CellBase.get_cells(page_id=page.id)
    assert len(cells) == 1
    assert isinstance(cells[0], TextCell)
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))

    resp = app.get('/manage/pages/%s/' % page.id)
    resp.form['cdata_textcell-%s-text' % cells[0].id].value = 'Hello : World'
    resp = resp.form.submit()
    assert TextCell.objects.get(id=cells[0].id).text == 'Hello : World'

    with override_settings(LANGUAGE_CODE='fr-fr'):
        resp = app.get('/manage/pages/%s/' % page.id)
        resp.form['cdata_textcell-%s-text' % cells[0].id].value = 'Hello : World'
        resp = resp.form.submit()
        assert TextCell.objects.get(id=cells[0].id).text == u'Hello\u00a0: World'


def test_edit_json_cell(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    data_add_url = [x for x in resp.html.find_all('option') if x.text == 'JSON Prototype'][0].get('data-add-url')
    resp = app.get(data_add_url)

    cells = CellBase.get_cells(page_id=page.id)
    assert len(cells) == 1
    assert isinstance(cells[0], JsonCell)
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))

    resp = app.get('/manage/pages/%s/' % page.id)
    resp.form['cdata_jsoncell-%s-url' % cells[0].id].value = 'xxx'
    resp = resp.form.submit()
    assert 'Enter a valid URL.' in resp.text
    assert JsonCell.objects.get(id=cells[0].id).url == ''

    resp = app.get('/manage/pages/%s/' % page.id)
    resp.form['cdata_jsoncell-%s-url' % cells[0].id].value = 'https://www.example.net/'
    resp = resp.form.submit()
    assert JsonCell.objects.get(id=cells[0].id).url == 'https://www.example.net/'

    resp = app.get('/manage/pages/%s/' % page.id)
    resp.form['cdata_jsoncell-%s-url' % cells[0].id].value = '{{url}}'
    resp = resp.form.submit()
    assert JsonCell.objects.get(id=cells[0].id).url == '{{url}}'


def test_edit_config_json_cell(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    options = [x.text for x in resp.html.find_all('option')]
    assert not 'Foobar' in options

    templates_settings = [settings.TEMPLATES[0].copy()]
    templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]
    with override_settings(JSON_CELL_TYPES={'test-config-json-cell': {'name': 'Foobar', 'url': 'http://test/'}},
            TEMPLATES=templates_settings):
        resp = app.get('/manage/pages/%s/' % page.id)
        options = [x.text for x in resp.html.find_all('option')]
        assert 'Foobar' in options
        data_add_url = [x for x in resp.html.find_all('option') if x.text == 'Foobar'][0].get('data-add-url')
        resp = app.get(data_add_url)

        cells = CellBase.get_cells(page_id=page.id)
        assert len(cells) == 1
        assert isinstance(cells[0], ConfigJsonCell)
        assert cells[0].key == 'test-config-json-cell'
        assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))

        resp = app.get('/manage/pages/%s/' % page.id)
        assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.text
        assert 'There are no options for this cell.' in resp.form.text

    # make it configurable
    with override_settings(JSON_CELL_TYPES=
            {'test-config-json-cell': {
                'name': 'Foobar',
                'url': 'http://test/',
                'form': [
                    {
                        'label': 'Test',
                        'type': 'string',
                        'varname': 'test',
                    },
                    {
                        'label': 'Test bool',
                        'type': 'bool',
                        'varname': 'test2',
                        'required': False,
                    },
                    {
                        'label': 'Test no type is string',
                        'varname': 'test3',
                    },
                    {
                        'label': 'Test text',
                        'type': 'text',
                        'varname': 'test4',
                        'required': False,
                    },
                ]}},
            TEMPLATES=templates_settings):
        resp = app.get('/manage/pages/%s/' % page.id)
        assert not 'There are no options for this cell.' in resp.form.text

        resp.form['c%s-test' % cells[0].get_reference()].value = 'Hello world'
        resp.form['c%s-test3' % cells[0].get_reference()].value = 'Hello again'
        resp = resp.form.submit()
        assert resp.status_int == 302
        assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))

        resp = app.get('/manage/pages/%s/' % page.id)
        assert resp.form['c%s-test' % cells[0].get_reference()].value == 'Hello world'
        assert resp.form['c%s-test2' % cells[0].get_reference()].checked is False
        assert resp.form['c%s-test3' % cells[0].get_reference()].value == 'Hello again'

        resp = app.get('/manage/pages/%s/' % page.id)
        assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.text
        resp.forms[0]['c%s-test' % cells[0].get_reference()].value = 'World Hello'
        resp.forms[0]['c%s-test2' % cells[0].get_reference()].checked = True
        assert resp.form['c%s-test4' % cells[0].get_reference()].tag == 'textarea'
        resp.forms[0]['c%s-test4' % cells[0].get_reference()].value = 'Text Area'
        resp = resp.form.submit(xhr=True)
        assert resp.status_int == 200
        assert resp.text.strip().startswith('<p><label')

        resp = app.get('/manage/pages/%s/' % page.id)
        assert resp.form['c%s-test' % cells[0].get_reference()].value == 'World Hello'
        assert resp.form['c%s-test2' % cells[0].get_reference()].checked is True
        assert resp.form['c%s-test3' % cells[0].get_reference()].value == 'Hello again'
        assert resp.form['c%s-test4' % cells[0].get_reference()].value == 'Text Area'


def test_logout(app, admin_user):
    app = login(app)
    app.get('/logout/')
    assert app.get('/manage/', status=302).location.endswith('/login/?next=/manage/')

def test_asset_management(app, admin_user):
    app = login(app)
    resp = app.get('/manage/assets/')
    assert 'have any asset yet.' in resp.text

    # test upload
    resp = app.get('/manage/assets/')
    resp = resp.click('Upload')
    resp.form['upload'] = Upload('test.png',
            base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='),
            'image/png')
    resp = resp.form.submit().follow()

    # check file is now there
    resp = app.get('/manage/assets/')
    assert 'have any asset yet.' not in resp.text

    # check there is also a thumbnail
    thumbnail_filename = re.findall('src="/media/(.*thumb.*)"', resp.text)[0]
    thumbnail_path = default_storage.path(thumbnail_filename)
    assert os.path.exists(thumbnail_path)
    thumbnail_contents = open(thumbnail_path, mode='rb').read()

    # check overwriting
    resp = resp.click('Overwrite')
    # test with the same extension but uppercased
    resp.form['upload'] = Upload(
        'test.PNG',
        base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
        'image/png')
    resp = resp.form.submit().follow()

    resp.click('test.png')
    assert re.findall('src="/media/(.*thumb.*)"', resp.text)[0] == thumbnail_filename
    assert os.path.exists(thumbnail_path)
    thumbnail_contents_new = open(thumbnail_path, mode='rb').read()
    assert thumbnail_contents_new != thumbnail_contents

    # try to overwrite with a different mimetype
    resp = resp.click('Overwrite')
    resp.form['upload'] = Upload(
        'test.pdf',
        base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
        'application/pdf')
    with mock.patch('combo.apps.assets.views.default_storage.delete') as mock_delete:
        resp = resp.form.submit().follow()
    # original file was not deleted
    assert mock_delete.call_args_list == []
    messages = resp.context['messages']
    assert len(messages._loaded_messages) == 1
    assert messages._loaded_messages[0].message == 'You have to upload a file with the same extension (.png).'

    # test deletion
    resp = resp.click('Delete')
    assert 'Are you sure you want to delete' in resp.text
    resp = resp.form.submit().follow()
    assert 'have any asset yet.' in resp.text

def test_asset_management_anchor(app, admin_user):
    app = login(app)
    resp = app.get('/manage/assets/')
    assert 'have any asset yet.' in resp.text

    for i in range(25):
        filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'cc.png')
        pix = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
        default_storage.save(filepath, BytesIO(base64.decodestring(pix)))

    # test upload
    resp = app.get('/manage/assets/')
    resp = resp.click('Upload')
    resp.form['upload'] = Upload('test.png',
            base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='),
            'image/png')
    resp = resp.form.submit()
    assert resp.location.endswith('?page=3')
    resp = resp.follow()

    # overwrite
    resp = resp.click(href=re.compile('overwrite.*test.png'))
    resp.form['upload'] = Upload('aa.png',
            base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
            'image/png')
    resp = resp.form.submit()
    assert resp.location.endswith('?page=3')
    resp = resp.follow()

    # delete
    resp = resp.click(href=re.compile('delete.*test.png'))
    resp = resp.form.submit()
    resp = resp.follow()
    assert 'test.png' not in resp.text

def test_asset_management_search(app, admin_user):
    app = login(app)

    filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'test.png')
    pix = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
    default_storage.save(filepath, BytesIO(base64.decodestring(pix)))
    assert os.path.exists(default_storage.path(filepath))

    filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'test.txt')
    default_storage.save(filepath, BytesIO(b'hello world'))

    filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'foo.txt')
    default_storage.save(filepath, BytesIO(b'foo bar baz'))

    resp = app.get('/manage/assets/')
    assert resp.text.count('<tr class="asset') == 3

    resp.form['q'] = 'test'
    resp = resp.form.submit()
    assert resp.text.count('<tr class="asset') == 2

def test_asset_slots_management(app, admin_user):
    app = login(app)
    assert Asset.objects.count() == 0

    with override_settings(COMBO_ASSET_SLOTS={'collectivity:banner': {'label': 'Banner'}}):
        resp = app.get('/manage/assets/')
        assert '>Banner<' in resp.text
        assert '>Delete<' not in resp.text

        resp = resp.click('Overwrite')
        resp.form['upload'] = Upload('test.png',
                base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
                'image/png')
        resp = resp.form.submit()
        assert resp.location.endswith('?page=1')
        resp = resp.follow()
        assert 'test.png' in resp.text
        assert '>Delete<' in resp.text
        assert Asset.objects.filter(key='collectivity:banner').count() == 1

        assert app.get('/assets/collectivity:banner', status=302)['location'].endswith('test.png')

        # upload a new version of image
        resp = resp.click('Overwrite')
        resp.form['upload'] = Upload('test2.png',
                base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='),
                'image/png')
        resp = resp.form.submit().follow()
        assert 'test2.png' in resp.text
        assert '<img data-href' in resp.text  # thumbnail
        assert '>Delete<' in resp.text
        assert Asset.objects.filter(key='collectivity:banner').count() == 1

        resp = resp.click('Delete')
        resp = resp.form.submit()
        assert resp.location.endswith('?page=1')
        resp = resp.follow()
        assert '>Banner<' in resp.text
        assert '>Delete<' not in resp.text
        assert Asset.objects.filter(key='collectivity:banner').count() == 0

        resp = app.get('/assets/collectivity:banner', status=404)

    with override_settings(COMBO_ASSET_SLOTS={'collectivity:cgu': {'label': 'CGU', 'asset-type': 'document'}}):
        resp = app.get('/manage/assets/')
        assert '>CGU<' in resp.text
        assert '>Delete<' not in resp.text

        resp = resp.click('Overwrite')
        resp.form['upload'] = Upload('test.pdf', b'PDF-1.4 ...', 'application.pdf')
        resp = resp.form.submit().follow()
        assert 'test.pdf' in resp.text
        assert '<img data-href' not in resp.text  # no thumbnail
        assert '>Delete<' in resp.text
        assert Asset.objects.filter(key='collectivity:cgu').count() == 1

        # badly remove the file, manager page is still visible
        os.unlink(Asset.objects.filter(key='collectivity:cgu')[0].asset.path)
        resp = app.get('/manage/assets/')
        assert '>CGU<' in resp.text


def test_asset_export_import(app, admin_user):
    for path in ('uploads', 'assets', 'cache'):
        if os.path.exists(default_storage.path(path)):
            shutil.rmtree(default_storage.path(path))

    app = login(app)

    # upload a file
    resp = app.get('/manage/assets/')
    resp = resp.click('Upload')
    resp.form['upload'] = Upload('test.png',
            base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='),
            'image/png')
    resp = resp.form.submit()

    resp = app.get('/manage/assets/')
    resp = resp.click('Export')
    assert resp.content_type == 'application/x-tar'
    content = resp.content

    for path in ('uploads', 'assets'):
        if os.path.exists(default_storage.path(path)):
            shutil.rmtree(default_storage.path(path))

    resp = app.get('/manage/assets/')
    assert 'have any asset yet.' in resp.text
    resp = resp.click('Import')
    resp.form['assets_file'] = Upload('test.tar', content)
    resp = resp.form.submit()
    assert sum([len(x[2]) for x in os.walk(default_storage.path(''))]) == 2
    resp = resp.follow()
    assert 'The assets file has been imported.' in resp.text

    # test no overwrite
    filename = re.findall('data-href="(.*?)"', resp.text)[0][7:]  # strip /media/
    with open(default_storage.path(filename), 'w') as fd:
        fd.write('test')  # 4 bytes
    assert os.stat(default_storage.path(filename)).st_size == 4

    resp = app.get('/manage/assets/')
    resp = resp.click('Import')
    resp.form['assets_file'] = Upload('test.tar', content)
    resp = resp.form.submit()
    resp = resp.follow()
    assert 'The assets file has been imported.' in resp.text

    assert os.stat(default_storage.path(filename)).st_size == 4

    # test overwrite
    resp = app.get('/manage/assets/')
    resp = resp.click('Import')
    resp.form['overwrite'] = True
    resp.form['assets_file'] = Upload('test.tar', content)
    resp = resp.form.submit()
    resp = resp.follow()
    assert 'The assets file has been imported.' in resp.text

    assert os.stat(default_storage.path(filename)).st_size == 67

    # test uploading garbage
    for path in ('uploads', 'assets'):
        if os.path.exists(default_storage.path(path)):
            shutil.rmtree(default_storage.path(path))

    resp = app.get('/manage/assets/')
    resp = resp.click('Import')
    resp.form['assets_file'] = Upload('test.tar', b'garbage')
    resp = resp.form.submit()
    assert sum([len(x[2]) for x in os.walk(default_storage.path(''))]) == 0
    resp = resp.follow()
    assert 'The assets file is not valid.' in resp.text


def test_menu_json(app, admin_user):
    app.get('/manage/menu.json', status=302)

    app = login(app)
    resp = app.get('/manage/menu.json')
    assert resp.headers['content-type'] == 'application/json'
    assert resp.json[0]['label'] == 'Content Management'

    resp = app.get('/manage/menu.json?callback=fooBar')
    assert resp.headers['content-type'] == 'application/javascript'
    assert resp.text.startswith('fooBar([{"')

def test_page_multiple_link_cells(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    for i in range(10):
        Page(title='page %d' % i, slug='page%d' % i, template_name='standard').save()
        cell = LinkCell(page=page, placeholder='content', order=0)
        cell.save()

    patched_orig = Page.get_as_reordered_flat_hierarchy
    app = login(app)
    with mock.patch('combo.data.models.Page.get_as_reordered_flat_hierarchy') as func:
        func.return_value = []
        resp = app.get('/manage/pages/%s/' % page.id)
        assert func.call_count == 1

def test_page_cell_placeholder(app, admin_user):
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    cell = JsonCell(page=page, placeholder='content', order=0, url='http://example.com')
    cell.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    assert re.findall('data-placeholder-key="(.*)">', resp.text) == ['content', 'footer']

    # check a placeholder within a cell is included
    cell.template_string = '{% load combo %}{% placeholder "foobar" name="Foobar" %}'
    cell.save()
    resp = app.get('/manage/pages/%s/' % page.id)
    assert re.findall('data-placeholder-key="(.*)">', resp.text) == ['content', 'foobar', 'footer']
    assert re.findall('<h2>(.*)</h2>', resp.text) == [
            'Page - One', 'Content', 'JSON Prototype / Foobar', 'Footer']

def test_page_familycell_placeholder(app, admin_user):
    page = Page(title='My family', slug='my-family', template_name='standard')
    page.save()
    cell = FamilyInfosCell(page=page, placeholder='content', order=0)
    cell.save()

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id)
    assert re.findall('data-placeholder-key="(.*)">', resp.text) == ['content', "family_unlinked_user", "footer"]
    assert re.findall('<h2>(.*)</h2>', resp.text) == [
        'Page - My family', 'Content',
        'Family Information Cell / Top content for unlinked users', 'Footer']

def test_page_discover_placeholder_with_error_cells(app, admin_user):
    page = Page(title='One', slug='one', template_name='standard')
    page.save()
    cell = JsonCell(page=page, placeholder='content', order=0, url='xx')
    cell.template_string = '{% xxx %}'
    cell.save()

    app = login(app)

    with mock.patch('combo.utils.requests.get') as requests_get:
        data = {'data': [{'url': 'xxx', 'text': 'xxx'}]}
        with pytest.raises(TemplateSyntaxError):
            cell.render({})

    resp = app.get('/manage/pages/%s/' % page.id)
    assert re.findall('data-placeholder-key="(.*)">', resp.text) == ['content', 'footer']

def test_page_versionning(app, admin_user):
    Page.objects.all().delete()
    PageSnapshot.objects.all()

    page = Page(title='One', slug='one')
    page.save()

    cell1 = TextCell(page=page, placeholder='content', text='Foobar1', order=0)
    cell1.save()
    cell2 = TextCell(page=page, placeholder='content', text='Foobar2', order=1)
    cell2.save()
    cell3 = TextCell(page=page, placeholder='content', text='Foobar3', order=1)
    cell3.save()

    anonymous_app = app

    app = login(app)
    resp = app.get('/manage/pages/%s/' % page.id, status=200)

    # update title
    resp = resp.click(href='.*/title')
    resp.form['title'].value = 'One Two'
    resp = resp.form.submit()
    resp = resp.follow()
    assert Page.objects.all()[0].title == 'One Two'
    assert PageSnapshot.objects.all().count() == 1

    # change cell text
    resp.forms[0]['c%s-text' % cell1.get_reference()].value = 'Hello world'
    resp = resp.forms[0].submit().follow()
    assert PageSnapshot.objects.all().count() == 2

    # reorder cells
    params = []
    for i, cell in enumerate([cell3, cell1, cell2]):
        params.append(('ph_data_textcell-%s' % cell.id, 'content')) # no placeholder change
        params.append(('pos_data_textcell-%s' % cell.id, str(i)))

    app.get('/manage/pages/%s/order?%s' % (page.id, urlencode(params)))
    assert PageSnapshot.objects.all().count() == 3

    resp = resp.click('history')
    assert resp.text.index('reordered cells') < resp.text.index('changed cell') < resp.text.index('changed title')

    resp2 = resp.click('view', index=1)
    assert resp2.text.index('Hello world') < resp2.text.index('Foobar3')

    resp2 = resp.click('view', index=0)
    assert resp2.text.index('Hello world') > resp2.text.index('Foobar3')

    resp2 = resp.click('view', index=2)
    assert 'Foobar1' in resp2.text
    assert not 'Hello world' in resp2.text

    assert Page.objects.all().count() == 1
    assert PageSnapshot.objects.filter(page=page)[0].user.username == 'admin'

    # check with asynchronous cells
    resp = app.get('/manage/pages/%s/add-cell-to-content/data_jsoncell/default/' % page.id)
    resp = resp.follow()
    resp.forms[3]['cdata_jsoncell-1-template_string'].value = 'A{{json.data.0.text}}B'
    resp.forms[3]['cdata_jsoncell-1-url'].value = 'http://example.com'
    resp = resp.forms[3].submit().follow()
    assert PageSnapshot.objects.all().count() == 5  # add + change

    resp.forms[3]['cdata_jsoncell-1-template_string'].value = 'C{{json.data.0.text}}D'
    resp = resp.forms[3].submit().follow()
    assert PageSnapshot.objects.all().count() == 6

    resp.forms[1]['c%s-text' % cell1.get_reference()].value = 'Foo back to 1'
    resp = resp.forms[0].submit().follow()

    resp = resp.click('history')
    assert 'added cell' in resp.text

    resp2 = resp.click('view', index=1)
    json_cell_url = re.findall(r'/ajax/cell/.*/data_jsoncell-.*/', resp2.text)[0]

    with mock.patch('combo.utils.requests.get') as requests_get:
        data = {'data': [{'url': 'xxx', 'text': 'xxx'}]}
        requests_get.return_value = mock.Mock(json=lambda: data, status_code=200)
        resp3 = app.get(json_cell_url)
        assert resp3.text.strip() == 'CxxxD'

    # previous version should return AxxxB
    resp2 = resp.click('view', index=2)
    json_cell_url = re.findall(r'/ajax/cell/.*/data_jsoncell-.*/', resp2.text)[0]

    with mock.patch('combo.utils.requests.get') as requests_get:
        data = {'data': [{'url': 'xxx', 'text': 'xxx'}]}
        requests_get.return_value = mock.Mock(json=lambda: data, status_code=200)
        resp3 = app.get(json_cell_url)
        assert resp3.text.strip() == 'AxxxB'

        # check anonymous users can't get to cells from snapshots
        app.get('/logout/')
        resp3 = app.get(json_cell_url, status=403)

    # restore a snapshot
    app = login(app)
    resp = resp.click('restore', index=6)
    resp = resp.form.submit()
    resp = resp.follow()

    resp2 = resp.click('see online')
    assert resp2.text.index('Foobar1') < resp2.text.index('Foobar2') < resp2.text.index('Foobar3')

    # clean it up
    Page.snapshots.all().delete()
    assert Page.objects.count() == 1
    assert TextCell.objects.count() == 3
    assert JsonCell.objects.count() == 0

def test_django_admin(app, admin_user):
    resp = app.get('/admin/')
    resp = resp.follow()  # -> /admin/login/
    resp = resp.follow()  # -> /login/
    resp.form['username'] = 'admin'
    resp.form['password'] = 'admin'
    resp = resp.form.submit()
    resp = resp.follow()  # -> /admin/
    assert '/admin/logout/' in resp.text
    resp = resp.click(href='/admin/logout/')
    resp = resp.follow()  # -> /logout/
    assert urlparse.urlparse(resp.location).path == '/'

def test_json_cell_syntax_validation(app, admin_user):
    Page.objects.all().delete()
    page = Page(title='One', slug='one')
    page.save()
    app = login(app)
    # syntax error
    resp = app.get('/manage/pages/%s/add-cell-to-content/data_jsoncell/default/' % page.id)
    resp = resp.follow()
    resp.forms[0]['cdata_jsoncell-1-template_string'].value = '{% syntax|error %}'
    resp.forms[0]['cdata_jsoncell-1-url'].value = 'http://example.com'
    resp = resp.forms[0].submit()
    assert 'syntax error: Invalid block tag' in resp.text
    assert JsonCell.objects.count() == 1
    assert JsonCell.objects.first().template_string is None
    # valid syntax
    resp = app.get('/manage/pages/%s/' % page.id)
    resp.forms[0]['cdata_jsoncell-1-template_string'].value = '{{ ok }}'
    resp.forms[0]['cdata_jsoncell-1-url'].value = 'http://example.com'
    resp = resp.forms[0].submit().follow()
    assert 'syntax error' not in resp.text
    assert JsonCell.objects.count() == 1
    assert JsonCell.objects.first().template_string == '{{ ok }}'