Fakultas Ilmu Komputer UI

test_vacancies.py 37.6 KB
Newer Older
1
from datetime import datetime, timedelta
2
from django.utils import timezone
3
import json
4
5
6
7
import requests_mock
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase
8

9
from core.models.accounts import Company, Student, Supervisor
10
from core.models.vacancies import Vacancy, Application
11
from core.serializers.vacancies import VacancySerializer
12
13


14
class ApplicationTests(APITestCase):
15
16
    @requests_mock.Mocker()
    def test_application_list(self, m):
Hafiyyan's avatar
Hafiyyan committed
17
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
18
19
20
21
22
23
24
25
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.mahasiswa",
            "nama": "Dummy Mahasiswa",
            "state": 1,
            "kode_org": "01.00.12.01:mahasiswa",
            "kodeidentitas": "1234567890",
            "nama_role": "mahasiswa"
        }, status_code=200)
Hafiyyan's avatar
Hafiyyan committed
26
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
27
28
29
30
31
32
33
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
34
35
36

        url = '/api/login/'

Zamil Majdy's avatar
Zamil Majdy committed
37
38
        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
39
40
        student_id = response.data.get('student').get('id')

Joshua Casey's avatar
Joshua Casey committed
41
        url = '/api/students/' + str(student_id) + '/applied-vacancies/'
42
43
44
45
46
47
        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_application_create_and_delete(self, m):
Hafiyyan's avatar
Hafiyyan committed
48
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
49
50
51
52
53
54
55
56
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.mahasiswa",
            "nama": "Dummy Mahasiswa",
            "state": 1,
            "kode_org": "01.00.12.01:mahasiswa",
            "kodeidentitas": "1234567890",
            "nama_role": "mahasiswa"
        }, status_code=200)
Hafiyyan's avatar
Hafiyyan committed
57
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
58
59
60
61
62
63
64
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
65
66
67
68
69
70
71
72

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

        new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
73
74
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
75
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
76
                                             description="lalala", close_time=(timezone.now() + timedelta(days=1)))
77

Joshua Casey's avatar
Joshua Casey committed
78
        url = '/api/students/' + str(student_id) + '/applied-vacancies/'
Zamil Majdy's avatar
Zamil Majdy committed
79
80
        response = self.client.post(url, {'vacancy_id': new_vacancy.pk, 'cover_letter': 'this is a cover letter.'},
                                    format='json')
81
82
        self.assertEqual(response.status_code, status.HTTP_200_OK)

Joshua Casey's avatar
Joshua Casey committed
83
        url = '/api/students/' + str(student_id) + '/applied-vacancies/' + str(new_vacancy.pk) + '/'
84
85
        response = self.client.delete(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    @requests_mock.Mocker()
    def test_cannot_create_application_if_vacancy_is_closed(self, m):
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.mahasiswa",
            "nama": "Dummy Mahasiswa",
            "state": 1,
            "kode_org": "01.00.12.01:mahasiswa",
            "kodeidentitas": "1234567890",
            "nama_role": "mahasiswa"
        }, status_code=200)
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)

        url = '/api/login/'
108

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

        new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
117
                                             description="lalala", close_time=(timezone.now() - timedelta(days=1)))
118
119
120
121
        url = '/api/students/' + str(student_id) + '/applied-vacancies/'
        response = self.client.post(url, {'vacancy_id': new_vacancy.pk, 'cover_letter': 'this is a cover letter.'},
                                    format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
122

123
class BookmarkApplicationTests(APITestCase):
124
125
    @requests_mock.Mocker()
    def test_application_list(self, m):
Hafiyyan's avatar
Hafiyyan committed
126
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
127
128
129
130
131
132
133
134
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.mahasiswa",
            "nama": "Dummy Mahasiswa",
            "state": 1,
            "kode_org": "01.00.12.01:mahasiswa",
            "kodeidentitas": "1234567890",
            "nama_role": "mahasiswa"
        }, status_code=200)
