Fakultas Ilmu Komputer UI

tests.py 18.2 KB
Newer Older
1
2
import json

3
from django.contrib.auth import get_user_model
4
from django.core import serializers
5
from django.core.files import File
6
7
8
9
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import Client, RequestFactory, TestCase
from django.urls import resolve

10
from administration.utils import id_generator
11
12
13
14
15
16
17
18
from app.views import UploadMateriHTML, UploadMateriView
from authentication.models import User

from .models import Category, Comment, Materi
from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri,
                    ProfilKontributorView, SuksesLoginAdminView,
                    SuksesLoginKontributorView, SuntingProfilView)

19
20
21
22

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

    def test_daftar_katalog_using_daftar_katalog_template(self):
27
28
29
        response = Client().get("/")
        self.assertTemplateUsed(response, "app/katalog_materi.html")

30
    def test_daftar_katalog_using_daftar_katalog_func(self):
31
        found = resolve("/")
32
33
34
35
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)

    def test_fields(self):
        materi = Materi()
36
37
38
        materi.title = "tes"
        materi.cover = "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg"
        materi.author = "input"
39
40
        materi.save()

41
42
43
44
        resp = Materi.objects.get(id=materi.id)
        self.assertEqual(resp, materi)


Saul Andre's avatar
Saul Andre committed
45
class DetailMateriTest(TestCase):
46
    def setUp(self):
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        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"
        )
67
68
        self.cover = SimpleUploadedFile(
            "Cherprang_Areekul40_nJM9dGt.jpg", b"Test file")
69
70
71
72
        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",
73
               status="APPROVE", cover=self.cover, content=self.content).save()
74
        self.materi1 = Materi.objects.first()
75
        self.url = "/materi/" + str(self.materi1.id) + "/"
76
77

    def test_detail_materi_url_exist(self):
78
        response = Client().get(self.url)
79
        self.assertEqual(response.status_code, 200)
80
81
82
        self.assertNotEqual(response.status_code, 404)

    def test_detail_materi_using_detail_materi_template(self):
83
        response = Client().get(self.url)
84
        self.assertTemplateUsed(response, 'app/detail_materi.html')
85

Saul Andre's avatar
Saul Andre committed
86
    def test_detail_materi_using_detail_materi_func(self):
87
        found = resolve(self.url)
Saul Andre's avatar
Saul Andre committed
88
        self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
89

Saul Andre's avatar
Saul Andre committed
90
    def test_category_models_can_create_new_object(self):
91
92
        test = Category.objects.create(
            id="1", name="medis", description="kategori medis")
Saul Andre's avatar
Saul Andre committed
93
94
95
96
97
        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
98

Saul Andre's avatar
Saul Andre committed
99
    def test_comment_models_can_create_new_object(self):
100
        test = Comment.objects.create(
101
            username="saul", profile="121212", comment="232323")
Saul Andre's avatar
Saul Andre committed
102
103
104
105
106
        countData = Comment.objects.all().count()
        self.assertEqual(1, countData)
        self.assertNotEqual(0, countData)
        self.assertEqual(test.__str__(), "saul")
        self.assertNotEqual(test.__str__(), "userlain")
107

108
    def test_post_blank_comment(self):
109
110
111
112
        url = self.url
        self.client.login(**self.anonymous_credential)
        response = self.client.post(url, {"comment": ""})
        self.assertIn("error_message", response.context)
113
114
        self.assertIn("Anda belum menuliskan komentar",
                      response.context["error_message"])
115
116
117
118
119

    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"})
120
121
        response = Client().get(url)
        self.assertContains(response, "This is my new comment")
122

123
    def test_comment_by_admin(self):
124
125
126
127
        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)
128
129
130
        self.assertContains(response, "Admin")

    def test_comment_by_kontributor(self):
131
132
        url = self.url
        self.client.login(**self.contributor_credential)
133
134
        self.client.post(
            url, {"comment": "This is new comment by Contributor"})
135
136
        response = self.client.get(url)
        self.assertContains(response, "Kontributor")
137
138

    def test_comment_by_anonymous(self):
