Fakultas Ilmu Komputer UI

test_vacancies.py 62.2 KB
Newer Older
1
import json
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
2
3
from datetime import datetime, timedelta

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

11
from core.models.accounts import Company, Student, Supervisor
12
from core.models.vacancies import Vacancy, Application, VacancyMilestone
13
from core.serializers.vacancies import VacancySerializer
14
from core.tests.mocks import mock_csui_oauth_verify, mock_csui_ldap_student, mock_csui_siak_student
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
15
from core.views.vacancies import date_validator
16
from core.views import views_constants
17

18
class ApplicationTests(APITestCase):
19
20
21
22
23
    def get_student_id(self, mock_obj):
        mock_csui_oauth_verify(mock_obj)
        mock_csui_ldap_student(mock_obj)
        mock_csui_siak_student(mock_obj)
        login_url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
24
25
        response = self.client.post(login_url,
                                    {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
Zamil Majdy's avatar
Zamil Majdy committed
26
                                    format='json')
27
        return response.data.get('student').get('id')
28

29
30
31
    @requests_mock.Mocker()
    def test_application_list(self, m):
        student_id = self.get_student_id(m)
Joshua Casey's avatar
Joshua Casey committed
32
        url = '/api/students/' + str(student_id) + '/applied-vacancies/'
33
34
35
36
        response = self.client.get(url)

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

37
38
    @requests_mock.Mocker()
    def test_application_search(self, m):
39
        student_id = self.get_student_id(m)
40
41
42
43
44
        url = '/api/students/' + str(student_id) + '/applied-vacancies/?search=engineer'
        response = self.client.get(url)

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

45
46
    @requests_mock.Mocker()
    def test_application_create_and_delete(self, m):
47
        student_id = self.get_student_id(m)
48
        new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
49
50
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
51
52
53
54
55
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", max_accepted_applicants=3, working_period="3 Bulan",
                                             requirements="requirements",
                                             close_time=(timezone.now() + timedelta(days=1)))
56

Joshua Casey's avatar
Joshua Casey committed
57
        url = '/api/students/' + str(student_id) + '/applied-vacancies/'
58
        response = self.client.post(url, {'vacancy_id': new_vacancy.pk, 'cover_letter': 'this is a cover letter. ' * 50},
Zamil Majdy's avatar
Zamil Majdy committed
59
                                    format='json')
60
61
        self.assertEqual(response.status_code, status.HTTP_200_OK)

Joshua Casey's avatar
Joshua Casey committed
62
        url = '/api/students/' + str(student_id) + '/applied-vacancies/' + str(new_vacancy.pk) + '/'
63
64
65
        response = self.client.delete(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    @requests_mock.Mocker()
    def test_application_cover_letter_validation_error(self, m):
        student_id = self.get_student_id(m)

        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(1541319300.0),
            description="lalala",
            max_accepted_applicants=3,
            working_period="3 Bulan",
            requirements="requirements",
            close_time=(timezone.now() + timedelta(days=1))
        )

        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)

        message_error = response.data[0]
        self.assertEqual(message_error, views_constants.ERROR_WORDS_COUNT)

111
112
113
114
115
116
117
118
119
120
121
122
    def test_count_application_a_person_is_already_register(self):
        new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
        new_user2 = User.objects.create_user('dummy.student', 'dummy.student@student.com', 'lalala123')

        self.client.force_authenticate(new_user)
        self.client.force_authenticate(new_user2)

        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
        new_student = Student.objects.create(user=new_user2, npm=1234123412)

        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
123
124
                                             description="lalala", close_time=datetime.today(),
                                             max_accepted_applicants=3, working_period="3 Bulan")
125

126
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="lorem ipsum")
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

        url = '/api/applications/' + str(new_vacancy.pk) + '/count/'

        response = self.client.get(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.json()['count'], 1)

    def test_count_application_nobody_register(self):
        new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
        new_user2 = User.objects.create_user('dummy.student', 'dummy.student@student.com', 'lalala123')

        self.client.force_authenticate(new_user)
        self.client.force_authenticate(new_user2)

        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
144
        Student.objects.create(user=new_user2, npm=1234123412)
145
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
146
147
                                             description="lalala", close_time=datetime.today(),
                                             max_accepted_applicants=3, working_period="3 Bulan")
