Fakultas Ilmu Komputer UI

tests.py 11.1 KB
Newer Older
1
from django.test import Client, TestCase
2
from django.urls import resolve
3

4
from authentication.models import User
5
from authentication.views import Login
6
7


8
9
class UserModelTest(TestCase):
    def test_create_user(self):
10
11
12
        self.assertRaises(
            ValueError, User.objects.create_user,
        )
13
        User.objects.create_user(
14
            email="alice@acme.com", password="acmecorp").save()
15
16
17
        self.assertEqual(User.objects.all().count(), 1)

    def test_create_contributor(self):
18
19
20
21
22
23
24
25
26
27
28
        self.assertRaises(
            ValueError, User.objects.create_contributor, email="bob@acme.com", password="acmecorp", is_admin=True
        )
        self.assertRaises(
            ValueError,
            User.objects.create_contributor,
            email="bob@acme.com",
            password="acmecorp",
            is_contributor=False,
        )

29
30
        User.objects.create_contributor(
            email="bob@acme.com", password="acmecorp").save()
31
32
33
34
35
36
        kontributor = User.objects.get(email="bob@acme.com")
        self.assertTrue(kontributor.is_contributor)
        self.assertFalse(kontributor.is_admin)
        self.assertFalse(kontributor.is_superuser)

    def test_create_admin(self):
37
38
39
40
41
42
43
        self.assertRaises(
            ValueError, User.objects.create_admin, email="carol@acme.com", password="acmecorp", is_contributor=True
        )
        self.assertRaises(
            ValueError, User.objects.create_admin, email="carol@acme.com", password="acmecorp", is_admin=False
        )

44
45
        User.objects.create_admin(
            email="carol@acme.com", password="acmecorp").save()
46
47
48
49
50
51
        admin = User.objects.get(email="carol@acme.com")
        self.assertTrue(admin.is_admin)
        self.assertFalse(admin.is_contributor)
        self.assertFalse(admin.is_superuser)

    def test_create_superuser(self):
52
53
54
55
56
57
58
59
60
61
62
63
64
        self.assertRaises(
            ValueError, User.objects.create_superuser, email="dan@acme.com", password="acmecorp", is_contributor=False
        )
        self.assertRaises(
            ValueError, User.objects.create_superuser, email="dan@acme.com", password="acmecorp", is_admin=False
        )
        self.assertRaises(
            ValueError, User.objects.create_superuser, email="dan@acme.com", password="acmecorp", is_staff=False
        )
        self.assertRaises(
            ValueError, User.objects.create_superuser, email="dan@acme.com", password="acmecorp", is_superuser=False
        )

65
66
        User.objects.create_superuser(
            email="dan@acme.com", password="acmecorp").save()
67
68
69
70
71
        superuser = User.objects.get(email="dan@acme.com")
        self.assertTrue(superuser.is_admin)
        self.assertTrue(superuser.is_contributor)
        self.assertTrue(superuser.is_staff)
        self.assertTrue(superuser.is_superuser)
igor lestin sianipar's avatar
igor lestin sianipar committed
72
73


74
class LoginPageContributorTest(TestCase):
75
    def setUp(self):
76
        self.client = Client()
Steven Kusuman's avatar
Steven Kusuman committed
77
        self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
78
                                                     password="kontributor")
79
80
81
        self.url = "/login/"
        self.view = Login
        self.template_name = "login.html"
82
        self.login_credential = {
83
            "email": "kontributor@gov.id", "pass": "kontributor", "g-recaptcha-response" : "testcaptcha"}
84
85
86
87
88
        self.error_message = {
            "empty_email_or_password": "Email atau Password anda kosong.",
            "wrong_email_or_password": "Email atau Password anda salah.",
        }

Steven Kusuman's avatar
Steven Kusuman committed
89
    def test_login_kontributor_view(self):
90
91
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)
igor lestin sianipar's avatar
igor lestin sianipar committed
92

