Fakultas Ilmu Komputer UI

tests.py 65.4 KB
Newer Older
1
from django.contrib.auth import get_user_model
2
3
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import Client, TestCase, tag
4
from django.urls import resolve
5
6

from administration import models, views
7
from administration.utils import id_generator
Arief Pratama's avatar
Arief Pratama committed
8
from administration.forms import EditAdminStatusForm, EditKontributorStatusForm
9
from app.models import Category, Materi, LaporanMateri
Rizkhi PH's avatar
Rizkhi PH committed
10
from authentication.models import User
11
from bs4 import BeautifulSoup
12

13
14
15
from datetime import datetime, timedelta

from .forms import PeriodForm
16

Rizkhi PH's avatar
Rizkhi PH committed
17
EDIT_ENDPOINT = "/edit"
18
ERROR_403_MESSAGE = 'Kamu harus login untuk mengakses halaman ini'
19
20
21
22

class VerifikasiMateriTest(TestCase):
    def setUp(self):
        self.client = Client()
23
24
25
26
27
28
29
30
31
        self.url = '/administration/'
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
32
        self.admin = get_user_model().objects.create_user(
33
            **self.admin_credential, name="Admin", is_admin=True)
34
        self.contributor = get_user_model().objects.create_user(
35
            **self.contributor_credential, name="Kontributor", is_contributor=True
36
        )
37
38
39
40
41
42
43
44
45
46
        self.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            b"Test file"
        )
        Materi(title="Materi 1", author="Agas", uploader=self.contributor,
               publisher="Kelas SC", descriptions="Deskripsi Materi 1",
47
               status="PENDING", cover=self.cover, content=self.content).save()
48
        self.materi1 = Materi.objects.first()
49

50
    def test_verification_detail_admin_access(self):
51
        # Login
52
53
54
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
55
        self.assertEqual(response.status_code, 200)
56
57
58
59
60
61
62
63
64
        # Logout
        self.client.logout()

    def test_verification_detail_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
65
66
67

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
68
69
70
71
72
73
74
        # Logout
        self.client.logout()

    def test_verification_detail_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
75

76
77
78
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

79
    def test_verifikasi_materi_using_correct_template(self):
80
        # Login
81
82
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
83
        self.assertEqual(found.func.__name__, views.VerificationView.__name__)
84
85

    def test_verifikasi_materi_title(self):
86
        # Login
87
88
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
89

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
90
        self.assertContains(response, 'Verifikasi Materi | Digipus')
91
92
93
94

        self.assertNotContains(response, 'Fake Title')

    def test_verifikasi_materi_content(self):
95
        # Login
96
97
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
98

99
100
101
102
103
104
105
        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTablePending"}).find(
            "td", string="Materi 1").find_parent().get_text()

        self.assertIn('Materi 1', el)
        self.assertIn(self.contributor.name, el)
        self.assertIn("Diproses", el)
106
107

        self.assertNotContains(response, 'Halaman Katalog')
108

109
110
111
112
113

class DetailVerifikasiMateriTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.view = views.DetailVerificationView
114
115
116
117
118
119
120
121
122
123
124
125
126
        self.template_name = "detail_verif.html"
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        self.kriteria = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
127
        self.admin = get_user_model().objects.create_user(
128
            **self.admin_credential, name="Admin", is_admin=True)
129
        self.contributor = get_user_model().objects.create_user(
130
            **self.contributor_credential, name="Kontributor", is_contributor=True
131
        )
132
133
134
135
136
137
138
139
140
141
        self.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"  # no b
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            b"Test file"  # no b
        )
        Materi(title="Materi 1", author="Agas", uploader=self.contributor,
               publisher="Kelas SC", descriptions="Deskripsi Materi 1",
142
               status="PENDING", cover=self.cover, content=self.content).save()
143
144
145
146
147
148
        models.VerificationSetting(
            title="Kriteria 1", description="Deskripsi Kriteria 1").save()
        self.materi1 = Materi.objects.first()
        self.kriteria1 = models.VerificationSetting.objects.first()
        self.url_materi_1 = f'/administration/detail-verif/{self.materi1.id}/'
        self.url_dashboard = "/administration/"
149

150
151
    def test_verification_detail_view(self):
        found = resolve(self.url_materi_1)
152
153
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)

154
    def test_verification_detail_template(self):
155
        # Login
156
        self.client.login(**self.admin_credential)
157
        # Test
