This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
/
test_templates.py
1091 lines (947 loc) · 44.6 KB
/
test_templates.py
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
# -*- coding: utf-8 -*-
import time
import urllib
from datetime import datetime
import mock
import pytest
from BeautifulSoup import BeautifulSoup
from constance import config
from django.conf import settings
from django.contrib.sites.models import Site
from django.core import mail
from django.test.utils import override_settings
from django.utils.http import urlquote
from pyquery import PyQuery as pq
from kuma.core.tests import eq_, ok_
from kuma.core.urlresolvers import reverse
from kuma.core.utils import urlparams
from kuma.users.tests import UserTestCase
from . import (WikiTestCase, create_topical_parents_docs, document,
new_document_data, revision)
from ..constants import REDIRECT_CONTENT, TEMPLATE_TITLE_PREFIX
from ..events import EditDocumentEvent
from ..models import Document, DocumentTag, Revision
DOCUMENT_EDITED_EMAIL_CONTENT = """
admin created a new revision to the document
%s.
To view this document's history, click the following
link, or paste it into your browser's location bar:
https://testserver/en-US/docs/%s$history
"""
class DocumentTests(UserTestCase, WikiTestCase):
"""Tests for the Document template"""
def test_document_view(self):
"""Load the document view page and verify the title and content."""
r = revision(save=True, content='Some text.', is_approved=True)
response = self.client.get(r.document.get_absolute_url())
eq_(200, response.status_code)
doc = pq(response.content)
eq_(r.document.title, doc('main#content div.document-head h1').text())
eq_(r.document.html, doc('article#wikiArticle').text())
@pytest.mark.breadcrumbs
def test_document_breadcrumbs(self):
"""Create docs with topical parent/child rel, verify breadcrumbs."""
d1, d2 = create_topical_parents_docs()
response = self.client.get(d1.get_absolute_url())
eq_(200, response.status_code)
doc = pq(response.content)
eq_(d1.title, doc('main#content div.document-head h1').text())
eq_(d1.title, doc('nav.crumbs li:last-child').text())
response = self.client.get(d2.get_absolute_url())
eq_(200, response.status_code)
doc = pq(response.content)
eq_(d2.title, doc('main#content div.document-head h1').text())
crumbs = "MDN %s %s" % (d1.title, d2.title)
eq_(crumbs, doc('nav.crumbs').text())
def test_english_document_no_approved_content(self):
"""Load an English document with no approved content."""
r = revision(save=True, content='Some text.', is_approved=False)
response = self.client.get(r.document.get_absolute_url())
eq_(200, response.status_code)
doc = pq(response.content)
eq_(r.document.title, doc('main#content div.document-head h1').text())
eq_("This article doesn't have approved content yet.",
doc('article#wikiArticle').text())
def test_translation_document_no_approved_content(self):
"""Load a non-English document with no approved content, with a parent
with no approved content either."""
r = revision(save=True, content='Some text.', is_approved=False)
d2 = document(parent=r.document, locale='fr', slug='french', save=True)
revision(document=d2, save=True, content='Moartext', is_approved=False)
response = self.client.get(d2.get_absolute_url())
eq_(200, response.status_code)
doc = pq(response.content)
eq_(d2.title, doc('main#content div.document-head h1').text())
# HACK: fr doc has different message if locale/ is updated
ok_(
("This article doesn't have approved content yet." in
doc('article#wikiArticle').text()) or
("Cet article n'a pas encore de contenu" in
doc('article#wikiArticle').text())
)
def test_document_fallback_with_translation(self):
"""The document template falls back to English if translation exists
but it has no approved revisions."""
r = revision(save=True, content='Test', is_approved=True)
d2 = document(parent=r.document, locale='fr', slug='french', save=True)
revision(document=d2, is_approved=False, save=True)
url = reverse('wiki.document', args=[d2.slug], locale='fr')
response = self.client.get(url)
doc = pq(response.content)
eq_(d2.title, doc('main#content div.document-head h1').text())
# Fallback message is shown.
eq_(1, len(doc('#doc-pending-fallback')))
ok_('$translate' in doc('#edit-button').attr('href'))
# Removing this as it shows up in text(), and we don't want to depend
# on its localization.
doc('#doc-pending-fallback').remove()
# Included content is English.
eq_(pq(r.document.html).text(), doc('article#wikiArticle').text())
def test_document_fallback_no_translation(self):
"""The document template falls back to English if no translation
exists."""
r = revision(save=True, content='Some text.', is_approved=True)
url = reverse('wiki.document', args=[r.document.slug], locale='fr')
response = self.client.get(url)
doc = pq(response.content)
eq_(r.document.title, doc('main#content div.document-head h1').text())
# Fallback message is shown.
eq_(1, len(doc('#doc-pending-fallback')))
ok_('$translate' in doc('#edit-button').attr('href'))
# Removing this as it shows up in text(), and we don't want to depend
# on its localization.
doc('#doc-pending-fallback').remove()
# Included content is English.
eq_(pq(r.document.html).text(), doc('article#wikiArticle').text())
def test_redirect(self):
"""Make sure documents with REDIRECT directives redirect properly.
Also check the backlink to the redirect page.
"""
target = document(save=True)
target_url = target.get_absolute_url()
# Ordinarily, a document with no approved revisions cannot have HTML,
# but we shove it in manually here as a shortcut:
redirect_html = REDIRECT_CONTENT % dict(title='Boo', href=target_url)
redirect = document(html=redirect_html)
redirect.save()
redirect_url = redirect.get_absolute_url()
self.client.login(username='admin', password='testpass')
response = self.client.get(redirect_url, follow=True)
self.assertRedirects(response, urlparams(target_url), status_code=301)
self.assertContains(response, redirect_url)
def test_redirect_from_nonexistent(self):
"""The template shouldn't crash or print a backlink if the "from" page
doesn't exist."""
d = document(save=True)
response = self.client.get(urlparams(d.get_absolute_url()))
self.assertNotContains(response, 'Redirected from ')
def test_watch_includes_csrf(self):
"""The watch/unwatch forms should include the csrf tag."""
self.client.login(username='testuser', password='testpass')
d = document(save=True)
resp = self.client.get(d.get_absolute_url())
doc = pq(resp.content)
assert doc('.page-watch input[type=hidden]')
def test_non_localizable_translate_disabled(self):
"""Non localizable document doesn't show tab for 'Localize'."""
self.client.login(username='testuser', password='testpass')
d = document(is_localizable=True, save=True)
resp = self.client.get(d.get_absolute_url())
doc = pq(resp.content)
assert 'Add a translation' in doc('.page-buttons #translations li').text()
# Make it non-localizable
d.is_localizable = False
d.save()
resp = self.client.get(d.get_absolute_url())
doc = pq(resp.content)
assert 'Add a translation' not in doc('.page-buttons #translations li').text()
@pytest.mark.toc
def test_toc_depth(self):
"""Toggling show_toc on/off through the toc_depth field should
cause table of contents to appear/disappear."""
doc_content = """
<h2>This is a section</h2>
<p>This is section content.</p>
<h2>This is another section</h2>
<p>This is more section content.</p>
"""
r = revision(save=True, content=doc_content, is_approved=True)
response = self.client.get(r.document.get_absolute_url())
eq_(200, response.status_code)
ok_('<div id="toc"' in response.content)
new_r = revision(document=r.document, content=r.content,
toc_depth=0, is_approved=True)
new_r.save()
response = self.client.get(r.document.get_absolute_url())
eq_(200, response.status_code)
ok_('<div class="page-toc">' not in response.content)
@pytest.mark.toc
def test_show_toc_hidden_input_for_templates(self):
"""Toggling show_toc on/off through the toc_depth field should
cause table of contents to appear/disappear."""
doc_content = """w00t"""
doc = document(slug="Template:w00t", save=True)
r = revision(document=doc, save=True, content=doc_content,
is_approved=True)
response = self.client.get(r.document.get_absolute_url())
eq_(200, response.status_code)
soup = BeautifulSoup(response.content)
hidden_inputs = soup.findAll("input", type="hidden")
for input in hidden_inputs:
if input['name'] == 'toc_depth':
eq_(0, input['value'])
class RevisionTests(UserTestCase, WikiTestCase):
"""Tests for the Revision template"""
localizing_client = True
def test_revision_view(self):
"""Load the revision view page and verify the title and content."""
d = _create_document()
r = d.current_revision
r.created = datetime(2011, 1, 1)
r.save()
url = reverse('wiki.revision', args=[d.slug, r.id])
response = self.client.get(url)
eq_(200, response.status_code)
doc = pq(response.content)
eq_('Revision id: %s' % r.id,
doc('div.revision-info li.revision-id').text())
eq_('Revision %s of %s' % (r.id, d.title), doc('h1').text())
eq_(r.content,
doc('#doc-source pre').text())
eq_('Created: Jan 1, 2011, 12:00:00 AM',
doc('div.revision-info li.revision-created').text().strip())
class NewDocumentTests(UserTestCase, WikiTestCase):
"""Tests for the New Document template"""
localizing_client = True
def test_new_document_GET_with_perm(self):
"""HTTP GET to new document URL renders the form."""
self.client.login(username='admin', password='testpass')
response = self.client.get(reverse('wiki.create'))
eq_(200, response.status_code)
doc = pq(response.content)
eq_(1, len(doc('form#wiki-page-edit input[name="title"]')))
def test_new_document_includes_review_block(self):
"""
New document page includes 'Review Needed?' section.
https://bugzil.la/1052047
"""
self.client.login(username='admin', password='testpass')
response = self.client.get(reverse('wiki.create'))
test_strings = ['Review needed?', 'Technical', 'Editorial']
eq_(200, response.status_code)
# TODO: push test_strings functionality up into a test helper
for test_string in test_strings:
ok_(test_string in response.content)
def test_new_document_preview_button(self):
"""HTTP GET to new document URL shows preview button for basic doc
and not for template doc"""
self.client.login(username='admin', password='testpass')
response = self.client.get(reverse('wiki.create'))
eq_(200, response.status_code)
doc = pq(response.content)
ok_(len(doc('.btn-preview')) > 0)
response = self.client.get(reverse('wiki.create') +
'?slug=' + TEMPLATE_TITLE_PREFIX)
doc = pq(response.content)
eq_(0, len(doc('.btn-preview')))
def test_new_document_form_defaults(self):
"""The new document form should have all all 'Relevant to' options
checked by default."""
self.client.login(username='admin', password='testpass')
response = self.client.get(reverse('wiki.create'))
doc = pq(response.content)
eq_("Name Your Article", doc('input#id_title').attr('placeholder'))
@mock.patch.object(Site.objects, 'get_current')
def test_new_document_POST(self, get_current):
"""HTTP POST to new document URL creates the document."""
get_current.return_value.domain = 'testserver'
self.client.login(username='admin', password='testpass')
tags = ['tag1', 'tag2']
data = new_document_data(tags)
response = self.client.post(reverse('wiki.create'), data,
follow=True)
d = Document.objects.get(title=data['title'])
eq_([('http://testserver/en-US/docs/%s' % d.slug, 302)],
response.redirect_chain)
eq_(settings.WIKI_DEFAULT_LANGUAGE, d.locale)
eq_(tags, sorted(t.name for t in d.tags.all()))
r = d.revisions.all()[0]
eq_(data['keywords'], r.keywords)
eq_(data['summary'], r.summary)
eq_(data['content'], r.content)
@mock.patch.object(Site.objects, 'get_current')
def test_new_document_other_locale(self, get_current):
"""Make sure we can create a document in a non-default locale."""
# You shouldn't be able to make a new doc in a non-default locale
# without marking it as non-localizable. Unskip this when the non-
# localizable bool is implemented.
get_current.return_value.domain = 'testserver'
self.client.login(username='admin', password='testpass')
data = new_document_data(['tag1', 'tag2'])
locale = 'es'
self.client.post(reverse('wiki.create', locale=locale),
data, follow=True)
d = Document.objects.get(title=data['title'])
eq_(locale, d.locale)
def test_new_document_POST_empty_title(self):
"""Trigger required field validation for title."""
self.client.login(username='admin', password='testpass')
data = new_document_data(['tag1', 'tag2'])
data['title'] = ''
response = self.client.post(reverse('wiki.create'), data,
follow=True)
doc = pq(response.content)
ul = doc('article ul.errorlist')
ok_(len(ul) > 0)
ok_('Please provide a title.' in ul('li').text())
def test_new_document_POST_empty_content(self):
"""Trigger required field validation for content."""
self.client.login(username='admin', password='testpass')
data = new_document_data(['tag1', 'tag2'])
data['content'] = ''
response = self.client.post(reverse('wiki.create'), data,
follow=True)
doc = pq(response.content)
ul = doc('article ul.errorlist')
eq_(1, len(ul))
eq_('Please provide content.', ul('li').text())
def test_slug_collision_validation(self):
"""Trying to create document with existing locale/slug should
show validation error."""
d = _create_document()
self.client.login(username='admin', password='testpass')
data = new_document_data()
data['slug'] = d.slug
response = self.client.post(reverse('wiki.create'), data)
eq_(200, response.status_code)
doc = pq(response.content)
ul = doc('article ul.errorlist')
eq_(1, len(ul))
eq_('Document with this Slug and Locale already exists.',
ul('li').text())
def test_title_no_collision(self):
"""Only slugs and not titles are required to be unique per
locale now, so test that we actually allow that."""
d = _create_document()
self.client.login(username='admin', password='testpass')
data = new_document_data()
data['slug'] = '%s-once-more-with-feeling' % d.slug
response = self.client.post(reverse('wiki.create'), data)
eq_(302, response.status_code)
def test_slug_3_chars(self):
"""Make sure we can create a slug with only 3 characters."""
self.client.login(username='admin', password='testpass')
data = new_document_data()
data['slug'] = 'ask'
response = self.client.post(reverse('wiki.create'), data)
eq_(302, response.status_code)
eq_('ask', Document.objects.all()[0].slug)
class NewRevisionTests(UserTestCase, WikiTestCase):
"""Tests for the New Revision template"""
localizing_client = True
def setUp(self):
super(NewRevisionTests, self).setUp()
self.d = _create_document()
self.username = 'admin'
self.client.login(username=self.username, password='testpass')
def test_new_revision_GET_logged_out(self):
"""Creating a revision without being logged in redirects to login page.
"""
self.client.logout()
response = self.client.get(reverse('wiki.edit',
args=[self.d.slug]))
eq_(302, response.status_code)
def test_new_revision_GET_with_perm(self):
"""HTTP GET to new revision URL renders the form."""
response = self.client.get(reverse('wiki.edit',
args=[self.d.slug]))
eq_(200, response.status_code)
doc = pq(response.content)
eq_(1, len(doc('article#edit-document '
'form#wiki-page-edit textarea[name="content"]')))
def test_new_revision_GET_based_on(self):
"""HTTP GET to new revision URL based on another revision.
This case should render the form with the fields pre-populated
with the based-on revision info.
"""
r = Revision(document=self.d, keywords='ky1, kw2',
summary='the summary',
content='<div>The content here</div>', creator_id=7)
r.save()
response = self.client.get(reverse('wiki.new_revision_based_on',
args=[self.d.slug, r.id]))
eq_(200, response.status_code)
doc = pq(response.content)
self.assertHTMLEqual(doc('#id_content')[0].value.strip(),
r.content.strip())
@override_settings(TIDINGS_CONFIRM_ANONYMOUS_WATCHES=False)
@mock.patch.object(Site.objects, 'get_current')
def test_new_revision_POST_document_with_current(self, get_current):
"""HTTP POST to new revision URL creates the revision on a document.
The document in this case already has a current_revision, therefore
the document document fields are not editable.
Also assert that the edited and reviewable notifications go out.
"""
get_current.return_value.domain = 'testserver'
# Sign up for notifications:
EditDocumentEvent.notify('sam@example.com', self.d).activate().save()
# Edit a document
data = {
'summary': 'A brief summary',
'content': 'The article content',
'keywords': 'keyword1 keyword2',
'slug': self.d.slug,
'toc_depth': 1,
'based_on': self.d.current_revision.id,
'form-type': 'rev',
}
edit_url = reverse('wiki.edit', args=[self.d.slug])
response = self.client.post(edit_url, data)
ok_(response.status_code in (200, 302))
eq_(2, self.d.revisions.count())
new_rev = self.d.revisions.order_by('-id')[0]
eq_(self.d.current_revision, new_rev.based_on)
# Assert notifications fired and have the expected content:
# 1 email for the first time edit notification
# 1 email for the EditDocumentEvent to sam@example.com
# Regression check:
# messing with context processors can
# cause notification emails to error
# and stop being sent.
time.sleep(1)
eq_(2, len(mail.outbox))
first_edit_email = mail.outbox[0]
expected_to = [config.EMAIL_LIST_SPAM_WATCH]
expected_subject = u'[MDN] %(username)s made their first edit, to: %(title)s' % ({'username': new_rev.creator.username, 'title': self.d.title})
eq_(expected_subject, first_edit_email.subject)
eq_(expected_to, first_edit_email.to)
edited_email = mail.outbox[1]
expected_to = [u'sam@example.com']
expected_subject = u'[MDN] Page "%s" changed by %s' % (self.d.title,
new_rev.creator)
eq_(expected_subject, edited_email.subject)
eq_(expected_to, edited_email.to)
ok_('%s changed %s.' % (unicode(self.username), unicode(self.d.title))
in edited_email.body)
ok_(u'https://testserver/en-US/docs/%s$history?utm_campaign=' %
self.d.slug
in edited_email.body)
@mock.patch.object(EditDocumentEvent, 'fire')
@mock.patch.object(Site.objects, 'get_current')
def test_new_revision_POST_document_without_current(
self, get_current, edited_fire):
"""HTTP POST to new revision URL creates the revision on a document.
The document in this case doesn't have a current_revision, therefore
the document fields are open for editing.
"""
get_current.return_value.domain = 'testserver'
self.d.current_revision = None
self.d.save()
tags = ['tag1', 'tag2', 'tag3']
data = new_document_data(tags)
data['form-type'] = 'rev'
response = self.client.post(reverse('wiki.edit',
args=[self.d.slug]), data)
eq_(302, response.status_code)
eq_(2, self.d.revisions.count())
new_rev = self.d.revisions.order_by('-id')[0]
# There are no approved revisions, so it's based_on nothing:
eq_(None, new_rev.based_on)
ok_(edited_fire.called)
def test_new_revision_POST_removes_old_tags(self):
"""Changing the tags on a document removes the old tags from
that document."""
self.d.current_revision = None
self.d.save()
tags = [u'tag1', u'tag2', u'tag3']
self.d.tags.add(*tags)
result_tags = list(self.d.tags.names())
result_tags.sort()
eq_(tags, result_tags)
tags = [u'tag1', u'tag4']
data = new_document_data(tags)
data['form-type'] = 'rev'
self.client.post(reverse('wiki.edit',
args=[self.d.slug]),
data)
result_tags = list(self.d.tags.names())
result_tags.sort()
eq_(tags, result_tags)
def test_new_form_maintains_based_on_rev(self):
"""Revision.based_on should be the rev that was current when the Edit
button was clicked, even if other revisions happen while the user is
editing."""
_test_form_maintains_based_on_rev(
self.client, self.d, 'wiki.edit',
{'summary': 'Windy', 'content': 'gerbils', 'form-type': 'rev',
'slug': self.d.slug, 'toc_depth': 1},
locale='en-US')
class DocumentEditTests(UserTestCase, WikiTestCase):
"""Test the editing of document level fields."""
localizing_client = True
def setUp(self):
super(DocumentEditTests, self).setUp()
self.d = _create_document()
self.client.login(username='admin', password='testpass')
def test_can_save_document_with_translations(self):
"""Make sure we can save a document with translations."""
# Create a translation
_create_document(title='Document Prueba', parent=self.d,
locale='es')
# Make sure is_localizable hidden field is rendered
response = self.client.get(reverse('wiki.edit', args=[self.d.slug]),
follow=True)
eq_(200, response.status_code)
doc = pq(response.content)
data = new_document_data()
new_title = 'A brand new title'
data.update(title=new_title)
data['form-type'] = 'doc'
data.update(is_localizable='True')
response = self.client.post(reverse('wiki.edit', args=[self.d.slug]),
data, follow=True)
eq_(200, response.status_code)
doc = Document.objects.get(pk=self.d.pk)
eq_(new_title, doc.title)
def test_change_slug_case(self):
"""Changing the case of some letters in the slug should work."""
data = new_document_data()
new_slug = 'Test-Document'
data.update(slug=new_slug)
data['form-type'] = 'doc'
response = self.client.post(reverse('wiki.edit', args=[self.d.slug]),
data, follow=True)
eq_(200, response.status_code)
doc = Document.objects.get(pk=self.d.pk)
eq_(new_slug, doc.slug)
def test_change_title_case(self):
"""Changing the case of some letters in the title should work."""
data = new_document_data()
new_title = 'TeST DoCuMent'
data.update(title=new_title)
data['form-type'] = 'doc'
response = self.client.post(reverse('wiki.edit', args=[self.d.slug]),
data, follow=True)
eq_(200, response.status_code)
doc = Document.objects.get(pk=self.d.pk)
eq_(new_title, doc.title)
class DocumentListTests(UserTestCase, WikiTestCase):
localizing_client = True
def setUp(self):
super(DocumentListTests, self).setUp()
self.locale = settings.WIKI_DEFAULT_LANGUAGE
self.doc = _create_document(locale=self.locale)
_create_document(locale=self.locale, title='Another one')
# Create a document in different locale to make sure it doesn't show
_create_document(parent=self.doc, locale='es')
def test_all_list(self):
"""Verify the all documents list view."""
response = self.client.get(reverse('wiki.all_documents'))
doc = pq(response.content)
eq_(Document.objects.filter(locale=self.locale).count(),
len(doc('#document-list ul.document-list li')))
@pytest.mark.tags
def test_tag_list(self):
"""Verify the tagged documents list view."""
tag = DocumentTag(name='Test Tag', slug='test-tag')
tag.save()
self.doc.tags.add(tag)
response = self.client.get(reverse('wiki.tag',
args=[tag.name]))
eq_(200, response.status_code)
doc = pq(response.content)
eq_(1, len(doc('#document-list ul.document-list li')))
@pytest.mark.tags
def test_tag_list_duplicates(self):
"""
Verify the tagged documents list view, even for duplicate tags
http://bugzil.la/871638
"""
en_tag = DocumentTag(name='CSS Reference', slug='css-reference')
en_tag.save()
fr_tag = DocumentTag(name=u'CSS Référence', slug='css-reference_1')
fr_tag.save()
self.doc.tags.add(en_tag)
self.doc.tags.add(fr_tag)
response = self.client.get(reverse('wiki.tag',
args=[en_tag.name]))
eq_(200, response.status_code)
doc = pq(response.content)
eq_(1, len(doc('#document-list ul.document-list li')))
class CompareRevisionTests(UserTestCase, WikiTestCase):
"""Tests for Review Revisions"""
localizing_client = True
def setUp(self):
super(CompareRevisionTests, self).setUp()
self.document = _create_document()
self.revision1 = self.document.current_revision
user = self.user_model.objects.get(username='testuser')
self.revision2 = Revision(summary="lipsum",
content='<div>Lorem Ipsum Dolor</div>',
keywords='kw1 kw2',
document=self.document, creator=user)
self.revision2.save()
self.client.login(username='admin', password='testpass')
def test_bad_parameters(self):
"""Ensure badly-formed revision parameters do not cause errors"""
url = reverse('wiki.compare_revisions', args=[self.document.slug])
query = {'from': '1e309', 'to': u'1e309'}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(404, response.status_code)
def test_no_tidied_content(self):
"""
Verify revisions without tidied content show appropriate message.
"""
# update() to skip the tidy_revision_content post_save signal handler
Revision.objects.filter(
id__in=[self.revision1.id, self.revision2.id]
).update(
tidied_content=''
)
url = reverse('wiki.compare_revisions', args=[self.document.slug])
query = {'from': self.revision1.id, 'to': self.revision2.id}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(200, response.status_code)
ok_('Please refresh this page in a few minutes.' in response.content)
url = url + '&raw=1'
response = self.client.get(url)
eq_(200, response.status_code)
ok_('Please refresh this page in a few minutes.' in response.content)
def test_compare_revisions(self):
"""Compare two revisions"""
url = reverse('wiki.compare_revisions', args=[self.document.slug])
query = {'from': self.revision1.id, 'to': self.revision2.id}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(200, response.status_code)
doc = pq(response.content)
eq_('Dolor', doc('span.diff_add').text())
def test_compare_revisions_invalid_to_int(self):
"""Provide invalid 'to' int for revision ids."""
url = reverse('wiki.compare_revisions', args=[self.document.slug])
query = {'from': '', 'to': 'invalid'}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(404, response.status_code)
def test_compare_revisions_invalid_from_int(self):
"""Provide invalid 'from' int for revision ids."""
url = reverse('wiki.compare_revisions', args=[self.document.slug])
query = {'from': 'invalid', 'to': ''}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(404, response.status_code)
def test_compare_revisions_missing_query_param(self):
"""Try to compare two revisions, with a missing query string param."""
url = reverse('wiki.compare_revisions', args=[self.document.slug])
query = {'from': self.revision1.id}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(404, response.status_code)
url = reverse('wiki.compare_revisions', args=[self.document.slug])
query = {'to': self.revision1.id}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(404, response.status_code)
def test_compare_unmatched_document_url(self):
"""Comparing two revisions of unlinked document should cause error."""
unmatched_document = _create_document(title='Invalid document')
url = reverse('wiki.compare_revisions', args=[unmatched_document.slug])
query = {'from': self.revision1.id, 'to': self.revision2.id}
url = urlparams(url, **query)
response = self.client.get(url)
eq_(404, response.status_code)
class TranslateTests(UserTestCase, WikiTestCase):
"""Tests for the Translate page"""
def setUp(self):
super(TranslateTests, self).setUp()
self.d = _create_document()
self.client.login(username='admin', password='testpass')
def _translate_uri(self):
translate_uri = reverse('wiki.translate',
locale='en-US',
args=[self.d.slug])
return '%s?tolocale=%s' % (translate_uri, 'es')
def test_translate_GET_logged_out(self):
"""Try to create a translation while logged out."""
self.client.logout()
translate_uri = self._translate_uri()
response = self.client.get(translate_uri)
eq_(302, response.status_code)
expected_url = '%s?next=%s' % (reverse('account_login', locale='en-US'),
urlquote(translate_uri))
ok_(expected_url in response['Location'])
def test_translate_GET_with_perm(self):
"""HTTP GET to translate URL renders the form."""
response = self.client.get(self._translate_uri())
eq_(200, response.status_code)
doc = pq(response.content)
eq_(1, len(doc('form textarea[name="content"]')))
# initial translation should include slug input
eq_(1, len(doc('form input[name="slug"]')))
assert (u'Espa' in doc('div.change-locale').text())
def test_translate_disallow(self):
"""HTTP GET to translate URL returns 400 when not localizable."""
self.d.is_localizable = False
self.d.save()
response = self.client.get(self._translate_uri())
eq_(400, response.status_code)
def test_invalid_document_form(self):
"""Make sure we handle invalid document form without a 500."""
translate_uri = self._translate_uri()
data = _translation_data()
data['slug'] = '' # Invalid slug
response = self.client.post(translate_uri, data)
eq_(200, response.status_code)
def test_invalid_revision_form(self):
"""When creating a new translation, an invalid revision form shouldn't
result in a new Document being created."""
translate_uri = self._translate_uri()
data = _translation_data()
data['content'] = '' # Content is required
response = self.client.post(translate_uri, data)
eq_(200, response.status_code)
eq_(0, self.d.translations.count())
@mock.patch.object(EditDocumentEvent, 'fire')
@mock.patch.object(Site.objects, 'get_current')
def test_first_translation_to_locale(self, get_current, edited_fire):
"""Create the first translation of a doc to new locale."""
get_current.return_value.domain = 'testserver'
translate_uri = self._translate_uri()
data = _translation_data()
response = self.client.post(translate_uri, data)
eq_(302, response.status_code)
new_doc = Document.objects.get(slug=data['slug'])
eq_('es', new_doc.locale)
eq_(data['title'], new_doc.title)
eq_(self.d, new_doc.parent)
rev = new_doc.revisions.all()[0]
eq_(data['keywords'], rev.keywords)
eq_(data['summary'], rev.summary)
eq_(data['content'], rev.content)
ok_(edited_fire.called)
def _create_and_approve_first_translation(self):
"""Returns the revision."""
# First create the first one with test above
self.test_first_translation_to_locale()
# Approve the translation
rev_es = Revision.objects.filter(document__locale='es')[0]
rev_es.is_approved = True
rev_es.save()
return rev_es
@mock.patch.object(EditDocumentEvent, 'fire')
@mock.patch.object(Site.objects, 'get_current')
def test_another_translation_to_locale(self, get_current, edited_fire):
"""Create the second translation of a doc."""
get_current.return_value.domain = 'testserver'
rev_es = self._create_and_approve_first_translation()
# Create and approve a new en-US revision
rev_enUS = Revision(summary="lipsum",
content='lorem ipsum dolor sit amet new',
keywords='kw1 kw2',
document=self.d, creator_id=8, is_approved=True)
rev_enUS.save()
# Verify the form renders with correct content
translate_uri = self._translate_uri()
response = self.client.get(translate_uri)
doc = pq(response.content)
eq_(rev_es.content, doc('#id_content').text())
eq_(rev_enUS.content, doc('article.approved .translate-rendered').text())
# Post the translation and verify
data = _translation_data()
data['content'] = 'loremo ipsumo doloro sito ameto nuevo'
response = self.client.post(translate_uri, data)
eq_(302, response.status_code)
eq_('http://testserver/es/docs/un-test-articulo',
response['location'])
doc = Document.objects.get(slug=data['slug'])
rev = doc.revisions.filter(content=data['content'])[0]
eq_(data['keywords'], rev.keywords)
eq_(data['summary'], rev.summary)
eq_(data['content'], rev.content)
ok_(edited_fire.called)
# subsequent translations should NOT include slug input
self.client.logout()
self.client.login(username='testuser', password='testpass')
response = self.client.get(translate_uri)
doc = pq(response.content)
eq_(0, len(doc('form input[name="slug"]')))
@pytest.mark.xfail(reason='Figure out wtf is going on with this test')
def test_translate_form_maintains_based_on_rev(self):
"""
Revision.based_on should be the rev that was current when the
Translate button was clicked, even if other revisions happen while the
user is editing.
"""
_test_form_maintains_based_on_rev(self.client,
self.d,
'wiki.translate',
_translation_data(),
trans_lang='es',
locale='en-US')
def test_translate_update_doc_only(self):
"""
Submitting the document form should update document.
No new revisions should be created.
"""
rev_es = self._create_and_approve_first_translation()
translate_uri = self._translate_uri()
data = _translation_data()
new_title = 'Un nuevo titulo'
data['title'] = new_title
data['form-type'] = 'doc'
response = self.client.post(translate_uri, data)
eq_(302, response.status_code)
eq_('http://testserver/es/docs/un-test-articulo$edit'
'?opendescription=1',
response['location'])
revisions = rev_es.document.revisions.all()
eq_(1, revisions.count()) # No new revisions
d = Document.objects.get(id=rev_es.document.id)
eq_(new_title, d.title) # Title is updated
def test_translate_update_rev_and_doc(self):
"""
Submitting the revision form should create a new revision.
And since Kuma docs default to approved, should update doc too.
"""
rev_es = self._create_and_approve_first_translation()
translate_uri = self._translate_uri()
data = _translation_data()
new_title = 'Un nuevo titulo'
data['title'] = new_title
data['form'] = 'rev'
response = self.client.post(translate_uri, data)
eq_(302, response.status_code)
eq_('http://testserver/es/docs/un-test-articulo',
response['location'])
revisions = rev_es.document.revisions.all()
eq_(2, revisions.count()) # New revision is created
d = Document.objects.get(id=rev_es.document.id)
eq_(data['title'], d.title) # Title isn't updated
def test_translate_form_content_fallback(self):
"""
If there are existing but unapproved translations, prefill
content with latest.
"""
self.test_first_translation_to_locale()
translate_uri = self._translate_uri()
response = self.client.get(translate_uri)
doc = pq(response.content)
document = Document.objects.filter(locale='es')[0]
existing_rev = document.revisions.all()[0]
eq_(existing_rev.content, doc('#id_content').text())
@pytest.mark.xfail(reason='Figure out wtf is going on with this test.')
def test_translate_based_on(self):
"""Test translating based on a non-current revision."""
# Create the base revision
base_rev = self._create_and_approve_first_translation()
# Create a new current revision
r = revision(document=base_rev.document, is_approved=True)
r.save()
d = Document.objects.get(pk=base_rev.document.id)
eq_(r, base_rev.document.current_revision)
uri = reverse('wiki.new_revision_based_on',
locale=d.locale,
args=[d.slug, base_rev.id])
response = self.client.get(uri)
eq_(200, response.status_code)
doc = pq(response.content)
eq_(doc('#id_content')[0].value, base_rev.content)
def _test_form_maintains_based_on_rev(client, doc, view, post_data,
trans_lang=None, locale=None):
"""Confirm that the based_on value set in the revision created by an edit
or translate form is the current_revision of the document as of when the
form was first loaded, even if other revisions have been approved in the
meantime."""
if trans_lang:
translate_path = doc.slug
uri = urllib.quote(reverse('wiki.translate',
locale=trans_lang,
args=[translate_path]))
else:
uri = reverse(view, locale=locale, args=[doc.slug])
response = client.get(uri)
orig_rev = doc.current_revision
eq_(orig_rev.id,
int(pq(response.content)('input[name=based_on]').attr('value')))
# While Fred is editing the above, Martha approves a new rev:
martha_rev = revision(document=doc)
martha_rev.is_approved = True
martha_rev.save()