Fakultas Ilmu Komputer UI

test_accounts.py 13.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/')

Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
160
161
162
163
164
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'website_url': 'https://www.example.com/'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(response.data.get('website_url'), 'https://www.example.com/')

165
166
167
168
169
        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
170
171
172
173
        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')

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

178
179
180
181
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'phone_number': '````'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

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

186
187
188
189
        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)

Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
190
191
192
193
        url = '/api/students/' + str(student_id) + "/profile/"
        response = self.client.patch(url, {'website_url': 'this is not valid url'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

194
195
196
        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
197
        url = '/api/students/' + str(new_student.pk) + "/profile/"
198
199
        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
200
201

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

206
207
208
209
        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
210

Kristianto's avatar
Kristianto committed
211
        url = '/api/students/' + str(student_id) + "/profile/"
212
213
214
215
216
217
218
219
        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')
220

HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
221
222
223
224
        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')
225

226
227
228
229
        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')
230
    
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
231
232
    def _create_test_file(self,path):
        f = open(path, 'r')
233
        return {'pdf_file': f}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

    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')
        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)