158
159
        response = self.client.get(self.url_materi_1)
        self.assertTemplateUsed(response, self.template_name)
160
161
162
        # Logout
        self.client.logout()

163
    def test_verification_detail_admin_access(self):
164
        # Login
165
        self.client.login(**self.admin_credential)
166
        # Test
167
        response = self.client.get(self.url_materi_1)
168
169
170
171
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

172
173
174
175
176
177
    def test_verification_detail_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url_materi_1)
        self.assertEqual(response.status_code, 403)
178
179
180

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
181
182
183
184
185
186
187
188
        # Logout
        self.client.logout()

    def test_verification_detail_anonymous_access(self):
        # Test
        response = self.client.get(self.url_materi_1)
        self.assertEqual(response.status_code, 403)

189
190
191
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
296
297
298
299
300
301
302
303
304
305
306
307
308
    def test_verification_detail_approve(self):
        # Login
        self.client.login(**self.admin_credential)

        # Book exists in dashboard
        response = self.client.get(self.url_dashboard)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTablePending"}).find(
            "td", string="Materi 1").find_parent().get_text()

        self.assertIn('Materi 1', el)
        self.assertIn(self.contributor.name, el)
        self.assertIn("Diproses", el)

        # acces detail verif
        # Test
        response = self.client.get(self.url_materi_1)
        self.assertContains(response, 'Tampilkan Kriteria')
        # aprove
        response = self.client.post(self.url_materi_1, {
                                    "action": "approve", "feedback": "materi bagus", f"kriteria-{self.kriteria1.id}": "1"}, follow=True)

        self.assertRedirects(response, self.url_dashboard)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTableRiwayat"}).find(
            "td", string="Materi 1").find_parent().get_text()

        self.assertIn('Materi 1', el)
        self.assertIn(self.contributor.name, el)
        self.assertIn("Diterima", el)

        # Logout
        self.client.logout()

    def test_verification_detail_disapprove(self):
        # Login
        self.client.login(**self.admin_credential)

        # Book exists in dashboard
        response = self.client.get(self.url_dashboard)
        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTablePending"}).find(
            "td", string="Materi 1").find_parent().get_text()

        self.assertIn('Materi 1', el)
        self.assertIn(self.contributor.name, el)
        self.assertIn("Diproses", el)

        # acces detail verif
        # Test
        response = self.client.get(self.url_materi_1)
        self.assertContains(response, 'Tampilkan Kriteria')
        # aprove
        response = self.client.post(self.url_materi_1, {
                                    "action": "disapprove", "feedback": "materi kurang bagus", f"kriteria-{self.kriteria1.id}": "0"}, follow=True)

        self.assertRedirects(response, self.url_dashboard)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTableRiwayat"}).find(
            "td", string="Materi 1").find_parent().get_text()

        self.assertIn('Materi 1', el)
        self.assertIn(self.contributor.name, el)
        self.assertIn("Ditolak", el)

        # Logout
        self.client.logout()

    def test_verification_detail_missing_feedback(self):
        # Login
        self.client.login(**self.admin_credential)

        # Book exists in dashboard
        response = self.client.get(self.url_dashboard)

        self.assertContains(response, 'Materi 1')
        self.assertContains(response, "Diproses")

        # acces detail verif
        # Test
        response = self.client.get(self.url_materi_1)
        self.assertContains(response, 'Tampilkan Kriteria')
        # missing feedback
        response = self.client.post(
            self.url_materi_1, {"action": "disapprove"}, follow=True)
        self.assertTemplateUsed(response, self.template_name)
        self.assertContains(response, 'Terjadi Kesalahan, coba lagi.')

        # Logout
        self.client.logout()

    def test_verification_detail_missing_action(self):
        # Login
        self.client.login(**self.admin_credential)

        # Book exists in dashboard
        response = self.client.get(self.url_dashboard)

        self.assertContains(response, 'Materi 1')
        self.assertContains(response, "Diproses")

        # acces detail verif
        # Test
        response = self.client.get(self.url_materi_1)
        self.assertContains(response, 'Tampilkan Kriteria')
        # missing feedback
        response = self.client.post(
            self.url_materi_1, {"feedback": "materi kurang bagus"}, follow=True)
        self.assertTemplateUsed(response, self.template_name)
        self.assertContains(response, 'Terjadi Kesalahan, coba lagi.')

        # Logout
        self.client.logout()

309