139
140
141
142
143
        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)
144
        self.assertContains(response, "Anonymous")
145

146
    def test_detail_materi_contains_form_comment(self):
147
        response = self.client.get(self.url)
148
        self.assertContains(response, "Beri komentar...")
149

150
151
152
    def test_delete_comments_by_admin(self):
        url = self.url
        self.client.post(url, {"comment": "This is new comment by Anonymous"})
153
154
        deleteURL = "/delete/" + str(self.materi1.id) + "/" + str(
            Comment.objects.get(comment="This is new comment by Anonymous").id)
155
        self.client.get(deleteURL)
156
157
158
        self.assertEqual(Comment.objects.all().filter(
            comment="This is new comment by Anonymous").count(), 0)

159

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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)
182
        self.assertEqual(response.status_code, 200)
183
        self.assertTemplateUsed(response, expected_template_name)
184

185

186
187
class UploadPageTest(TestCase):
    def setUp(self):
188
        self.client = Client()
189
190
191
192
193
        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/")
194
195
        self.assertEqual(found.func.__name__,
                         UploadMateriView.as_view().__name__)
196
197

    def test_uplaod_page_url_is_exist(self):
198
        # Positive test
199
200
201
        self.client.login(email="kontributor@gov.id",
                          password="kontributor")
        response = self.client.get("/unggah/")
202
        self.assertEqual(response.status_code, 200)
203
204
205

        # Negative tests
        self.client.login(email="kontributor@gov.id",
206
                          password="kontributor")
207
208
209
210
211
212
        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",
213
                          password="kontributor")
214
215
        response = self.client.get(url)
        expected_template_name = "unggah.html"
216
        self.assertTemplateUsed(response, expected_template_name)
217
218
219

    def test_upload_page_title(self):
        self.client.login(email="kontributor@gov.id",
220
                          password="kontributor")
221
222
223
224
225
226
227
228
229
230
        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",
231
                          password="kontributor")
232
233
234
235
236
237
238
        response = self.client.get("/unggah/")

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

        # Negative tests
        self.assertNotContains(response, "anything")
239
240
241
242
243
244


class DashboardKontributorViewTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
245
                                                           password="kontributor")
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
        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()
296

297
298
299
300
        # Anonim
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
301
302


303
304
305
306
class ProfilKontributorTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
307
                                                           password="kontributor")
308
309
310
311
312
        self.admin = User.objects.create_admin(email="admin@gov.id",
                                               password="admin")
        self.url = "/profil/"
        self.view = ProfilKontributorView
        self.template_name = "profil.html"
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    def test_profil_kontributor_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)

    def test_profil_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_profil_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_profil_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()
358

359
360
361
362
        # Anonim
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
363
364


365
366
367
368
class SuntingProfilTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
369
                                                           password="kontributor")
370
371
372
373
374
        self.admin = User.objects.create_admin(email="admin@gov.id",
                                               password="admin")
        self.url = "/sunting/"
        self.view = SuntingProfilView
        self.template_name = "sunting.html"
375

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
    def test_sunting_profile_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)

    def test_sunting_profile_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_sunting_profile_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_sunting_profile_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
409
410
        self.client.logout()

411

412
413
414
415
class SuksesLoginKontributorTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
416
                                                           password="kontributor")
417
418
419
420
421
        self.admin = User.objects.create_admin(email="admin@gov.id",
                                               password="admin")
        self.url = "/sukses-kontributor/"
        self.view = SuksesLoginKontributorView
        self.template_name = "sukses_kontri.html"
422

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
    def test_sukses_login_kontributor_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)

    def test_sukses_login_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_sukses_login_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_sukses_login_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()

458

459
460
461
462
class SuksesLoginAdminTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
463
                                                           password="kontributor")
464
465
466
467
468
        self.admin = User.objects.create_admin(email="admin@gov.id",
                                               password="admin")
        self.url = "/sukses-admin/"
        self.view = SuksesLoginAdminView
        self.template_name = "sukses_admin.html"
469

470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    def test_sukses_login_admin_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)

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

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

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