Fakultas Ilmu Komputer UI

test_accounts.py 14.4 KB
Newer Older
1
import requests_mock
INDRA SEPTAMA's avatar
INDRA SEPTAMA committed
2
from datetime import date, timedelta
3
from rest_framework import status
4
from rest_framework.test import APIClient, APITestCase
5
from django.contrib.auth.models import User
6
from django.core.exceptions import ValidationError
INDRA SEPTAMA's avatar
INDRA SEPTAMA committed
7
from core.models.accounts import Company, Supervisor, Student, get_current_age
8
9


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

        url = '/api/login/'

        response = self.client.post(url, { 'username' : 'dummy.mahasiswa', 'password' : 'lalala', 'login-type' : 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_successful_supervisor_login_relogin(self, m):
Hafiyyan's avatar
Hafiyyan committed
41
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.dosen",
            "nama": "Dummy Dosen",
            "state": 1,
            "kode_org": "01.00.12.01:dosen",
            "kodeidentitas": "1234567891",
            "nama_role": "dosen"
        }, status_code=200)

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.dosen', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        response = self.client.post(url, {'username': 'dummy.dosen', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    @requests_mock.Mocker()
    def test_failed_sso_login(self, m):
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "state": 0
        }, status_code=200)

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.salah', 'password': 'lalala', 'login-type': 'sso-ui'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_failed_company_login(self):
        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.company.failed', 'password': 'lalala', 'login-type': 'company'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_success_company_login(self):
        new_user = User.objects.create_user('dummy.login.company', 'dummy.login.company@company.com', 'lalala123')
75
        new_company = Company.objects.create(user=new_user, description="lalalala", status=Company.VERIFIED, logo=None, address=None)
76
77
78

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123', 'login-type': 'company'}, format='json')
79
80
81
82
83
84
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_bad_request_1(self):
        url = '/api/login/'
        response = self.client.post(url, {'username': 'lalala'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
85

86
    def test_bad_request_2(self):
87
        url = '/api/login/'
88
89
        response = self.client.post(url, {'username': 'lalala', 'password': 'lalalala', 'login-type' : 'lalala'}, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
90

INDRA SEPTAMA's avatar
INDRA SEPTAMA committed
91
92
93
94
95
    def test_return_age(self):
        birth_day = (date.today() -timedelta(days = 700))
        self.assertEqual(1, get_current_age(birth_day))
        

96
97
98
99
100

class RegisterTests(APITestCase):

    def test_create_and_recreate(self):
        url = '/api/register/'
101
        tc_post = {'password': 'corporatepass', 'name':'tutuplapak', 'description':'menutup lapak', 'email': 'email@email.com', 'logo':'lalala', 'address':'alamat', 'category':'Perusahaan Jasa', 'website':'www.tutuplapak.com'}
102
103
104
105
106
107
108
109
110
111
        response = self.client.post(url, tc_post, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        response = self.client.post(url, tc_post, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)

    def test_bad_request(self):
        url = '/api/register/'
        response = self.client.post(url, {'username': 'lalala'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
112

113
114
115
116
117
118
119
120
121
122
    def test_npm_validator(self):
        new_user = User.objects.create_user('dummy.student2', 'dummy.student@student.com', 'lalala123')
        short_npm = Student(user=new_user, npm="123456789")
        self.assertRaises(ValidationError, short_npm.full_clean)
        wrong_checksum = Student(user=new_user, npm="1234567890")
        self.assertRaises(ValidationError, wrong_checksum.full_clean)
        right_checksum = Student(user=new_user, npm="1234567894")
        right_checksum.full_clean()


123

124
125
126
class ProfileUpdateTests(APITestCase):
    @requests_mock.Mocker()
    def test_student_profile_update(self, m):
Hafiyyan's avatar
Hafiyyan committed
127
        m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200)
128
129
130
131
132
133
134
135
        m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
            "username": "dummy.mahasiswa",
            "nama": "Dummy Mahasiswa",
            "state": 1,
            "kode_org": "01.00.12.01:mahasiswa",
            "kodeidentitas": "1234567890",
            "nama_role": "mahasiswa"
        }, status_code=200)
Hafiyyan's avatar
Hafiyyan committed
136
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
137
138
139
140
141
142
143
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
144
        print("Hello World!")
145
146
147
148
149
        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
                                    format='json')
        student_id = response.data.get('student').get('id')

Zamil Majdy's avatar
Zamil Majdy committed
150
        url = '/api/students/' + str(student_id) + "/profile/"
151
152
153
154
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('phone_number'), '08123123123')

PRIAMBUDI LINTANG BAGASKARA's avatar
PRIAMBUDI LINTANG BAGASKARA committed
155
        url = '/api/students/' + str(student_id) + "/profile/"
156
157
158
159
        response = self.client.patch(url, {'linkedin_url': 'https://www.linkedin.com/in/jojo/'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('linkedin_url'), 'https://www.linkedin.com/in/jojo/')

160
161
162
163
164
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'recommendations': 'mantap kak'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('recommendations'), 'mantap kak')

PRIAMBUDI LINTANG BAGASKARA's avatar
PRIAMBUDI LINTANG BAGASKARA committed
165
166
167
168
        response = self.client.patch(url, {'region': 'Indonesia'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('region'), 'Indonesia')

Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
169
170
171
172
173
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'gender': 'Perempuan'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('gender'), 'Perempuan')

Tyagita Larasati's avatar
Tyagita Larasati committed
174
175
176
177
178
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'skills': 'Java, Python'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('skills'), 'Java, Python')

