forked from JustinWingChungHui/okKindred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
280 lines (216 loc) · 9.42 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from django.test import TestCase
from django.test.utils import override_settings
from rest_framework import status
from rest_framework.test import APIClient
from custom_user.models import User
from family_tree.models import Family, Person
import json
@override_settings(SECURE_SSL_REDIRECT=False, AXES_BEHIND_REVERSE_PROXY=False)
class UserApiTestCase(TestCase):
'''
Tests for the user API
'''
def setUp(self):
self.family = Family()
self.family.save()
self.user = User.objects.create_user(email='[email protected]',
password='user',
name='user',
family = self.family)
self.user2 = User.objects.create_user(email='[email protected]',
password='user2',
name='user2',
family = self.family)
self.family2 = Family()
self.family2.save()
self.user3 = User.objects.create_user(email='[email protected]',
password='user3',
name='user3',
family = self.family2)
super(UserApiTestCase, self).setUp()
def test_list_requires_authentication(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
url = '/api/users/'
response = client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
json.loads(response.content)
def test_list(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/users/'
response = client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(b'[email protected]' in response.content)
self.assertTrue(b'[email protected]' in response.content)
self.assertFalse(b'[email protected]' in response.content)
json.loads(response.content)
def test_get_requires_authentication(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
url = '/api/user_settings/'
response = client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
json.loads(response.content)
def test_retrieve(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/user_settings/'
response = client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(b'receive_update_emails' in response.content)
json.loads(response.content)
def test_update(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
data = {
'language': 'fi',
'receive_update_emails': True,
'receive_photo_update_emails': True,
}
url = '/api/user_settings/'
response = client.patch(url, data, format='json')
self.user = User.objects.get(id=self.user.id)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(b'fi' in response.content)
self.assertTrue(self.user.receive_update_emails)
json.loads(response.content)
def test_cannot_update_other_user_values(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
data = {
'language': 'fi',
'receive_update_emails': True,
'receive_photo_update_emails': True,
'is_superuser': True,
}
url = '/api/user_settings/'
response = client.patch(url, data, format='json')
self.user = User.objects.get(id=self.user.id)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(self.user.is_superuser)
json.loads(response.content)
def test_change_password_requires_authentication(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
url = '/api/password_change/'
data= {
'old_password': 'user1',
'new_password': 'user1a'
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
json.loads(response.content)
def test_change_password_success(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/password_change/'
data= {
'old_password': 'user',
'new_password': 'user12345'
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
json.loads(response.content)
user = User.objects.get(id=self.user.id)
result = user.check_password('user12345')
self.assertTrue(result)
def test_change_password_missing_parameters(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/password_change/'
data= {}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
json.loads(response.content)
def test_change_password_invalid_old_password(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/password_change/'
data= {
'old_password': 'not valid',
'new_password': 'user12345'
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
json.loads(response.content)
def test_change_password_too_short(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/password_change/'
data= {
'old_password': 'user',
'new_password': 'u'
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
json.loads(response.content)
def test_delete_account_requires_authentication(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
url = '/api/delete_account/'
response = client.post(url, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
json.loads(response.content)
def test_delete_account_no_password(self):
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/delete_account/'
data= {
'password': '',
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
json.loads(response.content)
def test_delete_account_last_user(self):
person3 = Person.objects.create(
name='User3',
gender='M',
email='[email protected]',
family_id=self.family2.id,
language='en',
user_id=self.user3.id)
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user3)
url = '/api/delete_account/'
data= {
'password': 'user3',
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(0, Person.objects.filter(id=person3.id).count())
self.assertEqual(0, User.objects.filter(id=self.user3.id).count())
def test_delete_account_multi_user_leave_profile(self):
person = Person.objects.create(
name='User',
gender='M',
email='[email protected]',
family_id=self.family.id,
language='en',
user_id=self.user.id)
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/delete_account/'
data= {
'password': 'user',
'delete_profile': False
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(1, Person.objects.filter(id=person.id).count())
self.assertEqual(0, User.objects.filter(id=self.user.id).count())
def test_delete_account_multi_user_delete_profile(self):
person = Person.objects.create(
name='User',
gender='M',
email='[email protected]',
family_id=self.family.id,
language='en',
user_id=self.user.id)
client = APIClient(HTTP_X_REAL_IP='127.0.0.1')
client.force_authenticate(user=self.user)
url = '/api/delete_account/'
data= {
'password': 'user',
'delete_profile': True
}
response = client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(0, Person.objects.filter(id=person.id).count())
self.assertEqual(0, User.objects.filter(id=self.user.id).count())