Hafiyyan's avatar
Hafiyyan committed
135
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
136
137
138
139
140
141
142
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
143
144
145

        url = '/api/login/'

Zamil Majdy's avatar
Zamil Majdy committed
146
147
        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
148
149
150
151
152
153
154
155
156
        student_id = response.data.get('student').get('id')

        url = '/api/students/' + str(student_id) + '/bookmarked-vacancies/'
        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_application_create_and_delete(self, m):
Hafiyyan's avatar
Hafiyyan committed
157
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
158
159
160
161
162
163
164
165
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.mahasiswa",
            "nama": "Dummy Mahasiswa",
            "state": 1,
            "kode_org": "01.00.12.01:mahasiswa",
            "kodeidentitas": "1234567890",
            "nama_role": "mahasiswa"
        }, status_code=200)
Hafiyyan's avatar
Hafiyyan committed
166
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
167
168
169
170
171
172
173
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
174
175
176
177
178
179
180

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

181
        new_user = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
182
183
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
184
185
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
186
187

        url = '/api/students/' + str(student_id) + '/bookmarked-vacancies/'
Zamil Majdy's avatar
Zamil Majdy committed
188
        response = self.client.post(url, {'vacancy_id': new_vacancy.pk}, format='json')
189
190
191
192
193
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        url = '/api/students/' + str(student_id) + '/bookmarked-vacancies/' + str(new_vacancy.pk) + '/'
        response = self.client.delete(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
194

195

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
class VacancyTest(APITestCase):
    def test_verified_vacancy_list(self):
        superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
        self.client.force_authenticate(user=superuser)

        url = '/api/vacancies/'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_unverified_vacancy_list(self):
        superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
        self.client.force_authenticate(user=superuser)

        url = '/api/vacancies/?verified=false'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
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
    
    def test_filter_vacancy_list_by_company_ids(self):
        superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
        self.client.force_authenticate(user=superuser)
        
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.companyzxc', 'dummy.companyzxc@company.com', 'lalala123')
        new_company2 = Company.objects.create(user=new_user2, description="lalalaasdsad", status=Company.VERIFIED,
                                              logo=None,
                                              address=None)
        open_time = datetime(2019, 10, 20)
        close_time = datetime(2019, 12, 20)
        vacancy1 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, 
                                            description='', close_time=close_time, name='vacancy1')
        vacancy2 = Vacancy.objects.create(company=new_company2, verified=True, open_time=open_time, 
                                            description='', close_time=close_time, name='vacancy2')
        url = '/api/vacancies/?company={}&company={}'.format(new_company.id, new_company2.id)
        response = self.client.get(url, format='json')
        vacancies = Vacancy.objects.filter(company__id__in=[new_company.id, new_company2.id])
        self.assertEqual(dict(response.data)['count'], Vacancy.objects.count())
        self.assertEqual(response.status_code, status.HTTP_200_OK)

237
238
239
240
241

    def test_fail_on_unverified_user_vacancy_list(self):
        url = '/api/vacancies/'
        response = self.client.post(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    def test_opened_only_vacancy_list(self):
        superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
        self.client.force_authenticate(user=superuser)

        url = '/api/vacancies/?opened_only=true'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_opened_and_unopened_vacancy_list(self):
        superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
        self.client.force_authenticate(user=superuser)

        url = '/api/vacancies/?opened_only=false'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
    
    @requests_mock.Mocker()
    def test_closed_vacancy_not_included(self, m):
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.mahasiswa",
            "nama": "Dummy Mahasiswa",
            "state": 1,
            "kode_org": "01.00.12.01:mahasiswa",
            "kodeidentitas": "1234567890",
            "nama_role": "mahasiswa"
        }, status_code=200)
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)

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

        new_user = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="expired vacancy", close_time=datetime.fromtimestamp(1541319301.0))

        url = '/api/vacancies/?opened_only=true'
        response = self.client.get(url, format='json')
        self.assertEqual("expired vacancy" in response, False)

295