148
149
150
151
152
153
154
155

        url = '/api/applications/' + str(new_vacancy.pk) + '/count/'

        response = self.client.get(url, format='json')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.json()['count'], 0)

156
157
    @requests_mock.Mocker()
    def test_cannot_create_application_if_vacancy_is_closed(self, m):
158
        student_id = self.get_student_id(m)
159
160
161
162
        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),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
163
164
                                             description="lalala", close_time=(timezone.now() - timedelta(days=1)),
                                             max_accepted_applicants=3, working_period="3 Bulan")
165
166
167
168
        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)
169

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
170

171
class BookmarkApplicationTests(APITestCase):
172
    @requests_mock.Mocker()
173
174
175
176
177
    def setUp(self, m):
        mock_csui_oauth_verify(m)
        mock_csui_ldap_student(m)
        mock_csui_siak_student(m)
        login_url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
178
179
        response = self.client.post(login_url,
                                    {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
Zamil Majdy's avatar
Zamil Majdy committed
180
                                    format='json')
181
        self.student_id = response.data.get('student').get('id')
182

183
184
    def test_application_list(self):
        url = '/api/students/' + str(self.student_id) + '/bookmarked-vacancies/'
185
186
187
188
        response = self.client.get(url)

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

189
190
    def test_search_bookmarked_application(self):
        url = '/api/students/' + str(self.student_id) + '/bookmarked-vacancies/?search=engineer'
191
192
193
194
        response = self.client.get(url)

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

195
    def test_application_create_and_delete(self):
196
        new_user = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
197
198
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
199
200
201
202
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, requirements="requirements",
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
203

204
        url = '/api/students/' + str(self.student_id) + '/bookmarked-vacancies/'
Zamil Majdy's avatar
Zamil Majdy committed
205
        response = self.client.post(url, {'vacancy_id': new_vacancy.pk}, format='json')
206
207
        self.assertEqual(response.status_code, status.HTTP_200_OK)

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

212

213
class VacancyTest(APITestCase):
214
215
216
217
218
    def login(self, mock_obj):
        mock_csui_oauth_verify(mock_obj)
        mock_csui_ldap_student(mock_obj)
        mock_csui_siak_student(mock_obj)
        login_url = '/api/login/'
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
219
220
        self.client.post(login_url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                         format='json')
221

222
223
224
225
226
227
228
229
    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)

230
231
232
233
    @requests_mock.Mocker()
    def test_search_vacancy(self, m):
        superuser = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
        company = Company.objects.create(user=superuser, description="This is a test company")
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
234
235
236
237
        Vacancy.objects.create(company=company, open_time=datetime.now(), close_time=datetime.now(),
                               name="Software Engineer", max_accepted_applicants=3, working_period="3 Bulan")
        Vacancy.objects.create(company=company, open_time=datetime.now(), close_time=datetime.now(),
                               name="Data Engineer", max_accepted_applicants=3, working_period="3 Bulan")
238

239
        self.login(m)
240
241
242
243
244
        url = '/api/vacancies/?search=software'
        response = self.client.get(url, format='json')
        self.assertEqual(1, Company.objects.all().count())
        self.assertEqual(2, Vacancy.objects.all().count())
        self.assertEqual(response.status_code, status.HTTP_200_OK)
root's avatar
root committed
245

246
247
248
249
250
251
252
    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)
253

254
255
256
    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)
257

258
259
260
261
262
263
264
265
266
267
        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)
268
        Vacancy.objects.create(company=new_company, verified=True, open_time=open_time,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
269
270
                               description='', close_time=close_time, name='vacancy1', max_accepted_applicants=3,
                               working_period="3 Bulan")
271
        Vacancy.objects.create(company=new_company2, verified=True, open_time=open_time,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
272
273
                               description='', close_time=close_time, name='vacancy2', max_accepted_applicants=3,
                               working_period="3 Bulan")
274
275
        url = '/api/vacancies/?company={}&company={}'.format(new_company.id, new_company2.id)
        response = self.client.get(url, format='json')
276
        Vacancy.objects.filter(company__id__in=[new_company.id, new_company2.id])
277
278
        self.assertEqual(dict(response.data)['count'], Vacancy.objects.count())
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
279

280
281
282
283
284
285
286
287
288
    def test_sort_vacancies(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)
        open_time = datetime(2019, 10, 20)
        close_time = datetime(2019, 12, 20)
