@@ -63,7 +63,7 @@ def test_auth(self):
6363 content_type = 'application/x-www-form-urlencoded' )
6464 self .assertEqual (response .status_code , 200 )
6565
66- @override_settings (REGISTRATION_ENABLED = False )
66+ @override_settings (REGISTRATION_MODE = "disabled" )
6767 def test_auth_registration_disabled (self ):
6868 """test that a new user cannot register when registration is disabled."""
6969 url = '/v1/auth/register'
@@ -79,6 +79,89 @@ def test_auth_registration_disabled(self):
7979 response = self .client .post (url , json .dumps (submit ), content_type = 'application/json' )
8080 self .assertEqual (response .status_code , 403 )
8181
82+ @override_settings (REGISTRATION_MODE = "admin_only" )
83+ def test_auth_registration_admin_only_fails_if_not_admin (self ):
84+ """test that a non superuser cannot register when registration is admin only."""
85+ url = '/v1/auth/register'
86+ submit = {
87+ 'username' : 'testuser' ,
88+ 'password' : 'password' ,
89+ 'first_name' : 'test' ,
90+ 'last_name' : 'user' ,
91+ 'email' : 'test@user.com' ,
92+ 'is_superuser' : False ,
93+ 'is_staff' : False ,
94+ }
95+ response = self .client .post (url , json .dumps (submit ), content_type = 'application/json' )
96+ self .assertEqual (response .status_code , 403 )
97+
98+ @override_settings (REGISTRATION_MODE = "admin_only" )
99+ def test_auth_registration_admin_only_works (self ):
100+ """test that a superuser can register when registration is admin only."""
101+
102+ user = User .objects .get (username = 'autotest' )
103+ token = Token .objects .get (user = user )
104+
105+ url = '/v1/auth/register'
106+
107+ username , password = 'newuser_by_admin' , 'password'
108+ first_name , last_name = 'Otto' , 'Test'
109+ email = 'autotest@deis.io'
110+
111+ submit = {
112+ 'username' : username ,
113+ 'password' : password ,
114+ 'first_name' : first_name ,
115+ 'last_name' : last_name ,
116+ 'email' : email ,
117+ # try to abuse superuser/staff level perms (not the first signup!)
118+ 'is_superuser' : True ,
119+ 'is_staff' : True ,
120+ }
121+ response = self .client .post (url , json .dumps (submit ), content_type = 'application/json' ,
122+ HTTP_AUTHORIZATION = 'token {}' .format (token ))
123+
124+ self .assertEqual (response .status_code , 201 )
125+ for key in response .data .keys ():
126+ self .assertIn (key , ['id' , 'last_login' , 'is_superuser' , 'username' , 'first_name' ,
127+ 'last_name' , 'email' , 'is_active' , 'is_superuser' , 'is_staff' ,
128+ 'date_joined' , 'groups' , 'user_permissions' ])
129+ expected = {
130+ 'username' : username ,
131+ 'email' : email ,
132+ 'first_name' : first_name ,
133+ 'last_name' : last_name ,
134+ 'is_active' : True ,
135+ 'is_superuser' : False ,
136+ 'is_staff' : False
137+ }
138+ self .assertDictContainsSubset (expected , response .data )
139+ # test login
140+ url = '/v1/auth/login/'
141+ payload = urllib .urlencode ({'username' : username , 'password' : password })
142+ response = self .client .post (url , data = payload ,
143+ content_type = 'application/x-www-form-urlencoded' )
144+ self .assertEqual (response .status_code , 200 )
145+
146+ @override_settings (REGISTRATION_MODE = "not_a_mode" )
147+ def test_auth_registration_fails_with_nonexistant_mode (self ):
148+ """test that a registration should fail with a nonexistant mode"""
149+ url = '/v1/auth/register'
150+ submit = {
151+ 'username' : 'testuser' ,
152+ 'password' : 'password' ,
153+ 'first_name' : 'test' ,
154+ 'last_name' : 'user' ,
155+ 'email' : 'test@user.com' ,
156+ 'is_superuser' : False ,
157+ 'is_staff' : False ,
158+ }
159+
160+ try :
161+ self .client .post (url , json .dumps (submit ), content_type = 'application/json' )
162+ except Exception , e :
163+ self .assertEqual (str (e ), 'not_a_mode is not a valid registation mode' )
164+
82165 def test_cancel (self ):
83166 """Test that a registered user can cancel her account."""
84167 # test registration workflow
0 commit comments