-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
316 lines (232 loc) · 10.3 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# sys.stderr.write(repr(response.data) + '\n')
from django.core.urlresolvers import reverse
from rest_framework.test import APIRequestFactory
from rest_framework.test import APITestCase
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from rest_framework import status
from rest_framework.test import force_authenticate
from rest_framework.test import APIClient
from .views import AuthUser
import sys
from django.test import override_settings
@override_settings(CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/4",
'TIMEOUT': None,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SERIALIZER": "django_redis.serializers.json.JSONSerializer",
}
}
})
class UserTest(APITestCase):
def setUp(self):
# URL for creating an account.
self.create_url = reverse('account-create')
# URL for the auth-view
self.auth_url = reverse('auth-view')
# URL for the adding follower
self.follower_add = reverse('follower-add')
# URL for the deleting follower
self.follower_delete = reverse('follower-delete')
# URL for the deleting follower
self.follower_list = reverse('follower-list')
# URL for the login
self.login = reverse('login')
self.test_data = {
'username': 'pierre',
'email': '[email protected]',
'password': 'hkjl'*3
}
# response test
self.test_response = self.client.post(self.create_url , self.test_data, format='json')
def tearDown(self):
from django_redis import get_redis_connection
get_redis_connection("default").flushdb()
def test_create_user(self):
"""
Ensure we can create a new user and a valid token is created with it.
"""
data = {
'username': 'foobar',
'email': '[email protected]',
'password': 'somepassword'
}
response = self.client.post(self.create_url , data, format='json')
user = User.objects.latest('id')
token = Token.objects.get(user=user)
self.assertEqual(User.objects.count(), 2)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data['username'], data['username'])
self.assertEqual(response.data['email'], data['email'])
self.assertEqual(response.data['token'], token.key)
self.assertFalse('password' in response.data)
def test_create_user_with_short_password(self):
"""
Ensures user is not created for password lengths less than 8.
"""
data = {
'username': 'foobar',
'email': '[email protected]',
'password': 'foo'
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['password']), 1)
def test_create_user_with_no_password(self):
data = {
'username': 'foobar',
'email': '[email protected]',
'password': ''
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['password']), 1)
def test_create_user_with_too_long_username(self):
data = {
'username': 'foo'*30,
'email': '[email protected]',
'password': 'foobar'
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['username']), 1)
def test_create_user_with_no_username(self):
data = {
'username': '',
'email': '[email protected]',
'password': 'foobarbaz'
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['username']), 1)
def test_create_user_with_preexisting_username(self):
data = {
'username': self.test_data['username'],
'email': '[email protected]',
'password': 'testuser'
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['username']), 1)
def test_create_user_with_preexisting_email(self):
data = {
'username': 'testuser2',
'email': self.test_data['email'],
'password': 'testuser'
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['email']), 1)
def test_create_user_with_invalid_email(self):
data = {
'username': 'foobarbaz',
'email': 'testing',
'passsword': 'foobarbaz'
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['email']), 1)
def test_create_user_with_no_email(self):
data = {
'username' : 'foobar',
'email': '',
'password': 'foobarbaz'
}
response = self.client.post(self.create_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(len(response.data['email']), 1)
def test_auth_view(self):
"""
Ensure we can Retrieve from token
"""
view = AuthUser.as_view()
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + self.test_response.data['token'])
response = client.get(self.auth_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['username'], self.test_response.data['username'])
client.logout()
def test_login(self):
"""
Ensure we can login
"""
view = AuthUser.as_view()
client = APIClient()
client.login(username=self.test_data['username'], password=self.test_data['password'])
user = User.objects.latest('id')
token = Token.objects.get(user=user)
# client.force_authenticate(user=user)
response = client.get(self.login)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['token'], token.key)
client.logout()
def test_add_delete_follower(self):
data = {'username': 'foobar','email': '[email protected]','password': 'somepassword'}
follower_response = self.client.post(self.create_url , data, format='json')
view = AuthUser.as_view()
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + self.test_response.data['token'])
# add
response = client.post(self.follower_add, {"follower": data['username']}, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
# list
response = client.get(self.follower_list)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# delete
response = client.post(self.follower_delete, {"follower": data['username']}, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
client.logout()
def test_notif_follower(self):
data = {'username': 'foobar','email': '[email protected]','password': 'somepassword'}
follower_response = self.client.post(self.create_url , data, format='json')
user = User.objects.latest('id')
token = Token.objects.get(user=user)
view = AuthUser.as_view()
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + self.test_response.data['token'])
# add
response = client.post(self.follower_add, {"follower": data['username']}, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
client.logout()
view = AuthUser.as_view()
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
response = client.get(self.auth_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# sys.stderr.write(repr(response.data) + '\n')
self.assertEqual(response.data['notif'][0]['type'], 'Follower')
self.assertEqual(response.data['notif'][0]['message'], 'pierre is following you')
self.assertEqual(response.data['notif'][0]['related'], ['pierre'])
def test_add_invalid_follower(self):
view = AuthUser.as_view()
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + self.test_response.data['token'])
# add
response = client.post(self.follower_add, {"follower": "nopeUser"}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
client.logout()
def test_delete_invalid_follower(self):
view = AuthUser.as_view()
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + self.test_response.data['token'])
# delete
response = client.post(self.follower_delete, {"follower": "nopeUser"}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
client.logout()
def test_auth_view_fail(self):
"""
Ensure we can fail when no token provided
"""
response = self.client.get(self.auth_url)
self.assertEqual(response.status_code, 401)