Fakultas Ilmu Komputer UI

test_accounts.py 29 KB
Newer Older
INDRA SEPTAMA's avatar
INDRA SEPTAMA committed
1
from datetime import date, timedelta
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
2
3

import requests_mock
4
from django.contrib.auth.models import User
5
from django.core.exceptions import ValidationError
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
6
7
8
from rest_framework import status
from rest_framework.test import APITestCase

9
10
from core.views.sso_login import get_access_token, get_riwayat_user

RANI LASMA ULI's avatar
RANI LASMA ULI committed
11
try:
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
12
    from StringIO import StringIO  ## for Python 2
RANI LASMA ULI's avatar
RANI LASMA ULI committed
13
except ImportError:
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
14
15
    from io import StringIO  ## for Python 3
from core.models.accounts import Company, Student, get_current_age
16
17
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)
18
19


20
class LoginTests(APITestCase):
21
22
    @requests_mock.Mocker()
    def test_succesful_student_login_relogin(self, m):
23
24
25
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)
26
27
28

        url = '/api/login/'

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
29
30
        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
31
32
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
33
34
        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
35
36
37
38
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_successful_supervisor_login_relogin(self, m):
39
40
        mock_csui_oauth_verify(m)
        mock_csui_ldap_lecturer(m)
41
42

        url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
43
44
        response = self.client.post(url, {'username': 'dummy.dosen', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
45
46
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
47
48
        response = self.client.post(url, {'username': 'dummy.dosen', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
49
50
51
52
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_failed_sso_login(self, m):
53
        mock_csui_ldap_fail(m)
54
55

        url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
56
57
        response = self.client.post(url, {'username': 'dummy.salah', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
58
59
60
61
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_failed_company_login(self):
        url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
62
63
64
        response = self.client.post(url,
                                    {'username': 'dummy.company.failed', 'password': 'lalala', 'login-type': 'company'},
                                    format='json')
65
66
67
68
        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')
69
        Company.objects.create(user=new_user, description="lalalala", status=Company.VERIFIED, logo=None, address=None)
70
71

        url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
72
73
        response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123',
                                          'login-type': 'company'}, format='json')
74
75
76
77
78
79
        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)
80

81
    def test_bad_request_2(self):
82
        url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
83
84
        response = self.client.post(url, {'username': 'lalala', 'password': 'lalalala', 'login-type': 'lalala'},
                                    format='json')
85
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
86

JONATHAN EDWIN's avatar
JONATHAN EDWIN committed
87
    def test_return_age(self):
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
88
        birth_day = (date.today() - timedelta(days=700))
JONATHAN EDWIN's avatar
JONATHAN EDWIN committed
89
90
        self.assertEqual(1, get_current_age(birth_day))

91
92
93
class RegisterTests(APITestCase):
    def test_create_and_recreate(self):
        url = '/api/register/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
94
        tc_post = {'password': 'corporatepass', 'name': 'tutuplapak', 'description': 'menutup lapak',
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
95
                   'founded': '2019-01-01',
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
96
97
98
                   'email': 'email@email.com',
                   'logo': 'lalala', 'address': 'alamat', 'category': 'Perusahaan Jasa',
                   'website': 'www.tutuplapak.com', 'size': '1000'}
99
100
101
102
103
104
105
106
107
108
        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)
109

110
111
112
113
114
115
116
117
118
119
    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()


120
class ProfileUpdateTests(APITestCase):
121
    def _create_test_file_pdf(self, path):
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
122
123
        file = open(path, 'rb')
        return {'pdf_sertifikat': file}
124

125
126
127
128
129
130
131
132
133
134
135
136
    @requests_mock.Mocker()
    def test_student_profile_update_validator(self, m):
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)

        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/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
137
        response = self.client.patch(url, {'self_description': 'I am very happy'}, format='multipart', encoding='utf-8')
138
139
140
141
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('self_description'), 'I am very happy')

        url = '/api/students/' + str(student_id) + "/profile/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
142
143
        response = self.client.patch(url, {'linkedin_url': 'https://www.linkedin.com/in/jojo/'}, format='multipart',
                                     encoding='utf-8')
144
145
146
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('linkedin_url'), 'https://www.linkedin.com/in/jojo/')

Wildan Fahmi's avatar
Wildan Fahmi committed
147
        url = '/api/students/' + str(student_id) + "/profile/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
148
149
        response = self.client.patch(url, {'linkedin_url': 'https://linkedin.com/in/jojo/'}, format='multipart',
                                     encoding='utf-8')
Wildan Fahmi's avatar
Wildan Fahmi committed
150
151
152
153
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('linkedin_url'), 'https://linkedin.com/in/jojo/')

        url = '/api/students/' + str(student_id) + "/profile/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
154
155
        response = self.client.patch(url, {'linkedin_url': 'https://id.linkedin.com/in/jojo/'}, format='multipart',
                                     encoding='utf-8')
Wildan Fahmi's avatar
Wildan Fahmi committed
156
157
158
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('linkedin_url'), 'https://id.linkedin.com/in/jojo/')

159
        url = '/api/students/' + str(student_id) + "/profile/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
