def test_check_host(self): from_url = Document.from_url d_en = DocumentFactory(locale='en-US', title=u'How to delete Google Chrome?') sumo_host = 'http://support.mozilla.org' invalid_url = urlparse.urljoin(sumo_host, d_en.get_absolute_url()) self.assertIsNone(from_url(invalid_url)) self.assertEqual(d_en, from_url(invalid_url, check_host=False))
def test_id_only(self): from_url = Document.from_url d = DocumentFactory(locale='en-US', title=u'How to delete Google Chrome?') doc = from_url(d.get_absolute_url(), id_only=True) self.assertEqual(d.title, doc.title) self.assertEqual(d.locale, doc.locale)
def test_from_french(self): # Create the English document d = DocumentFactory(title='A doc') d.save() # Returns English document for French obj = get_object_fallback(Document, 'A doc', 'fr', '!') eq_(d, obj)
def test_miscounting_archived(self): """ Verify that the l10n overview readout treats archived docs consistently. Bug 1012384 """ locale = 'nl' parent1 = DocumentFactory(category=CANNED_RESPONSES_CATEGORY, is_archived=False) translation1 = DocumentFactory(parent=parent1, locale=locale) parent2 = DocumentFactory(category=CANNED_RESPONSES_CATEGORY, is_archived=True) translation2 = DocumentFactory(parent=parent2, locale=locale) trans_rev1 = ApprovedRevisionFactory(document=parent1, is_ready_for_localization=True) ApprovedRevisionFactory(document=translation1, based_on=trans_rev1) trans_rev2 = ApprovedRevisionFactory(document=parent2, is_ready_for_localization=True) ApprovedRevisionFactory(document=translation2, based_on=trans_rev2) # Document.save() will enforce that parents and translations share is_archived. # The point of this is to test what happens when that isn't true though, # so bypass Document.save(). translation2.is_archived = False ModelBase.save(translation2) eq_(translation2.is_archived, False) overview = l10n_overview_rows(locale) eq_(1, overview['all']['denominator']) eq_(1, overview['all']['numerator'])
def test_majorly_outdated_with_unapproved_parents(self): """Migrations might introduce translated revisions without based_on set. Tolerate these. If based_on of a translation's current_revision is None, the translation should be considered out of date iff any major-significance, approved revision to the English article exists. """ # Create a parent doc with only an unapproved revision... parent_rev = RevisionFactory() # ...and a translation with a revision based on nothing. trans = DocumentFactory(parent=parent_rev.document, locale='de') trans_rev = RevisionFactory(document=trans, is_approved=True) assert trans_rev.based_on is None, \ ('based_on defaulted to something non-None, which this test ' "wasn't expecting.") assert not trans.is_majorly_outdated(), \ ('A translation was considered majorly out of date even though ' 'the English document has never had an approved revision of ' 'major significance.') ApprovedRevisionFactory( document=parent_rev.document, significance=MAJOR_SIGNIFICANCE, is_ready_for_localization=True) assert trans.is_majorly_outdated(), \ ('A translation was not considered majorly outdated when its ' "current revision's based_on value was None.")
def test_get_topics(self): """Test the get_topics() method.""" en_us = DocumentFactory(topics=TopicFactory.create_batch(2)) eq_(2, len(en_us.get_topics())) # Localized document inherits parent's topics. DocumentFactory(parent=en_us) eq_(2, len(en_us.get_topics()))
def test_no_redirect_on_unsaved_change(self): """No redirect should be made when an unsaved doc's title or slug is changed.""" d = DocumentFactory(title='Gerbil') d.title = 'Weasel' d.save() # There should be no redirect from Gerbil -> Weasel: assert not Document.objects.filter(title='Gerbil').exists()
def test_cannot_make_non_localizable_if_children(self): """You can't make a document non-localizable if it has children.""" # Make English rev: en_doc = DocumentFactory(is_localizable=True) # Make Deutsch translation: DocumentFactory(parent=en_doc, locale='de') en_doc.is_localizable = False self.assertRaises(ValidationError, en_doc.save)
def test_delete_tagged_document(self): """Make sure deleting a tagged doc deletes its tag relationships.""" # TODO: Move to wherever the tests for TaggableMixin are. # This works because Django's delete() sees the `tags` many-to-many # field (actually a manager) and follows the reference. d = DocumentFactory(tags=['grape']) eq_(1, TaggedItem.objects.count()) d.delete() eq_(0, TaggedItem.objects.count())
def test_add_and_delete(self): """Adding a doc should add it to the search index; deleting should delete it.""" doc = DocumentFactory() RevisionFactory(document=doc, is_approved=True) self.refresh() eq_(DocumentMappingType.search().count(), 1) doc.delete() self.refresh() eq_(DocumentMappingType.search().count(), 0)
def _make_document(self, **kwargs): defaults = { 'title': 'How to make a pie from scratch with email ' + str(time.time()), 'category': 10, } defaults.update(kwargs) d = DocumentFactory(**defaults) RevisionFactory(document=d, is_approved=True) d.save() return d
def test_redirect_to_translated_document(self): from_url = Document.from_url d_en = DocumentFactory(locale='en-US', title=u'How to delete Google Chrome?') d_tr = DocumentFactory(locale='tr', title=u'Google Chrome\'u nasıl silerim?', parent=d_en) # The /tr/kb/how-to-delete-google-chrome URL for Turkish locale # should be redirected to /tr/kb/google-chromeu-nasl-silerim # if there is a Turkish translation of the document. tr_translate_url = reverse('wiki.document', locale='tr', args=[d_en.slug]) self.assertEqual(d_en.translated_to('tr'), from_url(tr_translate_url)) self.assertEqual(d_tr, from_url(tr_translate_url)) self.assertEqual(d_en, from_url(d_en.get_absolute_url()))
def test_category_inheritance(self): """A document's categories must always be those of its parent.""" some_category = CATEGORIES[1][0] other_category = CATEGORIES[2][0] # Notice if somebody ever changes the default on the category field, # which would invalidate our test: assert some_category != DocumentFactory().category parent = DocumentFactory(category=some_category) child = DocumentFactory(parent=parent, locale='de') # Make sure child sees stuff set on parent: eq_(some_category, child.category) # Child'd category should revert to parent's on save: child.category = other_category child.save() eq_(some_category, child.category) # Changing the parent category should change the child's: parent.category = other_category parent.save() eq_(other_category, parent.translations.get(locale=child.locale).category)
def test_ready_for_l10n(self): d = DocumentFactory() r = RevisionFactory(document=d) d.current_revision = r d.save() data = kb_overview_rows() eq_(1, len(data)) eq_(False, data[0]["ready_for_l10n"]) ApprovedRevisionFactory(document=d, is_ready_for_localization=True) data = kb_overview_rows() eq_(True, data[0]["ready_for_l10n"])
def test_only_show_wiki_and_questions(self): """Tests that the simple search doesn't show forums This verifies that we're only showing documents of the type that should be shown and that the filters on model are working correctly. Bug #767394 """ p = ProductFactory(slug=u'desktop') ques = QuestionFactory(title=u'audio', product=p) ans = AnswerFactory(question=ques, content=u'volume') AnswerVoteFactory(answer=ans, helpful=True) doc = DocumentFactory(title=u'audio', locale=u'en-US', category=10) doc.products.add(p) RevisionFactory(document=doc, is_approved=True) thread1 = ThreadFactory(title=u'audio') PostFactory(thread=thread1) self.refresh() response = self.client.get(reverse('search'), { 'q': 'audio', 'format': 'json'}) eq_(200, response.status_code) content = json.loads(response.content) eq_(content['total'], 2) # Archive the article and question. They should no longer appear # in simple search results. ques.is_archived = True ques.save() doc.is_archived = True doc.save() self.refresh() response = self.client.get(reverse('search'), { 'q': 'audio', 'format': 'json'}) eq_(200, response.status_code) content = json.loads(response.content) eq_(content['total'], 0)
def test_only_localizable_allowed_children(self): """You can't have children for a non-localizable document.""" # Make English rev: en_doc = DocumentFactory(is_localizable=False) # Make Deutsch translation: de_doc = DocumentFactory.build(parent=en_doc, locale='de') self.assertRaises(ValidationError, de_doc.save)
def test_locales_exists(self): """Links should use the correct locale.""" d1 = DocumentFactory(title='Foo', locale='en-US') RevisionFactory(document=d1, content='', is_approved=True) d2 = DocumentFactory(title='Foo', locale='de') RevisionFactory(document=d2, content='', is_approved=True) d3 = DocumentFactory(title='Bar', locale='de') RevisionFactory(document=d3, content='[[Foo]]', is_approved=True) eq_(len(d1.links_to()), 0) eq_(len(d1.links_from()), 0) eq_(len(d2.links_to()), 1) eq_(len(d2.links_from()), 0) eq_(len(d3.links_to()), 0) eq_(len(d3.links_from()), 1) eq_(d2.links_to()[0].kind, 'link')
def create_documents(self, locale): """Create a document in English and a translated document for the locale""" en = settings.WIKI_DEFAULT_LANGUAGE en_content = 'This article is in English' trans_content = 'This article is translated into %slocale' % locale # Create an English article and a translation for the locale en_doc = DocumentFactory(locale=en) ApprovedRevisionFactory(document=en_doc, content=en_content, is_ready_for_localization=True) trans_doc = DocumentFactory(parent=en_doc, locale=locale) # Create a new revision of the localized document trans_rev = ApprovedRevisionFactory(document=trans_doc, content=trans_content) # Make the created revision the current one for the localized document trans_doc.current_revision = trans_rev trans_doc.save() # Return both the English version and the localized version of the document return en_doc, trans_doc
def test_validate_category_on_save(self): """Make sure invalid categories can't be saved. Invalid categories cause errors when viewing documents. """ d = DocumentFactory.build(category=9999) self.assertRaises(ValidationError, d.save)
def test_locales_renames(self): """Links should use the correct locale, even if the title has been translated.""" d1 = DocumentFactory(title='Foo', locale='en-US') RevisionFactory(document=d1, content='', is_approved=True) d2 = DocumentFactory(title='German Foo', locale='de', parent=d1) RevisionFactory(document=d2, content='', is_approved=True) d3 = DocumentFactory(title='German Bar', locale='de') RevisionFactory(document=d3, content='[[Foo]]', is_approved=True) eq_(len(d1.links_to()), 0) eq_(len(d1.links_from()), 0) eq_(len(d2.links_to()), 1) eq_(len(d2.links_from()), 0) eq_(len(d3.links_to()), 0) eq_(len(d3.links_from()), 1) eq_(d2.links_to()[0].kind, 'link')
def test_new_doc_does_not_update_categories(self): """Make sure that creating a new document doesn't change the category of all the other documents.""" d1 = DocumentFactory(category=20) assert d1.pk d2 = DocumentFactory.build(category=30) assert not d2.pk d2._clean_category() d1prime = Document.objects.get(pk=d1.pk) eq_(20, d1prime.category)
def test_wiki_section(self): """Verify the wiki doc appears on the landing page.""" # If "Mozilla News" article doesn't exist, home page # should still work and omit the section. response = self.client.get(urlparams(reverse("community.home"))) eq_(response.status_code, 200) doc = pq(response.content) eq_(len(doc("#doc-content")), 0) # Create the "Mozilla News" article and verify it on home page. d = DocumentFactory(title="Community Hub News", slug="community-hub-news") rev = ApprovedRevisionFactory(document=d, content="splendid") d.current_revision = rev d.save() response = self.client.get(urlparams(reverse("community.home"))) eq_(response.status_code, 200) doc = pq(response.content) community_news = doc("#doc-content") eq_(len(community_news), 1) assert "splendid" in community_news.text()
def test_search(self, _out): """Test that es_search command doesn't fail""" call_command('essearch', 'cupcakes') p = ProductFactory(title=u'firefox', slug=u'desktop') doc = DocumentFactory(title=u'cupcakes rock', locale=u'en-US', category=10, products=[p]) RevisionFactory(document=doc, is_approved=True) self.refresh() call_command('essearch', 'cupcakes')
def test_status(self, _out): p = ProductFactory(title=u'firefox', slug=u'desktop') doc = DocumentFactory(title=u'cupcakes rock', locale=u'en-US', category=10, products=[p]) RevisionFactory(document=doc, is_approved=True) self.refresh() call_command('esstatus')
def test_wiki_section(self): """Verify the wiki doc appears on the landing page.""" # If "Mozilla News" article doesn't exist, home page # should still work and omit the section. response = self.client.get(urlparams(reverse('community.home'))) eq_(response.status_code, 200) doc = pq(response.content) eq_(len(doc('#doc-content')), 0) # Create the "Mozilla News" article and verify it on home page. d = DocumentFactory(title='Community Hub News', slug='community-hub-news') rev = ApprovedRevisionFactory(document=d, content='splendid') d.current_revision = rev d.save() response = self.client.get(urlparams(reverse('community.home'))) eq_(response.status_code, 200) doc = pq(response.content) community_news = doc('#doc-content') eq_(len(community_news), 1) assert 'splendid' in community_news.text()
def test_document_is_template(self): """is_template stays in sync with the title""" d = DocumentFactory(title='test') assert not d.is_template d.title = TEMPLATE_TITLE_PREFIX + 'test' d.category = TEMPLATES_CATEGORY d.save() assert d.is_template d.title = 'Back to document' d.category = CATEGORIES[0][0] d.save() assert not d.is_template
def test_wiki_no_revisions(self): """Don't index documents without approved revisions""" # Create a document with no revisions and make sure the # document is not in the index. doc = DocumentFactory() self.refresh() eq_(DocumentMappingType.search().count(), 0) # Create a revision that's not approved and make sure the # document is still not in the index. RevisionFactory(document=doc, is_approved=False) self.refresh() eq_(DocumentMappingType.search().count(), 0)
def test_counting_unready_docs(self): """Docs without a ready-for-l10n rev shouldn't count in total.""" # Make a doc with an approved but not-ready-for-l10n rev: d = DocumentFactory(is_localizable=True) r = ApprovedRevisionFactory(document=d, is_ready_for_localization=False) # It shouldn't show up in the total: eq_(0, l10n_overview_rows('de')['all']['denominator']) r.is_ready_for_localization = True r.save() eq_(1, l10n_overview_rows('de')['all']['denominator'])
def test_empty_reply_errors(self): """Posting an empty reply shows errors.""" u = UserFactory() self.client.login(username=u.username, password="******") d = DocumentFactory() t = ThreadFactory(document=d) response = post(self.client, "wiki.discuss.reply", {"content": ""}, args=[d.slug, t.id]) doc = pq(response.content) error_msg = doc("ul.errorlist li a")[0] eq_(error_msg.text, "Please provide a message.")
def test_reindex(self, _out): p = ProductFactory(title=u'firefox', slug=u'desktop') doc = DocumentFactory(title=u'cupcakes rock', locale=u'en-US', category=10, products=[p]) RevisionFactory(document=doc, is_approved=True) self.refresh() call_command('esreindex') call_command('esreindex', '--percent=50') call_command('esreindex', '--criticalmass') call_command('esreindex', '--mapping_types=wiki_documents') call_command('esreindex', '--delete')
def test_search_suggestions_questions(self): """Verifies the view doesn't kick up an HTTP 500""" p = ProductFactory(slug=u"firefox") locale = QuestionLocale.objects.get(locale=settings.LANGUAGE_CODE) p.questions_locales.add(locale) TopicFactory(title="Fix problems", slug="fix-problems", product=p) q = QuestionFactory(product=p, title=u"CupcakesQuestion cupcakes") d = DocumentFactory(title=u"CupcakesKB cupcakes", category=10) d.products.add(p) RevisionFactory(document=d, is_approved=True) self.refresh() url = urlparams( reverse("questions.aaq_step4", args=["desktop", "fix-problems"]), search="cupcakes", ) response = self.client.get(url, follow=True) eq_(200, response.status_code) assert "CupcakesQuestion" in response.content assert "CupcakesKB" in response.content # Verify that archived articles and questions aren't shown... # Archive both and they shouldn't appear anymore. q.is_archived = True q.save() d.is_archived = True d.save() self.refresh() response = self.client.get(url, follow=True) eq_(200, response.status_code) assert "CupcakesQuestion" not in response.content assert "CupcakesKB" not in response.content
def test_document_listing_order(self): """Verify documents are sorted by display_order and number of helpful votes.""" # Create topic, product and documents. p = ProductFactory() t = TopicFactory(product=p) docs = [] # FIXME: Can't we do this with create_batch and build the document # in the approvedrevisionfactory for i in range(3): doc = DocumentFactory(products=[p], topics=[t]) ApprovedRevisionFactory(document=doc) docs.append(doc) # Add a lower display order to the second document. It should be first now. docs[1].display_order = 0 docs[1].save() self.refresh() url = reverse("products.documents", args=[p.slug, t.slug]) r = self.client.get(url, follow=True) eq_(200, r.status_code) doc = pq(r.content) eq_( doc("#document-list > ul > li:first-child > a").text(), docs[1].title) # Add a helpful vote to the third document. It should be second now. rev = docs[2].current_revision HelpfulVoteFactory(revision=rev, helpful=True) docs[2].save() # Votes don't trigger a reindex. self.refresh() cache.clear() # documents_for() is cached url = reverse("products.documents", args=[p.slug, t.slug]) r = self.client.get(url, follow=True) eq_(200, r.status_code) doc = pq(r.content) eq_( doc("#document-list > ul > li:nth-child(2) > a").text(), docs[2].title) # Add 2 helpful votes the first document. It should be second now. rev = docs[0].current_revision HelpfulVoteFactory(revision=rev, helpful=True) HelpfulVoteFactory(revision=rev, helpful=True) docs[0].save() # Votes don't trigger a reindex. self.refresh() cache.clear() # documents_for() is cached r = self.client.get(url, follow=True) eq_(200, r.status_code) doc = pq(r.content) eq_( doc("#document-list > ul > li:nth-child(2) > a").text(), docs[0].title)
def test_by_product(self): """Test the product filtering of the readout.""" locale = settings.WIKI_DEFAULT_LANGUAGE p = ProductFactory(title='Firefox', slug='firefox') d = DocumentFactory() ApprovedRevisionFactory(document=d) # There shouldn't be any rows yet. eq_(0, len(self.rows(locale=locale, product=p))) # Add the product to the document, and verify it shows up. d.products.add(p) eq_(self.row(locale=locale, product=p)['title'], d.title)
def test_preview_async(self): """Preview a reply.""" u = UserFactory() self.client.login(username=u.username, password="******") d = DocumentFactory() content = "Full of awesome." response = post( self.client, "wiki.discuss.post_preview_async", {"content": content}, args=[d.slug], ) eq_(200, response.status_code) doc = pq(response.content) eq_(content, doc("div.content").text())
def test_top_l10n(self): d = DocumentFactory(locale="es") r1 = RevisionFactory(document=d) r2 = RevisionFactory(document=d) self.refresh() response = self.client.get(urlparams(reverse("community.top_contributors", args=["l10n"]))) eq_(200, response.status_code) doc = pq(response.content) eq_(2, len(doc("li.results-user"))) assert str(r1.creator.username) in response.content assert str(r2.creator.username) in response.content
def test_fire_on_new_thread(self, fire): """The event fires when there is a new thread.""" d = DocumentFactory() u = UserFactory() self.client.login(username=u.username, password='******') post(self.client, 'wiki.discuss.new_thread', { 'title': 'a title', 'content': 'a post' }, args=[d.slug]) # NewThreadEvent.fire() is called. assert fire.called
def test_search_suggestions_questions(self): """Verifies the view doesn't kick up an HTTP 500""" p = ProductFactory(slug=u'firefox') l = QuestionLocale.objects.get(locale=settings.LANGUAGE_CODE) p.questions_locales.add(l) TopicFactory(title='Fix problems', slug='fix-problems', product=p) q = QuestionFactory(product=p, title=u'CupcakesQuestion cupcakes') d = DocumentFactory(title=u'CupcakesKB cupcakes', category=10) d.products.add(p) RevisionFactory(document=d, is_approved=True) self.refresh() url = urlparams( reverse('questions.aaq_step4', args=['desktop', 'fix-problems']), search='cupcakes') response = self.client.get(url, follow=True) eq_(200, response.status_code) assert 'CupcakesQuestion' in response.content assert 'CupcakesKB' in response.content # Verify that archived articles and questions aren't shown... # Archive both and they shouldn't appear anymore. q.is_archived = True q.save() d.is_archived = True d.save() self.refresh() response = self.client.get(url, follow=True) eq_(200, response.status_code) assert 'CupcakesQuestion' not in response.content assert 'CupcakesKB' not in response.content
def test_top_kb(self): d = DocumentFactory(locale='en-US') r1 = RevisionFactory(document=d) r2 = RevisionFactory(document=d) self.refresh() response = self.client.get(urlparams(reverse('community.top_contributors', args=['kb']))) eq_(200, response.status_code) doc = pq(response.content) eq_(2, len(doc('li.results-user'))) assert str(r1.creator.username) in response.content assert str(r2.creator.username) in response.content
def test_only_templates(self): """Test that only templates are shown""" locale = settings.WIKI_DEFAULT_LANGUAGE p = ProductFactory(title='Firefox', slug='firefox') d = DocumentFactory(products=[p]) t = TemplateDocumentFactory(products=[p]) ApprovedRevisionFactory(document=d) ApprovedRevisionFactory(document=TemplateDocumentFactory()) eq_(1, len(self.rows(locale=locale, product=p))) eq_(t.title, self.row(locale=locale, product=p)['title']) eq_('', self.row(locale=locale, product=p)['status'])
def test_template_rendered_non_en_US_locale(self, mocked_experiments_flag, mocked_switch): """Test that the default template is used for a non en-US locale.""" mocked_switch.return_value = True mocked_experiments_flag.return_value = True ProductFactory() doc = DocumentFactory(slug='insecure-password-warning-firefox') url = reverse('wiki.document', args=[doc.slug], locale='es') client = Client(HTTP_USER_AGENT='Mozilla/5.0') response = client.get(url, follow=True) eq_(response.status_code, 200) assert template_used(response, 'wiki/document.html')
def test_by_product(self): """Test the product filtering of the readout.""" p = ProductFactory(title='Firefox', slug='firefox') d = DocumentFactory(title='Foo', category=CANNED_RESPONSES_CATEGORY) ApprovedRevisionFactory(document=d, is_ready_for_localization=True) # There shouldn't be any rows yet. eq_(0, len(self.rows(product=p))) # Add the product to the document, and verify it shows up. d.products.add(p) eq_(1, len(self.rows(product=p))) eq_(self.row(product=p)['title'], d.title)
def test_filter_by_doctype(self): desktop = ProductFactory(slug="desktop") ques = QuestionFactory(title="audio", product=desktop) ans = AnswerFactory(question=ques, content="volume") AnswerVoteFactory(answer=ans, helpful=True) doc = DocumentFactory(title="audio", locale="en-US", category=10, products=[desktop]) RevisionFactory(document=doc, is_approved=True) doc = DocumentFactory(title="audio too", locale="en-US", category=10, products=[desktop]) RevisionFactory(document=doc, is_approved=True) self.refresh() # There should be 2 results for kb (w=1) and 1 for questions (w=2). response = self.client.get(reverse("search"), { "q": "audio", "format": "json", "w": "1" }) eq_(200, response.status_code) content = json.loads(response.content) eq_(content["total"], 2) response = self.client.get(reverse("search"), { "q": "audio", "format": "json", "w": "2" }) eq_(200, response.status_code) content = json.loads(response.content) eq_(content["total"], 1)
def test_filter_by_doctype(self): desktop = ProductFactory(slug=u'desktop') ques = QuestionFactory(title=u'audio', product=desktop) ans = AnswerFactory(question=ques, content=u'volume') AnswerVoteFactory(answer=ans, helpful=True) doc = DocumentFactory(title=u'audio', locale=u'en-US', category=10, products=[desktop]) RevisionFactory(document=doc, is_approved=True) doc = DocumentFactory(title=u'audio too', locale=u'en-US', category=10, products=[desktop]) RevisionFactory(document=doc, is_approved=True) self.refresh() # There should be 2 results for kb (w=1) and 1 for questions (w=2). response = self.client.get(reverse('search'), { 'q': 'audio', 'format': 'json', 'w': '1' }) eq_(200, response.status_code) content = json.loads(response.content) eq_(content['total'], 2) response = self.client.get(reverse('search'), { 'q': 'audio', 'format': 'json', 'w': '2' }) eq_(200, response.status_code) content = json.loads(response.content) eq_(content['total'], 1)
def test_search_products(self): p = ProductFactory(title=u'Product One', slug='product') doc = DocumentFactory(title=u'cookies', locale='en-US', category=10, products=[p]) RevisionFactory(document=doc, is_approved=True) self.refresh() response = self.client.get( reverse('search.advanced'), {'a': '1', 'product': 'product', 'q': 'cookies', 'w': '1'}) assert "We couldn't find any results for" not in response.content eq_(200, response.status_code) assert 'Product One' in response.content
def test_edit_post(self): """Changing post content works.""" u = UserFactory() self.client.login(username=u.username, password='******') d = DocumentFactory() t = ThreadFactory(document=d) p = t.new_post(creator=u, content='foo') post(self.client, 'wiki.discuss.edit_post', {'content': 'Some new content'}, args=[d.slug, t.id, p.id]) edited_p = t.post_set.get(pk=p.id) eq_('Some new content', edited_p.content)
def test_fallback_for_zero_results(self): """If there are no results, fallback to a list of top articles.""" firefox = ProductFactory(title="firefox", slug="desktop") doc = DocumentFactory(title="audio1", locale="en-US", category=10, products=[firefox]) RevisionFactory(document=doc, is_approved=True) doc = DocumentFactory(title="audio2", locale="en-US", category=10, products=[firefox]) RevisionFactory(document=doc, is_approved=True) self.refresh() # Verify there are no real results but 2 fallback results are rendered response = self.client.get(reverse("search"), {"q": "piranha"}) eq_(200, response.status_code) assert b"We couldn't find any results for" in response.content doc = pq(response.content) eq_(2, len(doc("#search-results .result")))
def test_page_invalid(self): """Ensure non-integer param doesn't throw exception.""" doc = DocumentFactory( title="How to fix your audio", locale="en-US", category=10, tags="desktop" ) ApprovedRevisionFactory(document=doc) self.refresh() response = self.client.get( reverse("search"), {"q": "audio", "format": "json", "page": "invalid"} ) eq_(200, response.status_code) eq_(1, json.loads(response.content)["total"])
def test_translations_get_parent_tags(self): t1 = TopicFactory(display_order=1) t2 = TopicFactory(display_order=2) p = ProductFactory() doc1 = DocumentFactory(title='Audio too loud', products=[p], topics=[t1, t2]) RevisionFactory(document=doc1, is_approved=True) doc2 = DocumentFactory(title='Audio too loud bork bork', parent=doc1, tags=['badtag']) RevisionFactory(document=doc2, is_approved=True) # Verify the parent has the right tags. doc_dict = DocumentMappingType.extract_document(doc1.id) eq_(sorted(doc_dict['topic']), sorted([t1.slug, t2.slug])) eq_(doc_dict['product'], [p.slug]) # Verify the translation has the parent's tags. doc_dict = DocumentMappingType.extract_document(doc2.id) eq_(sorted(doc_dict['topic']), sorted([t1.slug, t2.slug])) eq_(doc_dict['product'], [p.slug])
def test_fallback_for_zero_results(self): """If there are no results, fallback to a list of top articles.""" firefox = ProductFactory(title=u'firefox', slug=u'desktop') doc = DocumentFactory(title=u'audio1', locale=u'en-US', category=10, products=[firefox]) RevisionFactory(document=doc, is_approved=True) doc = DocumentFactory(title=u'audio2', locale=u'en-US', category=10, products=[firefox]) RevisionFactory(document=doc, is_approved=True) self.refresh() # Verify there are no real results but 2 fallback results are rendered response = self.client.get(reverse('search'), {'q': 'piranha'}) eq_(200, response.status_code) assert "We couldn't find any results for" in response.content doc = pq(response.content) eq_(2, len(doc('#search-results .result')))
def test_empty_thread_errors(self): """Posting an empty thread shows errors.""" u = UserFactory() self.client.login(username=u.username, password="******") d = DocumentFactory() response = post( self.client, "wiki.discuss.new_thread", {"title": "", "content": ""}, args=[d.slug], ) doc = pq(response.content) errors = doc("ul.errorlist li a") eq_(errors[0].text, "Please provide a title.") eq_(errors[1].text, "Please provide a message.")
def test_by_product(self): """Test the product filtering of the readout.""" p = ProductFactory(title='Firefox', slug='firefox') d = DocumentFactory( needs_change=True, needs_change_comment='Please update for Firefox.next') RevisionFactory(document=d) # There shouldn't be any rows yet. eq_(0, len(self.rows(product=p))) # Add the product to the document, and verify it shows up. d.products.add(p) eq_(self.row(product=p)['title'], d.title)
def test_search(self, _out): """Test that es_search command doesn't fail""" call_command("essearch", "cupcakes") p = ProductFactory(title="firefox", slug="desktop") doc = DocumentFactory(title="cupcakes rock", locale="en-US", category=10, products=[p]) RevisionFactory(document=doc, is_approved=True) self.refresh() call_command("essearch", "cupcakes")
def test_watch_forum_then_new_post_as_self(self, get_current): """Watching a forum and replying as myself should not send email.""" get_current.return_value.domain = 'testserver' u = UserFactory() d = DocumentFactory(title='an article title') f = self._toggle_watch_kbforum_as(u.username, d, turn_on=True) t = ThreadFactory(document=d) self.client.login(username=u.username, password='******') post(self.client, 'wiki.discuss.reply', {'content': 'a post'}, args=[f.slug, t.id]) # Assert no email is sent. assert not mail.outbox
def test_unicode(self): """Unicode is hard. Test that.""" # \u03C0 is pi and \u2764 is a heart symbol. d1 = DocumentFactory(title=u'\u03C0', slug='pi') ApprovedRevisionFactory(document=d1, content=u'I \u2764 \u03C0') d2 = DocumentFactory(title=u'\u2764', slug='heart') ApprovedRevisionFactory(document=d2, content=u'What do you think about [[\u03C0]]?') eq_(len(d1.links_to()), 1) eq_(len(d1.links_from()), 0) eq_(len(d2.links_to()), 0) eq_(len(d2.links_from()), 1) eq_(d1.links_to()[0].kind, 'link')
def test_old_revisions(self): """Bug 862436. Updating old revisions could cause bad WLH data.""" d1 = DocumentFactory(title='D1') RevisionFactory(document=d1, content='', is_approved=True) d2 = DocumentFactory(title='D2') RevisionFactory(document=d2, content='', is_approved=True) # Make D3, then make a revision that links to D1, then a # revision that links to D2. Only the link to D2 should count. d3 = DocumentFactory(title='D3') r3_old = ApprovedRevisionFactory(document=d3, content='[[D1]]') ApprovedRevisionFactory(document=d3, content='[[D2]]') # This could cause stale data r3_old.content_parsed # D1 is not linked to in any current revisions. eq_(len(d1.links_to()), 0) eq_(len(d1.links_from()), 0) eq_(len(d2.links_to()), 1) eq_(len(d2.links_from()), 0) eq_(len(d3.links_to()), 0) eq_(len(d3.links_from()), 1)
def test_template_title_and_category_to_template(self): d = DocumentFactory() # First, try and change just the title. It should fail. d.title = TEMPLATE_TITLE_PREFIX + d.title self.assertRaises(ValidationError, d.save) # Next, try and change just the category. It should also fail. d = Document.objects.get(id=d.id) # reset d.category = TEMPLATES_CATEGORY self.assertRaises(ValidationError, d.save) # Finally, try and change both title and category. It should work. d = Document.objects.get(id=d.id) # reset d.title = TEMPLATE_TITLE_PREFIX + d.title d.category = TEMPLATES_CATEGORY d.save()
def test_template_title_and_category_localized(self): # Because localized articles are required to match templates with their # parents, this deserves extra testing. d_en = DocumentFactory() d_fr = DocumentFactory(parent=d_en, locale='fr') # Just changing the title isn't enough d_fr.title = TEMPLATE_TITLE_PREFIX + d_fr.title self.assertRaises(ValidationError, d_fr.save) # Trying to change the category won't work, since `d_en` will force the # old category. d_fr = Document.objects.get(id=d_fr.id) # reset d_fr.title = TEMPLATE_TITLE_PREFIX + d_fr.title d_fr.category = TEMPLATES_CATEGORY self.assertRaises(ValidationError, d_fr.save) # Change the parent d_en.title = TEMPLATE_TITLE_PREFIX + d_en.title d_en.category = TEMPLATES_CATEGORY d_en.save() # Now the French article can be changed too. d_fr = Document.objects.get(id=d_fr.id) # reset d_fr.title = TEMPLATE_TITLE_PREFIX + d_fr.title d_fr.category = TEMPLATES_CATEGORY d_fr.save()
def test_none(self): """If there are no revisions, return None.""" d = DocumentFactory() eq_(None, d.localizable_or_latest_revision())
def _test_remembering_setter_unsaved(self, field): """A remembering setter shouldn't kick in until the doc is saved.""" old_field = 'old_' + field d = DocumentFactory.build() setattr(d, field, 'Foo') assert not hasattr(d, old_field), "Doc shouldn't have %s until it's saved." % old_field