Skip to content

Commit

Permalink
Add some unit tests for PostPageView
Browse files Browse the repository at this point in the history
  • Loading branch information
Termuellinator committed Sep 27, 2023
1 parent 85a9cf6 commit f8fc288
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions apps/post/tests/ut/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase, Client
from django.urls import reverse

from apps.post import models
from apps.user.models import User


Expand All @@ -11,9 +12,45 @@ def setUp(self):
username="testuser",
password="testpassword",
)

def test_returns_200(self):
response = self.client.get(reverse("home"),
HTTP_USER_AGENT="Mozilla/5.0",
HTTP_CONTENT_TYPE="text/xml")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, 200)


class TestPostPageView(TestCase):
def setUp(self):
self.post_title = "Testpost"
self.username = "test"
self.category = "testcat"
self.user = User.objects.create_user(
username=self.username,
password="test",
)
self.cat = models.Category.objects.create(cat=self.category)
self.post = models.Post.objects.create(
user_id=self.user,
cat_id=self.cat,
title=self.post_title,
image="uploads/image.jpg"
)

def test_redirects_to_login_if_not_authed(self):
response = self.client.get(reverse("post-detail",
kwargs={'post_id':self.post.id}),
HTTP_USER_AGENT="Mozilla/5.0",
HTTP_CONTENT_TYPE="text/xml")

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/?next=/post/1")

def test_returns_200_if_authed(self):
self.client.login(username="test", password="test")
response = self.client.get(reverse("post-detail",
kwargs={'post_id':self.post.id}),
HTTP_USER_AGENT="Mozilla/5.0",
HTTP_CONTENT_TYPE="text/xml")

self.assertEqual(response.status_code, 200)

0 comments on commit f8fc288

Please sign in to comment.