def test_drafts_article_not_found(self): article = test_utils.create_article() response = test_utils.request_article(self.client, article) self.assertEqual(response.status_code, 200) article = test_utils.create_article(is_draft=True) response = test_utils.request_article(self.client, article) self.assertEqual(response.status_code, 404)
def test_show_or_hide_form(self): article = test_utils.create_article() # As anonymous. response = test_utils.request_article(self.client, article) expected = "login" self.assertIn(expected, response.content) # As member. test_utils.create_and_login_user(self.client) response = test_utils.request_article(self.client, article) self.assertIn("<button>Add comment</button>", response.content)
def test_moderated(self): article = test_utils.create_article(is_comments_moderated=True) user = test_utils.create_and_login_user(self.client) comment = test_utils.create_comment(article=article, author=user, is_moderated=False) response = test_utils.request_article(self.client, article) self.assertIn(comment.content, response.content) self.assertIn("waiting_approval", response.content) self.client.logout() response = test_utils.request_article(self.client, article) self.assertNotIn(comment.content, response.content) self.assertNotIn("waiting_approval", response.content)
def test_single_article(self): # Wrong PK. response = self.client.get(reverse("blog_article", kwargs={"article_pk": 9999})) self.assertEqual(404, response.status_code) article = test_utils.create_article() # No slug. response = self.client.get(reverse("blog_article", kwargs={"article_pk": article.pk})) self.assertEqual(301, response.status_code) # Wrong slug. response = self.client.get(reverse( "blog_article", kwargs={ "article_pk": article.pk, "slug": slugify(test_utils.get_data()), }, )) self.assertEqual(301, response.status_code) # All OK. response = test_utils.request_article(self.client, article) self.assertEqual(200, response.status_code) self.assertIn(article.title, response.content) self.assertIn(article.content, response.content)
def test_has_tags(self): article = test_utils.create_article() tag1 = test_utils.create_tag(article) tag2 = test_utils.create_tag(article) response = test_utils.request_article(self.client, article) tags = response.context[-1]["article"].tag_set.all() self.assertEqual(tag1, tags[0]) self.assertEqual(tag2, tags[1])
def test_nested_comments(self): """Testing order and depth of comments.""" article = test_utils.create_article() comment1 = test_utils.create_comment(article=article) comment2 = test_utils.create_comment(article=article) comment3 = test_utils.create_comment(article=article, parent=comment1) comment4 = test_utils.create_comment(article=article, parent=comment3) response = test_utils.request_article(self.client, article) expected_comments = [comment1, comment3, comment4, comment2] actual_comments = list(response.context[-1]["article"].get_comments()) self.assertEqual(expected_comments, actual_comments) # Order. expected_depth = [1, 2, 3, 1] actual_depth = [comment.depth for comment in actual_comments] self.assertEqual(expected_depth, actual_depth) # Depth.
def test_tweet_link(self): article = test_utils.create_article(tweet_id=42) response = test_utils.request_article(self.client, article) expected = article.get_tweet_link() actual = response.content self.assertIn(expected, actual)
def test_tag_has_link_to_blog_search(self): article = test_utils.create_article() tag = test_utils.create_tag(article) response = test_utils.request_article(self.client, article) link = "{}?tags={}".format(reverse("blog_search"), tag.content) self.assertIn(link, response.content)
def test_tags_displayed(self): article = test_utils.create_article() tag = test_utils.create_tag(article) response = test_utils.request_article(self.client, article) self.assertIn(tag.content, response.content)
def test_no_tags(self): article = test_utils.create_article() response = test_utils.request_article(self.client, article) tags = response.context[-1]["article"].tag_set.all() self.assertEqual(0, len(tags))
def test_no_comments(self): article = test_utils.create_article() response = test_utils.request_article(self.client, article) expected = "No comments." self.assertIn(expected, response.content)