Ejemplo n.º 1
0
 def test_helpful_answer_not_editable(self):
     q = QuestionFactory(is_locked=True)
     a = AnswerFactory(question=q)
     u = UserFactory()
     self.client.force_authenticate(user=u)
     res = self.client.post(reverse('answer-helpful', args=[a.id]))
     eq_(res.status_code, 403)
     eq_(Answer.objects.get(id=a.id).num_votes, 0)
Ejemplo n.º 2
0
 def test_is_taken(self):
     q = QuestionFactory()
     u = UserFactory()
     q.take(u)
     url = reverse('question-detail', args=[q.id])
     res = self.client.get(url)
     eq_(res.status_code, 200)
     eq_(res.data['taken_by']['username'], u.username)
Ejemplo n.º 3
0
 def test_take(self):
     q = QuestionFactory()
     u = UserFactory()
     self.client.force_authenticate(user=u)
     res = self.client.post(reverse('question-take', args=[q.id]))
     eq_(res.status_code, 204)
     q = Question.objects.get(id=q.id)
     eq_(q.taken_by, u)
Ejemplo n.º 4
0
    def test_vote_updates_count(self):
        q = QuestionFactory()
        eq_(0, q.num_votes_past_week)

        QuestionVoteFactory(question=q, anonymous_id="abc123")

        q = Question.objects.get(id=q.id)
        eq_(1, q.num_votes_past_week)
Ejemplo n.º 5
0
 def test_solution_is_readonly(self):
     q = QuestionFactory()
     a = AnswerFactory(question=q)
     self.data['solution'] = a.id
     serializer = api.QuestionSerializer(context=self.context, data=self.data, instance=q)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     eq_(q.solution, None)
Ejemplo n.º 6
0
 def test_helpful(self):
     q = QuestionFactory()
     u = UserFactory()
     self.client.force_authenticate(user=u)
     res = self.client.post(reverse('question-helpful', args=[q.id]))
     eq_(res.status_code, 200)
     eq_(res.data, {'num_votes': 1})
     eq_(Question.objects.get(id=q.id).num_votes, 1)
Ejemplo n.º 7
0
 def test_answer_create_action(self):
     """When an answer is created, an Action is created too."""
     q = QuestionFactory()
     ans = AnswerFactory(question=q)
     act = Action.objects.action_object(ans).get()
     eq_(act.actor, ans.creator)
     eq_(act.verb, "answered")
     eq_(act.target, q)
Ejemplo n.º 8
0
    def test_from_invalid_url(self):
        """Verify question returned from valid URL."""
        q = QuestionFactory()

        eq_(None, Question.from_url("/en-US/questions/%s/edit" % q.id))
        eq_(None, Question.from_url("/en-US/kb/%s" % q.id))
        eq_(None, Question.from_url("/random/url"))
        eq_(None, Question.from_url("/en-US/questions/dashboard/metrics"))
Ejemplo n.º 9
0
 def test_bleaching(self):
     """Tests whether question content is bleached."""
     q = QuestionFactory(
         content="<unbleached>Cupcakes are the best</unbleached>")
     url = reverse("question-detail", args=[q.id])
     res = self.client.get(url)
     eq_(res.status_code, 200)
     assert "<unbleached>" not in res.data["content"]
Ejemplo n.º 10
0
    def test_filter_solved_by(self):
        q1 = QuestionFactory()
        a1 = AnswerFactory(question=q1)
        q1.solution = a1
        q1.save()
        q2 = QuestionFactory()
        AnswerFactory(question=q2, creator=a1.creator)
        q3 = QuestionFactory()
        a3 = AnswerFactory(question=q3)
        q3.solution = a3
        q3.save()

        qs = self.filter_instance.filter_solved_by(self.queryset, a1.creator.username)
        eq_(list(qs), [q1])

        qs = self.filter_instance.filter_solved_by(self.queryset, a3.creator.username)
        eq_(list(qs), [q3])
Ejemplo n.º 11
0
 def test_follow(self):
     q = QuestionFactory()
     u = UserFactory()
     self.client.force_authenticate(user=u)
     res = self.client.post(reverse('question-follow', args=[q.id]))
     eq_(res.status_code, 204)
     f = Follow.objects.get(user=u)
     eq_(f.follow_object, q)
     eq_(f.actor_only, False)
