def test_intermediate(self): """ Test that the intermediate DocumentAttachment gets created correctly when adding an Attachment with a document_id. """ doc = document(locale='en-US', slug='attachment-test-intermediate', save=True) revision(document=doc, is_approved=True, save=True) file_for_upload = make_test_file( content='A file for testing intermediate attachment model.') post_data = { 'title': 'Intermediate test file', 'description': 'Intermediate test file', 'comment': 'Initial upload', 'file': file_for_upload, } files_url = reverse('attachments.edit_attachment', kwargs={'document_path': doc.slug}, locale='en-US') response = self.client.post(files_url, data=post_data) self.assertEqual(response.status_code, 302) self.assertEqual(doc.files.count(), 1) intermediates = DocumentAttachment.objects.filter(document__pk=doc.id) self.assertEqual(intermediates.count(), 1) intermediate = intermediates[0] self.assertEqual(intermediate.attached_by.username, 'admin') self.assertEqual(intermediate.name, file_for_upload.name.split('/')[-1])
def test_revisions_feed_diffs(self): d = document(title='HTML9') d.save() revision(save=True, document=d, title='HTML9', comment='Revision 1', content="First Content", is_approved=True, created=datetime.datetime.now()) r = revision(save=True, document=d, title='HTML9', comment='Revision 2', content="First Content", is_approved=True, created=(datetime.datetime.now() + datetime.timedelta(seconds=1)), tags='"some", "more", "tags"') r.review_tags.set(*[u'editorial']) resp = self.client.get(reverse('wiki.feeds.recent_revisions', args=(), kwargs={'format': 'rss'})) eq_(200, resp.status_code) feed = pq(resp.content) for i, item in enumerate(feed.find('item')): desc_text = pq(item).find('description').text() if "Edited" in desc_text: ok_('<h3>Tag changes:</h3>' in desc_text) ok_('<span class="diff_add" style="background-color: #afa; ' 'text-decoration: none;">"more"<br /> </span>' in desc_text) ok_('<h3>Review changes:</h3>' in desc_text) ok_('<span class="diff_add" style="background-color: #afa; ' 'text-decoration: none;">editorial</span>' in desc_text)
def test_bug869301_revisions_feed_locale(self): """Links to documents in revisions feed with ?all_locales should reflect proper document locale, regardless of requestor's locale""" d = document(title='HTML9', locale="fr") d.save() now = datetime.datetime.now() for i in xrange(1, 6): created = now + datetime.timedelta(seconds=5 * i) revision(save=True, document=d, title='HTML9', comment='Revision %s' % i, content="Some Content %s" % i, is_approved=True, created=created) resp = self.client.get('%s?all_locales' % reverse('wiki.feeds.recent_revisions', args=(), kwargs={'format': 'rss'}, locale='en-US')) eq_(200, resp.status_code) feed = pq(resp.content) eq_(5, len(feed.find('item'))) for i, item in enumerate(feed.find('item')): href = pq(item).find('link').text() ok_('/fr/' in href)
def test_zone_url_ends_with_slash(self): """Ensure urls only rewrite with a '/' at the end of url_root bug 1189596 """ zone_url_root = "Firéfox" zone_root_content = "This is the Firéfox zone" root_rev = revision( title="Firéfox", slug="Mozilla/Firéfox", content=zone_root_content, is_approved=True, save=True ) root_doc = root_rev.document root_zone = DocumentZone(document=root_doc) root_zone.url_root = zone_url_root root_zone.save() none_zone_rev = revision( title="Firéfox for iOS", slug="Mozilla/Firéfox_for_iOS", content="Page outside zone with same prefix", is_approved=True, save=True, ) non_zone_doc = none_zone_rev.document non_zone_doc.save() url = "/en-US/docs/%s" % non_zone_doc.slug response = self.client.get(url, follow=False) eq_(200, response.status_code)
def setUp(self): super(DocumentZoneTests, self).setUp() self.root_links_content = """ <p>Links content</p> """ self.root_content = """ <h4 id="links">Links</h4> %s """ % (self.root_links_content) root_rev = revision(title='ZoneRoot', slug='ZoneRoot', content=self.root_content, is_approved=True, save=True) self.root_doc = root_rev.document self.root_doc.rendered_html = self.root_content self.root_doc.save() self.root_zone = DocumentZone(document=self.root_doc) self.root_zone.save() sub_rev = revision(title='SubPage', slug='SubPage', content='This is a subpage', is_approved=True, save=True) self.sub_doc = sub_rev.document self.sub_doc.parent_topic = self.root_doc self.sub_doc.rendered_html = sub_rev.content self.sub_doc.save() self.sub_sub_links_content = """ <p>Sub-page links content</p> """ self.sub_sub_content = """ <h4 id="links">Links</h4> %s """ % (self.sub_sub_links_content) sub_sub_rev = revision(title='SubSubPage', slug='SubSubPage', content='This is a subpage', is_approved=True, save=True) self.sub_sub_doc = sub_sub_rev.document self.sub_sub_doc.parent_topic = self.sub_doc self.sub_sub_doc.rendered_html = self.sub_sub_content self.sub_sub_doc.save() other_rev = revision(title='otherPage', slug='otherPage', content='This is an other page', is_approved=True, save=True) self.other_doc = other_rev.document self.other_doc.save() memcache.clear()
def test_doc_short_short_slug_and_title(self): slug = 'NotSpam' html = '<p>This page is not spam.</p>' doc = document(title='blah', slug=slug, html=html, save=True) revision(document=doc, content=html, is_approved=True, save=True) dsa = DocumentSpamAttempt(slug=slug, document=doc) assert self.admin.doc_short(dsa) == u'/en-US/docs/NotSpam (blah)' assert self.admin.doc_short(dsa) == str(doc)
def test_doc_short_long_slug_and_title(self): slug = 'Web/Guide/HTML/Sections_and_Outlines_of_an_HTML5_document' title = 'Sections and Outlines of an HTML5 Document' html = '<p>This German page is not spam.</p>' doc = document(title=title, slug=slug, html=html, save=True, locale='de') revision(document=doc, content=html, is_approved=True, save=True) dsa = DocumentSpamAttempt(slug=slug, document=doc) expected = u'/de/docs/Web/Guide/HTML/… (Sections and Outlines of…)' assert self.admin.doc_short(dsa) == expected
def test_doc_short_long_unicode(self): slug = u'Web/Guide/HTML/HTML5_ডকুমেন্টের_সেকশন_এবং_আউটলাইন' title = u'HTML5 ডকুমেন্টের সেকশন এবং আউটলাইন' html = '<p>This Bengali page is not spam.</p>' doc = document(title=title, slug=slug, html=html, save=True, locale='bn-BD') revision(document=doc, content=html, is_approved=True, save=True) dsa = DocumentSpamAttempt(slug=slug, document=doc) expected = u'/bn-BD/docs/Web/Guide/HT… (HTML5 ডকুমেন্টের সেকশন এব…)' assert self.admin.doc_short(dsa) == expected
def setUp(self): super(DocumentZoneMiddlewareTestCase, self).setUp() s_cache = get_cache(SECONDARY_CACHE_ALIAS) s_cache.clear() self.zone_root = 'ExtraWiki' self.zone_root_content = 'This is the Zone Root' root_rev = revision(title='ZoneRoot', slug='Zones/Root', content=self.zone_root_content, is_approved=True, save=True) self.root_doc = root_rev.document middle_rev = revision(title='middlePage', slug='Zones/Root/Middle', content='This is a middlepage', is_approved=True, save=True) self.middle_doc = middle_rev.document self.middle_doc.parent_topic = self.root_doc self.middle_doc.save() sub_rev = revision(title='SubPage', slug='Zones/Root/Middle/SubPage', content='This is a subpage', is_approved=True, save=True) self.sub_doc = sub_rev.document self.sub_doc.parent_topic = self.middle_doc self.sub_doc.save() self.root_zone = DocumentZone(document=self.root_doc) self.root_zone.url_root = self.zone_root self.root_zone.save() self.middle_zone = DocumentZone(document=self.middle_doc) self.middle_zone.save() other_rev = revision(title='otherPage', slug='otherPage', content='This is an otherpage', is_approved=True, save=True) self.other_doc = other_rev.document self.other_doc.save() self.other_zone = DocumentZone(document=self.other_doc) self.other_zone.url_root = '' self.other_zone.save() # One more doc, just to be sure we can have multiple blank url_roots onemore_rev = revision(title='onemorePage', slug='onemorePage', content='This is an onemorepage', is_approved=True, save=True) self.onemore_doc = onemore_rev.document self.onemore_doc.save() self.onemore_zone = DocumentZone(document=self.onemore_doc) self.onemore_zone.url_root = '' self.onemore_zone.save()
def setUp(self): super(DocumentZoneMiddlewareTestCase, self).setUp() memcache.clear() self.zone_root = "ExtraWiki" self.zone_root_content = "This is the Zone Root" root_rev = revision( title="ZoneRoot", slug="Zones/Root", content=self.zone_root_content, is_approved=True, save=True ) self.root_doc = root_rev.document middle_rev = revision( title="middlePage", slug="Zones/Root/Middle", content="This is a middlepage", is_approved=True, save=True ) self.middle_doc = middle_rev.document self.middle_doc.parent_topic = self.root_doc self.middle_doc.save() sub_rev = revision( title="SubPage", slug="Zones/Root/Middle/SubPage", content="This is a subpage", is_approved=True, save=True ) self.sub_doc = sub_rev.document self.sub_doc.parent_topic = self.middle_doc self.sub_doc.save() self.root_zone = DocumentZone(document=self.root_doc) self.root_zone.url_root = self.zone_root self.root_zone.save() self.middle_zone = DocumentZone(document=self.middle_doc) self.middle_zone.save() other_rev = revision( title="otherPage", slug="otherPage", content="This is an otherpage", is_approved=True, save=True ) self.other_doc = other_rev.document self.other_doc.save() self.other_zone = DocumentZone(document=self.other_doc) self.other_zone.url_root = "" self.other_zone.save() # One more doc, just to be sure we can have multiple blank url_roots onemore_rev = revision( title="onemorePage", slug="onemorePage", content="This is an onemorepage", is_approved=True, save=True ) self.onemore_doc = onemore_rev.document self.onemore_doc.save() self.onemore_zone = DocumentZone(document=self.onemore_doc) self.onemore_zone.url_root = "" self.onemore_zone.save()
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_xss_file_attachment_title(self): title = '"><img src=x onerror=prompt(navigator.userAgent);>' # use view to create new attachment file_for_upload = make_test_file() post_data = { 'title': title, 'description': 'xss', 'comment': 'xss', 'file': file_for_upload, } self.client = Client() # file views don't need LocalizingClient self.client.login(username='******', password='******') resp = self.client.post(reverse('attachments.new_attachment'), data=post_data) eq_(302, resp.status_code) # now stick it in/on a document attachment = Attachment.objects.get(title=title) rev = revision(content='<img src="%s" />' % attachment.get_file_url(), save=True) # view it and verify markup is escaped response = self.client.get(rev.document.get_absolute_url()) eq_(200, response.status_code) doc = pq(response.content) eq_('%s xss' % title, doc('#page-attachments-table .attachment-name-cell').text()) ok_('><img src=x onerror=prompt(navigator.userAgent);>' in doc('#page-attachments-table .attachment-name-cell').html())
def test_from_revision_none(self): rev = revision() try: diff = revisions_unified_diff(None, rev) except AttributeError: self.fail("Should not throw AttributeError") eq_("Diff is unavailable.", diff)
def setUp(self): super(AttachmentViewTests, self).setUp() self.client.login(username='******', password='******') self.revision = revision(save=True) self.document = self.revision.document self.files_url = reverse('attachments.edit_attachment', kwargs={'document_path': self.document.slug})
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.full_path]) 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() # Then Fred saves his edit: post_data_copy = {"based_on": orig_rev.id, "slug": orig_rev.slug} post_data_copy.update(post_data) # Don't mutate arg. response = client.post(uri, data=post_data_copy) ok_(response.status_code in (200, 302)) fred_rev = Revision.objects.all().order_by("-id")[0] eq_(orig_rev, fred_rev.based_on)
def test_intermediate(self): """ Test that the intermediate DocumentAttachment gets created correctly when adding an Attachment with a document_id. """ doc = document(locale="en", slug="attachment-test-intermediate") doc.save() rev = revision(document=doc, is_approved=True) rev.save() file_for_upload = make_test_file(content="A file for testing intermediate attachment model.") post_data = { "title": "Intermediate test file", "description": "Intermediate test file", "comment": "Initial upload", "file": file_for_upload, } add_url = urlparams(reverse("attachments.new_attachment"), document_id=doc.id) resp = self.client.post(add_url, data=post_data) eq_(302, resp.status_code) eq_(1, doc.files.count()) intermediates = DocumentAttachment.objects.filter(document__pk=doc.id) eq_(1, intermediates.count()) intermediate = intermediates[0] eq_("admin", intermediate.attached_by.username) eq_(file_for_upload.name.split("/")[-1], intermediate.name)
def test_intermediate(self): """ Test that the intermediate DocumentAttachment gets created correctly when adding an Attachment with a document_id. """ doc = document(locale='en', slug='attachment-test-intermediate') doc.save() rev = revision(document=doc, is_approved=True) rev.save() file_for_upload = make_test_file( content='A file for testing intermediate attachment model.') post_data = { 'title': 'Intermediate test file', 'description': 'Intermediate test file', 'comment': 'Initial upload', 'file': file_for_upload, } add_url = urlparams(reverse('attachments.new_attachment'), document_id=doc.id) resp = self.client.post(add_url, data=post_data) eq_(302, resp.status_code) eq_(1, doc.files.count()) intermediates = DocumentAttachment.objects.filter(document__pk=doc.id) eq_(1, intermediates.count()) intermediate = intermediates[0] eq_('admin', intermediate.attached_by.username) eq_(file_for_upload.name.split('/')[-1], intermediate.name)
def test_xss_file_attachment_title(self): title = '"><img src=x onerror=prompt(navigator.userAgent);>' # use view to create new attachment file_for_upload = make_test_file() post_data = { 'title': title, 'description': 'xss', 'comment': 'xss', 'file': file_for_upload, } self.client.login(username='******', password='******') response = self.client.post(self.files_url, data=post_data) self.assertEqual(response.status_code, 302) # now stick it in/on a document attachment = Attachment.objects.get(title=title) rev = revision(content='<img src="%s" />' % attachment.get_file_url(), document=self.document, save=True) # view it and verify markup is escaped response = self.client.get(rev.document.get_edit_url()) self.assertEqual(response.status_code, 200) doc = pq(response.content) self.assertEqual( '%s xss' % title, doc('.page-attachments-table .attachment-name-cell').text()) self.assertIn( '><img src=x onerror=prompt(navigator.userAgent);>', doc('.page-attachments-table .attachment-name-cell').html())
def test_context_dict_no_previous_revision(self): rev = revision() try: cd = context_dict(rev) except AttributeError: self.fail("Should not throw AttributeError") eq_(cd, cd)
def test_xss_file_attachment_title(self): title = '"><img src=x onerror=prompt(navigator.userAgent);>' # use view to create new attachment file_for_upload = make_test_file() post_data = { 'title': title, 'description': 'xss', 'comment': 'xss', 'file': file_for_upload, } self.client.login(username='******', password='******') resp = self.client.post(reverse('attachments.new_attachment'), data=post_data) eq_(302, resp.status_code) # now stick it in/on a document attachment = Attachment.objects.get(title=title) rev = revision(content='<img src="%s" />' % attachment.get_file_url(), save=True) # view it and verify markup is escaped response = self.client.get(rev.document.get_edit_url()) eq_(200, response.status_code) doc = pq(response.content) eq_('%s xss' % title, doc('#page-attachments-table .attachment-name-cell').text()) ok_('><img src=x onerror=prompt(navigator.userAgent);>' in doc('#page-attachments-table .attachment-name-cell').html())
def test_xss_file_attachment_title(self): title = '"><img src=x onerror=prompt(navigator.userAgent);>' # use view to create new attachment file_for_upload = make_test_file() post_data = { 'title': title, 'description': 'xss', 'comment': 'xss', 'file': file_for_upload, } self.client.login(username='******', password='******') response = self.client.post(self.files_url, data=post_data) self.assertEqual(response.status_code, 302) # now stick it in/on a document attachment = Attachment.objects.get(title=title) rev = revision(content='<img src="%s" />' % attachment.get_file_url(), document=self.document, save=True) # view it and verify markup is escaped response = self.client.get(rev.document.get_edit_url()) self.assertEqual(response.status_code, 200) doc = pq(response.content) self.assertEqual( '%s xss' % title, doc('.page-attachments-table .attachment-name-cell').text() ) self.assertIn( '><img src=x onerror=prompt(navigator.userAgent);>', doc('.page-attachments-table .attachment-name-cell').html() ) # security bug 1272791 for script in doc('script'): self.assertNotIn(title, script.text_content())
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())
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_revisions_feed(self): d = document(title='HTML9') d.save() now = datetime.datetime.now() for i in xrange(1, 6): created = now + datetime.timedelta(seconds=5 * i) revision(save=True, document=d, title='HTML9', comment='Revision %s' % i, content="Some Content %s" % i, is_approved=True, created=created) resp = self.client.get(reverse('wiki.feeds.recent_revisions', args=(), kwargs={'format': 'rss'})) eq_(200, resp.status_code) feed = pq(resp.content) eq_(5, len(feed.find('item'))) for i, item in enumerate(feed.find('item')): desc_text = pq(item).find('description').text() ok_('by:</h3><p>testuser</p>' in desc_text) ok_('<h3>Comment:</h3><p>Revision' in desc_text) if "Edited" in desc_text: ok_('$compare?to' in desc_text) ok_('diff_chg' in desc_text) ok_('$edit' in desc_text) ok_('$history' in desc_text) resp = self.client.get(reverse('wiki.feeds.recent_revisions', args=(), kwargs={'format': 'rss'}) + '?limit=2') feed = pq(resp.content) eq_(2, len(feed.find('item'))) resp = self.client.get(reverse('wiki.feeds.recent_revisions', args=(), kwargs={'format': 'rss'}) + '?limit=2&page=1') ok_('Revision 5' in resp.content) ok_('Revision 4' in resp.content) resp = self.client.get(reverse('wiki.feeds.recent_revisions', args=(), kwargs={'format': 'rss'}) + '?limit=2&page=2') ok_('Revision 3' in resp.content) ok_('Revision 2' in resp.content)
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)
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_feed_locale_filter(self): """Documents and Revisions in feeds should be filtered by locale""" d1 = document(title="Doc1", locale='en-US', save=True) r1 = revision(document=d1, save=True) r1.review_tags.set('editorial') d1.tags.set('foobar') d2 = document(title="TransDoc1", locale='de', parent=d1, save=True) r2 = revision(document=d2, save=True) r2.review_tags.set('editorial') d2.tags.set('foobar') d3 = document(title="TransDoc1", locale='fr', parent=d1, save=True) r3 = revision(document=d3, save=True) r3.review_tags.set('editorial') d3.tags.set('foobar') show_alls = (False, True) locales = ('en-US', 'de', 'fr') for show_all in show_alls: for locale in locales: feed_urls = ( reverse('wiki.feeds.recent_revisions', locale=locale, args=(), kwargs={'format': 'json'}), reverse('wiki.feeds.recent_documents', locale=locale, args=(), kwargs={'format': 'json'}), reverse('wiki.feeds.recent_documents', locale=locale, args=(), kwargs={'format': 'json', 'tag': 'foobar'}), reverse('wiki.feeds.list_review', locale=locale, args=('json',)), reverse('wiki.feeds.list_review_tag', locale=locale, args=('json', 'editorial',)), ) for feed_url in feed_urls: if show_all: feed_url = '%s?all_locales' % feed_url resp = self.client.get(feed_url) data = json.loads(resp.content) if show_all: eq_(3, len(data)) else: eq_(1, len(data))
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"])
def test_feed_unchanged_after_render(self): """Rendering a document shouldn't affect feed contents, unless the document content has actually been changed.""" d1 = document(title="FeedDoc1", locale='en-US', save=True) revision(document=d1, save=True) time.sleep(1) # Let timestamps tick over. d2 = document(title="FeedDoc2", locale='en-US', save=True) revision(document=d2, save=True) time.sleep(1) # Let timestamps tick over. d3 = document(title="FeedDoc3", locale='en-US', save=True) revision(document=d3, save=True) time.sleep(1) # Let timestamps tick over. d4 = document(title="FeedDoc4", locale='en-US', save=True) # No r4, so we can trigger the no-current-rev edge case feed_url = reverse('wiki.feeds.recent_documents', locale='en-US', args=(), kwargs={'format': 'rss'}) # Force a render, hash the feed for d in (d1, d2, d3, d4): d.render(cache_control="no-cache") resp = self.client.get(feed_url) feed_hash_1 = hashlib.md5(resp.content).hexdigest() # Force another render, hash the feed time.sleep(1) # Let timestamps tick over. for d in (d1, d2, d3, d4): d.render(cache_control="no-cache") resp = self.client.get(feed_url) feed_hash_2 = hashlib.md5(resp.content).hexdigest() # The hashes should match eq_(feed_hash_1, feed_hash_2) # Make a real edit. time.sleep(1) # Let timestamps tick over. revision(document=d2, content="Hah! An edit!", save=True) for d in (d1, d2, d3, d4): d.render(cache_control="no-cache") # This time, the hashes should *not* match resp = self.client.get(feed_url) feed_hash_3 = hashlib.md5(resp.content).hexdigest() ok_(feed_hash_2 != feed_hash_3)
def test_live_indexing_docs(self): S = DocumentType.search count_before = S().count() r = revision(save=True) r.document.render() self.refresh() eq_(count_before + 1, S().count()) r.document.delete() self.refresh() eq_(count_before, S().count())
def test_update_l10n_metric_cron(self): """Verify the cron job creates the correct metric.""" l10n_kind = metric_kind(code=L10N_METRIC_CODE, save=True) # Create the en-US document with an approved revision. doc = document(save=True) rev = revision( document=doc, is_approved=True, save=True) time.sleep(1) # Create an es translation es_doc = document(parent=doc, locale='es', save=True) revision( document=es_doc, is_approved=True, based_on=rev, save=True) # Run it and verify results. # Value should be 100% update_l10n_metric() metrics = Metric.objects.filter(kind=l10n_kind) eq_(1, len(metrics)) eq_(100, metrics[0].value) # Update the en-US document revision(document=doc, is_approved=True, save=True) # Run it and verify results. # Value should be 0% update_l10n_metric() metrics = Metric.objects.filter(kind=l10n_kind) eq_(1, len(metrics)) eq_(0, metrics[0].value) time.sleep(1) # Create a pt-BR translation ptBR_doc = document(parent=doc, locale='pt-BR', save=True) revision( document=ptBR_doc, is_approved=True, based_on=rev, save=True) # Run it and verify results. # Value should be 50% update_l10n_metric() metrics = Metric.objects.filter(kind=l10n_kind) eq_(1, len(metrics)) eq_(50, metrics[0].value)
def test_intermediate(self): """ Test that the intermediate DocumentAttachment gets created correctly when adding an Attachment with a document_id. """ doc = document(locale='en-US', slug='attachment-test-intermediate', save=True) revision(document=doc, is_approved=True, save=True) file_for_upload = make_test_file( content='A file for testing intermediate attachment model.') post_data = { 'title': 'Intermediate test file', 'description': 'Intermediate test file', 'comment': 'Initial upload', 'file': file_for_upload, } files_url = reverse('attachments.edit_attachment', kwargs={'document_path': doc.slug}, locale='en-US') response = self.client.post(files_url, data=post_data) assert response.status_code == 302 assert 'max-age=0' in response['Cache-Control'] assert 'no-cache' in response['Cache-Control'] assert 'no-store' in response['Cache-Control'] assert 'must-revalidate' in response['Cache-Control'] assert doc.files.count() == 1 intermediates = DocumentAttachment.objects.filter(document__pk=doc.id) assert intermediates.count() == 1 intermediate = intermediates[0] assert intermediate.attached_by.username == 'admin' assert intermediate.name == file_for_upload.name.split('/')[-1]
def test_revisions_feed_diffs(self): d = document(title='HTML9') d.save() revision(save=True, document=d, title='HTML9', comment='Revision 1', content="First Content", is_approved=True, created=datetime.datetime.now()) r = revision(save=True, document=d, title='HTML9', comment='Revision 2', content="First Content", is_approved=True, created=(datetime.datetime.now() + datetime.timedelta(seconds=1)), tags='"some", "more", "tags"') r.review_tags.set(*[u'editorial']) resp = self.client.get( reverse('wiki.feeds.recent_revisions', args=(), kwargs={'format': 'rss'})) eq_(200, resp.status_code) feed = pq(resp.content) for i, item in enumerate(feed.find('item')): desc_text = pq(item).find('description').text() if "Edited" in desc_text: ok_('<h3>Tag changes:</h3>' in desc_text) ok_('<span class="diff_add" style="background-color: #afa; ' 'text-decoration: none;">"more"<br /> </span>' in desc_text) ok_('<h3>Review changes:</h3>' in desc_text) ok_('<span class="diff_add" style="background-color: #afa; ' 'text-decoration: none;">editorial</span>' in desc_text)
def test_zone_url_ends_with_slash(self): """Ensure urls only rewrite with a '/' at the end of url_root""" zone_url_root = 'Firéfox' zone_root_content = 'This is the Firéfox zone' root_rev = revision(title='Firéfox', slug='Mozilla/Firéfox', content=zone_root_content, is_approved=True, save=True) root_doc = root_rev.document root_zone = DocumentZone(document=root_doc) root_zone.url_root = zone_url_root root_zone.save() none_zone_rev = revision(title='Firéfox for iOS', slug='Mozilla/Firéfox_for_iOS', content='Page outside zone with same prefix', is_approved=True, save=True) non_zone_doc = none_zone_rev.document non_zone_doc.save() url = '/en-US/docs/%s' % non_zone_doc.slug response = self.client.get(url, follow=False) eq_(200, response.status_code)
def test_files_dict(self): doc = document(locale='en', slug='attachment-test-files-dict') doc.save() rev = revision(document=doc, is_approved=True) rev.save() test_file_1 = make_test_file( content='A file for testing the files dict') post_data = { 'title': 'Files dict test file', 'description': 'Files dict test file', 'comment': 'Initial upload', 'file': test_file_1, } add_url = urlparams(reverse('attachments.new_attachment'), document_id=doc.id) self.client.post(add_url, data=post_data) test_file_2 = make_test_file( content='Another file for testing the files dict') post_data = { 'title': 'Files dict test file 2', 'description': 'Files dict test file 2', 'comment': 'Initial upload', 'file': test_file_2, } self.client.post(add_url, data=post_data) doc = Document.objects.get(pk=doc.id) files_dict = doc.files_dict() file1 = files_dict[test_file_1.name.split('/')[-1]] eq_('admin', file1['attached_by']) eq_('Files dict test file', file1['description']) eq_('text/plain', file1['mime_type']) ok_(test_file_1.name.split('/')[-1] in file1['url']) file2 = files_dict[test_file_2.name.split('/')[-1]] eq_('admin', file2['attached_by']) eq_('Files dict test file 2', file2['description']) eq_('text/plain', file2['mime_type']) ok_(test_file_2.name.split('/')[-1] in file2['url'])
def test_live_indexing_docs(self): # Live indexing uses tasks which pass the index by pk, so we need to # create and save one to the database here. index = Index.objects.create(promoted=True, populated=True) index.populate() S = WikiDocumentType.search count_before = S().count() r = revision(save=True) r.document.render() self.refresh() assert count_before + 1 == S().count() r.document.delete() self.refresh() # TODO: Investigate this test failure. The ES debug output appears to # be doing the correct thing but the ES delete call is returning a 404. assert count_before == S().count()
def test_wiki_revisions(self): user = self.user_model.objects.get(username="******") rev = revision(save=True, is_approved=True) assert rev.pk in user.wiki_revisions().values_list("pk", flat=True)
def test_wiki_activity(self): user = User.objects.get(username='******') profile = UserProfile.objects.get(user=user) revision(save=True, is_approved=True) eq_(1, len(profile.wiki_activity()))
def setUp(self): super(DocumentZoneMiddlewareTestCase, self).setUp() memcache.clear() self.zone_root = 'ExtraWiki' self.zone_root_content = 'This is the Zone Root' root_rev = revision(title='ZoneRoot', slug='Zones/Root', content=self.zone_root_content, is_approved=True, save=True) self.root_doc = root_rev.document middle_rev = revision(title='middlePage', slug='Zones/Root/Middle', content='This is a middlepage', is_approved=True, save=True) self.middle_doc = middle_rev.document self.middle_doc.parent_topic = self.root_doc self.middle_doc.save() sub_rev = revision(title='SubPage', slug='Zones/Root/Middle/SubPage', content='This is a subpage', is_approved=True, save=True) self.sub_doc = sub_rev.document self.sub_doc.parent_topic = self.middle_doc self.sub_doc.save() self.root_zone = DocumentZone(document=self.root_doc) self.root_zone.url_root = self.zone_root self.root_zone.save() self.middle_zone = DocumentZone(document=self.middle_doc) self.middle_zone.save() other_rev = revision(title='otherPage', slug='otherPage', content='This is an otherpage', is_approved=True, save=True) self.other_doc = other_rev.document self.other_doc.save() self.other_zone = DocumentZone(document=self.other_doc) self.other_zone.url_root = '' self.other_zone.save() # One more doc, just to be sure we can have multiple blank url_roots onemore_rev = revision(title='onemorePage', slug='onemorePage', content='This is an onemorepage', is_approved=True, save=True) self.onemore_doc = onemore_rev.document self.onemore_doc.save() self.onemore_zone = DocumentZone(document=self.onemore_doc) self.onemore_zone.url_root = '' self.onemore_zone.save()
def test_wiki_revisions(self): user = self.user_model.objects.get(username='******') rev = revision(save=True, is_approved=True) ok_(rev.pk in user.wiki_revisions().values_list('pk', flat=True))
def test_wiki_activity(self): user = User.objects.get(username='******') profile = UserProfile.objects.get(user=user) rev = revision(save=True, is_approved=True) ok_(rev.pk in profile.wiki_activity().values_list('pk', flat=True))