def test_wiki_topics(self): """Search wiki for topics, includes multiple.""" t1 = TopicFactory(slug="doesnotexist") t2 = TopicFactory(slug="extant") t3 = TopicFactory(slug="tagged") doc = DocumentFactory(locale="en-US", category=10) doc.topics.add(t2) RevisionFactory(document=doc, is_approved=True) doc = DocumentFactory(locale="en-US", category=10) doc.topics.add(t2) doc.topics.add(t3) RevisionFactory(document=doc, is_approved=True) self.refresh() topic_vals = ( (t1.slug, 0), (t2.slug, 2), (t3.slug, 1), ([t2.slug, t3.slug], 1), ) qs = {"a": 1, "w": 1, "format": "json"} for topics, number in topic_vals: qs.update({"topics": topics}) response = self.client.get(reverse("search.advanced"), qs) eq_(number, json.loads(response.content)["total"])
def test_subtopics(self): """Verifies subtopics appear on document listing page.""" # Create a topic and product. p = ProductFactory() t = TopicFactory(product=p, visible=True) # Create a documents with the topic and product doc = DocumentFactory(products=[p], topics=[t]) ApprovedRevisionFactory(document=doc) self.refresh() # GET the page and verify no subtopics yet. url = reverse("products.documents", args=[p.slug, t.slug]) r = self.client.get(url, follow=True) eq_(200, r.status_code) pqdoc = pq(r.content) eq_(0, len(pqdoc("li.subtopic"))) # Create a subtopic, it still shouldn't show up because no # articles are assigned. subtopic = TopicFactory(parent=t, product=p, visible=True) r = self.client.get(url, follow=True) eq_(200, r.status_code) pqdoc = pq(r.content) eq_(0, len(pqdoc("li.subtopic"))) # Add a document to the subtopic, now it should appear. doc.topics.add(subtopic) self.refresh() r = self.client.get(url, follow=True) eq_(200, r.status_code) pqdoc = pq(r.content) eq_(1, len(pqdoc("li.subtopic")))
def test_expected_output(self): p = ProductFactory() t1 = TopicFactory(product=p, visible=True, display_order=1) t2 = TopicFactory(product=p, visible=True, display_order=2) url = reverse('topic-list', kwargs={'product': p.slug}) res = self.client.get(url) eq_(res.status_code, 200) eq_( res.data, { 'count': 2, 'next': None, 'previous': None, 'results': [ { 'title': t1.title, 'slug': t1.slug, }, { 'title': t2.title, 'slug': t2.slug, }, ], })
def test_absolute_url_subtopic(self): p = ProductFactory() t1 = TopicFactory(product=p) t2 = TopicFactory(parent=t1, product=p) expected = '/products/{p}/{t1}/{t2}'.format(p=p.slug, t1=t1.slug, t2=t2.slug) actual = t2.get_absolute_url() eq_(actual, expected)
def test_topic_delete(self): RevisionFactory(document=self.document, is_approved=True) topic = TopicFactory() self.document.topics.add(topic) topic.delete() self.assertEqual(self.get_doc().topic_ids, [])
def test_wiki_topics(self): """Search wiki for topics, includes multiple.""" t1 = TopicFactory(slug='doesnotexist') t2 = TopicFactory(slug='extant') t3 = TopicFactory(slug='tagged') doc = DocumentFactory(locale=u'en-US', category=10) doc.topics.add(t2) RevisionFactory(document=doc, is_approved=True) doc = DocumentFactory(locale=u'en-US', category=10) doc.topics.add(t2) doc.topics.add(t3) RevisionFactory(document=doc, is_approved=True) self.refresh() topic_vals = ( (t1.slug, 0), (t2.slug, 2), (t3.slug, 1), ([t2.slug, t3.slug], 1), ) qs = {'a': 1, 'w': 1, 'format': 'json'} for topics, number in topic_vals: qs.update({'topics': topics}) response = self.client.get(reverse('search.advanced'), qs) eq_(number, json.loads(response.content)['total'])
def test_expected_output(self): p = ProductFactory() t1 = TopicFactory(product=p, visible=True, display_order=1) t2 = TopicFactory(product=p, visible=True, display_order=2) url = reverse("topic-list", kwargs={"product": p.slug}) res = self.client.get(url) eq_(res.status_code, 200) eq_( res.data, { "count": 2, "next": None, "previous": None, "results": [ { "title": t1.title, "slug": t1.slug, }, { "title": t2.title, "slug": t2.slug, }, ], }, )
def test_absolute_url_subtopic(self): p = ProductFactory() t1 = TopicFactory(product=p) t2 = TopicFactory(parent=t1, product=p) expected = "/products/{p}/{t1}/{t2}".format(p=p.slug, t1=t1.slug, t2=t2.slug) actual = t2.get_absolute_url() eq_(actual, expected)
def test_path(self): """Verify that the path property works.""" p = ProductFactory(slug="p") t1 = TopicFactory(product=p, slug="t1") t2 = TopicFactory(product=p, slug="t2", parent=t1) t3 = TopicFactory(product=p, slug="t3", parent=t2) eq_(t1.path, [t1.slug]) eq_(t2.path, [t1.slug, t2.slug]) eq_(t3.path, [t1.slug, t2.slug, t3.slug])
def test_only_marked_topics(self): t1 = TopicFactory(in_aaq=True) TopicFactory(in_aaq=False) url = reverse("questions.aaq_step1") response = self.client.get(url, follow=True) doc = pq(response.content) topics = json.loads(doc(".data[name=topics]").text()) eq_(len(topics), 1) eq_(topics[0]["id"], t1.id)
def test_locale_filter(self): """Only questions for the current locale should be shown on the questions front page for AAQ locales.""" eq_(Question.objects.count(), 0) p = ProductFactory(slug=u'firefox') TopicFactory(title='Fix problems', slug='fix-problems', product=p) QuestionFactory(title='question cupcakes?', product=p, locale='en-US') QuestionFactory(title='question donuts?', product=p, locale='en-US') QuestionFactory(title='question pies?', product=p, locale='pt-BR') QuestionFactory(title='question pastries?', product=p, locale='de') def sub_test(locale, *titles): url = urlparams( reverse('questions.list', args=['all'], locale=locale)) response = self.client.get(url, follow=True) doc = pq(response.content) eq_msg(len(doc('section[id^=question]')), len(titles), 'Wrong number of results for {0}'.format(locale)) for substr in titles: assert substr in doc('.questions section .content h2 a').text() # en-US and pt-BR are both in AAQ_LANGUAGES, so should be filtered. sub_test('en-US', 'cupcakes?', 'donuts?') sub_test('pt-BR', 'pies?') # de is not in AAQ_LANGUAGES, so should show en-US, but not pt-BR sub_test('de', 'cupcakes?', 'donuts?', 'pastries?')
def test_search_suggestion_questions_locale(self): """Verifies the right languages show up in search suggestions.""" QuestionLocaleFactory(locale='de') p = ProductFactory(slug=u'firefox') for l in QuestionLocale.objects.all(): p.questions_locales.add(l) TopicFactory(title='Fix problems', slug='fix-problems', product=p) QuestionFactory(title='question cupcakes?', product=p, locale='en-US') QuestionFactory(title='question donuts?', product=p, locale='en-US') QuestionFactory(title='question pies?', product=p, locale='pt-BR') QuestionFactory(title='question pastries?', product=p, locale='de') self.refresh() def sub_test(locale, *titles): url = urlparams(reverse('questions.aaq_step4', args=['desktop', 'fix-problems'], locale=locale), search='question') response = self.client.get(url, follow=True) doc = pq(response.content) eq_msg(len(doc('.result.question')), len(titles), 'Wrong number of results for {0}'.format(locale)) for substr in titles: assert substr in doc('.result.question h3 a').text() sub_test('en-US', 'cupcakes?', 'donuts?') sub_test('pt-BR', 'cupcakes?', 'donuts?', 'pies?') sub_test('de', 'cupcakes?', 'donuts?', 'pastries?')
def test_locale_filter(self): """Only questions for the current locale should be shown on the questions front page for AAQ locales.""" eq_(Question.objects.count(), 0) p = ProductFactory(slug=u"firefox") TopicFactory(title="Fix problems", slug="fix-problems", product=p) QuestionFactory(title="question cupcakes?", product=p, locale="en-US") QuestionFactory(title="question donuts?", product=p, locale="en-US") QuestionFactory(title="question pies?", product=p, locale="pt-BR") QuestionFactory(title="question pastries?", product=p, locale="de") def sub_test(locale, *titles): url = urlparams( reverse("questions.list", args=["all"], locale=locale)) response = self.client.get(url, follow=True) doc = pq(response.content) eq_msg( len(doc("section[id^=question]")), len(titles), "Wrong number of results for {0}".format(locale), ) for substr in titles: assert substr in doc(".questions section .content h2 a").text() # en-US and pt-BR are both in AAQ_LANGUAGES, so should be filtered. sub_test("en-US", "cupcakes?", "donuts?") sub_test("pt-BR", "pies?") # de is not in AAQ_LANGUAGES, so should show en-US, but not pt-BR sub_test("de", "cupcakes?", "donuts?", "pastries?")
def test_bleaching(self): """Tests whether summaries are bleached""" 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) QuestionFactory( product=p, title=u"CupcakesQuestion cupcakes", content=u"cupcakes are best with <unbleached>flour</unbleached>", ) 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 "<unbleached>" not in response.content assert "cupcakes are best with" in response.content
def test_ratelimit(self): """Make sure posting new questions is ratelimited""" data = { "title": "A test question", "content": "I have this question that I hope...", "sites_affected": "http://example.com", "ff_version": "3.6.6", "os": "Intel Mac OS X 10.6", "plugins": "* Shockwave Flash 10.1 r53", "useragent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X " "10.6; en-US; rv:1.9.2.6) Gecko/20100625 " "Firefox/3.6.6", } p = ProductFactory(slug="firefox") locale = QuestionLocale.objects.get(locale=settings.LANGUAGE_CODE) p.questions_locales.add(locale) TopicFactory(slug="fix-problems", product=p) url = urlparams( reverse("questions.aaq_step5", args=["desktop", "fix-problems"]), search="A test question", ) u = UserFactory() self.client.login(username=u.username, password="******") for i in range(0, 5): self.client.post(url, data, follow=True) response = self.client.post(url, data, follow=True) eq_(403, response.status_code)
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_topics_change(self): topic = TopicFactory() self.document.topics.add(topic) self.assertIn(topic.id, self.get_doc().topic_ids) self.document.topics.remove(topic) self.assertEqual(None, self.get_doc().topic_ids)
def test_topic_disambiguation(self): # First make another product, and a colliding topic. # It has the same slug, but a different product. new_product = ProductFactory() TopicFactory(product=new_product, slug=self.topic.slug) serializer = api.QuestionSerializer( context=self.context, data=self.data) serializer.is_valid(raise_exception=True) obj = serializer.save() eq_(obj.topic, self.topic)
def test_topics_change(self): topic = TopicFactory() RevisionFactory(document=self.document, is_approved=True) self.document.topics.add(topic) self.assertIn(topic.id, self.get_doc().topic_ids) self.document.topics.remove(topic) self.assertEqual(None, self.get_doc().topic_ids)
def _new_question(self, post_it=False): """Post a new question and return the response.""" p = ProductFactory(slug='mobile') l = QuestionLocale.objects.get(locale=settings.LANGUAGE_CODE) p.questions_locales.add(l) t = TopicFactory(slug='fix-problems', product=p) url = urlparams(reverse('questions.aaq_step5', args=[p.slug, t.slug]), search='A test question') if post_it: return self.client.post(url, self.data, follow=True) return self.client.get(url, follow=True)
def setUp(self): super(TestFacetHelpers, self).setUp() # Create products self.desktop = ProductFactory(slug="firefox") self.mobile = ProductFactory(slug="mobile") # Create topics self.general_d = TopicFactory(product=self.desktop, slug="general") self.bookmarks_d = TopicFactory(product=self.desktop, slug="bookmarks") self.sync_d = TopicFactory(product=self.desktop, slug="sync") self.general_m = TopicFactory(product=self.mobile, slug="general") self.bookmarks_m = TopicFactory(product=self.mobile, slug="bookmarks") self.sync_m = TopicFactory(product=self.mobile, slug="sync") # Set up documents. doc1 = DocumentFactory(products=[self.desktop], topics=[self.general_d, self.bookmarks_d]) doc1_revision = ApprovedRevisionFactory(document=doc1, is_ready_for_localization=True) doc1_localized = DocumentFactory(locale="de", products=[], topics=[], parent=doc1) ApprovedRevisionFactory(document=doc1_localized, based_on=doc1_revision) doc2 = DocumentFactory( products=[self.desktop, self.mobile], topics=[ self.bookmarks_d, self.bookmarks_m, self.sync_d, self.sync_m ], ) ApprovedRevisionFactory(document=doc2) # An archived article shouldn't show up doc3 = DocumentFactory(is_archived=True, products=[self.desktop], topics=[self.general_d, self.bookmarks_d]) ApprovedRevisionFactory(document=doc3) # A template article shouldn't show up either doc4 = TemplateDocumentFactory( products=[self.desktop], topics=[self.general_d, self.bookmarks_d]) ApprovedRevisionFactory(document=doc4) # An article without current revision should be "invisible" # to everything. doc5 = DocumentFactory( products=[self.desktop, self.mobile], topics=[ self.general_d, self.bookmarks_d, self.sync_d, self.general_m, self.bookmarks_m, self.sync_m, ], ) RevisionFactory(is_approved=False, document=doc5)
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_bad_data(self): """Test for bad data""" data = { "product": ProductFactory().id, "topic": TopicFactory().id, "locale": self.question.locale, } response = self._request(data=data) eq_(400, response.status_code) data = {"product": self.product.id, "topic": self.topic.id, "locale": "zu"} response = self._request(data=data) eq_(400, response.status_code)
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_search_suggestion_questions_locale(self): """Verifies the right languages show up in search suggestions.""" QuestionLocaleFactory(locale="de") product = ProductFactory(slug="firefox") for loc in QuestionLocale.objects.all(): product.questions_locales.add(loc) TopicFactory(title="Fix problems", slug="fix-problems", product=product) QuestionFactory(title="question cupcakes?", product=product, locale="en-US") QuestionFactory(title="question donuts?", product=product, locale="en-US") QuestionFactory(title="question pies?", product=product, locale="pt-BR") QuestionFactory(title="question pastries?", product=product, locale="de") self.refresh() def sub_test(locale, *titles): url = urlparams( reverse( "questions.aaq_step4", args=["desktop", "fix-problems"], locale=locale, ), search="question", ) response = self.client.get(url, follow=True) doc = pq(response.content) eq_msg( len(doc(".result.question")), len(titles), "Wrong number of results for {0}".format(locale), ) for substr in titles: assert substr in doc(".result.question h3 a").text() sub_test("en-US", "cupcakes?", "donuts?") sub_test("pt-BR", "cupcakes?", "donuts?", "pies?") sub_test("de", "cupcakes?", "donuts?", "pastries?")
def setUp(self): u = UserFactory() add_permission(u, Question, "change_question") assert u.has_perm("questions.change_question") self.user = u p = ProductFactory() t = TopicFactory(product=p) q = QuestionFactory(product=p, topic=t) self.product = p self.topic = t self.question = q
def test_wiki_topics_inherit(self): """Translations inherit topics from their parents.""" doc = DocumentFactory(locale=u'en-US', category=10) doc.topics.add(TopicFactory(slug='extant')) RevisionFactory(document=doc, is_approved=True) translated = DocumentFactory(locale=u'es', parent=doc, category=10) RevisionFactory(document=translated, is_approved=True) self.refresh() qs = {'a': 1, 'w': 1, 'format': 'json', 'topics': 'extant'} response = self.client.get(reverse('search.advanced', locale='es'), qs) eq_(1, json.loads(response.content)['total'])
def test_wiki_topics_inherit(self): """Translations inherit topics from their parents.""" doc = DocumentFactory(locale="en-US", category=10) doc.topics.add(TopicFactory(slug="extant")) RevisionFactory(document=doc, is_approved=True) translated = DocumentFactory(locale="es", parent=doc, category=10) RevisionFactory(document=translated, is_approved=True) self.refresh() qs = {"a": 1, "w": 1, "format": "json", "topics": "extant"} response = self.client.get(reverse("search.advanced", locale="es"), qs) eq_(1, json.loads(response.content)["total"])
def test_product_landing(self): """Verify that /products/<slug> page renders topics.""" # Create a product. p = ProductFactory() locale = QuestionLocale.objects.get(locale=settings.LANGUAGE_CODE) p.questions_locales.add(locale) # Create some topics. TopicFactory(slug=HOT_TOPIC_SLUG, product=p, visible=True) topics = TopicFactory.create_batch(11, product=p, visible=True) # Create a document and assign the product and 10 topics. d = DocumentFactory(products=[p], topics=topics[:10]) ApprovedRevisionFactory(document=d) self.refresh() # GET the product landing page and verify the content. url = reverse("products.product", args=[p.slug]) r = self.client.get(url, follow=True) eq_(200, r.status_code) doc = pq(r.content) eq_(11, len(doc("#help-topics li"))) eq_(p.slug, doc("#support-search input[name=product]").attr["value"])
def new_document_data(topic_ids=None, product_ids=None): product_ids = product_ids or [ProductFactory().id] p = Product.objects.get(id=product_ids[0]) topic_ids = topic_ids or [TopicFactory(product=p).id] return { 'title': 'A Test Article', 'slug': 'a-test-article', 'locale': 'en-US', 'topics': topic_ids, 'products': product_ids, 'category': CATEGORIES[0][0], 'keywords': 'key1, key2', 'summary': 'lipsum', 'content': 'lorem ipsum dolor sit amet', }
def test_question_topics(self): """Search questions for topics.""" p = ProductFactory() t1 = TopicFactory(slug="doesnotexist", product=p) t2 = TopicFactory(slug="cookies", product=p) t3 = TopicFactory(slug="sync", product=p) QuestionFactory(topic=t2) QuestionFactory(topic=t2) QuestionFactory(topic=t3) self.refresh() topic_vals = ( (t1.slug, 0), (t2.slug, 2), (t3.slug, 1), ) qs = {"a": 1, "w": 2, "format": "json"} for topics, number in topic_vals: qs.update({"topics": topics}) response = self.client.get(reverse("search.advanced"), qs) eq_(number, json.loads(response.content)["total"])
def test_product_landing(self): """Verify that /products/<slug> page renders topics.""" # Create a product. p = ProductFactory() l = QuestionLocale.objects.get(locale=settings.LANGUAGE_CODE) p.questions_locales.add(l) # Create some topics. TopicFactory(slug=HOT_TOPIC_SLUG, product=p, visible=True) topics = TopicFactory.create_batch(11, product=p, visible=True) # Create a document and assign the product and 10 topics. d = DocumentFactory(products=[p], topics=topics[:10]) ApprovedRevisionFactory(document=d) self.refresh() # GET the product landing page and verify the content. url = reverse('products.product', args=[p.slug]) r = self.client.get(url, follow=True) eq_(200, r.status_code) doc = pq(r.content) eq_(11, len(doc('#help-topics li'))) eq_(p.slug, doc('#support-search input[name=product]').attr['value'])
def test_absolute_url(self): p = ProductFactory() t = TopicFactory(product=p) expected = '/products/{p}/{t}'.format(p=p.slug, t=t.slug) actual = t.get_absolute_url() eq_(actual, expected)