Fakultas Ilmu Komputer UI

test_accounts.py 19.2 KB
Newer Older
1
import requests_mock
INDRA SEPTAMA's avatar
INDRA SEPTAMA committed
2
from datetime import date, timedelta
3
from rest_framework import status
4
from rest_framework.test import APIClient, APITestCase
5
from django.contrib.auth.models import User
6
from django.core.exceptions import ValidationError
7
from StringIO import StringIO
INDRA SEPTAMA's avatar
INDRA SEPTAMA committed
8
from core.models.accounts import Company, Supervisor, Student, get_current_age
9
10
from core.tests.mocks import (mock_csui_oauth_verify, mock_csui_ldap_student, mock_csui_ldap_lecturer,
                              mock_csui_ldap_fail, mock_csui_siak_student)
11
12


13
class LoginTests(APITestCase):
14
15
    @requests_mock.Mocker()
    def test_succesful_student_login_relogin(self, m):
16
17
18
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)
19
20
21
22
23
24
25
26
27
28
29

        url = '/api/login/'

        response = self.client.post(url, { 'username' : 'dummy.mahasiswa', 'password' : 'lalala', 'login-type' : 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_successful_supervisor_login_relogin(self, m):
30
31
        mock_csui_oauth_verify(m)
        mock_csui_ldap_lecturer(m)
32
33
34
35
36
37
38
39
40
41

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.dosen', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        response = self.client.post(url, {'username': 'dummy.dosen', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_failed_sso_login(self, m):
42
        mock_csui_ldap_fail(m)
43
44
45
46
47
48
49
50
51
52
53
54

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.salah', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_failed_company_login(self):
        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.company.failed', 'password': 'lalala', 'login-type': 'company'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_success_company_login(self):
        new_user = User.objects.create_user('dummy.login.company', 'dummy.login.company@company.com', 'lalala123')
55
        Company.objects.create(user=new_user, description="lalalala", status=Company.VERIFIED, logo=None, address=None)
56
57
58

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123', 'login-type': 'company'}, format='json')
59
60
61
62
63
64
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_bad_request_1(self):
        url = '/api/login/'
        response = self.client.post(url, {'username': 'lalala'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
65

66
    def test_bad_request_2(self):
67
        url = '/api/login/'
68
69
        response = self.client.post(url, {'username': 'lalala', 'password': 'lalalala', 'login-type' : 'lalala'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
70
71
72
73

class RegisterTests(APITestCase):
    def test_create_and_recreate(self):
        url = '/api/register/'
74
        tc_post = {'password': 'corporatepass', 'name':'tutuplapak', 'description':'menutup lapak', 'email': 'email@email.com', 'logo':'lalala', 'address':'alamat', 'category':'Perusahaan Jasa', 'website':'www.tutuplapak.com'}
75
76
77
78
79
80
81
82
83
84
        response = self.client.post(url, tc_post, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        response = self.client.post(url, tc_post, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)

    def test_bad_request(self):
        url = '/api/register/'
        response = self.client.post(url, {'username': 'lalala'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
85

86
87
88
89
90
91
92
93
94
95
    def test_npm_validator(self):
        new_user = User.objects.create_user('dummy.student2', 'dummy.student@student.com', 'lalala123')
        short_npm = Student(user=new_user, npm="123456789")
        self.assertRaises(ValidationError, short_npm.full_clean)
        wrong_checksum = Student(user=new_user, npm="1234567890")
        self.assertRaises(ValidationError, wrong_checksum.full_clean)
        right_checksum = Student(user=new_user, npm="1234567894")
        right_checksum.full_clean()


96

97
class ProfileUpdateTests(APITestCase):
98
    def _create_test_file_pdf(self, path):
Yumna Pratista Tastaftian's avatar
Yumna Pratista Tastaftian committed
99
100
        file =  open(path, 'rb')
        return {'pdf_sertifikat':file}
101
102
103

    @requests_mock.Mocker()
    def test_student_profile_update(self, m):
104
105
106
107
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)

108
109
110
111
112
        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
        student_id = response.data.get('student').get('id')

Zamil Majdy's avatar
Zamil Majdy committed
113
        url = '/api/students/' + str(student_id) + "/profile/"
114
115
116
117
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('phone_number'), '08123123123')

PRIAMBUDI LINTANG BAGASKARA's avatar
PRIAMBUDI LINTANG BAGASKARA committed
118
        url = '/api/students/' + str(student_id) + "/profile/"
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
119
120
121
        response = self.client.patch(url, {'portfolio_link': 'https://www.example.com/myproject'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('portfolio_link'), 'https://www.example.com/myproject')
122

Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
123
124
125
126
127
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'portfolio_link': 'asdfghjkl'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        url = '/api/students/' + str(student_id) + "/profile/"
128
129
130
131
        response = self.client.patch(url, {'self_description': 'I am very happy'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('self_description'), 'I am very happy')

132
133
134
135
        response = self.client.patch(url, {'linkedin_url': 'https://www.linkedin.com/in/jojo/'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('linkedin_url'), 'https://www.linkedin.com/in/jojo/')

136
        response = self.client.patch(url, {'hackerrank_url': 'https://www.hackerrank.com/james'}, format='multipart')
Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
137
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
138
        self.assertEqual(response.data.get('hackerrank_url'), 'https://www.hackerrank.com/james')
Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
139

140
141
142
143
        response = self.client.patch(url, {'alamat': 'JL.Satya Raya No.41'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('alamat'), 'JL.Satya Raya No.41')

144
145
146
147
148
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'recommendations': 'mantap kak'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('recommendations'), 'mantap kak')

PRIAMBUDI LINTANG BAGASKARA's avatar
PRIAMBUDI LINTANG BAGASKARA committed
149
150
151
152
        response = self.client.patch(url, {'region': 'Indonesia'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('region'), 'Indonesia')

Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
153
154
155
156
157
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'gender': 'Perempuan'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('gender'), 'Perempuan')

Tyagita Larasati's avatar
Tyagita Larasati committed
158
159
160
161
162
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'skills': 'Java, Python'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('skills'), 'Java, Python')

Zamil Majdy's avatar
Zamil Majdy committed
163
        url = '/api/students/' + str(student_id) + "/profile/"
164
165
166
        response = self.client.patch(url, {'email': 'saasdasd'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

167
168
169
170
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'phone_number': '````'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Zamil Majdy's avatar
Zamil Majdy committed
171
        url = '/api/students/123123123/profile/'
172
173
174
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

175
176
177
178
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'linkedin_url': 'this is not valid url'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
179
        url = '/api/students/' + str(student_id) + "/profile/"
180
        response = self.client.patch(url, {'hackerrank_url': 'this is not valid url'}, format='multipart')
Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
181
182
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

183
184
185
        new_user = User.objects.create_user('dummy.student2', 'dummy.student@student.com', 'lalala123')
        new_student = Student.objects.create(user=new_user, npm="1212121212")

Zamil Majdy's avatar
Zamil Majdy committed
186
        url = '/api/students/' + str(new_student.pk) + "/profile/"
187
188
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
189
190

        url = '/api/students/' + str(student_id) + "/profile/"
191
        data = self._create_test_file('./assets/pdf/pdf-coba.pdf')
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
192
193
194
        response = self.client.patch(url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

195
196
197
198
        url = '/api/students/' + str(student_id) + "/profile/"
        data = self._create_test_file('./assets/img/image-coba.jpg')
        response = self.client.patch(url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
199

Kristianto's avatar
Kristianto committed
200
        url = '/api/students/' + str(student_id) + "/profile/"
201
202
203
204
205
206
207
208
        response = self.client.patch(url, {'latest_work': 'Teaching assistant at Fasilkom UI'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('latest_work'), 'Teaching assistant at Fasilkom UI')

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'latest_work_desc': 'Evaluate weekly assignment for 15 students'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('latest_work_desc'), 'Evaluate weekly assignment for 15 students')
209

HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
210
211
212
213
        url = '/api/students/' + str(student_id) + '/profile/'
        response = self.client.patch(url, {'intro': 'Saya tertarik dengan dunia front-end development'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('intro'), 'Saya tertarik dengan dunia front-end development')
214

Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
215
216
217
218
219
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'awards': 'Juara 2 UIUX Gemastik 2019'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('awards'), 'Juara 2 UIUX Gemastik 2019')

Aviliani Pramestya's avatar
Aviliani Pramestya committed
220
221
222
223
224
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'projects': 'Ow-Jek - Android Mobile Project'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('projects'), 'Ow-Jek - Android Mobile Project')

Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
225
226
227
228
229
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'certification': 'TOEFL'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('certification'), 'TOEFL')
        
Tyagita Larasati's avatar
Tyagita Larasati committed
230
231
232
233
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'languages': 'Indonesia, Inggris'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('languages'), 'Indonesia, Inggris')
Aldo Bima Syahputra's avatar
Aldo Bima Syahputra committed
234

Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
235
236
237
238
239
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'seminar': 'Seminar CompFest'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('seminar'), 'Seminar CompFest')

Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
240
        url = '/api/students/' + str(student_id) + "/profile/"
Aldo Bima Syahputra's avatar
Aldo Bima Syahputra committed
241
242
243
244
245
246
247
        response = self.client.patch(url, {'student_gpa': '4'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'student_gpa': '8'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
248
249
250
251
        response = self.client.patch(url, {'interests': 'Machine Learning'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('interests'), 'Machine Learning')

252
253
    @requests_mock.Mocker()
    def test_student_profile_update_filetype_validation(self, m):
254
255
256
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
        student_id = response.data.get('student').get('id')

        url = '/api/students/' + str(student_id) + '/profile/'
        imgfile = StringIO('JPG\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
                     '\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
        imgfile.name = 'test_img_file.jpg'
        response = self.client.patch(url, {'photo': imgfile}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        imgfile.name = 'test_img_file.pdf'
        response = self.client.patch(url, {'photo': imgfile}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
273
        url = '/api/students/' + str(student_id) + "/profile/"
Yumna Pratista Tastaftian's avatar
Yumna Pratista Tastaftian committed
274
275
276
277
278
279
280
281
282
283
        data = self._create_test_file_pdf('./assets/img/UI.png')
        response = self.client.patch(url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)

        url = '/api/students/' + str(student_id) + "/profile/"
        data = self._create_test_file_pdf('./assets/pdf/pdf_coba.PDF')
        response = self.client.patch(url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_200_OK)


284
285
286
        response = self.client.patch(url, {'expected_salary': '4000000'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('expected_salary'), '4000000')
287

Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
288
289
290
        response = self.client.patch(url, {'job_seeking_status': 'Active'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('job_seeking_status'), 'Active')
291
292
293
294
295
296
297
        response = self.client.patch(url, {'GPA': 1.0}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('student_gpa'), 1.0)

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'GPA': "this is not a valid input"}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
298

299
300
301
302
        url = '/api/students/' + str(student_id) + '/profile/'
        response = self.client.patch(url, {'volunteer': 'Ketua BEM UI - 2020'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('volunteer'), 'Ketua BEM UI - 2020')
303
304
305
306
307
308
309
310

        response = self.client.patch(url, {'github_url': 'https://github.com/bob'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('github_url'), 'https://github.com/bob')

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'github_url': 'this is not valid url'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
311
312
313
314
315
316

        url = '/api/students/' + str(student_id) + '/profile/'
        response = self.client.patch(url, {'work_experience': 'Magang 3 bulan di Pusilkom'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('work_experience'), 'Magang 3 bulan di Pusilkom')

Dwi Nanda Susanto's avatar
Dwi Nanda Susanto committed
317
318
319
320
321
322
323
324
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'dependants': 0}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('dependants'), 0)

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'dependants': 'this is not valid dependants input'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
325

ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
326
327
    def _create_test_file(self,path):
        f = open(path, 'r')
328
        return {'pdf_file': f}
329
330
331

    def test_company_profile_update(self):
        new_user = User.objects.create_user('dummy.login.company', 'dummy.login.company@company.com', 'lalala123')
332
        Company.objects.create(user=new_user, description="lalalala", status=Company.VERIFIED, logo=None, address=None)
333
334

        url = '/api/login/'
335
336
        response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123', 'login-type': 'company'}, 
        format='json')
337
338
339
340
341
342
343
344
345
346
347
        company_id = response.data.get('company').get('id')

        url = '/api/companies/' + str(company_id) + "/profile/"

        response = self.client.patch(url, {'name': 'Restopedia'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        response = self.client.patch(url, {'address': 'Isekai'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        response = self.client.patch(url, {'description': 'Masak-Masak'}, format='multipart')
348
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
349
350
351

        response = self.client.patch(url, {'website': 'www.huehue.com'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)