Ejemplo n.º 12
0
    def test_filter_involved(self):
        q1 = QuestionFactory()
        a1 = AnswerFactory(question=q1)
        q2 = QuestionFactory(creator=a1.creator)

        querystring = '?involved={0}'.format(q1.creator.username)
        res = self.client.get(reverse('question-list') + querystring)
        eq_(res.status_code, 200)
        eq_(len(res.data['results']), 1)
        eq_(res.data['results'][0]['id'], q1.id)

        querystring = '?involved={0}'.format(q2.creator.username)
        res = self.client.get(reverse('question-list') + querystring)
        eq_(res.status_code, 200)
        eq_(len(res.data['results']), 2)
        # The API has a default sort, so ordering will be consistent.
        eq_(res.data['results'][0]['id'], q2.id)
        eq_(res.data['results'][1]['id'], q1.id)
Ejemplo n.º 13
0
 def test_unfollow(self):
     q = QuestionFactory()
     u = UserFactory()
     actstream.actions.follow(u, q, actor_only=False)
     eq_(Follow.objects.filter(user=u).count(), 1)  # pre-condition
     self.client.force_authenticate(user=u)
     res = self.client.post(reverse('question-unfollow', args=[q.id]))
     eq_(res.status_code, 204)
     eq_(Follow.objects.filter(user=u).count(), 0)
Ejemplo n.º 14
0
 def test_case_insensitive_search(self):
     """Ensure the default searcher is case insensitive."""
     q = QuestionFactory(title="lolrus", content="I am the lolrus.")
     AnswerVoteFactory(answer__question=q)
     self.refresh()
     # This is an AND operation
     result = QuestionMappingType.search().query(
         question_title__match="LOLRUS", question_content__match="LOLRUS")
     assert result.count() > 0
Ejemplo n.º 15
0
    def setUp(self):
        self.client = APIClient()

        self.follower = UserFactory()
        self.followed = UserFactory()
        self.question = QuestionFactory(creator=self.followed)
        # The above might make follows, which this test isn't about. Clear them out.
        Follow.objects.all().delete()
        follow(self.follower, self.followed)
Ejemplo n.º 16
0
    def test_done(self):
        """Verify the done queryset."""
        # Create a question, there shouldn't be any done yet.
        q = QuestionFactory()
        eq_(0, Question.objects.done().count())

        # Add an answer, there shouldn't be any done yet.
        a = AnswerFactory(question=q)
        eq_(0, Question.objects.done().count())

        # Make it the solution, there should be one done.
        q.solution = a
        q.save()
        eq_(1, Question.objects.done().count())

        # Create a locked questions, there should be two done.
        QuestionFactory(is_locked=True)
        eq_(2, Question.objects.done().count())
Ejemplo n.º 17
0
 def test_is_taken_clears(self):
     u = UserFactory()
     taken_until = datetime.now() - timedelta(seconds=30)
     q = QuestionFactory(taken_by=u, taken_until=taken_until)
     # Testin q.is_taken should clear out ``taken_by`` and ``taken_until``,
     # since taken_until is in the past.
     eq_(q.is_taken, False)
     eq_(q.taken_by, None)
     eq_(q.taken_until, None)
Ejemplo n.º 18
0
    def test_questions(self):
        """Test questions API call."""
        # A question with a solution:
        a = AnswerFactory()
        a.question.solution = a
        a.question.save()
        # A question with an answer:
        AnswerFactory()
        # A question without answers:
        QuestionFactory()
        # A locked question that shouldn't be counted for anything
        QuestionFactory(is_locked=True)

        r = self._get_api_result('api.kpi.questions')
        eq_(r['objects'][0]['solved'], 1)
        eq_(r['objects'][0]['responded_24'], 2)
        eq_(r['objects'][0]['responded_72'], 2)
        eq_(r['objects'][0]['questions'], 3)
Ejemplo n.º 19
0
    def test_no_notification_on_update(self):
        """Saving an existing question does not watch it."""

        q = QuestionFactory()
        QuestionReplyEvent.stop_notifying(q.creator, q)
        assert not QuestionReplyEvent.is_notifying(q.creator, q)

        q.save()
        assert not QuestionReplyEvent.is_notifying(q.creator, q)
