Fully test the post view
This commit is contained in:
parent
30526d9f30
commit
56bea8020b
3 changed files with 26 additions and 6 deletions
|
|
@ -46,7 +46,7 @@ class ModelTests(TestCase):
|
|||
|
||||
class PostViewsTests(TestCase):
|
||||
def setUp(self):
|
||||
self.user = get_user_model().objects.create(
|
||||
self.user = get_user_model().objects.create_user(
|
||||
email="someone@example.com",
|
||||
username="someone",
|
||||
password="secret",
|
||||
|
|
@ -68,17 +68,20 @@ class PostViewsTests(TestCase):
|
|||
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, settings.LOGIN_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(
|
||||
get_user_model().objects.create_user(
|
||||
email="someone2@example.com",
|
||||
username="foobar",
|
||||
password="secret",
|
||||
)
|
||||
c = Client()
|
||||
c.login(username="foobar", password="foobar")
|
||||
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)
|
||||
|
|
@ -89,3 +92,17 @@ class PostViewsTests(TestCase):
|
|||
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())
|
||||
|
|
|
|||
|
|
@ -22,10 +22,13 @@ class PostDetailView(LoginRequiredMixin, DetailView, ModelFormMixin):
|
|||
comment.save()
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
def get_queryset(self):
|
||||
return Post.objects.filter(user=self.request.user)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
form = self.form_class(request.POST)
|
||||
if form.is_valid:
|
||||
if form.is_valid():
|
||||
return self.form_valid(form)
|
||||
else:
|
||||
return self.form_invalid(form)
|
||||
|
|
|
|||
0
flangr/templates/registration/login.html
Normal file
0
flangr/templates/registration/login.html
Normal file
Loading…
Add table
Reference in a new issue