Fakultas Ilmu Komputer UI

tests.py 7.16 KB
Newer Older
1
2
3
import json

from django.core import serializers
4
from django.test import Client, TestCase, RequestFactory
Putu Agastya's avatar
Putu Agastya committed
5
from django.urls import resolve
6

7
from .views import DaftarKatalog, DetailMateri, DashboardKontributorView
8
from .models import Materi, Category, Comment
9
10
11
from django.core import serializers
import json

12
13
14
from app.views import UploadMateriHTML, UploadMateriView
from authentication.models import User

Samuel Dimas's avatar
Samuel Dimas committed
15

Putu Agastya's avatar
Putu Agastya committed
16
17
18
class DaftarKatalogTest(TestCase):
    def test_daftar_katalog_url_exist(self):
        url = "/"
19
20
        response = Client().get(f"{url}")
        self.assertEqual(response.status_code, 200)
21
22

    def test_daftar_katalog_using_daftar_katalog_template(self):
23
24
25
        response = Client().get("/")
        self.assertTemplateUsed(response, "app/katalog_materi.html")

26
    def test_daftar_katalog_using_daftar_katalog_func(self):
27
        found = resolve("/")
28
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)
Samuel Dimas's avatar
Samuel Dimas committed
29

30
31
    def test_fields(self):
        materi = Materi()
32
33
34
        materi.title = "tes"
        materi.cover = "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg"
        materi.author = "input"
35
36
        materi.save()

37
38
39
40
        resp = Materi.objects.get(id=materi.id)
        self.assertEqual(resp, materi)


41
class DetailMateriTest(TestCase):
42

43
    def test_detail_materi_using_detail_materi_func(self):
44
        found = resolve("/materi/1/")
45
        self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
46

47
    def test_category_models_can_create_new_object(self):
48
49
        test = Category.objects.create(
            id="1", name="medis", description="kategori medis")
50
51
        countData = Category.objects.all().count()
        self.assertEqual(1, countData)
52
        self.assertNotEqual(0, countData)
53
        self.assertEqual(test.__str__(), "medis")
54
        self.assertNotEqual(test.__str__(), "saul")
55

56
    def test_comment_models_can_create_new_object(self):
57
58
        test = Comment.objects.create(
            user="saul", profile="121212", comment="232323")
59
60
61
62
63
        countData = Comment.objects.all().count()
        self.assertEqual(1, countData)
        self.assertNotEqual(0, countData)
        self.assertEqual(test.__str__(), "saul")
        self.assertNotEqual(test.__str__(), "userlain")
64

65

66
67
68
69
class TemplateLoaderTest(TestCase):
    def test_template_loader_url_exist(self):
        url = "/test-page.html"
        response = Client().get(url)
70
        self.assertEqual(response.status_code, 200)
71
72
73
74
75
76
77
78
79
80
81

    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"
82
83
84
85
86
87
88
        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)
        self.assertEqual(response.status_code,200)
89
        self.assertTemplateUsed(response, expected_template_name)
90
91
92
93


class UploadPageTest(TestCase):
    def setUp(self):
94
        self.client = Client()
95
96
97
98
99
        self.user = User.objects._create_user(email="kontributor@gov.id",
                                              password="kontributor", is_contributor=True)

    def test_upload_page_using_login_func(self):
        found = resolve("/unggah/")
100
101
        self.assertEqual(found.func.__name__,
                         UploadMateriView.as_view().__name__)
102
103

    def test_uplaod_page_url_is_exist(self):
104
        # Positive test
105
106
107
        self.client.login(email="kontributor@gov.id",
                          password="kontributor")
        response = self.client.get("/unggah/")
108
        self.assertEqual(response.status_code, 200)
109
110
111

        # Negative tests
        self.client.login(email="kontributor@gov.id",
112
                          password="kontributor")
113
114
115
116
117
118
        response = Client().get("/fake/")
        self.assertEqual(response.status_code, 404)

    def test_upload_page_template(self):
        url = "/unggah/"
        self.client.login(email="kontributor@gov.id",
119
                          password="kontributor")
120
121
122
123
124
125
        response = self.client.get(url)
        expected_template_name = "unggah.html"
        self.assertTemplateUsed(response, expected_template_name)

    def test_upload_page_title(self):
        self.client.login(email="kontributor@gov.id",
126
                          password="kontributor")
127
128
129
130
131
132
133
134
135
136
        response = self.client.get("/unggah/")

        # Positive tests
        self.assertContains(response, "Unggah Materi")

        # Negative tests
        self.assertNotContains(response, "Anything")

    def test_upload_page_form_field(self):
        self.client.login(email="kontributor@gov.id",
137
                          password="kontributor")
138
139
140
141
142
143
144
        response = self.client.get("/unggah/")

        # Positive tests
        self.assertContains(response, "title")

        # Negative tests
        self.assertNotContains(response, "anything")
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207


class DashboardKontributorViewTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
                                                     password="kontributor")
        self.admin = User.objects.create_admin(email="admin@gov.id",
                                               password="admin")
        self.url = "/dashboard/"
        self.view = DashboardKontributorView
        self.template_name = "dashboard.html"

    def test_dashboard_kontributor_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)

    def test_dashboard_kontributor_template(self):
        # Login
        self.client.login(email="kontributor@gov.id",
                          password="kontributor")
        # Test
        response = self.client.get(self.url)
        self.assertTemplateUsed(response, self.template_name)
        # Logout
        self.client.logout()

    def test_dashboard_kontributor_url(self):
        # Login
        self.client.login(email="kontributor@gov.id",
                          password="kontributor")
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

    def test_dashboard_kontributor_access(self):
        # Kontributor
        # Login
        self.client.login(email="kontributor@gov.id",
                          password="kontributor")
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

        # Admin
        # Login
        self.client.login(email="admin@gov.id",
                          password="admin")
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
        # Logout
        self.client.logout()
        
        # Anonim
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)