Zamil Majdy's avatar
Zamil Majdy committed
179
        url = '/api/students/' + str(student_id) + "/profile/"
180
181
182
        response = self.client.patch(url, {'email': 'saasdasd'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

183
184
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'phone_number': '````'}, format='multipart')
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
185
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) 
186

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

191
192
193
194
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'linkedin_url': 'this is not valid url'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

195
196
197
        new_user = User.objects.create_user('dummy.student2', 'dummy.student@student.com', 'lalala123')
        new_student = Student.objects.create(user=new_user, npm="1212121212")

Zamil Majdy's avatar
Zamil Majdy committed
198
        url = '/api/students/' + str(new_student.pk) + "/profile/"
199
200
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
201
202

        url = '/api/students/' + str(student_id) + "/profile/"
203
        data = self._create_test_file('./assets/pdf/pdf-coba.pdf')
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
204
205
206
        response = self.client.patch(url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

207
208
209
210
        url = '/api/students/' + str(student_id) + "/profile/"
        data = self._create_test_file('./assets/img/image-coba.jpg')
        response = self.client.patch(url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
211

Kristianto's avatar
Kristianto committed
212
        url = '/api/students/' + str(student_id) + "/profile/"
213
214
215
216
217
218
219
220
        response = self.client.patch(url, {'latest_work': 'Teaching assistant at Fasilkom UI'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('latest_work'), 'Teaching assistant at Fasilkom UI')

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'latest_work_desc': 'Evaluate weekly assignment for 15 students'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('latest_work_desc'), 'Evaluate weekly assignment for 15 students')
221

HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
222
223
224
225
        url = '/api/students/' + str(student_id) + '/profile/'
        response = self.client.patch(url, {'intro': 'Saya tertarik dengan dunia front-end development'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('intro'), 'Saya tertarik dengan dunia front-end development')
226

227
        url = '/api/students/' + str(student_id) + "/profile/"
228
229
230
231
        response = self.client.patch(url, {'expected_salary': '4000000'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('expected_salary'), '4000000')
        
Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
232
233
234
        response = self.client.patch(url, {'job_seeking_status': 'Active'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('job_seeking_status'), 'Active')
235
236
237
238
239
240
241
242
        response = self.client.patch(url, {'GPA': 1.0}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('student_gpa'), 1.0)

        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'GPA': "this is not a valid input"}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        
243
244
245
246
        url = '/api/students/' + str(student_id) + '/profile/'
        response = self.client.patch(url, {'volunteer': 'Ketua BEM UI - 2020'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('volunteer'), 'Ketua BEM UI - 2020')
247
    
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
248
249
    def _create_test_file(self,path):
        f = open(path, 'r')
250
        return {'pdf_file': f}
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

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

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123', 'login-type': 'company'}, format='json')
        company_id = response.data.get('company').get('id')

        url = '/api/companies/' + str(company_id) + "/profile/"

        response = self.client.patch(url, {'name': 'Restopedia'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        response = self.client.patch(url, {'address': 'Isekai'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

        response = self.client.patch(url, {'description': 'Masak-Masak'}, format='multipart')
269
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)