Fakultas Ilmu Komputer UI

tests.py 7.17 KB
Newer Older
1
import json
2
from io import StringIO
3

4
from django.contrib.auth import get_user_model
5
from django.core.exceptions import PermissionDenied, ValidationError
6
from django.core.files.uploadedfile import SimpleUploadedFile
7
8
from django.core.management import call_command
from django.test import Client, RequestFactory, TestCase
9
from django.db import IntegrityError
10
11
from django.urls import resolve

12
from administration.models import VerificationSetting, VerificationReport
13
from administration.utils import id_generator
14
from app.views import UploadMateriView
15
from authentication.models import User
16
from .models import Category, Comment, Materi, Like, Rating
17
18
from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri,
                    ProfilKontributorView, SuksesLoginAdminView,
19
                    SuksesLoginKontributorView, SuntingProfilView,
20
                    ProfilAdminView, PostsView, SuntingProfilAdminView, RevisiMateriView)
21
from app.forms import SuntingProfilForm
22

23
24
25
26

class DaftarKatalogTest(TestCase):
    def test_daftar_katalog_url_exist(self):
        url = "/"
27
28
        response = Client().get(f"{url}")
        self.assertEqual(response.status_code, 200)
29
30

    def test_daftar_katalog_using_daftar_katalog_template(self):
31
32
33
        response = Client().get("/")
        self.assertTemplateUsed(response, "app/katalog_materi.html")

34
    def test_daftar_katalog_using_daftar_katalog_func(self):
35
        found = resolve("/")
36
37
38
39
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)

    def test_fields(self):
        materi = Materi()
40
41
42
        materi.title = "tes"
        materi.cover = "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg"
        materi.author = "input"
43
44
        materi.save()

45
46
47
48
        resp = Materi.objects.get(id=materi.id)
        self.assertEqual(resp, materi)


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

    def test_detail_materi_url_exist(self):
82
        response = Client().get(self.url)
83
        self.assertEqual(response.status_code, 200)
84
85
86
        self.assertNotEqual(response.status_code, 404)

    def test_detail_materi_using_detail_materi_template(self):
87
        response = Client().get(self.url)
88
        self.assertTemplateUsed(response, 'app/detail_materi.html')
89

Saul Andre's avatar
Saul Andre committed
90
    def test_detail_materi_using_detail_materi_func(self):
91
        found = resolve(self.url)
Saul Andre's avatar
Saul Andre committed
92
        self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
93

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

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

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

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

127
    def test_comment_by_admin(self):
128
129
130
131
        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)
132
133
134
        self.assertContains(response, "Admin")

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

    def test_comment_by_anonymous(self):
143
144
145
146
147
        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)
148
        self.assertContains(response, "Anonymous")
149

150
    def test_detail_materi_contains_form_comment(self):
151
        response = self.client.get(self.url)
152
        self.assertContains(response, "Beri komentar...")
153

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

163
164
165
    def test_tombol_citasiAPA(self):
        response = self.client.get(self.url)
        self.assertContains(response, 'Citate APA')
166

167
    def test_hasil_citasi_APA(self):
168
        response = self.client.get(self.url)
169
170
171
172
173
174
175
176
        publishedDate = ''
        if(self.materi1.published_date == None):
            publishedDate = 'n.d'
        else :
            publishedDate = self.materi1.published_date
        expected = self.materi1.author+' . ('  + publishedDate +') . ' + self.materi1.title +' . '+ self.materi1.publisher
        self.assertEqual(expected,
                      response.context["citationAPA"])
177