Fakultas Ilmu Komputer UI

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

4
from authentication.models import User
Rizkhi PH's avatar
Rizkhi PH committed
5
from administration.utils import id_generator
6
7
from register import views

8
9
10
11
12
13
14
15

# Create your tests here.
class RegisterPageTest(TestCase):
    def setUp(self):
        self.client = Client()

    def test_register_url_is_exist(self):
        # Positive tests
16
        response = Client().get("/registrasi/")
17
18
19
        self.assertEqual(response.status_code, 200)

        # Negative tests
20
        response = Client().get("/fake/")
21
        self.assertEqual(response.status_code, 404)
22

23
24
    def test_status_using_index_func(self):
        # Positive tests
25
        found = resolve("/registrasi/")
26
27
28
        self.assertEqual(found.func.__name__, views.index.__name__)

        # Negative tests
29
        found = resolve("/admin/")
30
        self.assertNotEqual(found.func, views.index)
31

32
    def test_register_title(self):
33
        response = Client().get("/registrasi/")
34
35

        # Positive tests
36
        self.assertContains(response, "Registrasi Kontributor")
37
38

        # Negative tests
39
        self.assertNotContains(response, "Fake Title")
40
41

    def test_register_form_field(self):
42
        response = Client().get("/registrasi/")
43
44

        # Positive tests
45
46
47
48
49
50
51
52
53
        self.assertContains(response, "Nama")
        self.assertContains(response, "Instansi/Pekerjaan")
        self.assertContains(response, "NIK")
        self.assertContains(response, "Alamat")
        self.assertContains(response, "Email")
        self.assertContains(response, "Nomor Telepon")
        self.assertContains(response, "Kata Sandi")
        self.assertContains(response, "Ketik Ulang Kata Sandi")

54
        # Negative tests
55
        self.assertNotContains(response, "Jenis Kelamin")
56
57

    def test_create_user(self):
58
        self.client.post(
59
60
61
62
            "/registrasi/",
            {
                "name": "bob",
                "instansi": "university",
63
                "nik": "3201234567890001",
64
65
66
67
68
69
70
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": "1234",
                "password2": "1234",
            },
        )
71
72
73
        self.assertEqual(User.objects.all().count(), 1)

    def test_create_user_wrong_password_combination(self):
74
75
76
77
78
        response = self.client.post(
            "/registrasi/",
            {
                "name": "bob",
                "instansi": "university",
79
                "nik": "3201234567890001",
80
81
82
83
84
85
86
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": "1234",
                "password2": "12345",
            },
        )
87
88
89
90
        self.assertEqual(User.objects.all().count(), 0)
        self.assertIn(b"Password tidak sama", response.content)

    def test_create_user_with_existing_email(self):
91
92
93
94
95
        response = self.client.post(
            "/registrasi/",
            {
                "name": "bob",
                "instansi": "university",
96
                "nik": "3201234567890001",
97
98
99
100
101
102
103
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": "1234",
                "password2": "1234",
            },
        )
104
        self.assertEqual(User.objects.all().count(), 1)
105
106
107
108
109
        response = self.client.post(
            "/registrasi/",
            {
                "name": "bob",
                "instansi": "university",
110
                "nik": "3201234567890001",
111
112
113
114
115
116
117
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": "1234",
                "password2": "1234",
            },
        )
118
        self.assertEqual(User.objects.all().count(), 1)
119
120
        self.assertIn(
            b"Email sudah digunakan untuk mendaftar akun.", response.content)
