33
44Run the tests with "./manage.py test api"
55"""
6-
7-
8- import json
9- from urllib .parse import urlencode
10-
116from django .contrib .auth .models import User
12- from django .test import TestCase
7+ from rest_framework .test import APITestCase
138from django .test .utils import override_settings
149from rest_framework .authtoken .models import Token
1510
1611
17- class AuthTest (TestCase ):
12+ class AuthTest (APITestCase ):
1813
1914 fixtures = ['test_auth.json' ]
2015
@@ -47,7 +42,7 @@ def test_auth(self):
4742 'is_staff' : True ,
4843 }
4944 url = '/v2/auth/register'
50- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' )
45+ response = self .client .post (url , submit )
5146 self .assertEqual (response .status_code , 201 )
5247 for key in response .data :
5348 self .assertIn (key , ['id' , 'last_login' , 'is_superuser' , 'username' , 'first_name' ,
@@ -63,12 +58,10 @@ def test_auth(self):
6358 'is_staff' : False
6459 }
6560 self .assertDictContainsSubset (expected , response .data )
61+
6662 # test login
67- url = '/v2/auth/login/'
68- payload = urlencode ({'username' : username , 'password' : password })
69- response = self .client .post (url , data = payload ,
70- content_type = 'application/x-www-form-urlencoded' )
71- self .assertEqual (response .status_code , 200 )
63+ response = self .client .login (username = username , password = password )
64+ self .assertEqual (response , True )
7265
7366 @override_settings (REGISTRATION_MODE = "disabled" )
7467 def test_auth_registration_disabled (self ):
@@ -83,7 +76,7 @@ def test_auth_registration_disabled(self):
8376 'is_superuser' : False ,
8477 'is_staff' : False ,
8578 }
86- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' )
79+ response = self .client .post (url , submit )
8780 self .assertEqual (response .status_code , 403 )
8881
8982 @override_settings (REGISTRATION_MODE = "admin_only" )
@@ -99,7 +92,7 @@ def test_auth_registration_admin_only_fails_if_not_admin(self):
9992 'is_superuser' : False ,
10093 'is_staff' : False ,
10194 }
102- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' )
95+ response = self .client .post (url , submit )
10396 self .assertEqual (response .status_code , 403 )
10497
10598 @override_settings (REGISTRATION_MODE = "admin_only" )
@@ -121,7 +114,7 @@ def test_auth_registration_admin_only_works(self):
121114 'is_superuser' : True ,
122115 'is_staff' : True ,
123116 }
124- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' ,
117+ response = self .client .post (url , submit ,
125118 HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
126119
127120 self .assertEqual (response .status_code , 201 )
@@ -139,12 +132,10 @@ def test_auth_registration_admin_only_works(self):
139132 'is_staff' : False
140133 }
141134 self .assertDictContainsSubset (expected , response .data )
135+
142136 # test login
143- url = '/v2/auth/login/'
144- payload = urlencode ({'username' : username , 'password' : password })
145- response = self .client .post (url , data = payload ,
146- content_type = 'application/x-www-form-urlencoded' )
147- self .assertEqual (response .status_code , 200 )
137+ response = self .client .login (username = username , password = password )
138+ self .assertEqual (response , True )
148139
149140 @override_settings (REGISTRATION_MODE = "not_a_mode" )
150141 def test_auth_registration_fails_with_nonexistant_mode (self ):
@@ -161,7 +152,7 @@ def test_auth_registration_fails_with_nonexistant_mode(self):
161152 }
162153
163154 try :
164- self .client .post (url , json . dumps ( submit ), content_type = 'application/json' )
155+ self .client .post (url , submit )
165156 except Exception as e :
166157 self .assertEqual (str (e ), 'not_a_mode is not a valid registation mode' )
167158
@@ -191,33 +182,30 @@ def test_cancel(self):
191182 'is_staff' : False ,
192183 }
193184 url = '/v2/auth/register'
194- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' )
185+ response = self .client .post (url , submit )
195186 self .assertEqual (response .status_code , 201 )
196187
197188 # cancel the account
198189 url = '/v2/auth/cancel'
199190 user = User .objects .get (username = username )
200191 token = Token .objects .get (user = user ).key
201- response = self .client .delete (url ,
202- HTTP_AUTHORIZATION = 'token {}' .format (token ))
192+ response = self .client .delete (url , HTTP_AUTHORIZATION = 'token {}' .format (token ))
203193 self .assertEqual (response .status_code , 204 )
204194
205195 url = '/v2/auth/register'
206- response = self .client .post (url , json . dumps ( other_submit ), content_type = 'application/json' )
196+ response = self .client .post (url , other_submit )
207197 self .assertEqual (response .status_code , 201 )
208198
209199 # normal user can't delete another user
210200 url = '/v2/auth/cancel'
211201 other_user = User .objects .get (username = other_username )
212202 other_token = Token .objects .get (user = other_user ).key
213- response = self .client .delete (url , json .dumps ({'username' : self .admin .username }),
214- content_type = 'application/json' ,
203+ response = self .client .delete (url , {'username' : self .admin .username },
215204 HTTP_AUTHORIZATION = 'token {}' .format (other_token ))
216205 self .assertEqual (response .status_code , 403 )
217206
218207 # admin can delete another user
219- response = self .client .delete (url , json .dumps ({'username' : other_username }),
220- content_type = 'application/json' ,
208+ response = self .client .delete (url , {'username' : other_username },
221209 HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
222210 self .assertEqual (response .status_code , 204 )
223211
@@ -230,8 +218,7 @@ def test_cancel(self):
230218 app_id = response .data ['id' ] # noqa
231219 self .assertIn ('id' , response .data )
232220
233- response = self .client .delete (url , json .dumps ({'username' : str (self .admin )}),
234- content_type = 'application/json' ,
221+ response = self .client .delete (url , {'username' : str (self .admin )},
235222 HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
236223 self .assertEqual (response .status_code , 409 )
237224
@@ -249,7 +236,7 @@ def test_passwd(self):
249236 'email' : email ,
250237 }
251238 url = '/v2/auth/register'
252- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' )
239+ response = self .client .post (url , submit )
253240 self .assertEqual (response .status_code , 201 )
254241 # change password
255242 url = '/v2/auth/passwd'
@@ -259,7 +246,7 @@ def test_passwd(self):
259246 'password' : 'password2' ,
260247 'new_password' : password ,
261248 }
262- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' ,
249+ response = self .client .post (url , submit ,
263250 HTTP_AUTHORIZATION = 'token {}' .format (token ))
264251 self .assertEqual (response .status_code , 400 )
265252 self .assertEqual (response .data , {'detail' : 'Current password does not match' })
@@ -268,20 +255,17 @@ def test_passwd(self):
268255 'password' : password ,
269256 'new_password' : 'password2' ,
270257 }
271- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' ,
258+ response = self .client .post (url , submit ,
272259 HTTP_AUTHORIZATION = 'token {}' .format (token ))
273260 self .assertEqual (response .status_code , 200 )
261+
274262 # test login with old password
275- url = '/v2/auth/login/'
276- payload = urlencode ({'username' : username , 'password' : password })
277- response = self .client .post (url , data = payload ,
278- content_type = 'application/x-www-form-urlencoded' )
279- self .assertEqual (response .status_code , 400 )
263+ response = self .client .login (username = username , password = password )
264+ self .assertEqual (response , False )
265+
280266 # test login with new password
281- payload = urlencode ({'username' : username , 'password' : 'password2' })
282- response = self .client .post (url , data = payload ,
283- content_type = 'application/x-www-form-urlencoded' )
284- self .assertEqual (response .status_code , 200 )
267+ response = self .client .login (username = username , password = 'password2' )
268+ self .assertEqual (response , True )
285269
286270 def test_change_user_passwd (self ):
287271 """
@@ -295,63 +279,52 @@ def test_change_user_passwd(self):
295279 'username' : self .user1 .username ,
296280 'new_password' : new_password ,
297281 }
298- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' ,
282+ response = self .client .post (url , submit ,
299283 HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
300284 self .assertEqual (response .status_code , 200 )
285+
301286 # test login with old password
302- url = '/v2/auth/login/'
303- payload = urlencode ({'username' : self .user1 .username , 'password' : old_password })
304- response = self .client .post (url , data = payload ,
305- content_type = 'application/x-www-form-urlencoded' )
306- self .assertEqual (response .status_code , 400 )
287+ response = self .client .login (username = self .user1 .username , password = old_password )
288+ self .assertEqual (response , False )
289+
307290 # test login with new password
308- payload = urlencode ({'username' : self .user1 .username , 'password' : new_password })
309- response = self .client .post (url , data = payload ,
310- content_type = 'application/x-www-form-urlencoded' )
311- self .assertEqual (response .status_code , 200 )
291+ response = self .client .login (username = self .user1 .username , password = new_password )
292+ self .assertEqual (response , True )
293+
312294 # Non-admins can't change another user's password
313295 submit ['password' ], submit ['new_password' ] = submit ['new_password' ], old_password
314296 url = '/v2/auth/passwd'
315- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' ,
297+ response = self .client .post (url , submit ,
316298 HTTP_AUTHORIZATION = 'token {}' .format (self .user2_token ))
317299 self .assertEqual (response .status_code , 403 )
300+
318301 # change back password with a regular user
319- response = self .client .post (url , json . dumps ( submit ), content_type = 'application/json' ,
302+ response = self .client .post (url , submit ,
320303 HTTP_AUTHORIZATION = 'token {}' .format (self .user1_token ))
321304 self .assertEqual (response .status_code , 200 )
305+
322306 # test login with new password
323- url = '/v2/auth/login/'
324- payload = urlencode ({'username' : self .user1 .username , 'password' : old_password })
325- response = self .client .post (url , data = payload ,
326- content_type = 'application/x-www-form-urlencoded' )
327- self .assertEqual (response .status_code , 200 )
307+ response = self .client .login (username = self .user1 .username , password = old_password )
308+ self .assertEqual (response , True )
328309
329310 def test_regenerate (self ):
330311 """ Test that token regeneration works"""
331-
332312 url = '/v2/auth/tokens/'
333313
334- response = self .client .post (url , '{}' , content_type = 'application/json' ,
335- HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
336-
314+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + self .admin_token )
315+ response = self .client .post (url , {})
337316 self .assertEqual (response .status_code , 200 )
338317 self .assertNotEqual (response .data ['token' ], self .admin_token )
339318
340- self .admin_token = Token .objects .get (user = self .admin )
341-
342- response = self .client .post (url , '{"username" : "autotest2"}' ,
343- content_type = 'application/json' ,
344- HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
319+ self .admin_token = Token .objects .get (user = self .admin ).key
320+ self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + self .admin_token )
345321
322+ response = self .client .post (url , {"username" : "autotest2" })
346323 self .assertEqual (response .status_code , 200 )
347324 self .assertNotEqual (response .data ['token' ], self .user1_token )
348325
349- response = self .client .post (url , '{"all" : "true"}' ,
350- content_type = 'application/json' ,
351- HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
326+ response = self .client .post (url , {"all" : "true" })
352327 self .assertEqual (response .status_code , 200 )
353328
354- response = self .client .post (url , '{}' , content_type = 'application/json' ,
355- HTTP_AUTHORIZATION = 'token {}' .format (self .admin_token ))
356-
329+ response = self .client .post (url , {})
357330 self .assertEqual (response .status_code , 401 )
0 commit comments