Steven Kusuman's avatar
Steven Kusuman committed
93
    def test_login_kontributor_template(self):
94
95
96
97
        # Test
        response = self.client.get(self.url)
        self.assertTemplateUsed(response, self.template_name)

Steven Kusuman's avatar
Steven Kusuman committed
98
    def test_login_kontributor_url(self):
99
100
101
102
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

Steven Kusuman's avatar
Steven Kusuman committed
103
    def test_login_kontributor_title(self):
104
105
106
107
108
        response = self.client.get(self.url)

        # Positive tests
        self.assertContains(response, "Halo, kontributor")

Steven Kusuman's avatar
Steven Kusuman committed
109
    def test_login_kontributor_form_field(self):
110
        response = self.client.get(self.url)
111

112
        # Positive tests
113
114
115
        self.assertContains(response, "Email")
        self.assertContains(response, "Kata Sandi")

Steven Kusuman's avatar
Steven Kusuman committed
116
    def test_kontributor_login_missing_email_or_password(self):
117
        response = self.client.post(self.url, {"email": "kontributor@gov.id", "g-recaptcha-response" : "testcaptcha"})
118
119
120
        self.assertIn("error_message", response.context_data)
        self.assertIn(self.error_message["empty_email_or_password"],
                      response.context_data["error_message"])
121
        response = self.client.post(self.url, {"pass": "kontributor", "g-recaptcha-response" : "testcaptcha"})
122
123
124
125
        self.assertIn("error_message", response.context_data)
        self.assertIn(self.error_message["empty_email_or_password"],
                      response.context_data["error_message"])

Steven Kusuman's avatar
Steven Kusuman committed
126
    def test_kontributor_login_wrong_email_or_password(self):
127
128
        # Wrong password
        response = self.client.post(
129
            self.url, {"email": "kontributor@gov.id", "pass": "kontributor1", "g-recaptcha-response" : "testcaptcha"})
130
131
132
133
134
        self.assertIn("error_message", response.context_data)
        self.assertIn(self.error_message["wrong_email_or_password"],
                      response.context_data["error_message"])
        # Wrong email
        response = self.client.post(
135
            self.url, {"email": "kontributor1@gov.id", "pass": "kontributor", "g-recaptcha-response" : "testcaptcha"})
136
137
138
139
140
        self.assertIn("error_message", response.context_data)
        self.assertIn(self.error_message["wrong_email_or_password"],
                      response.context_data["error_message"])
        # Wrong email and password
        response = self.client.post(
141
            self.url, {"email": "kontributor1@gov.id", "pass": "kontributor1", "g-recaptcha-response" : "testcaptcha"})
142
143
144
145
        self.assertIn("error_message", response.context_data)
        self.assertIn(self.error_message["wrong_email_or_password"],
                      response.context_data["error_message"])

Steven Kusuman's avatar
Steven Kusuman committed
146
    def test_kontributor_login(self):
147
        # 302 meaning successful login and redirected
148
        expected_redirect_url = "/sukses-kontributor/"
149
150
151
152
153
        response = self.client.post(
            self.url, self.login_credential)
        self.assertEqual(302, response.status_code)
        self.assertEqual(response.url, expected_redirect_url)

Azhar Rais's avatar
Azhar Rais committed
154
155
156
157
158
159
    def test_login_has_next_query_string(self):
        next_querystring = "?next=/forum/discussion/create"
        response = self.client.post(self.url + next_querystring, self.login_credential)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/forum/discussion/create")

Steven Kusuman's avatar
Steven Kusuman committed
160
161
162
163
164
165
166
167
168
169
170
171
172
    def test_kontributor_visit_login_after_auth(self):
        # 302 meaning successful login and redirected
        expected_redirect_url = "/sukses-kontributor/"
        response = self.client.post(
            self.url, self.login_credential)
        self.assertEqual(302, response.status_code)
        self.assertEqual(response.url, expected_redirect_url)
        response = self.client.get(
            self.url
        )
        self.assertEqual(302, response.status_code)
        self.assertEqual(response.url, expected_redirect_url)

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

class LoginPageAdminTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.admin = User.objects.create_admin(email="admin@gov.id",
                                               password="admin")
        self.url = "/login_admin/"
        self.view = Login
        self.template_name = "login_admin.html"
        self.login_credential = {"email": "admin@gov.id", "pass": "admin"}
        self.error_message = {
            "empty_email_or_password": "Email atau Password anda kosong.",
            "wrong_email_or_password": "Email atau Password anda salah.",
        }

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

    def test_login_admin_template(self):
        # Test
        response = self.client.get(self.url)
        self.assertTemplateUsed(response, self.template_name)

    def test_login_admin_url(self):
        # Test
        response = self.client.get(self.url)
200
        self.assertEqual(response.status_code, 200)
201

202
203
    def test_login_admin_title(self):
        response = self.client.get(self.url)
igor lestin sianipar's avatar
igor lestin sianipar committed
204
205

        # Positive tests
206
        self.assertContains(response, "Halo, Admin")
igor lestin sianipar's avatar
igor lestin sianipar committed
207

208
    def test_login_admin_form_field(self):
209
        response = self.client.get(self.url)
igor lestin sianipar's avatar
igor lestin sianipar committed
210
211

        # Positive tests
212
213
        self.assertContains(response, "Email")
        self.assertContains(response, "Kata Sandi")
igor lestin sianipar's avatar
igor lestin sianipar committed
214

215
216
    def test_admin_login_missing_email_or_password(self):
        response = self.client.post(self.url, {"email": "admin@gov.id"})
217
        self.assertIn("error_message", response.context_data)
218
219
220
        self.assertIn(self.error_message["empty_email_or_password"],
                      response.context_data["error_message"])
        response = self.client.post(self.url, {"pass": "admin"})
221
        self.assertIn("error_message", response.context_data)
222
223
        self.assertIn(self.error_message["empty_email_or_password"],
                      response.context_data["error_message"])
224

225
    def test_admin_login_wrong_email_or_password(self):
226
        # Wrong password
227
228
        response = self.client.post(
            self.url, {"email": "admin@gov.id", "pass": "admin1"})
229
        self.assertIn("error_message", response.context_data)
230
231
        self.assertIn(self.error_message["wrong_email_or_password"],
                      response.context_data["error_message"])
232
        # Wrong email
233
234
        response = self.client.post(
            self.url, {"email": "admin1@gov.id", "pass": "admin"})
235
        self.assertIn("error_message", response.context_data)
236
237
        self.assertIn(self.error_message["wrong_email_or_password"],
                      response.context_data["error_message"])
238
        # Wrong email and password
239
240
        response = self.client.post(
            self.url, {"email": "admin1@gov.id", "pass": "admin"})
241
        self.assertIn("error_message", response.context_data)
242
243
        self.assertIn(self.error_message["wrong_email_or_password"],
                      response.context_data["error_message"])
244

245
    def test_admin_login(self):
246
        # 302 meaning successful login and redirected
247
        expected_redirect_url = "/sukses-admin/"
248
249
        response = self.client.post(
            self.url, self.login_credential)
250
        self.assertEqual(302, response.status_code)
251
        self.assertEqual(response.url, expected_redirect_url)
Steven Kusuman's avatar
Steven Kusuman committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265


    def test_admin_visit_login_after_auth(self):
        # 302 meaning successful login and redirected
        expected_redirect_url = "/sukses-admin/"
        response = self.client.post(
            self.url, self.login_credential)
        self.assertEqual(302, response.status_code)
        self.assertEqual(response.url, expected_redirect_url)
        response = self.client.get(
            self.url
        )
        self.assertEqual(302, response.status_code)
        self.assertEqual(response.url, expected_redirect_url)