310
311
312
class SettingVerifikasiTest(TestCase):
    def setUp(self):
        self.client = Client()
313
        self.view = views.VerificationSettingView
314
        self.url = "/administration/setting/verification/"
315
        self.template_name = "settings.html"
316
        self.model = models.VerificationSetting
317
318
319
320
321
322
323
324
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
325
        self.admin = get_user_model().objects.create_user(
326
            **self.admin_credential, name="Admin", is_admin=True)
327
        self.contributor = get_user_model().objects.create_user(
328
            **self.contributor_credential, name="Kontributor", is_contributor=True
329
        )
330

331
332
333
    def test_setting_verification_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)
334

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    def test_setting_verification_template(self):
        # Login
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
        self.assertTemplateUsed(response, self.template_name)
        # Logout
        self.client.logout()

    def test_setting_verification_admin_access(self):
        # Login
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

    def test_setting_verification_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
359
360
361

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
362
363
364
365
366
367
368
369
        # Logout
        self.client.logout()

    def test_setting_verification_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

370
371
372
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

373
374
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
409
410
411
412
413
414
415
416
417
418
419
420
421
    def test_setting_verification_add(self):
        # Login
        self.client.login(**self.admin_credential)

        # Test
        response = self.client.get(self.url)
        self.assertContains(response, 'Pengaturan Point Verifikasi Materi')

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTable"}).get_text()

        self.assertNotIn('Kriteria 1', el)
        self.assertNotIn('Deskripsi Kriteria 1', el)

        # added new item
        response = self.client.post(self.url, {
                                    "title": "Kriteria 1", "description": "Deskripsi Kriteria 1"}, follow=True)

        self.assertRedirects(response, self.url)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTable"}).get_text()

        self.assertIn('Kriteria 1', el)
        self.assertIn('Deskripsi Kriteria 1', el)

        # Logout
        self.client.logout()

    def test_setting_verification_add_missing_attribute(self):
        # Login
        self.client.login(**self.admin_credential)

        # added new item
        response = self.client.post(
            self.url, {"description": "Deskripsi Kriteria 1"}, follow=True)

        self.assertTemplateUsed(response, self.template_name)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTable"}).get_text()

        self.assertNotIn('Kriteria 1', el)
        self.assertNotIn('Deskripsi Kriteria 1', el)
        # Logout
        self.client.logout()


class SettingCategoriTest(TestCase):
422
423
    def setUp(self):
        self.client = Client()
424
        self.view = views.CategorySettingView
425
        self.url = "/administration/setting/category/"
426
        self.template_name = "settings.html"
427
        self.model = Category
428
429
430
431
432
433
434
435
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
436
        self.admin = get_user_model().objects.create_user(
437
            **self.admin_credential, name="Admin", is_admin=True)
438
        self.contributor = get_user_model().objects.create_user(
439
            **self.contributor_credential, name="Kontributor", is_contributor=True
440
        )
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469

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

    def test_setting_category_template(self):
        # Login
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
        self.assertTemplateUsed(response, self.template_name)
        # Logout
        self.client.logout()

    def test_setting_category_admin_access(self):
        # Login
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

    def test_setting_category_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
470
471
472

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
473
474
475
476
477
478
479
480
        # Logout
        self.client.logout()

    def test_setting_category_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

481
482
483
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
    def test_setting_category_add(self):
        # Login
        self.client.login(**self.admin_credential)

        # Test
        response = self.client.get(self.url)
        self.assertContains(response, 'Pengaturan Kategori Materi')

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTable"}).get_text()

        self.assertNotIn('Kategori 1', el)
        self.assertNotIn('Deskripsi Kategori 1', el)

        # added new item
        response = self.client.post(self.url, {
                                    "name": "Kategori 1", "description": "Deskripsi Kategori 1"}, follow=True)

        self.assertRedirects(response, self.url)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTable"}).get_text()

        self.assertIn('Kategori 1', el)
        self.assertIn('Deskripsi Kategori 1', el)

        # Logout
        self.client.logout()

    def test_setting_category_add_missing_attribute(self):
        # Login
        self.client.login(**self.admin_credential)

        # added new item
        response = self.client.post(
            self.url, {"description": "Deskripsi Kategori 1"}, follow=True)

        self.assertTemplateUsed(response, self.template_name)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTable"}).get_text()

        self.assertNotIn('Kategori 1', el)
        self.assertNotIn('Deskripsi Kategori 1', el)
        # Logout
        self.client.logout()
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
530

