def test_sortby_documents_helpful(self): """Tests advanced search with a sortby_documents by helpful""" r1 = RevisionFactory(is_approved=True) r2 = RevisionFactory(is_approved=True) HelpfulVoteFactory(revision=r2, helpful=True) # Note: We have to wipe and rebuild the index because new # helpful_votes don't update the index data. self.setup_indexes() self.reindex_and_refresh() # r2.document should come first with 1 vote. response = self.client.get(reverse('search.advanced'), { 'w': '1', 'a': '1', 'sortby_documents': 'helpful', 'format': 'json' }) eq_(200, response.status_code) content = json.loads(response.content) eq_(r2.document.title, content['results'][0]['title']) # Vote twice on r1, now it should come first. HelpfulVoteFactory(revision=r1, helpful=True) HelpfulVoteFactory(revision=r1, helpful=True) self.setup_indexes() self.reindex_and_refresh() response = self.client.get(reverse('search.advanced'), { 'w': '1', 'a': '1', 'sortby_documents': 'helpful', 'format': 'json'}) eq_(200, response.status_code) content = json.loads(response.content) eq_(r1.document.title, content['results'][0]['title'])
def test_recent_helpful_votes(self): """Recent helpful votes are indexed properly.""" # Create a document and verify it doesn't show up in a # query for recent_helpful_votes__gt=0. r = RevisionFactory(is_approved=True) self.refresh() eq_( DocumentMappingType.search().filter( document_recent_helpful_votes__gt=0).count(), 0) # Add an unhelpful vote, it still shouldn't show up. HelpfulVoteFactory(revision=r, helpful=False) r.document.save() # Votes don't trigger a reindex. self.refresh() eq_( DocumentMappingType.search().filter( document_recent_helpful_votes__gt=0).count(), 0) # Add an helpful vote created 31 days ago, it still shouldn't show up. created = datetime.now() - timedelta(days=31) HelpfulVoteFactory(revision=r, helpful=True, created=created) r.document.save() # Votes don't trigger a reindex. self.refresh() eq_( DocumentMappingType.search().filter( document_recent_helpful_votes__gt=0).count(), 0) # Add an helpful vote created 29 days ago, it should show up now. created = datetime.now() - timedelta(days=29) HelpfulVoteFactory(revision=r, helpful=True, created=created) r.document.save() # Votes don't trigger a reindex. self.refresh() eq_( DocumentMappingType.search().filter( document_recent_helpful_votes__gt=0).count(), 1)
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_kb_vote(self): """Test vote API call.""" r1 = RevisionFactory(document__locale='en-US') r2 = RevisionFactory(document__locale='es') r3 = RevisionFactory(document__locale='es') for r in [r1, r2, r3]: HelpfulVoteFactory(revision=r) HelpfulVoteFactory(revision=r) HelpfulVoteFactory(revision=r, helpful=True) # Assign 2 documents to Firefox OS and 1 to Firefox firefox_os = ProductFactory(slug='firefox-os') firefox = ProductFactory(slug='firefox') r1.document.products.add(firefox_os) r2.document.products.add(firefox_os) r3.document.products.add(firefox) # All votes should be counted if we don't specify a locale r = self._get_api_result('api.kpi.kb-votes') eq_(r['objects'][0]['kb_helpful'], 3) eq_(r['objects'][0]['kb_votes'], 9) # Only en-US votes: r = self._get_api_result('api.kpi.kb-votes', locale='en-US') eq_(r['objects'][0]['kb_helpful'], 1) eq_(r['objects'][0]['kb_votes'], 3) # Only es votes: r = self._get_api_result('api.kpi.kb-votes', locale='es') eq_(r['objects'][0]['kb_helpful'], 2) eq_(r['objects'][0]['kb_votes'], 6) # Only Firefox OS votes: r = self._get_api_result('api.kpi.kb-votes', product='firefox-os') eq_(r['objects'][0]['kb_helpful'], 2) eq_(r['objects'][0]['kb_votes'], 6) # Only Firefox votes: r = self._get_api_result('api.kpi.kb-votes', product='firefox') eq_(r['objects'][0]['kb_helpful'], 1) eq_(r['objects'][0]['kb_votes'], 3) # Only Firefox OS + es votes: r = self._get_api_result('api.kpi.kb-votes', product='firefox-os', locale='es') eq_(r['objects'][0]['kb_helpful'], 1) eq_(r['objects'][0]['kb_votes'], 3)
def test_unhelpful_survey(self): """The unhelpful survey is stored as vote metadata""" vote = HelpfulVoteFactory() url = reverse("wiki.unhelpful_survey") data = { "vote_id": vote.id, "button": "Submit", "confusing": 1, "too-long": 1, "comment": "lorem ipsum dolor", } response = self.client.post(url, data) eq_(200, response.status_code) eq_(b'{"message": "Thanks for making us better!"}', response.content) vote_meta = vote.metadata.all() eq_(1, len(vote_meta)) eq_("survey", vote_meta[0].key) survey = json.loads(vote_meta[0].value) eq_(3, len(list(survey.keys()))) assert "confusing" in survey assert "too-long" in survey eq_("lorem ipsum dolor", survey["comment"]) # Posting the survey again shouldn't add a new survey result. self.client.post(url, data) eq_(1, vote.metadata.filter(key="survey").count())
def test_unhelpful_survey(self): """The unhelpful survey is stored as vote metadata""" vote = HelpfulVoteFactory() url = reverse('wiki.unhelpful_survey') data = { 'vote_id': vote.id, 'button': 'Submit', 'confusing': 1, 'too-long': 1, 'comment': 'lorem ipsum dolor' } response = self.client.post(url, data) eq_(200, response.status_code) eq_('{"message": "Thanks for making us better!"}', response.content) vote_meta = vote.metadata.all() eq_(1, len(vote_meta)) eq_('survey', vote_meta[0].key) survey = json.loads(vote_meta[0].value) eq_(3, len(survey.keys())) assert 'confusing' in survey assert 'too-long' in survey eq_('lorem ipsum dolor', survey['comment']) # Posting the survey again shouldn't add a new survey result. self.client.post(url, data) eq_(1, vote.metadata.filter(key='survey').count())
def test_vote(self): """Test vote API call.""" r = RevisionFactory(is_approved=True) HelpfulVoteFactory(revision=r, helpful=False) HelpfulVoteFactory(revision=r, helpful=False) HelpfulVoteFactory(revision=r, helpful=True) a = AnswerFactory() AnswerVoteFactory(answer=a, helpful=False) AnswerVoteFactory(answer=a, helpful=True) AnswerVoteFactory(answer=a, helpful=True) r = self._get_api_result('api.kpi.votes') eq_(r['objects'][0]['kb_helpful'], 1) eq_(r['objects'][0]['kb_votes'], 3) eq_(r['objects'][0]['ans_helpful'], 2) eq_(r['objects'][0]['ans_votes'], 3)
def test_unhelpful_survey_on_helpful_vote(self): """Verify a survey doesn't get saved on helpful votes.""" vote = HelpfulVoteFactory(helpful=True) url = reverse("wiki.unhelpful_survey") data = { "vote_id": vote.id, "button": "Submit", "confusing": 1, "too-long": 1, "comment": "lorem ipsum dolor", } self.client.post(url, data) eq_(0, vote.metadata.count())
def test_unhelpful_survey_on_helpful_vote(self): """Verify a survey doesn't get saved on helpful votes.""" vote = HelpfulVoteFactory(helpful=True) url = reverse('wiki.unhelpful_survey') data = { 'vote_id': vote.id, 'button': 'Submit', 'confusing': 1, 'too-long': 1, 'comment': 'lorem ipsum dolor' } self.client.post(url, data) eq_(0, vote.metadata.count())
def test_unhelpful_truncation(self): """Give helpful_vote a survey that is too long. It should be truncated safely, instead of generating bad JSON. """ vote = HelpfulVoteFactory() too_long_comment = ('lorem ipsum' * 100) + 'bad data' self.client.post(reverse('wiki.unhelpful_survey'), { 'vote_id': vote.id, 'button': 'Submit', 'comment': too_long_comment }) vote_meta = vote.metadata.all()[0] # Will fail if it is not proper json, ie: bad truncation happened. survey = json.loads(vote_meta.value) # Make sure the right value was truncated. assert 'bad data' not in survey['comment']
def test_unhelpful_truncation(self): """Give helpful_vote a survey that is too long. It should be truncated safely, instead of generating bad JSON. """ vote = HelpfulVoteFactory() too_long_comment = ("lorem ipsum" * 100) + "bad data" self.client.post( reverse("wiki.unhelpful_survey"), { "vote_id": vote.id, "button": "Submit", "comment": too_long_comment }, ) vote_meta = vote.metadata.all()[0] # Will fail if it is not proper json, ie: bad truncation happened. survey = json.loads(vote_meta.value) # Make sure the right value was truncated. assert "bad data" not in survey["comment"]
def _add_vote_in_past(rev, vote, days_back): HelpfulVoteFactory(revision=rev, helpful=vote, created=date.today() - timedelta(days=days_back))