Completed
Pull Request — master (#12)
by Camille
56s
created

UserTests.test_change_pwd_no_pwd()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 6
rs 9.4286
1
import json
2
3
from django.core import mail
4
5
from rest_framework import status
6
from rest_framework.test import APITestCase, force_authenticate
7
8
from sigma_core.tests.factories import UserFactory, AdminUserFactory
9
from sigma_core.serializers.user import UserWithoutPermissionsSerializer as UserSerializer
10
11
12
class UserTests(APITestCase):
13
    @classmethod
14
    def setUpTestData(self):
15
        super(UserTests, self).setUpTestData()
16
17
        self.user = UserFactory()
18
        self.user2 = UserFactory()
19
        self.admin_user = AdminUserFactory()
20
21
        serializer = UserSerializer(self.user)
22
        self.user_data = serializer.data
23
        self.user_url = '/user/%d/' % self.user.id
24
25
        self.users_list = [self.user, self.user2, self.admin_user]
26
27
        self.new_user_data = {'lastname': 'Doe', 'firstname': 'John', 'email': '[email protected]', 'password': 'password'}
28
29
#### List requests
30
    def test_get_users_list_unauthed(self):
31
        # Client not authenticated
32
        response = self.client.get('/user/')
33
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
34
35
    # def test_get_users_list_forbidden(self):
36
    #     # Client authenticated but has no permission
37
    #     self.client.force_authenticate(user=self.user)
38
    #     response = self.client.get('/user/')
39
    #     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
40
41
    def test_get_users_list_ok(self):
42
        # Client has permissions
43
        self.client.force_authenticate(user=self.user)
44
        response = self.client.get('/user/')
45
        self.assertEqual(response.status_code, status.HTTP_200_OK)
46
        self.assertEqual(len(response.data), len(self.users_list))
47
48
#### Get requests
49
    def test_get_user_unauthed(self):
50
        # Client is not authenticated
51
        response = self.client.get(self.user_url)
52
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
53
54
    # def test_get_user_forbidden(self):
55
    #     # Client authenticated but has no permission
56
    #     self.client.force_authenticate(user=self.user2)
57
    #     response = self.client.get(self.user_url)
58
    #     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
59
60
    def test_get_user_ok(self):
61
        # Client has permissions
62
        self.client.force_authenticate(user=self.user)
63
        response = self.client.get(self.user_url)
64
        self.assertEqual(response.status_code, status.HTTP_200_OK)
65
        response.data.pop('permissions', None) # Workaround because DRY rest permissions needs a request
66
        self.assertEqual(response.data, self.user_data)
67
68
#### "Get my data" requests
69
    def test_get_my_data_unauthed(self):
70
        # Client is not authenticated
71
        response = self.client.get('/user/me/')
72
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
73
74
    def test_get_my_data_ok(self):
75
        # Client is authenticated
76
        self.client.force_authenticate(user=self.user)
77
        response = self.client.get('/user/me/')
78
        self.assertEqual(response.status_code, status.HTTP_200_OK)
79
        self.assertEqual(response.data['id'], self.user.id)
80
81
#### Create requests
82
    def test_create_user_unauthed(self):
83
        # Client is not authenticated
84
        response = self.client.post('/user/', self.new_user_data)
85
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
86
87
    def test_create_user_forbidden(self):
88
        # Client has no permission
89
        self.client.force_authenticate(user=self.user)
90
        response = self.client.post('/user/', self.new_user_data)
91
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
92
93
    def test_create_user_ok(self):
94
        # Client has permissions
95
        self.client.force_authenticate(user=self.admin_user)
96
        response = self.client.post('/user/', self.new_user_data)
97
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
98
        self.assertEqual(response.data['lastname'], self.new_user_data['lastname'])
99
100
#### Modification requests
101
    def test_edit_email_wrong_permission(self):
102
        # Client wants to change another user's email
103
        self.client.force_authenticate(user=self.user)
104
        user_data = UserSerializer(self.user2).data
105
        user_data['email'] = "[email protected]"
106
        response = self.client.put("/user/%d/" % self.user2.id, user_data)
107
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
108
109
    def test_edit_email_nonvalid_email(self):
110
        # Client wants to change his email with a non valid value
111
        self.client.force_authenticate(user=self.user)
112
        user_data = self.user_data.copy()
113
        user_data['email'] = "ThisIsNotAnEmail"
114
        response = self.client.put("/user/%d/" % self.user.id, user_data)
115
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
116
117
    def test_edit_email_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
        # Client wants to change his email and succeed in
119
        self.client.force_authenticate(user=self.user)
120
        user_data = self.user_data.copy()
121
        user_data['email'] = "[email protected]"
122
        response = self.client.put("/user/%d/" % self.user.id, user_data)
123
        self.assertEqual(response.status_code, status.HTTP_200_OK)
124
        self.assertEqual(response.data['email'], user_data['email'])
125
        # Guarantee that tests are independant
126
        self.user.email = self.user_data['email']
127
        self.user.save()
128
129
    def test_edit_profile_wrong_permission(self):
130
        # Client wants to change another user's phone number
131
        self.client.force_authenticate(user=self.user)
132
        user_data = UserSerializer(self.user2).data
133
        user_data['phone'] = "0123456789"
134
        response = self.client.put("/user/%d/" % self.user2.id, user_data)
135
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
136
137
    def test_edit_profile_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
        # Client wants to change his phone number
139
        self.client.force_authenticate(user=self.user)
140
        user_data = self.user_data.copy()
141
        user_data['phone'] = "0123456789"
142
        response = self.client.put("/user/%d/" % self.user.id, user_data)
143
        self.assertEqual(response.status_code, status.HTTP_200_OK)
144
        self.assertEqual(response.data['phone'], user_data['phone'])
145
        # Guarantee that tests are independant
146
        self.user.phone = self.user_data['phone']
147
        self.user.save()
148
149
    def test_edit_lastname_wrong_permission(self):
150
        # Client wants to change his lastname
151
        self.client.force_authenticate(user=self.user)
152
        user_data = self.user_data.copy()
153
        user_data['lastname'] = "Daudet"
154
        response = self.client.put("/user/%d/" % self.user.id, user_data)
155
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
156
157
    def test_edit_lastname_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
        # Admin wants to change an user's lastname
159
        self.client.force_authenticate(user=self.admin_user)
160
        user_data = self.user_data.copy()
161
        user_data['lastname'] = "Daudet"
162
        response = self.client.put("/user/%d/" % self.user.id, user_data)
163
        self.assertEqual(response.status_code, status.HTTP_200_OK)
164
        self.assertEqual(response.data['lastname'], user_data['lastname'])
165
        # Guarantee that tests are independant
166
        self.user.lastname = self.user_data['lastname']
167
        self.user.save()
168
169
170
#### "Change password" requests
171
    def test_change_pwd_wrong_pwd(self):
172
        # Client gives a wrong old password
173
        self.user.set_password('old_pwd')
174
        self.client.force_authenticate(user=self.user)
175
        response = self.client.put('/user/change_password/', {'old_password': 'wrong', 'password': 'new_pwd'})
176
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
177
178
    def test_change_pwd_no_pwd(self):
179
        # Client gives no new password
180
        self.user.set_password('old_pwd')
181
        self.client.force_authenticate(user=self.user)
182
        response = self.client.put('/user/change_password/', {'old_password': 'old_pwd', 'password': ''})
183
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
184
185
    def test_change_pwd_ok(self):
186
        # Client successfully changes his password
187
        self.user.set_password('old_pwd')
188
        self.client.force_authenticate(user=self.user)
189
        response = self.client.put('/user/change_password/', {'old_password': 'old_pwd', 'password': 'new_pwd'})
190
        self.assertEqual(response.status_code, status.HTTP_200_OK)
191
192
#### "Reset password" requests
193
    def test_reset_pwd_no_email(self):
194
        # Client gives no email
195
        response = self.client.post('/user/reset_password/', {'email': ''})
196
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
197
198
    def test_reset_pwd_no_user(self):
199
        # Client's email is not found
200
        response = self.client.post('/user/reset_password/', {'email': '[email protected]'})
201
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
202
203
    def test_reset_pwd_ok(self):
204
        # Client successfully resets his password
205
        response = self.client.post('/user/reset_password/', {'email': self.user.email})
206
        self.assertEqual(response.status_code, status.HTTP_200_OK)
207
        self.assertEqual(len(mail.outbox), 1)
208
        from sigma_core.views.user import reset_mail
209
        self.assertEqual(mail.outbox[0].subject, reset_mail['subject'])
210
211
#### Deletion requests
212