531

532
class KelolaKontributorTest(TestCase):
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
533
534
    def setUp(self):
        self.client = Client()
535
        self.url = '/administration/kelola-kontributor/'
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        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.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            b"Test file"
        )
        Materi(title="Materi 1", author="Agas", uploader=self.contributor,
               publisher="Kelas SC", descriptions="Deskripsi Materi 1",
559
               status="PENDING", cover=self.cover, content=self.content).save()
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
        self.materi1 = Materi.objects.first()

    def test_contributor_list_access(self):
        # Login
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

    def test_contributor_list_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
577
578
579

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
580
581
582
583
584
585
586
587
        # Logout
        self.client.logout()

    def test_contributor_list_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

588
589
590
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
591
592
593
594
    def test_contributor_list_using_correct_template(self):
        # Login
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
595
        self.assertEqual(found.func.__name__,
596
                         views.KelolaKontributorView.__name__)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
597
598
599
600
601
602

    def test_contributor_list_title(self):
        # Login
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
603
        self.assertContains(response, 'Kelola Kontributor | Digipus')
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
604
605

        self.assertNotContains(response, 'Fake Title')
606

607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
class KelolaAdminTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.url = '/administration/kelola-admin/'
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        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.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            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).save()
        self.materi1 = Materi.objects.first()

    def test_admin_list_access(self):
        # Login
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

    def test_admin_list_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
652
653
654

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
655
656
657
658
659
660
661
662
        # Logout
        self.client.logout()

    def test_admin_list_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

663
664
665
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

666
667
668
669
670
671
672
673
674
675
676
677
    def test_admin_list_using_correct_template(self):
        # Login
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
        self.assertEqual(found.func.__name__,
                         views.KelolaAdminView.__name__)

    def test_admin_list_title(self):
        # Login
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
678
        self.assertContains(response, 'Kelola Admin | Digipus')
679
680

        self.assertNotContains(response, 'Fake Title')
681

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
class ProfileContributorAdminTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.url = '/administration/profil/1/'
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        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.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            b"Test file"
        )
        Materi(title="Materi 1", author="Agas", uploader=self.contributor,
               publisher="Kelas SC", descriptions="Deskripsi Materi 1",
709
               status="PENDING", cover=self.cover, content=self.content).save()
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
710
711
712
713
714
715
716
        self.materi1 = Materi.objects.first()

    def test_profile_contributor_admin_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

717
718
719
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
720
721
722
723
    def test_profile_contributor_admin_using_correct_template(self):
        # Login
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
724
725
        self.assertEqual(found.func.__name__,
                         views.ProfileContributorAdminView.__name__)
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763

class ProfileAdminAdministrationTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.url = '/administration/profil-admin/1/'
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        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.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            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).save()
        self.materi1 = Materi.objects.first()


    def test_profile_admin_administration_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
764
765
766

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
767
768
769
770
771
772
773
774
        # Logout
        self.client.logout()

    def test_profile_admin_administration_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

775
776
777
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
    def test_profile_admin_administration_using_correct_template(self):
        # Login
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
        self.assertEqual(found.func.__name__,
                         views.ProfileAdminAdministrationView.__name__)

class RegistrasiAdminTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.url = '/administration/kelola-admin/tambah/'
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        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.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            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).save()
        self.materi1 = Materi.objects.first()

    def test_registrasi_admin_admin_access(self):
        # Login
        self.client.login(**self.admin_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

    def test_registrasi_admin_admin_contributor_access(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
830
831
832

        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)
833
834
835
836
837
838
839
840
        # Logout
        self.client.logout()

    def test_registrasi_admin_admin_anonymous_access(self):
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

841
842
843
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

844
845
846
847
848
849
850
851
852
853
854
855
856
    def test_registrasi_admin_using_correct_template(self):
        # Login
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, views.RegistrasiAdminView.__name__)

    def test_registrasi_admin_title(self):
        # Login
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)

        self.assertContains(response, 'Dasbor - Kelola Admin')

857
858
859
860
861
        self.assertNotContains(response, 'Fake Title')

class EditVerificationTest(TestCase):
    def setUp(self):
        self.client = Client()
862
        self.template_name = "edit_settings.html"
863
864
865
866
867
868
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.admin = get_user_model().objects.create_user(
            **self.admin_credential, name="Admin", is_admin=True)
869
        models.VerificationSetting(title='test', description="desc test").save()