121

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
    def test_create_user_with_existing_nik(self):
        response = self.client.post(
            "/registrasi/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726601",
                "password": "123456",
                "password2": "123456",
            },
        )
        self.assertEqual(User.objects.all().count(), 1)

        response = self.client.post(
            "/registrasi/",
            {
                "name": "budi",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "budi@company.com",
                "nomor_telpon": "087878726602",
                "password": "123456",
                "password2": "123456",
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        self.assertIn(
            b"NIK sudah digunakan untuk mendaftar akun", response.content)

    def test_create_user_with_existing_nomor_telpon(self):
        response = self.client.post(
            "/registrasi/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": "123456",
                "password2": "123456",
            },
        )
        self.assertEqual(User.objects.all().count(), 1)

        response = self.client.post(
            "/registrasi/",
            {
                "name": "budi",
                "instansi": "university",
                "nik": "3201234567890002",
                "alamat": "bekasi",
                "email": "budi@company.com",
                "nomor_telpon": "087878726602",
                "password": "123456",
                "password2": "123456",
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        self.assertIn(
            b"Nomor telepon sudah digunakan untuk mendaftar akun", response.content)

    def test_create_user_input_wrong_phone_number_format(self):
        response = self.client.post(
            "/registrasi/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "abcdefghijkl",
                "password": "1234",
                "password2": "12345",
            },
        )
        self.assertEqual(User.objects.all().count(), 0)
        self.assertIn(b"Hanya masukkan angka", response.content)

205
206
207
class RegisterAdminTest(TestCase):
    def setUp(self):
        self.client = Client()
Rizkhi PH's avatar
Rizkhi PH committed
208
        self.random_password = id_generator()
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

    def test_register_url_is_exist(self):
        # Positive tests
        response = Client().get("/registrasi/admin/")
        self.assertEqual(response.status_code, 200)

        # Negative tests
        response = Client().get("/fake/")
        self.assertEqual(response.status_code, 404)

    def test_status_using_index_func(self):
        # Positive tests
        found = resolve("/registrasi/admin/")
        self.assertEqual(found.func.__name__, views.RegistrasiAdmin.__name__)

        # Negative tests
        found = resolve("/admin/")
        self.assertNotEqual(found.func, views.RegistrasiAdmin)

    def test_register_title(self):
        response = Client().get("/registrasi/admin/")

        # Positive tests
        self.assertContains(response, "Registrasi Admin")

        # Negative tests
        self.assertNotContains(response, "Fake Title")

    def test_register_form_field(self):
        response = Client().get("/registrasi/admin/")

        # Positive tests
        self.assertContains(response, "Nama")
        self.assertContains(response, "Instansi/Pekerjaan")
        self.assertContains(response, "NIK")
        self.assertContains(response, "Alamat")
        self.assertContains(response, "Email")
        self.assertContains(response, "Nomor Telepon")
        self.assertContains(response, "Kata Sandi")
        self.assertContains(response, "Ketik Ulang Kata Sandi")

        # Negative tests
        self.assertNotContains(response, "Jenis Kelamin")

    def test_create_user(self):
        self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
259
                "nik": "3201234567890002",
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": "1234",
                "password2": "1234",
            },
        )
        self.assertEqual(User.objects.all().count(), 1)

    def test_create_user_wrong_password_combination(self):
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
275
                "nik": "3201234567890002",
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": "1234",
                "password2": "12345",
            },
        )
        self.assertEqual(User.objects.all().count(), 0)
        self.assertIn(b"Password tidak sama", response.content)

    def test_create_user_with_existing_email(self):
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
292
                "nik": "3201234567890001",
293
294
295
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
Rizkhi PH's avatar
Rizkhi PH committed
296
297
                "password": self.random_password,
                "password2": self.random_password,
298
299
300
301
302
303
304
305
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
306
                "nik": "3201234567890002",
307
308
309
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
Rizkhi PH's avatar
Rizkhi PH committed
310
311
                "password": self.random_password,
                "password2": self.random_password,
312
313
314
315
316
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        self.assertIn(
            b"Email sudah digunakan untuk mendaftar akun.", response.content)
Rizkhi PH's avatar
Rizkhi PH committed
317
318
319
320
321
322
323

    def test_create_user_success_but_status_is_inactive(self):
        self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
324
                "nik": "3201234567890002",
Rizkhi PH's avatar
Rizkhi PH committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": self.random_password,
                "password2": self.random_password,
            },
        )
        self.assertEqual(User.objects.filter(name="bob").get().is_active, False)

    def test_create_user_success_and_show_message_to_wait_for_internal_team(self):
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
340
                "nik": "3201234567890002",
Rizkhi PH's avatar
Rizkhi PH committed
341
342
343
344
345
346
347
348
349
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": self.random_password,
                "password2": self.random_password,
            },
        )
        self.assertEqual(User.objects.count(), 1)
        self.assertIn("Please wait for our internal team to accept your admin account", response.content.decode())
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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
422
423
424
425
426
427
428
429
430

    def test_create_user_with_existing_nik(self):
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726601",
                "password": self.random_password,
                "password2": self.random_password,
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726602",
                "password": self.random_password,
                "password2": self.random_password,
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        self.assertIn(
            b"NIK sudah digunakan untuk mendaftar akun", response.content)

    def test_create_user_with_existing_nomor_telpon(self):
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726601",
                "password": self.random_password,
                "password2": self.random_password,
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890002",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "087878726601",
                "password": self.random_password,
                "password2": self.random_password,
            },
        )
        self.assertEqual(User.objects.all().count(), 1)
        self.assertIn(
            b"Nomor telepon sudah digunakan untuk mendaftar akun", response.content)

    def test_create_user_input_wrong_phone_number_format(self):
        response = self.client.post(
            "/registrasi/admin/",
            {
                "name": "bob",
                "instansi": "university",
                "nik": "3201234567890001",
                "alamat": "bekasi",
                "email": "bob@company.com",
                "nomor_telpon": "abcdefghij",
                "password": self.random_password,
                "password2": self.random_password,
            },
        )
        self.assertEqual(User.objects.all().count(), 0)
        self.assertIn(b"Hanya masukkan angka", response.content)