108 lines
3.4 KiB
Python
108 lines
3.4 KiB
Python
import os
|
|
|
|
from django.test import TestCase
|
|
from django.core.files import File
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.test import Client
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.conf import settings
|
|
|
|
from .models import Post, Collection
|
|
|
|
|
|
# Create your tests here.
|
|
|
|
|
|
class ModelTests(TestCase):
|
|
def setUp(self):
|
|
self.user = get_user_model().objects.create(email="someone@example.com")
|
|
with open(
|
|
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
|
|
mode="rb",
|
|
) as f:
|
|
self.post = Post.objects.create(
|
|
img=File(f, "somefile.png"),
|
|
title="Foobar",
|
|
body="Some file",
|
|
user=self.user,
|
|
)
|
|
|
|
def tearDown(self):
|
|
self.post.img.delete()
|
|
|
|
def test_post_has_sensible_str(self):
|
|
self.assertIn("Foobar", str(self.post))
|
|
|
|
def test_collection_has_sensible_str(self):
|
|
col = Collection.objects.create(
|
|
title="A collection", description="foobar", user=self.user
|
|
)
|
|
self.assertIn("A collection", str(col))
|
|
|
|
|
|
class PostViewsTests(TestCase):
|
|
def setUp(self):
|
|
self.user = get_user_model().objects.create_user(
|
|
email="someone@example.com",
|
|
username="someone",
|
|
password="secret",
|
|
)
|
|
with open(
|
|
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
|
|
mode="rb",
|
|
) as f:
|
|
self.post = Post.objects.create(
|
|
img=File(f, "somefile.png"),
|
|
title="Foobar",
|
|
body="Some file",
|
|
user=self.user,
|
|
)
|
|
|
|
def tearDown(self):
|
|
self.post.img.delete()
|
|
|
|
def test_anon_cant_see_post(self):
|
|
c = Client()
|
|
url = reverse("posts:post_detail", kwargs={"pk": self.post.pk})
|
|
with self.settings(LOGIN_URL="/loginurl/"):
|
|
response = c.get(url)
|
|
self.assertRedirects(
|
|
response, f"/loginurl/?next={url}", fetch_redirect_response=False
|
|
)
|
|
|
|
def test_logged_in_other_user_cant_see_post(self):
|
|
get_user_model().objects.create_user(
|
|
email="someone2@example.com",
|
|
username="foobar",
|
|
password="secret",
|
|
)
|
|
c = Client()
|
|
c.login(username="foobar", password="secret")
|
|
url = reverse("posts:post_detail", kwargs={"pk": self.post.pk})
|
|
response = c.get(url)
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
def test_loggin_in_user_can_see_own_posts(self):
|
|
c = Client()
|
|
c.login(username="someone", password="secret")
|
|
url = reverse("posts:post_detail", kwargs={"pk": self.post.pk})
|
|
response = c.get(url)
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_can_post_a_comment(self):
|
|
c = Client()
|
|
c.login(username="someone", password="secret")
|
|
url = reverse("posts:post_detail", kwargs={"pk": self.post.pk})
|
|
c.post(url, {"comment": "some comment"})
|
|
self.assertTrue(self.post.comments.filter(comment="some comment").exists())
|
|
|
|
def test_posting_a_comment_requires_a_comment(self):
|
|
c = Client()
|
|
c.login(username="someone", password="secret")
|
|
url = reverse("posts:post_detail", kwargs={"pk": self.post.pk})
|
|
c.post(url, {"comment": ""})
|
|
self.assertFalse(self.post.comments.all().exists())
|