diff --git a/app/tests.py b/app/tests.py index a4c31ff23c5107f7cc719cf721600a4374598b3e..31d6478aca906f16bad802d3011c8aecbb00aa53 100644 --- a/app/tests.py +++ b/app/tests.py @@ -159,7 +159,53 @@ class DetailMateriTest(TestCase): self.client.get(deleteURL) self.assertEqual(Comment.objects.all().filter( comment="This is new comment by Anonymous").count(), 0) + +class ViewCommentsTest(TestCase): + def setUp(self): + self.client = Client() + self.contributor_credential = { + "email": "kontributor@gov.id", + "password": "passwordtest" + } + self.contributor = get_user_model().objects.create_user( + **self.contributor_credential, name="Kontributor", is_contributor=True) + self.mattDamon = get_user_model().objects.create_user( + **self.contributor_credential, name="Matt Damon", is_contributor=True) + self.cover = SimpleUploadedFile( + "Cherprang_Areekul40_nJM9dGt.jpg", b"Test file") + self.content = SimpleUploadedFile("Bahan_PA_RKK.pdf", b"Test file") + Materi(title="Materi 1", author="Agas", uploader=self.contributor, + publisher="Kelas SC", descriptions="Deskripsi Materi 1", + status="APPROVE", cover=self.cover, content=self.content).save() + self.materi1 = Materi.objects.first() + self.commentByKontributor = Comment(username='saul', materi=self.materi1, user=self.contributor) + self.commentByMatt = Comment(username='saul',comment="this is Matt Damon" materi=self.materi1, user=self.mattDamon) + self.url = '/comments/' + + def test_comments_url_exist(self): + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertNotEqual(response.status_code, 404) + + def test_comments_using_comments_template(self): + response = self.client.get(url) + self.assertTemplateUsed(response, 'app/comments.html') + def test_comments_using_comments_func(self): + found = resolve(self.url) + self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__) + + def test_comments_page_render_comments(self): + response = self.client.get(self.url) + self.assertContains(response, "comments") + self.assertNotContains(response, 'bukan comment') + + def test_comments_page_only_render_specific_user_comments(self): + self.client.login(**self.contributor_credential) + response = self.client.get(self.url) + self.assertContains(response, "comments") + self.assertNotContains(response, "this is Matt Damon") + class TemplateLoaderTest(TestCase): def test_template_loader_url_exist(self):