Fakultas Ilmu Komputer UI

test_vacancies.py 10.8 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
from core.models.accounts import Company, Supervisor
9
10
11
from core.models.vacancies import Vacancy


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
173
174
class CompanyListsTests(APITestCase):

    def test_company_vacancy_list_exist(self):
        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
181
182

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

    def test_company_application_list_exist(self):
        new_user = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
183
        new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
184
185
186
187
188
                                             address=None)

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

class SupervisorStudentApplicationTests(APITestCase):

    def test_list_student_application(self):
193
194
195
196

class SupervisorApprovalTests(APITestCase):

    def test_supervisor_approve_vacancy(self):
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
        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)

        url = '/api/student-applications/'
        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)

        url = '/api/student-applications/'
        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
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
        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,
                                             address=None)
        new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=False, open_time=datetime.fromtimestamp(0),
                                             description="lalala", close_time=datetime.today())

        url = '/api/vacancies/' + str(new_vacancy2.pk) + '/verify/'
        response = self.client.patch(url, {'verified' : True}, format='json')
        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)

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

        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)