289
        v1 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, salary=3000000,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
290
291
                               description='', close_time=close_time, name='vacancy1', max_accepted_applicants=3,
                               working_period="3 Bulan")
292
293
294
        v1.created = datetime(2019, 12, 10)
        v1.save()
        v2 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, salary=3500000,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
295
                               description='', close_time=close_time, name='vacancy2', max_accepted_applicants=3,
296
297
298
                               working_period="3 Bulan", created=datetime(2018, 12, 10))
        v2.created = datetime(2018, 12, 10)
        v2.save()
299
300
301
302
        factory = APIRequestFactory()
        url_name_asc = '/api/vacancies/?sort=name&order=ascending'
        request = factory.get(url_name_asc)
        response = self.client.get(url_name_asc, format='json')
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
303
304
        self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('name'), many=True,
                                                                           context={'request': request}).data)
305
306
307
308

        url_salary_asc = '/api/vacancies/?sort=salary'
        response = self.client.get(url_salary_asc)
        request = factory.get(url_salary_asc)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
309
310
311
        self.assertEqual(dict(response.data)['results'],
                         VacancySerializer(Vacancy.objects.order_by('salary'), many=True,
                                           context={'request': request}).data)
312
313
314
315

        url_name_desc = '/api/vacancies/?sort=name&order=descending'
        response = self.client.get(url_name_desc)
        request = factory.get(url_name_desc)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
316
317
        self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('-name'), many=True,
                                                                           context={'request': request}).data)
318
319
320
321

        url_salary_desc = '/api/vacancies/?sort=salary&order=descending'
        response = self.client.get(url_salary_desc)
        request = factory.get(url_salary_desc)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
322
323
324
        self.assertEqual(dict(response.data)['results'],
                         VacancySerializer(Vacancy.objects.order_by('-salary'), many=True,
                                           context={'request': request}).data)
325

326
327
328
329
330
331
332
        url_recent = '/api/vacancies/?sort=recent'
        response = self.client.get(url_recent)
        request = factory.get(url_recent)
        self.assertEqual(dict(response.data)['results'],
                         VacancySerializer(Vacancy.objects.order_by('-created'), many=True,
                                           context={'request': request}).data)

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
    def test_sort_vacancies_by_close_time(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)

        open_time = datetime(2019, 12, 7)
        close_time1 = datetime(2019, 12, 20)
        close_time2 = datetime(2020, 2, 22)
        close_time3 = datetime(2020, 2, 21)

        created_time = datetime(2019, 12, 6)

        v1 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, salary=3000000,
                               description='', close_time=close_time1, name='vacancy1', max_accepted_applicants=3,
                               working_period="3 Bulan", created = created_time)
        v1.save()

        v2 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, salary=3500000,
                               description='', close_time=close_time2, name='vacancy2', max_accepted_applicants=3,
                               working_period="3 Bulan", created=created_time)
        v2.save()

        v3 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, salary=3500000,
                               description='', close_time=close_time3, name='vacancy2', max_accepted_applicants=3,
                               working_period="3 Bulan", created=created_time)
        v3.save()

        factory = APIRequestFactory()

        url_close_time_asc = '/api/vacancies/?sort=close_time&order=ascending'
        request = factory.get(url_close_time_asc)
        response = self.client.get(url_close_time_asc, format='json')
        self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('close_time'), many=True,
                                                                           context={'request': request}).data)

        url_close_time_desc = '/api/vacancies/?sort=close_time&order=descending'
        request = factory.get(url_close_time_desc)
        response = self.client.get(url_close_time_desc, format='json')
        self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('-close_time'), many=True,
                                                                           context={'request': request}).data)

