Fakultas Ilmu Komputer UI

test_accounts.py 11.5 KB
Newer Older
1
2
import requests_mock
from rest_framework import status
3
from rest_framework.test import APIClient, APITestCase
4
from django.contrib.auth.models import User
5
from django.core.exceptions import ValidationError
6
from core.models.accounts import Company, Supervisor, Student
7
8


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

        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
40
        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)
41
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
        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')
74
        new_company = Company.objects.create(user=new_user, description="lalalala", status=Company.VERIFIED, logo=None, address=None)
75
76
77

        url = '/api/login/'
        response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123', 'login-type': 'company'}, format='json')
78
79
80
81
82
83
        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)
84

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


class RegisterTests(APITestCase):

    def test_create_and_recreate(self):
        url = '/api/register/'
95
        tc_post = {'password': 'corporatepass', 'name':'tutuplapak', 'description':'menutup lapak', 'email': 'email@email.com', 'logo':'lalala', 'address':'alamat', 'category':'Perusahaan Jasa', 'website':'www.tutuplapak.com'}
96
97
98
99
100
101
102
103
104
105
        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)
106

107
108
109
110
111
112
113
114
115
116
    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()


117

118
119
120
121
class ProfileUpdateTests(APITestCase):

    @requests_mock.Mocker()
    def test_student_profile_update(self, m):
Hafiyyan's avatar
Hafiyyan committed
122
        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)
123
124
125
126
127
128
129
130
        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
131
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
132
133
134
135
136
137
138
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)
139
        print("Hello World!")
140
141
142
143
144
        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
145
        url = '/api/students/' + str(student_id) + "/profile/"
146
147
148
149
        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
150
        url = '/api/students/' + str(student_id) + "/profile/"
151
152
153
154
        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/')

155
156
157
158
159
        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
160
161
162
163
        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
164
        url = '/api/students/' + str(student_id) + "/profile/"
165
166
167
        response = self.client.patch(url, {'email': 'saasdasd'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

168
169
170
171
        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
172
        url = '/api/students/123123123/profile/'
173
174
175
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

176
177
178
179
        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)

180
181
182
        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
183
        url = '/api/students/' + str(new_student.pk) + "/profile/"
184
185
        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
186
187

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

192
193
194
195
        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
196

197
198
199
200
201
202
203
204
        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')
205

HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
206
207
208
209
        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')
210

211
212
213
214
        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')
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
215
216
    def _create_test_file(self,path):
        f = open(path, 'r')
217
        return {'pdf_file': f}