296
class CompanyListsTests(APITestCase):
297
    def test_company_vacancy_list(self):
298
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
299
300
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
301

302
303
304
305
        self.client.force_authenticate(new_user)

        url = '/api/companies/' + str(new_company.pk) + '/vacancies/'
        response = self.client.get(url, format='json')
306
307
        self.assertEqual(response.status_code, status.HTTP_200_OK)

308
309
    def test_company_vacancy_list_unauthorized(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
310
311
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
312
313

        new_user2 = User.objects.create_user('dummy.companyzxc', 'dummy.companyzxc@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
314
315
316
        new_company2 = Company.objects.create(user=new_user2, description="lalalaasdsad", status=Company.VERIFIED,
                                              logo=None,
                                              address=None)
317
318
319
320
321
322
323
324

        self.client.force_authenticate(new_user2)

        url = '/api/companies/' + str(new_company.pk) + '/vacancies/'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_company_application_list(self):
325
        new_user = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
326
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
327
328
                                             address=None)

329
330
331
332
333
334
        self.client.force_authenticate(new_user)

        url = '/api/companies/' + str(new_company.pk) + '/applications/'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

335
336
337
338
339
340
341
342
343
344
345
    def test_company_application_list_with_status(self):
        new_user = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        self.client.force_authenticate(new_user)

        url = '/api/companies/' + str(new_company.pk) + '/applications/?status=0'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
    def test_company_application_list_unauthorized(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.companyzxc', 'dummy.companyzxc@company.com', 'lalala123')
        new_company2 = Company.objects.create(user=new_user2, description="lalalaasdsad", status=Company.VERIFIED,
                                              logo=None,
                                              address=None)

        self.client.force_authenticate(new_user2)

        url = '/api/companies/' + str(new_company.pk) + '/applications/'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_company_application_list_by_vacancy(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

370
371
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
372
373
374
375
376
377
        new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")

        self.client.force_authenticate(new_user)

        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(new_vacancy.pk) + '/by_vacancy/'
        response = self.client.get(url, format='json')
378
        self.assertEqual(response.status_code, status.HTTP_200_OK)
379
380
381
382
383
384
385
386
387

    def test_company_application_list_by_vacancy_unauthorized_1(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.student', 'dummy.company3@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

388
389
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
390
391
392
393
        new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")

        new_user3 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_company3 = Company.objects.create(user=new_user3, description="lalala", status=Company.VERIFIED, logo=None,
Zamil Majdy's avatar
Zamil Majdy committed
394
                                              address=None)
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

        self.client.force_authenticate(new_user3)

        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(new_vacancy.pk) + '/by_vacancy/'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_company_application_list_by_vacancy_unauthorized_2(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.student', 'dummy.company3@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

410
411
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
412
413
414
415
416
417
418
419
420
421
        new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")

        new_user3 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_company3 = Company.objects.create(user=new_user3, description="lalala", status=Company.VERIFIED, logo=None,
                                              address=None)

        self.client.force_authenticate(new_user3)

        url = '/api/companies/' + str(new_company3.pk) + '/applications/' + str(new_vacancy.pk) + '/by_vacancy/'
        response = self.client.get(url, format='json')
422
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
423
424
425
426
427
428
429
430
431

    def test_company_application_list_by_vacancy_with_status(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

432
433
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")

        self.client.force_authenticate(new_user)

        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(new_vacancy.pk) + '/by_vacancy/?status=2'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_company_application_list_by_vacancy_with_bad_status_string(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

450
451
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
452
453
454
455
        new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")

        self.client.force_authenticate(new_user)

Zamil Majdy's avatar
Zamil Majdy committed
456
457
        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(
            new_vacancy.pk) + '/by_vacancy/?status=lalala'
458
459
460
461
462
463
464
465
466
467
468
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_company_application_list_by_vacancy_with_bad_status_negative(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

469
470
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
471
472
473
474
        new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")

        self.client.force_authenticate(new_user)

Zamil Majdy's avatar
Zamil Majdy committed
475
476
        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(
            new_vacancy.pk) + '/by_vacancy/?status=-1'
477
478
479
480
481
482
483
484
485
486
487
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_company_application_list_by_vacancy_with_bad_status_large(self):
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

488
489
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
490
491
492
493
494
495
496
        new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")

        self.client.force_authenticate(new_user)

        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(new_vacancy.pk) + '/by_vacancy/?status=5'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
497
498
499
500
501
502
		
    def test_company_application_list_with_major(self):
        new_user = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None, address=None)

        self.client.force_authenticate(new_user)
503

504
505
506
        url = '/api/companies/' + str(new_company.pk) + '/applications/?major=0'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
507

Zamil Majdy's avatar
Zamil Majdy committed
508
class SupervisorStudentApplicationTests(APITestCase):
509
510
511
512
513
    def test_list_student_application(self):
        new_user = User.objects.create_user('dummy.supervisor', 'dummy.supervisor@asd.asd', 'lalala123')
        new_supervisor = Supervisor.objects.create(user=new_user, nip=1212121212)
        self.client.force_authenticate(user=new_user)

Zamil Majdy's avatar
Zamil Majdy committed
514
        url = '/api/applications/'
515
516
517
518
519
520
521
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_list_student_application_unauthorized(self):
        new_user = User.objects.create_user('dummy.supervisor', 'dummy.supervisor@asd.asd', 'lalala123')
        self.client.force_authenticate(user=new_user)

Zamil Majdy's avatar
Zamil Majdy committed
522
        url = '/api/applications/'
523
524
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
M. Reza Qorib's avatar
M. Reza Qorib committed
525

526

Zamil Majdy's avatar
Zamil Majdy committed
527
class SupervisorApprovalTests(APITestCase):
528
529
530
531
532
    def test_supervisor_approve_vacancy(self):
        new_user = User.objects.create_user('dummy.supervisor', 'dummy.supervisor@asd.asd', 'lalala123')
        new_supervisor = Supervisor.objects.create(user=new_user, nip=1212121212)
        self.client.force_authenticate(user=new_user)

533
534
        new_user2 = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
        new_company2 = Company.objects.create(user=new_user2, description="lalala", status=Company.VERIFIED, logo=None,
Zamil Majdy's avatar
Zamil Majdy committed
535
                                              address=None)
536
537
        new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=False, open_time=datetime.fromtimestamp(1541319300.0),
                                              description="lalala", close_time=timezone.now())
538
539

        url = '/api/vacancies/' + str(new_vacancy2.pk) + '/verify/'
Zamil Majdy's avatar
Zamil Majdy committed
540
        response = self.client.patch(url, {'verified': True}, format='json')
541
542
543
544
545
546
547
548
549
550
551
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        retrieve_vacancy = Vacancy.objects.get(pk=new_vacancy2.pk)
        self.assertEqual(retrieve_vacancy.verified, True)

    def test_unauthorized_approve_vacancy(self):
        new_user = User.objects.create_user('dummy.companyz', 'dummy.companyz@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalalaz", status=Company.VERIFIED, logo=None,
                                             address=None)
        self.client.force_authenticate(user=new_user)

552
553
        new_vacancy = Vacancy.objects.create(company=new_company, verified=False, open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now())
554
555
556
557
558

        url = '/api/vacancies/' + str(new_vacancy.pk) + '/verify/'
        response = self.client.patch(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(new_vacancy.verified, False)
559

560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
class ValidationPositionNameinCreateLowonganKP(APITestCase):
    def setUp(self):
        user = User.objects.create_user('dummy', 'dummy@dummy.com', 'dummy')
        self.client.force_authenticate(user=user)
        self.company = Company.objects.create(
            user=user,
            description="dummy",
            status=Company.VERIFIED,
            logo=None,
            address=None
        )
        self.url = '/api/vacancies/'
        self.payload = {
            "company" : self.company.id,
            "open_time" : datetime.fromtimestamp(0),
            "close_time" : datetime.today()
        }
        self.message = "Name must alphabets only"

    def test_name_contains_only_alphabets(self):
        self.payload["name"] = "Engineer"

        response = self.client.post(
            self.url,
            self.payload,
            format="json"
        )

        response_status_code = response.status_code

        self.assertEqual(response_status_code, 201)

    def test_name_contains_only_alphabets_and_spaces(self):
        self.payload["name"] = "Software Engineer "

        response = self.client.post(
            self.url,
            self.payload,
            format="json"
        )

        response_status_code = response.status_code

        self.assertEqual(response_status_code, 201)
    
    def test_name_contains_alphabets_and_numerics(self):
        self.payload["name"] = "Software18 Engineer"

        response = self.client.post(
            self.url,
            self.payload,
            format="json"
        )

        response_status_code = response.status_code
        self.assertEqual(response_status_code, 400)

        response_message = response.data["name"][0]
        self.assertEqual(response_message, self.message)
    
    def test_name_contains_only_numerics(self):
        self.payload["name"] = "123654789"

        response = self.client.post(
            self.url,
            self.payload,
            format="json"
        )

        response_status_code = response.status_code
        self.assertEqual(response_status_code, 400)

        response_message = response.data["name"][0]
        self.assertEqual(response_message, self.message)
    
    def test_name_contains_characters_besides_alphabets(self):
        self.payload["name"] = "!@#$%^&*()"

        response = self.client.post(
            self.url,
            self.payload,
            format="json"
        )

        response_status_code = response.status_code
        self.assertEqual(response_status_code, 400)

        response_message = response.data["name"][0]
        self.assertEqual(response_message, self.message)

650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
class AcceptOneOfferTests(APITestCase):        

    def generateObject(self):
        new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
        
        new_user2 = User.objects.create_user('dummy.company2', 'dummy.company2@company.com', 'lalala123')
        new_company2 = Company.objects.create(user=new_user2, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)

        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
                                             description="lalala", close_time=datetime.today())
        
        new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=True, open_time=datetime.fromtimestamp(0),
                                             description="lalala", close_time=datetime.today())

        new_user3 = User.objects.create_user('dummy.student', 'dummy.student@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user3, npm=1234123412)

        return new_user3, new_vacancy, new_vacancy2, new_student

    def test_number_of_content_response_object_given_id_auth(self):
        
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()
        
        self.client.force_authenticate(new_user3)

        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=new_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        url = '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + str(new_vacancy.pk) + '/'

        response = self.client.patch(url, format='json')
        body = json.loads(response.content)

        status_response = []
        for app in body:
            status_response.append(app['status'])        

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(len(body) >= 2)
        self.assertFalse(len(body) == 0)
        self.assertTrue('new' in status_response)
        self.assertTrue('aborted' in status_response)

    def test_student_not_exist_given_auth(self):
        new_user3,new_vacancy, new_vacancy2, new_student = self.generateObject()

        self.client.force_authenticate(new_user3)

        user4 = User.objects.create_user('student_user_4', 'student_user_4@company.com', 'lalala123')
        other_student = Student.objects.create(user=user4, npm=1098765432)

        Application.objects.create(student=other_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=other_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        url = '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + str(new_vacancy.pk) + '/'

        response = self.client.patch(url, format='json')
        body = json.loads(response.content)  

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(len(body) == 0)

    def test_type_error_if_input_null(self):
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()

        self.client.force_authenticate(new_user3)

        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=new_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        with self.assertRaises(TypeError):
            url = '/api/acceptoffer/' + None + '/vacancy/' + str(new_vacancy.pk) + '/'
        
        with self.assertRaises(TypeError):
            url = '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + None + '/'
    
    def test_if_requester_is_not_authenticated(self):
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()
        
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=new_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        url = '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + str(new_vacancy.pk) + '/'

        response = self.client.patch(url, format='json')
        body = json.loads(response.content) 

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)