Raden Fikri Ihza Dwi Nanda's avatar
Raden Fikri Ihza Dwi Nanda committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
    def test_sort_vacancies_by_open_time(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)

        open_time1 = datetime(2019, 12, 20)
        open_time2 = datetime(2020, 2, 22)
        open_time3 = datetime(2020, 2, 21)
        close_time = datetime(2020, 3, 1)

        created_time = datetime(2019, 12, 6)

        v1 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time1, salary=3000000,
                               description='', close_time=close_time, name='vacancy1', max_accepted_applicants=3,
                               working_period="3 Bulan", created = created_time)
        v1.save()

        v2 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time2, salary=3500000,
                               description='', close_time=close_time, name='vacancy2', max_accepted_applicants=3,
                               working_period="3 Bulan", created=created_time)
        v2.save()

        v3 = Vacancy.objects.create(company=new_company, verified=True, open_time=open_time3, salary=3500000,
                               description='', close_time=close_time, name='vacancy2', max_accepted_applicants=3,
                               working_period="3 Bulan", created=created_time)
        v3.save()

        factory = APIRequestFactory()

        url_open_time_asc = '/api/vacancies/?sort=open_time&order=ascending'
        request = factory.get(url_open_time_asc)
        response = self.client.get(url_open_time_asc, format='json')
        self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('open_time'), many=True,
                                                                           context={'request': request}).data)

        url_open_time_desc = '/api/vacancies/?sort=open_time&order=descending'
        request = factory.get(url_open_time_desc)
        response = self.client.get(url_open_time_desc, format='json')
        self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('-open_time'), many=True,
                                                                           context={'request': request}).data)
420

421
422
423
424
    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)
425

426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    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)
441

442
443
    @requests_mock.Mocker()
    def test_closed_vacancy_not_included(self, m):
444
        self.login(m)
445
446
447
        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)
448
        Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
449
450
                               description="expired vacancy", close_time=datetime.fromtimestamp(1541319301.0),
                               max_accepted_applicants=3, working_period="3 Bulan")
451
452
453
454
455

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

root's avatar
root committed
456
457
458
459
    def test_vacancy_is_closed(self):
        superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
        new_company = Company.objects.create(user=superuser, description="lalalaz", status=Company.VERIFIED, logo=None,
                                             address=None)
460

root's avatar
root committed
461
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
462
463
                                             description="lalala", close_time=timezone.now() - timedelta(minutes=10),
                                             working_period="3 Bulan")
root's avatar
root committed
464
465
466
467
468
469
470
471
472
473
        self.client.force_authenticate(user=superuser)

        response = new_vacancy.apply_before

        self.assertEqual(response, "Pendaftaran ditutup")

    def test_vacancy_is_closed(self):
        superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
        new_company = Company.objects.create(user=superuser, description="lalalaz", status=Company.VERIFIED, logo=None,
                                             address=None)
474

root's avatar
root committed
475
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
476
                                             description="lalala", close_time=timezone.now() + timedelta(minutes=10),
477
                                             max_accepted_applicants=3, working_period="3 Bulan")
root's avatar
root committed
478
479
480
481
482
483
        self.client.force_authenticate(user=superuser)

        response = new_vacancy.apply_before

        self.assertNotEqual(response, "Pendaftaran ditutup")

484
    def test_vacancy_valid_date(self):
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
485
486
        self.assertEqual(True, date_validator(str(datetime.today() + timedelta(days=1)),
                                              str(datetime.today() + timedelta(days=2)))['is_valid'])
487
488
489
490
491
492
493
494
495
496
497
498

    def test_vacancy_invalid_date(self):
        self.assertEqual(
            status.HTTP_400_BAD_REQUEST,
            date_validator(str(datetime.today() - timedelta(days=1)), str(datetime.today()))['status'])

        self.assertEqual(status.HTTP_400_BAD_REQUEST,
                         date_validator(str(datetime.today()), str(datetime.today() - timedelta(days=1)))['status'])

        self.assertRaisesMessage(status.HTTP_400_BAD_REQUEST,
                                 date_validator(str(datetime.today()), str(datetime.today()))['status'])

root's avatar
root committed
499

500
class CompanyListsTests(APITestCase):
501
    def test_company_vacancy_list(self):
502
        new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
Zamil Majdy's avatar
Zamil Majdy committed
503
504
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
505

506
507
508
509
        self.client.force_authenticate(new_user)

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

512
513
    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
514
515
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
516
517

        new_user2 = User.objects.create_user('dummy.companyzxc', 'dummy.companyzxc@company.com', 'lalala123')
518
        Company.objects.create(user=new_user2, description="lalalaasdsad", status=Company.VERIFIED,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
519
520
                               logo=None,
                               address=None)
521
522
523
524
525
526
527
528

        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):
529
        new_user = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
530
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
531
532
                                             address=None)

533
534
535
536
537
538
        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)

539
540
541
542
543
544
545
546
547
548
549
    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)

