def test_search_query(self): question = QuestionFactory(title=self.title) response = SearchView.as_view()(self.request) self.assertEqual(200, response.status_code) rendered_content = response.rendered_content needle = self.SEARCH_RESULT_SNIPPET.format(id=question.id, title=question.title, body=question.question) self.assertInHTML(needle, rendered_content)
def test_GET_on_day_with_many_questions(self): todays_questions = [QuestionFactory() for _ in range(10)] response = DailyQuestionList.as_view()(self.REQUEST, year=self.TODAY.year, month=self.TODAY.month, day=self.TODAY.day) self.assertEqual(200, response.status_code) self.assertEqual(10, response.context_data['object_list'].count()) rendered_content = response.rendered_content for question in todays_questions: needle = self.QUESTION_LIST_NEEDLE_TEMPLATE.format( id=question.id, title=question.title, username=question.user.username, date=question.created.strftime(QUESTION_CREATED_STRFTIME)) self.assertInHTML(needle, rendered_content)
def test_logged_in_user_can_post_answers(self): question = QuestionFactory() self.assertTrue( self.client.login(username=question.user.username, password=UserFactory.password)) response = self.client.get('/q/{}'.format(question.id)) rendered_content = response.rendered_content self.assertEqual(200, response.status_code) self.assertInHTML(self.NO_ANSWER_SNIPPET, rendered_content) template_names = [t.name for t in response.templates] self.assertIn('qanda/common/post_answer.html', template_names) question_needle = self.QUESTION_DISPLAY_SNIPPET.format( title=question.title, user=question.user.username, date=question.created.strftime(QUESTION_CREATED_STRFTIME), body=question.question)
def test_anonymous_user_cannot_post_answers(self): question = QuestionFactory() response = self.client.get("/q/{}".format(question.id)) rendered_content = response.rendered_content self.assertEqual(200, response.status_code) template_names = [t.name for t in response.templates] self.assertNotIn("qanda/common/post_answer.html", template_names) self.assertIn(self.LOGIN_TO_POST_ANSWERS, rendered_content) self.assertInHTML(self.NO_ANSWERS_SNIPPET, rendered_content) question_needle = self.QUESTION_DISPLAY_SNIPPET.format( title=question.title, user=question.user.username, date=question.created.strftime(QUESTION_CREATED_STRFTIME), body=DEFAULT_BODY_HTML, ) self.assertInHTML(question_needle, rendered_content)