from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from main import models
class PostCreateTestCase(TestCase):
def setUp(self):
self.user = models.User.objects.create(username="alice")
self.client.force_login(self.user)
def test_post_create(self):
data = {
"title": "New post",
"slug": "new-post",
"body": "Content sentence.",
}
response = self.client.post(reverse("post_create"), data)
self.assertEqual(response.status_code, 302)
self.assertTrue(models.Post.objects.get(title=data["title"]))
def test_post_multiline_create(self):
data = {
"title": "multiline post",
"slug": "multiline-post",
"body": """What I’m really concerned about is reaching
one person. And that person may be myself for all I know.
— Jorge Luis Borges.""",
}
response = self.client.post(reverse("post_create"), data)
self.assertEqual(response.status_code, 302)
self.assertTrue(models.Post.objects.get(title=data["title"]))
self.assertEqual(
models.Post.objects.get(title=data["title"]).body, data["body"]
)
class PostCreateAnonTestCase(TestCase):
"""Test non logged in user cannot create post."""
def test_post_create_anon(self):
data = {
"title": "New post",
"slug": "new-post",
"body": "Content sentence.",
}
response = self.client.post(reverse("post_create"), data)
self.assertEqual(response.status_code, 302)
self.assertTrue(reverse("login") in response.url)
class PostCreateDraftTestCase(TestCase):
def setUp(self):
self.user = models.User.objects.create(username="alice")
self.client.force_login(self.user)
self.data = {
"title": "New post",
"slug": "new-post",
"body": "Content sentence.",
"published_at": "",
}
self.client.post(reverse("post_create"), self.data)
def test_post_create_draft(self):
"""Test draft post gets created."""
self.assertTrue(models.Post.objects.get(title=self.data["title"]))
def test_post_draft_index(self):
"""Test draft post appears on blog index as draft."""
response = self.client.get(
reverse("index"),
HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Drafts")
class PostCreateDraftAnonTestCase(TestCase):
"""Test draft post does not appear on blog index for non-logged in users."""
def setUp(self):
self.user = models.User.objects.create(username="alice")
self.data_published = {
"title": "New post",
"slug": "new-post",
"body": "Content sentence.",
}
models.Post.objects.create(owner=self.user, **self.data_published)
self.data_nonpublished = {
"title": "Draft post",
"slug": "draft-post",
"body": "Incomplete content sentence.",
"published_at": None,
}
models.Post.objects.create(owner=self.user, **self.data_nonpublished)
def test_post_draft_index(self):
response = self.client.get(
reverse("index"),
HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.data_published["title"])
self.assertNotContains(response, self.data_nonpublished["title"])
self.assertNotContains(response, "Drafts")
class PostDetailTestCase(TestCase):
def setUp(self):
self.user = models.User.objects.create(username="alice")
self.client.force_login(self.user)
self.data = {
"title": "New post",
"slug": "new-post",
"body": "Content sentence.",
}
self.post = models.Post.objects.create(owner=self.user, **self.data)
def test_post_detail(self):
response = self.client.get(
reverse("post_detail", args=(self.post.slug,)),
# needs HTTP_HOST because we need to request it on the subdomain
HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.data["title"])
self.assertContains(response, self.data["body"])
def test_post_detail_redir_a(self):
response = self.client.get(
reverse("post_detail_redir_a", args=(self.post.slug,)),
# needs HTTP_HOST because we need to request it on the subdomain
HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
)
self.assertEqual(response.status_code, 301)
self.assertEqual(response.url, reverse("post_detail", args=(self.post.slug,)))
def test_post_detail_redir_b(self):
response = self.client.get(
reverse("post_detail_redir_b", args=(self.post.slug,)),
# needs HTTP_HOST because we need to request it on the subdomain
HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
)
self.assertEqual(response.status_code, 301)
self.assertEqual(response.url, reverse("post_detail", args=(self.post.slug,)))
class PostSanitizeHTMLTestCase(TestCase):
"""Test is bleach is sanitizing illegal tags."""
def setUp(self):
self.user = models.User.objects.create(username="alice")
self.data = {
"title": "New post",
"slug": "new-post",
"body": "Content sentence. ",
}
models.Post.objects.create(owner=self.user, **self.data)
def test_get_sanitized(self):
post = models.Post.objects.get(slug=self.data["slug"])
self.assertTrue("<script>" in post.body_as_html)
self.assertFalse("