550
551
552
553
554
555
    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')
556
        Company.objects.create(user=new_user2, description="lalalaasdsad", status=Company.VERIFIED,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
557
558
                               logo=None,
                               address=None)
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573

        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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
574
575
576
577
578
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
579
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
580
581
582
583
584

        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')
585
        self.assertEqual(response.status_code, status.HTTP_200_OK)
586
587
588
589
590
591
592
593
594

    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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
595
596
597
598
599
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
600
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
601
602

        new_user3 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
603
        Company.objects.create(user=new_user3, description="lalala", status=Company.VERIFIED, logo=None,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
604
                               address=None)
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619

        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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
620
621
622
623
624
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
625
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
626
627
628
629
630
631
632
633
634

        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')
635
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
636
637
638
639
640
641
642
643
644

    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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
645
646
647
648
649
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
650
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665

        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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
666
667
668
669
670
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
671
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
672
673
674

        self.client.force_authenticate(new_user)

Zamil Majdy's avatar
Zamil Majdy committed
675
676
        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(
            new_vacancy.pk) + '/by_vacancy/?status=lalala'
677
678
679
680
681
682
683
684
685
686
687
        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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
688
689
690
691
692
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
693
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
694
695
696

        self.client.force_authenticate(new_user)

Zamil Majdy's avatar
Zamil Majdy committed
697
698
        url = '/api/companies/' + str(new_company.pk) + '/applications/' + str(
            new_vacancy.pk) + '/by_vacancy/?status=-1'
699
700
701
702
703
704
705
706
707
708
709
        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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
710
711
712
713
714
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3,
                                             working_period="3 Bulan")
715
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
716
717
718
719
720
721

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

723
724
    def test_company_application_list_with_major(self):
        new_user = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
725
726
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
                                             address=None)
727

728
        self.client.force_authenticate(new_user)
729

730
731
732
        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)
733

734

Zamil Majdy's avatar
Zamil Majdy committed
735
class SupervisorStudentApplicationTests(APITestCase):
736
737
    def test_list_student_application(self):
        new_user = User.objects.create_user('dummy.supervisor', 'dummy.supervisor@asd.asd', 'lalala123')
738
        Supervisor.objects.create(user=new_user, nip=1212121212)
739
740
        self.client.force_authenticate(user=new_user)

Zamil Majdy's avatar
Zamil Majdy committed
741
        url = '/api/applications/'
742
743
744
745
746
747
748
        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
749
        url = '/api/applications/'
750
751
        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
752

753

Zamil Majdy's avatar
Zamil Majdy committed
754
class SupervisorApprovalTests(APITestCase):
755
756
    def test_supervisor_approve_vacancy(self):
        new_user = User.objects.create_user('dummy.supervisor', 'dummy.supervisor@asd.asd', 'lalala123')
757
        Supervisor.objects.create(user=new_user, nip=1212121212)
758
759
        self.client.force_authenticate(user=new_user)

760
761
        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
762
                                              address=None)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
763
764
765
766
        new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=False,
                                              open_time=datetime.fromtimestamp(1541319300.0),
                                              description="lalala", requirements="requirements",
                                              close_time=timezone.now(), max_accepted_applicants=3)
767
768

        url = '/api/vacancies/' + str(new_vacancy2.pk) + '/verify/'
Zamil Majdy's avatar
Zamil Majdy committed
769
        response = self.client.patch(url, {'verified': True}, format='json')
770
771
772
773
774
775
776
777
778
779
780
        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)

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
781
782
783
784
        new_vacancy = Vacancy.objects.create(company=new_company, verified=False,
                                             open_time=datetime.fromtimestamp(1541319300.0),
                                             description="lalala", requirements="requirements",
                                             close_time=timezone.now(), max_accepted_applicants=3)
785
786
787
788
789

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

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
791

792
793
794
795
796
797
798
799
800
801
802
803
804
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 = {
805
            "name": "dummy",
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
806
807
808
809
            "company": self.company.id,
            "open_time": datetime.today(),
            "close_time": datetime.today() + timedelta(days=10),
            "description": "dummy",
810
811
            "working_period": "Summer 2019",
            "max_accepted_applicants": 10,
812
813
814
815
816
        }
        self.message = "Name must alphabets only"

    def test_name_contains_only_alphabets(self):
        self.payload["name"] = "Engineer"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