160
161
        response = self.client.patch(url, {'linkedin_url': 'https://www.linkedin.com/jojo/'}, format='multipart',
                                     encoding='utf-8')
162
163
164
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data.get('linkedin_url'), None)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
165
166
        response = self.client.patch(url, {'hackerrank_url': 'https://www.hackerrank.com/james'}, format='multipart',
                                     encoding='utf-8')
167
168
169
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('hackerrank_url'), 'https://www.hackerrank.com/james')

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
170
171
        response = self.client.patch(url, {'hackerrank_url': 'https://www.hackerank.com/james'}, format='multipart',
                                     encoding='utf-8')
172
173
174
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data.get('hackerrank_url'), None)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
175
176
        response = self.client.patch(url, {'github_url': 'https://github.com/bob'}, format='multipart',
                                     encoding='utf-8')
177
178
179
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('github_url'), 'https://github.com/bob')

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
180
181
        response = self.client.patch(url, {'github_url': 'https://gitlab.com/bob'}, format='multipart',
                                     encoding='utf-8')
182
183
184
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data.get('github_url'), None)

185
186
    @requests_mock.Mocker()
    def test_student_profile_update(self, m):
187
188
189
190
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)

191
192
193
194
195
        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
196
        url = '/api/students/' + str(student_id) + "/profile/"
197
198
199
200
        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
201
        url = '/api/students/' + str(student_id) + "/profile/"
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
202
203
204
        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')
205

Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
206
207
208
209
210
        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/"
211
212
213
214
        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')

215
216
217
218
        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/')

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

223
224
225
226
        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')

PRIAMBUDI LINTANG BAGASKARA's avatar
PRIAMBUDI LINTANG BAGASKARA committed
227
228
229
230
        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
231
232
233
234
235
        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
236
237
238
239
240
        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
241
        url = '/api/students/' + str(student_id) + "/profile/"
242
243
244
        response = self.client.patch(url, {'email': 'saasdasd'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

245
246
247
248
        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)

JONATHAN EDWIN's avatar
JONATHAN EDWIN committed
249
250
251
252
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'expected_salary': 'abcd'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

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

257
258
259
260
        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
261
        url = '/api/students/' + str(student_id) + "/profile/"
262
        response = self.client.patch(url, {'hackerrank_url': 'this is not valid url'}, format='multipart')
Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
263
264
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

265
266
267
        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
268
        url = '/api/students/' + str(new_student.pk) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
269
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart', encoding='utf-8')
270
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
271
272

        url = '/api/students/' + str(student_id) + "/profile/"
273
        data = self._create_test_file('./assets/pdf/pdf-coba.pdf')
RANI LASMA ULI's avatar
RANI LASMA ULI committed
274
        response = self.client.patch(url, data, format='multipart', encoding='utf-8')
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
275
276
        self.assertEqual(response.status_code, status.HTTP_200_OK)

277
278
279
280
        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
281

Kristianto's avatar
Kristianto committed
282
        url = '/api/students/' + str(student_id) + "/profile/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
283
284
        response = self.client.patch(url, {'latest_work': 'Teaching assistant at Fasilkom UI'}, format='multipart',
                                     encoding='utf-8')
285
286
287
288
        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/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
289
290
        response = self.client.patch(url, {'latest_work_desc': 'Evaluate weekly assignment for 15 students'},
                                     format='multipart', encoding='utf-8')
291
292
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('latest_work_desc'), 'Evaluate weekly assignment for 15 students')
293

HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
294
        url = '/api/students/' + str(student_id) + '/profile/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
295
296
        response = self.client.patch(url, {'intro': 'Saya tertarik dengan dunia front-end development'},
                                     format='multipart', encoding='utf-8')
HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
297
298
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('intro'), 'Saya tertarik dengan dunia front-end development')
299

Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
300
        url = '/api/students/' + str(student_id) + "/profile/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
301
302
        response = self.client.patch(url, {'awards': 'Juara 2 UIUX Gemastik 2019'}, format='multipart',
                                     encoding='utf-8')
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
303
304
305
        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
306
        url = '/api/students/' + str(student_id) + "/profile/"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
307
308
        response = self.client.patch(url, {'projects': 'Ow-Jek - Android Mobile Project'}, format='multipart',
                                     encoding='utf-8')
Aviliani Pramestya's avatar
Aviliani Pramestya committed
309
310
311
        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
312
        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
313
        response = self.client.patch(url, {'certification': 'TOEFL'}, format='multipart', encoding='utf-8')
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
314
315
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('certification'), 'TOEFL')
JONATHAN EDWIN's avatar
JONATHAN EDWIN committed
316

Tyagita Larasati's avatar
Tyagita Larasati committed
317
        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
318
        response = self.client.patch(url, {'languages': 'Indonesia, Inggris'}, format='multipart', encoding='utf-8')
Tyagita Larasati's avatar
Tyagita Larasati committed
319
320
        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
321

Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
322
        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
323
        response = self.client.patch(url, {'seminar': 'Seminar CompFest'}, format='multipart', encoding='utf-8')
Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
324
325
326
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('seminar'), 'Seminar CompFest')