870
        self.verification = models.VerificationSetting.objects.first()
Rizkhi PH's avatar
Rizkhi PH committed
871
        self.url = "/administration/setting/verification/" + str(self.verification.id) + EDIT_ENDPOINT
872
873
874
875
876
877
878

    def test_edit_verification_admin_access(self):
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.status_code, 404)

879
    def test_edit_verifikasi_anonymous_access(self):
880
881
882
883
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
        self.assertNotEqual(response.status_code, 200)

884
885
886
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

887
    def test_edit_verifikasi_using_correct_template(self):
888
889
890
891
892
893
894
895
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, views.EditVerificationView.__name__)

    def test_edit_verifikasi_item_content(self):
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
        self.assertContains(response, self.verification.title)
896
897
898
899
900
901
902
903
904
905
906
907
        self.assertContains(response, self.verification.description)

    def test_post_edit_verifikasi_item_content(self):
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
        self.assertContains(response, self.verification.title)
        self.assertContains(response, self.verification.description)
    
    def test_delete_verification_data(self):
        self.client.login(**self.admin_credential)
        deleteObject = models.VerificationSetting.objects.create(title="deletelah", description="delete test")
        self.client.get("/administration/delete-verification/" + str(deleteObject.id) + "/")
908
909
        self.assertEqual(models.VerificationSetting.objects.get(
            title="deletelah").archived, True)
910
911
912
913
914
915
    
    def test_delete_verification_data_by_anonymous(self):
        self.client.logout()
        deleteObject = models.VerificationSetting.objects.create(title="deletelah", description="delete test")
        response = self.client.get("/administration/delete-verification/" + str(deleteObject.id) + "/")
        self.assertEqual(response.status_code, 403)
916

917
918
919
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
    def test_edit_verification(self):
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("input", {"id": "id_title"})["value"]

        self.assertEqual(self.verification.title, el)

        response = self.client.post(self.url, {
                                    "title": "Kriteria 1", "description": "Deskripsi Kriteria 1"}, follow=True)

        bs = BeautifulSoup(response.rendered_content, features="html.parser")
        el = bs.find("table", {"id": "dataTable"}).get_text()

        self.assertIn('Kriteria 1', el)
        self.assertIn('Deskripsi Kriteria 1', el)
    
    def test_edit_verification_missing_attribute(self):
        # Login
        self.client.login(**self.admin_credential)
        response = self.client.post(
            self.url, {"description": "Deskripsi Kriteria 1"}, follow=True)

        self.assertTemplateUsed(response, self.template_name)
945
946
947
948
949
950
951
952
953
954
955
956
957
958
        self.assertContains(response, "Edit Verifikasi Materi")

class EditCategoryTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.template_name = "edit_settings.html"
        self.admin_credential = {
            "email": "admin@gov.id",
            "password": id_generator()
        }
        self.admin = get_user_model().objects.create_user(
            **self.admin_credential, name="Admin", is_admin=True)
        Category(name='test', description="desc test").save()
        self.category = Category.objects.first()
Rizkhi PH's avatar
Rizkhi PH committed
959
        self.url = "/administration/setting/category/" + str(self.category.id) + EDIT_ENDPOINT
960
961
962
963
964
965
966
967
968
969
970
971

    def test_edit_category_admin_access(self):
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.status_code, 404)

    def test_edit_category_anonymous_access(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)
        self.assertNotEqual(response.status_code, 200)

972
973
974
        html = response.content.decode("utf-8")
        self.assertIn(ERROR_403_MESSAGE, html)

975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
    def test_edit_category_using_correct_template(self):
        self.client.login(**self.admin_credential)
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, views.EditCategoryView.__name__)

    def test_edit_category_item_content(self):
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
        self.assertContains(response, self.category.name)
        self.assertContains(response, self.category.description)

    def test_post_edit_category_item_content(self):
        self.client.login(**self.admin_credential)
        response = self.client.get(self.url)
        self.assertContains(response, self.category.name)
        self.assertContains(response, self.category.description)
    
    def test_delete_category_data(self):
        self.client.login(**self.admin_credential)
        deleteObject = Category.objects.create(name="deletelah", description="delete test")
        self.client.get("/administration/delete-category/" + str(deleteObject.id) + "/")
996
997
        self.assertEqual(Category.objects.get(
            name="deletelah").archived, True)
998
999
1000
    
    def test_delete_category_data_by_anonymous(self):
        self.client.logout()