817
        self.payload["requirements"] = "lalala"
818
819
820
821
822
823
824
825
826

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

        response_status_code = response.status_code

827
        self.assertEqual(response_status_code, 200)
828
829

    def test_name_contains_only_alphabets_and_spaces(self):
830
        self.payload["name"] = "Software Engineer"
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
831
        self.payload["requirements"] = "lala la"
832
833
834
835
836
837
838
839
840

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

        response_status_code = response.status_code

841
        self.assertEqual(response_status_code, 200)
842

843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
    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)

    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)

    def test_name_contains_characters_besides_alphabets(self):
        self.payload["name"] = "!@#$%^&*()"

        response = self.client.post(
            self.url,
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
            self.payload,
            format="json"
        )

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

    def test_salary_contains_number_only(self):
        self.payload["salary"] = 2312312

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

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

    def test_salary_contains_number_and_alphabet(self):
        self.payload["salary"] = "sdsaa"

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

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

    def test_amount_contains_number_only(self):
Yusuf Sholeh's avatar
Yusuf Sholeh committed
904
        self.payload["amount"] = 231231
905
906
907
908
909
910
911
912
913
914

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

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

Fajrin Kingwijati's avatar
Fajrin Kingwijati committed
915
    def test_max_accepted_applicants_contains_only_number(self):
Yusuf Sholeh's avatar
Yusuf Sholeh committed
916
        self.payload["max_accepted_applicants"] = "11"
Fajrin Kingwijati's avatar
Fajrin Kingwijati committed
917
918
919
920
921
922
923
924
925
926
927

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

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

    def test_max_accepted_applicants_contains_only_alphabet(self):
Yusuf Sholeh's avatar
Yusuf Sholeh committed
928
        self.payload["max_accepted_applicants"] = "abcd"
Fajrin Kingwijati's avatar
Fajrin Kingwijati committed
929
930
931
932
933
934
935
936
937

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

        response_status_code = response.status_code
        self.assertEqual(response_status_code, 400)
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
938

Fajrin Kingwijati's avatar
Fajrin Kingwijati committed
939
    def test_max_accepted_applicants_contains_number_and_alphabet(self):
Yusuf Sholeh's avatar
Yusuf Sholeh committed
940
        self.payload["max_accepted_applicants"] = "a1r1"
Fajrin Kingwijati's avatar
Fajrin Kingwijati committed
941
942
943
944
945
946
947
948
949
950

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

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

951
952
953
954
955
    def test_amount_contains_number_and_alphabet(self):
        self.payload["salary"] = "sadas231"

        response = self.client.post(
            self.url,
956
957
958
959
960
961
962
            self.payload,
            format="json"
        )

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

963