NABILA FAKHIRAH YUSRONI's avatar
NABILA FAKHIRAH YUSRONI committed
327
328
        failing_desc = "a" * 501
        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
329
        response = self.client.patch(url, {'self_description': failing_desc}, format='multipart', encoding='utf-8')
NABILA FAKHIRAH YUSRONI's avatar
NABILA FAKHIRAH YUSRONI committed
330
331
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
332
        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
333
        response = self.client.patch(url, {'student_gpa': '4'}, format='multipart', encoding='utf-8')
Aldo Bima Syahputra's avatar
Aldo Bima Syahputra committed
334
335
336
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
337
        response = self.client.patch(url, {'student_gpa': '8'}, format='multipart', encoding='utf-8')
Aldo Bima Syahputra's avatar
Aldo Bima Syahputra committed
338
339
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

RANI LASMA ULI's avatar
RANI LASMA ULI committed
340
        response = self.client.patch(url, {'interests': 'Machine Learning'}, format='multipart', encoding='utf-8')
Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
341
342
343
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('interests'), 'Machine Learning')

344
        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
345
        response = self.client.patch(url, {'skills': ''}, format='multipart', encoding='utf-8')
346
347
348
349
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('skills'), '')

        url = '/api/students/' + str(student_id) + "/profile/"
RANI LASMA ULI's avatar
RANI LASMA ULI committed
350
        response = self.client.patch(url, {'skills': '1231231231'}, format='multipart', encoding='utf-8')
351
352
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
353
354
355
356
357
358
359
360
361
362
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'related_course': 'Interaction System, Machine Learning'}, format='multipart',
                                     encoding='utf-8')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('related_course'), 'Interaction System, Machine Learning')

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

363
364
    @requests_mock.Mocker()
    def test_student_profile_update_filetype_validation(self, m):
365
366
367
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)
368
369
370
371
372
373
374
375

        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'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
376
                           '\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
377
378
379
380
381
382
383
        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)
384
        url = '/api/students/' + str(student_id) + "/profile/"
Yumna Pratista Tastaftian's avatar
Yumna Pratista Tastaftian committed
385
386
387
388
389
390
391
392
393
        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)

394
395
396
        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')
397

Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
398
399
400
        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')
401
402
403
404
405
406
407
        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)
408

409
        url = '/api/students/' + str(student_id) + "/profile/"
Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
410
        response = self.client.patch(url, {'TOEFL': 0}, format='multipart')
411
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
412
        self.assertEqual(response.data.get('student_toefl'), 0)
413

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

418
419
420
421
        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')
422
423
424
425
426
427
428
429

        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)
430

431
432
433
434
435
436
437
438
439
440
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'gitlab_url': 'https://gitlab.com/dummy'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('gitlab_url'), 'https://gitlab.com/dummy')

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'gitlab_url': 'not an URL'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertNotEqual(response.data.get('gitlab_url'), "not an URL")

441
442
443
444
445
        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
446
447
448
449
450
451
452
453
        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)
454

Dwi Nanda Susanto's avatar
Dwi Nanda Susanto committed
455
456
457
458
459
460
461
462
463
464
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'ielts': 6.0}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('ielts'), 6.0)

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


Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
465
    def _create_test_file(self, path):
466
        f = open(path, 'rb')
467
        return {'pdf_file': f}
468
469
470

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

        url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
474
475
476
        response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123',
                                          'login-type': 'company'},
                                    format='json')
477
478
479
480
481
482
483
484
485
486
487
        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')
488
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
489

Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
490
491
492
        response = self.client.patch(url, {'founded': '2019-01-01'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

Hadlina Rahmadinni's avatar
Hadlina Rahmadinni committed
493
494
495
        response = self.client.patch(url, {'size': '1000'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

496
        response = self.client.patch(url, {'website': 'www.huehue.com'}, format='multipart')
JONATHAN EDWIN's avatar
JONATHAN EDWIN committed
497
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
498
499
500
501
502
503

        response = self.client.patch(url, {'linkedin_url': 'https://www.linkedin.com/company/bob'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        response = self.client.patch(url, {'linkedin_url': 'this is not valid url'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
504
505
506
507
508
509
510
511
512
513
514
515
516
517

class ResponseApiLoginCompany(APITestCase):
    def setUp(self):
        self.username = "fesbuk"
        self.password = "mo12ni34"

        self.user = User(username=self.username)
        self.user.set_password(self.password)
        self.user.save()

    def test_field_last_login_exists(self):
        response = self.client.post(
            '/api/login/',
            {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
518
519
                "login-type": "company",
                "password": self.password,
520
521
522
523
524
525
                "username": self.username,
            },
            format="json"
        )

        self.assertTrue("last_login" in response.data.keys())
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545

class TranscriptTest(APITestCase):

    @requests_mock.Mocker()
    def test_transcript_fail(self, m):
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)

        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) + "/transcript/"
        response = self.client.get(url)
        
        self.assertEqual(response.json()['name'], 'Dummy Mahasiswa')
        self.assertEqual(response.json()['error'], 'student does not allow transcript to be shown')