Ejemplo n.º 20
0
    def test_weird_list_troubleshooting_info(self):
        """Test the corner case in which 'modifiedPReferences' is in a
        list in troubleshooting data. This is weird, but caused a bug."""
        q = QuestionFactory()
        q.add_metadata(troubleshooting='["modifiedPreferences"]')

        # This case should not raise an error.
        response = get(self.client, "questions.details", args=[q.id])
        eq_(200, response.status_code)
Ejemplo n.º 21
0
    def test_tagged_feed(self):
        """Test the tagged feed."""
        t = TagFactory(name="green", slug="green")
        q = QuestionFactory()
        q.tags.add("green")
        items = TaggedQuestionsFeed().items(t)
        eq_(1, len(items))
        eq_(q.id, items[0].id)

        cache.clear()

        q = QuestionFactory()
        q.tags.add("green")
        q.updated = datetime.now() + timedelta(days=1)
        q.save()
        items = TaggedQuestionsFeed().items(t)
        eq_(2, len(items))
        eq_(q.id, items[0].id)
Ejemplo n.º 22
0
 def test_it_works_with_users_who_have_gotten_first_contrib_emails(self):
     # This flag caused a regression, tracked in bug 1163855.
     # The error was that the help text on the field was a str instead of a
     # unicode. Yes, really, that matters apparently.
     u = UserFactory(profile__first_answer_email_sent=True)
     QuestionFactory(creator=u)
     url = reverse('question-list')
     res = self.client.get(url)
     eq_(res.status_code, 200)
Ejemplo n.º 23
0
    def test_num_votes_none(self):
        """Tests num_voted filtering where num_votes is ''"""
        q = QuestionFactory()
        QuestionVoteFactory(question=q)

        self.refresh()

        qs = {"q": "", "w": 2, "a": 1, "num_voted": 2, "num_votes": ""}
        response = self.client.get(reverse("search.advanced"), qs)
        eq_(200, response.status_code)
Ejemplo n.º 24
0
    def test_question_no_answers_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='Does this work?')
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Ejemplo n.º 25
0
    def test_question_is_unindexed_on_creator_delete(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='Does this work?')
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.creator.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Ejemplo n.º 26
0
    def test_num_votes_none(self):
        """Tests num_voted filtering where num_votes is ''"""
        q = QuestionFactory()
        QuestionVoteFactory(question=q)

        self.refresh()

        qs = {'q': '', 'w': 2, 'a': 1, 'num_voted': 2, 'num_votes': ''}
        response = self.client.get(reverse('search.advanced'), qs)
        eq_(200, response.status_code)
Ejemplo n.º 27
0
    def test_solve(self):
        q = QuestionFactory()
        a = AnswerFactory(question=q)

        self.client.force_authenticate(user=q.creator)
        res = self.client.post(reverse('question-solve', args=[q.id]),
                               data={'answer': a.id})
        eq_(res.status_code, 204)
        q = Question.objects.get(id=q.id)
        eq_(q.solution, a)
Ejemplo n.º 28
0
 def test_helpful_double_vote(self):
     q = QuestionFactory()
     u = UserFactory()
     QuestionVoteFactory(question=q, creator=u)
     self.client.force_authenticate(user=u)
     res = self.client.post(reverse('question-helpful', args=[q.id]))
     eq_(res.status_code, 409)
     # It's 1, not 0, because one was created above. The failure cause is
     # if the number of votes is 2, one from above and one from the api call.
     eq_(Question.objects.get(id=q.id).num_votes, 1)
Ejemplo n.º 29
0
 def test_take_conflict(self):
     u1 = UserFactory()
     u2 = UserFactory()
     taken_until = datetime.now() + timedelta(seconds=30)
     q = QuestionFactory(taken_until=taken_until, taken_by=u1)
     self.client.force_authenticate(user=u2)
     res = self.client.post(reverse('question-take', args=[q.id]))
     eq_(res.status_code, 409)
     q = Question.objects.get(id=q.id)
     eq_(q.taken_by, u1)
Ejemplo n.º 30
0
    def test_not_by_asker(self):
        """Verify that only answers by users other than the original asker are returned"""
        q = QuestionFactory()

        # Add an answer by the original asker
        AnswerFactory(question=q, creator=q.creator)
        eq_(0, Answer.objects.not_by_asker().count())

        # Add an answer by someone else
        AnswerFactory(question=q)
        eq_(1, Answer.objects.not_by_asker().count())