Fakultas Ilmu Komputer UI

tests.py 7.21 KB
Newer Older
1
2
import json

3
4

from django.core.files.uploadedfile import SimpleUploadedFile
5
6
from django.core import serializers
from django.test import Client, TestCase
7
from django.urls import resolve
8
9
from authentication.models import User
from django.core.files import File
10
from .models import Category, Comment, Materi
Saul Andre's avatar
Saul Andre committed
11
from .views import DaftarKatalog, DetailMateri
12
13
14
15
from django.contrib.auth import get_user_model



16
17
18
19
20


class DaftarKatalogTest(TestCase):
    def test_daftar_katalog_url_exist(self):
        url = "/"
21
22
        response = Client().get(f"{url}")
        self.assertEqual(response.status_code, 200)
23
24

    def test_daftar_katalog_using_daftar_katalog_template(self):
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        response = Client().get("/")
        self.assertTemplateUsed(response, "app/katalog_materi.html")

    # def test_daftar_kategori(self):
    #     kategori = Category()
    #     kategori.name = "baru"
    #     kategori.description = "kategori baru saya"
    #     kategori.save()

    #     kategori2 = Category()
    #     kategori2.name = "baru2"
    #     kategori2.save()

    #     materi1 = Materi()
    #     materi1.title = "baru lah"
    #     materi1.save()
    #     materi1.categories.add(kategori.id)
    #     materi1.save()

    #     materi2 = Materi()
    #     materi2.save()
    #     materi2.categories.add(kategori2)
    #     materi2.save()

    #     cl = Client()
    #     res = cl.get('/?kategori='+str(kategori.pk))
    #     self.assertIn(materi1, res.context['materi_list'])
    #     self.assertNotIn(materi2,res.context['materi_list'])
53

54
    def test_daftar_katalog_using_daftar_katalog_func(self):
55
        found = resolve("/")
56
57
58
59
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)

    def test_fields(self):
        materi = Materi()
60
61
62
        materi.title = "tes"
        materi.cover = "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg"
        materi.author = "input"
63
64
        materi.save()

65
66
67
68
        resp = Materi.objects.get(id=materi.id)
        self.assertEqual(resp, materi)


Saul Andre's avatar
Saul Andre committed
69
class DetailMateriTest(TestCase):
70
    def setUp(self):
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        self.client = Client()
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": "passwordtest"
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": "passwordtest"
        }
        self.anonymous_credential = {
            "email": "anonymous@gov.id",
            "password": "passwordtest"
        }
        self.admin = get_user_model().objects.create_user(
            **self.admin_credential, name="Admin", is_admin=True)
        self.contributor = get_user_model().objects.create_user(
            **self.contributor_credential, name="Kontributor", is_contributor=True)
        self.anonymous = get_user_model().objects.create_user(
            **self.anonymous_credential, name="Anonymous"
        )
        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="PENDING", cover=self.cover, content=self.content, feedback="").save()
        self.materi1 = Materi.objects.first()
        self.url= "/materi/" + str(self.materi1.id) + "/"
99
100

    def test_detail_materi_url_exist(self):
101
        response = Client().get(self.url)
102
103
104
105
        self.assertEqual(response.status_code,200)
        self.assertNotEqual(response.status_code, 404)

    def test_detail_materi_using_detail_materi_template(self):
106
        response = Client().get(self.url)
107
        self.assertTemplateUsed(response, 'app/detail_materi.html')
108

Saul Andre's avatar
Saul Andre committed
109
    def test_detail_materi_using_detail_materi_func(self):
110
        found = resolve(self.url)
Saul Andre's avatar
Saul Andre committed
111
        self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
112

Saul Andre's avatar
Saul Andre committed
113
114
115
116
117
118
119
    def test_category_models_can_create_new_object(self):
        test = Category.objects.create(id="1", name="medis", description="kategori medis")
        countData = Category.objects.all().count()
        self.assertEqual(1, countData)
        self.assertNotEqual(0, countData)
        self.assertEqual(test.__str__(), "medis")
        self.assertNotEqual(test.__str__(), "saul")
Samuel Dimas's avatar
Samuel Dimas committed
120

Saul Andre's avatar
Saul Andre committed
121
    def test_comment_models_can_create_new_object(self):
122
        test = Comment.objects.create(user="saul", profile="121212", comment="232323")
Saul Andre's avatar
Saul Andre committed
123
124
125
126
127
        countData = Comment.objects.all().count()
        self.assertEqual(1, countData)
        self.assertNotEqual(0, countData)
        self.assertEqual(test.__str__(), "saul")
        self.assertNotEqual(test.__str__(), "userlain")
128
129
    
    def test_post_blank_comment(self):
130
131
132
133
134
135
136
137
138
139
        url = self.url
        self.client.login(**self.anonymous_credential)
        response = self.client.post(url, {"comment": ""})
        self.assertIn("error_message", response.context)
        self.assertIn("Anda belum menuliskan komentar", response.context["error_message"])

    def test_comment_rendered_to_template(self):
        url = self.url
        self.client.login(**self.contributor_credential)
        self.client.post(url, {"comment": "This is my new comment"})
140
141
        response = Client().get(url)
        self.assertContains(response, "This is my new comment")
142

143
    def test_comment_by_admin(self):
144
145
146
147
        url = self.url
        self.client.login(**self.admin_credential)
        self.client.post(url, {"comment": "This is new comment by Admin"})
        response = self.client.get(url)
148
149
150
        self.assertContains(response, "Admin")

    def test_comment_by_kontributor(self):
151
152
153
154
155
        url = self.url
        self.client.login(**self.contributor_credential)
        self.client.post(url, {"comment": "This is new comment by Contributor"})
        response = self.client.get(url)
        self.assertContains(response, "Kontributor")
156
157

    def test_comment_by_anonymous(self):
158
159
160
161
162
163
        url = self.url
        self.client.get("/logout/")
        self.client.login(**self.anonymous_credential)
        self.client.post(url, {"comment": "This is new comment by Anonymous"})
        response = self.client.get(url)
        self.assertContains(response, "Anonim")
164
165
    
    def test_detail_materi_contains_form_comment(self):
166
        response = self.client.get(self.url)
167
168
169
        self.assertContains(response, "komentar...")

    
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
class TemplateLoaderTest(TestCase):
    def test_template_loader_url_exist(self):
        url = "/test-page.html"
        response = Client().get(url)
        self.assertEqual(response.status_code, 200)

    def test_template_loader_using_template_loader_template(self):
        url = "/test-page.html"
        response = Client().get(url)
        expected_template_name = "test-page.html"
        self.assertTemplateUsed(response, expected_template_name)

    def test_template_loader_using_template_loader_func(self):
        url = "/test-page.html"
        found = resolve(url)
        expected_view_name = "pages"
        self.assertEqual(found.func.__name__, expected_view_name)

    def test_template_loader_handle_non_existent_html(self):
        url = "/test.html"
        expected_template_name = "error-404.html"
        response = Client().get(url)
193
        self.assertEqual(response.status_code, 200)
194
        self.assertTemplateUsed(response, expected_template_name)