Fakultas Ilmu Komputer UI

test_accounts.py 8.26 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 core.models.accounts import Company, Supervisor, Student
6
7


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

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

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

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


class RegisterTests(APITestCase):

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

106

107
108
109
110
class ProfileUpdateTests(APITestCase):

    @requests_mock.Mocker()
    def test_student_profile_update(self, m):
Hafiyyan's avatar
Hafiyyan committed
111
        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)
112
113
114
115
116
117
118
119
        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
120
        m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={
121
122
123
124
125
126
127
128
129
130
131
132
133
            "kota_lahir": "kota_kota",
            "tgl_lahir": "2017-12-31",
            "program": [{
                "nm_org": "Ilmu Informasi",
                "angkatan": "2017"
            }]
        }, status_code=200)

        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
134
        url = '/api/students/' + str(student_id) + "/profile/"
135
136
137
138
        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
139
140
141
142
143
        url = '/api/students/' + str(student_id) + "/profile/"
        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
144
        url = '/api/students/' + str(student_id) + "/profile/"
145
146
147
        response = self.client.patch(url, {'email': 'saasdasd'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Zamil Majdy's avatar
Zamil Majdy committed
148
        url = '/api/students/123123123/profile/'
149
150
151
152
153
154
        response = self.client.patch(url, {'phone_number': '08123123123'}, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        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
155
        url = '/api/students/' + str(new_student.pk) + "/profile/"
156
157
        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
158
159
160
161
162
163
164
165
166

        url = '/api/students/' + str(student_id) + "/profile/"
        data = self._create_test_file('./.tmp/pdf-coba.pdf')
        response = self.client.patch(url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_200_OK)


    def _create_test_file(self,path):
        f = open(path, 'r')
ALIF AHSANIL SATRIA's avatar
ALIF AHSANIL SATRIA committed
167
        return {'pdf_file': f}