964
965
966
967
class VacancyMilestoneTests(APITestCase):
    def setUp(self):
        super(VacancyMilestoneTests, self).setUp()
        self.user = User.objects.create_user('dummy.student', 'dummy.student@home.com', 'lalala123')
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
968
969
970
        self.company_user = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
        self.company = Company.objects.create(user=self.company_user, description="lalala", status=Company.VERIFIED,
                                              logo=None,
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
                                              address=None)
        self.vacancy = Vacancy.objects.create(company=self.company, verified=True, open_time=datetime.fromtimestamp(0),
                                              description="lalala", close_time=datetime.today())

    def create_milestone_object(self):
        return VacancyMilestone.objects.create(vacancy=self.vacancy, name="initiate", detail="install things",
                                               expected_start=datetime.fromtimestamp(0),
                                               expected_finish=datetime.fromtimestamp(86400))

    def test_vacancy_milestone_model(self):
        milestone1 = VacancyMilestone(vacancy=self.vacancy, name="initiate", detail="install things",
                                      expected_start=datetime.fromtimestamp(0),
                                      expected_finish=datetime.fromtimestamp(86400))
        milestone1.full_clean()

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
986
        milestone2 = VacancyMilestone(vacancy=self.vacancy, name="a" * 101, detail="install things",
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
                                      expected_start=datetime.fromtimestamp(0),
                                      expected_finish=datetime.fromtimestamp(86400))
        with self.assertRaises(ValidationError, msg="Name with more than 100 character should raise ValidationError"):
            milestone2.full_clean()

        milestone3 = VacancyMilestone(vacancy=self.vacancy, name="initiate", detail="install things",
                                      expected_start=datetime.fromtimestamp(86400),
                                      expected_finish=datetime.fromtimestamp(0))
        with self.assertRaises(ValidationError, msg="Expected finish earlier than tart should raise ValidationError"):
            milestone3.full_clean()

    def test_vacancy_milestones_list(self):
        milestone1 = self.create_milestone_object()
        self.client.force_authenticate(user=self.user)

        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data["results"]), 1)
        self.assertEqual(response.data["results"][0]["name"], milestone1.name)
        self.assertEqual(response.data["results"][0]["detail"], milestone1.detail)

    def test_create_new_milestone_on_a_vacancy_success(self):
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/'
        data = {"name": "initiate", "detail": "install things", "expected_start": "2019-01-20",
                "expected_finish": "2019-01-21"}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, data["name"])
        self.assertEqual(new_milestone.detail, data["detail"])

    def test_create_new_milestone_on_a_vacancy_invalid_data(self):
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/'
        data = {"name": "initiate", "detail": "install things", "expected_start": "2019-01-20",
                "expected_finish": "2019-01-01"}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(self.vacancy.milestones.count(), 0)

    def test_create_new_milestone_on_a_vacancy_unauthorized_user(self):
        self.client.force_authenticate(user=self.user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/'
        data = {"name": "initiate", "detail": "install things", "expected_start": "2019-01-20",
                "expected_finish": "2019-01-21"}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(self.vacancy.milestones.count(), 0)

    def test_modify_milestone_on_a_vacancy_success(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        data = {"name": "initiate env", "detail": "install all things", "expected_start": "2019-01-20",
                "expected_finish": "2019-01-21"}
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, data["name"])
        self.assertEqual(new_milestone.detail, data["detail"])

    def test_modify_milestone_on_a_vacancy_not_found(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/1000/milestones/' + str(milestone.pk) + '/'
        data = {"name": "initiate env", "detail": "install all things", "expected_start": "2019-01-20",
                "expected_finish": "2019-01-21"}
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        url2 = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/1000/'
        response2 = self.client.put(url2, data, format='json')
        self.assertEqual(response2.status_code, status.HTTP_404_NOT_FOUND)
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, milestone.name)
        self.assertEqual(new_milestone.detail, milestone.detail)

    def test_modify_milestone_on_a_vacancy_invalid_data(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        data = {"name": "a" * 101, "detail": "install all things", "expected_start": "2019-01-21",
                "expected_finish": "2019-01-19"}
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, milestone.name)
        self.assertEqual(new_milestone.detail, milestone.detail)

    def test_modify_milestone_on_a_vacancy_unauthorized_user(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        data = {"name": "initiate env", "detail": "install all things", "expected_start": "2019-01-20",
                "expected_finish": "2019-01-21"}
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, milestone.name)
        self.assertEqual(new_milestone.detail, milestone.detail)

    def test_delete_milestone_on_a_vacancy_success(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(self.vacancy.milestones.count(), 0)

    def test_delete_milestone_on_a_vacancy_not_found(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/1000/milestones/' + str(milestone.pk) + '/'
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        url2 = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/1000/'
        response2 = self.client.delete(url2, format='json')
        self.assertEqual(response2.status_code, status.HTTP_404_NOT_FOUND)
        self.assertEqual(self.vacancy.milestones.count(), 1)

    def test_delete_milestone_on_a_vacancy_unauthorized_user(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(self.vacancy.milestones.count(), 1)

1121

1122
class AcceptOneOfferTests(APITestCase):
1123
1124
1125
1126
    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)
1127

1128
1129
        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,
1130
                                              address=None)
1131
1132

        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
1133
1134
                                             description="lalala", requirements="requirements",
                                             close_time=datetime.today(), max_accepted_applicants=3)
1135

1136
        new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=True, open_time=datetime.fromtimestamp(0),
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
1137
1138
                                              description="lalala", requirements="requirements",
                                              close_time=datetime.today(), max_accepted_applicants=3)
1139
1140
1141
1142
1143
1144
1145
1146

        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()
1147

1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
        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:
1160
            status_response.append(app['status'])
1161
1162
1163
1164
1165
1166
1167
1168

        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):
1169
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181

        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')
1182
        body = json.loads(response.content)
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195

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

    def test_type_error_if_input_null