Fakultas Ilmu Komputer UI

test_vacancies.py 13.9 KB
Newer Older
1
from datetime import datetime
2

3
4
5
6
import requests_mock
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase
7

8
9
from core.models.accounts import Company, Student
from core.models.vacancies import Vacancy, Application
10
11


12
class ApplicationTests(APITestCase):
13
14
15
16
17
18
19
20
21
22
    @requests_mock.Mocker()
    def test_application_list(self, m):
        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)
23
24
25
26
27
28
29
30
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890/', json={
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
31
32
33
34
35
36

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

Joshua Casey's avatar
Joshua Casey committed
37
        url = '/api/students/' + str(student_id) + '/applied-vacancies/'
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        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):
        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)
52
53
54
55
56
57
58
59
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890/', json={
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
60
61
62
63
64
65
66
67

        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')
68
        new_company = Company.objects.create(user=new_user, description="lalala",status=Company.VERIFIED,logo=None,address=None)
69
70
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0), description="lalala", close_time=datetime.today())

Joshua Casey's avatar
Joshua Casey committed
71
        url = '/api/students/' + str(student_id) + '/applied-vacancies/'
72
        response = self.client.post(url, {'vacancy_id' : new_vacancy.pk, 'cover_letter' : 'this is a cover letter.'}, format='json')
73
74
        self.assertEqual(response.status_code, status.HTTP_200_OK)

Joshua Casey's avatar
Joshua Casey committed
75
        url = '/api/students/' + str(student_id) + '/applied-vacancies/' + str(new_vacancy.pk) + '/'
76
77
78
79
        response = self.client.delete(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)


80
class BookmarkApplicationTests(APITestCase):
81
82
83
84
85
86
87
88
89
90
    @requests_mock.Mocker()
    def test_application_list(self, m):
        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)
91
92
93
94
95
96
97
98
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890/', json={
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

        url = '/api/login/'

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

        url = '/api/students/' + str(student_id) + '/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):
        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)
120
121
122
123
124
125
126
127
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890/', json={
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
128
129
130
131
132
133
134

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

135
        new_user = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
136
        new_company = Company.objects.create(user=new_user, description="lalala",status=Company.VERIFIED,logo=None,address=None)
137
138
139
140
141
142
143
144
145
        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0), description="lalala", close_time=datetime.today())

        url = '/api/students/' + str(student_id) + '/bookmarked-vacancies/'
        response = self.client.post(url, {'vacancy_id' : new_vacancy.pk}, format='json')
        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)
146

147

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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)

    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)
169
170


171
172
class CompanyListsTests(APITestCase):

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

177
178
179
180
        self.client.force_authenticate(new_user)

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

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    def test_company_vacancy_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) + '/vacancies/'
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

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

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
        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)

    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)

        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
                                             description="lalala", close_time=datetime.today())
        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')
240
        self.assertEqual(response.status_code, status.HTTP_200_OK)
241
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

    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)

        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
                                             description="lalala", close_time=datetime.today())
        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_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)

        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
                                             description="lalala", close_time=datetime.today())
        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')
284
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)