2626
2727from .models import TestUser
2828
29- User = get_user_model ()
29+ User = get_user_model () # = TestUser
3030
3131
32- class BackendUtilsTests (TestCase ):
32+ class BackendUtilMethodsTests (TestCase ):
3333 def test_get_model_ok (self ):
3434 user_model = get_model ('testprofiles.TestUser' )
3535 self .assertEqual (user_model , TestUser )
@@ -84,13 +84,14 @@ def test_set_attribute(self):
8484 self .assertEqual (u .custom_attribute , 'new_value' )
8585
8686
87- class Saml2BackendTests (TestCase ):
88- def test_update_user (self ):
89- # we need a user
90- user = User .objects .create (username = 'john' )
87+ class DefaultSaml2BackendTests (TestCase ):
9188
92- backend = Saml2Backend ()
89+ def setUp (self ):
90+ self .backend = Saml2Backend ()
91+ self .user = TestUser .objects .create (username = 'john' )
92+ # self.test_user = TestUser.objects.create(username='john')
9393
94+ def test_update_user (self ):
9495 attribute_mapping = {
9596 'uid' : ('username' , ),
9697 'mail' : ('email' , ),
@@ -103,20 +104,17 @@ def test_update_user(self):
103104 'cn' : ('John' , ),
104105 'sn' : ('Doe' , ),
105106 }
106- backend ._update_user (user , attributes , attribute_mapping )
107- self .assertEqual (user .email , 'john@example.com' )
108- self .assertEqual (user .first_name , 'John' )
109- self .assertEqual (user .last_name , 'Doe' )
107+ self . backend ._update_user (self . user , attributes , attribute_mapping )
108+ self .assertEqual (self . user .email , 'john@example.com' )
109+ self .assertEqual (self . user .first_name , 'John' )
110+ self .assertEqual (self . user .last_name , 'Doe' )
110111
111112 attribute_mapping ['saml_age' ] = ('age' , )
112113 attributes ['saml_age' ] = ('22' , )
113- backend ._update_user (user , attributes , attribute_mapping )
114- self .assertEqual (user .age , '22' )
114+ self . backend ._update_user (self . user , attributes , attribute_mapping )
115+ self .assertEqual (self . user .age , '22' )
115116
116117 def test_update_user_callable_attributes (self ):
117- user = User .objects .create (username = 'john' )
118-
119- backend = Saml2Backend ()
120118 attribute_mapping = {
121119 'uid' : ('username' , ),
122120 'mail' : ('email' , ),
@@ -129,15 +127,15 @@ def test_update_user_callable_attributes(self):
129127 'cn' : ('John' , ),
130128 'sn' : ('Doe' , ),
131129 }
132- backend ._update_user (user , attributes , attribute_mapping )
133- self .assertEqual (user .email , 'john@example.com' )
134- self .assertEqual (user .first_name , 'John' )
135- self .assertEqual (user .last_name , 'Doe' )
130+ self . backend ._update_user (self . user , attributes , attribute_mapping )
131+ self .assertEqual (self . user .email , 'john@example.com' )
132+ self .assertEqual (self . user .first_name , 'John' )
133+ self .assertEqual (self . user .last_name , 'Doe' )
136134
137135 def test_update_user_empty_attribute (self ):
138- user = User .objects .create (username = 'john' , last_name = 'Smith' )
136+ self .user .last_name = 'Smith'
137+ self .user .save ()
139138
140- backend = Saml2Backend ()
141139 attribute_mapping = {
142140 'uid' : ('username' , ),
143141 'mail' : ('email' , ),
@@ -151,20 +149,17 @@ def test_update_user_empty_attribute(self):
151149 'sn' : (),
152150 }
153151 with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
154- backend ._update_user (user , attributes , attribute_mapping )
155- self .assertEqual (user .email , 'john@example.com' )
156- self .assertEqual (user .first_name , 'John' )
152+ self . backend ._update_user (self . user , attributes , attribute_mapping )
153+ self .assertEqual (self . user .email , 'john@example.com' )
154+ self .assertEqual (self . user .first_name , 'John' )
157155 # empty attribute list: no update
158- self .assertEqual (user .last_name , 'Smith' )
156+ self .assertEqual (self . user .last_name , 'Smith' )
159157 self .assertIn (
160- 'DEBUG:djangosaml2:Could not find value for "sn", not '
161- 'updating fields "(\' last_name\' ,)"' ,
158+ 'DEBUG:djangosaml2:Could not find value for "sn", not updating fields "(\' last_name\' ,)"' ,
162159 logs .output ,
163160 )
164161
165162 def test_invalid_model_attribute_log (self ):
166- backend = Saml2Backend ()
167-
168163 attribute_mapping = {
169164 'uid' : ['username' ],
170165 'cn' : ['nonexistent' ],
@@ -175,17 +170,15 @@ def test_invalid_model_attribute_log(self):
175170 }
176171
177172 with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
178- user , _ = backend .get_or_create_user (get_django_user_lookup_attribute (get_saml_user_model ()), 'john' , True )
179- backend ._update_user (user , attributes , attribute_mapping )
173+ user , _ = self . backend .get_or_create_user (get_django_user_lookup_attribute (get_saml_user_model ()), 'john' , True )
174+ self . backend ._update_user (user , attributes , attribute_mapping )
180175
181176 self .assertIn (
182177 'DEBUG:djangosaml2:Could not find attribute "nonexistent" on user "john"' ,
183178 logs .output ,
184179 )
185180
186181 def test_django_user_main_attribute (self ):
187- backend = Saml2Backend ()
188-
189182 old_username_field = User .USERNAME_FIELD
190183 User .USERNAME_FIELD = 'slug'
191184 self .assertEqual (get_django_user_lookup_attribute (get_saml_user_model ()), 'slug' )
@@ -206,74 +199,63 @@ def test_django_user_main_attribute(self):
206199 self .assertEqual (get_django_user_lookup_attribute (get_saml_user_model ()), 'foo' )
207200
208201 def test_get_or_create_user_existing (self ):
209- backend = Saml2Backend ()
210-
211- TestUser .objects .create (username = 'john' )
212-
213202 with override_settings (SAML_USER_MODEL = 'testprofiles.TestUser' ):
214- john , created = backend .get_or_create_user (
203+ user , created = self . backend .get_or_create_user (
215204 get_django_user_lookup_attribute (get_saml_user_model ()),
216205 'john' ,
217206 False ,
218207 )
219208
220- self .assertTrue (isinstance (john , TestUser ))
209+ self .assertTrue (isinstance (user , TestUser ))
221210 self .assertFalse (created )
222211
223212 def test_get_or_create_user_duplicates (self ):
224- backend = Saml2Backend ()
225-
226- TestUser .objects .create (username = 'john' , age = 1 )
227- TestUser .objects .create (username = 'paul' , age = 1 )
213+ TestUser .objects .create (username = 'paul' )
228214
229215 with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
230216 with override_settings (SAML_USER_MODEL = 'testprofiles.TestUser' ):
231- john , created = backend .get_or_create_user (
217+ user , created = self . backend .get_or_create_user (
232218 'age' ,
233- 1 ,
219+ '' ,
234220 False ,
235221 )
236222
237- self .assertTrue (john is None )
223+ self .assertTrue (user is None )
238224 self .assertFalse (created )
239225 self .assertIn (
240- "ERROR:djangosaml2:Multiple users match, model: testprofiles.testuser, lookup: {'age': 1 }" ,
226+ "ERROR:djangosaml2:Multiple users match, model: testprofiles.testuser, lookup: {'age': '' }" ,
241227 logs .output ,
242228 )
243229
244230 def test_get_or_create_user_no_create (self ):
245- backend = Saml2Backend ()
246-
247231 with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
248232 with override_settings (SAML_USER_MODEL = 'testprofiles.TestUser' ):
249- john , created = backend .get_or_create_user (
233+ user , created = self . backend .get_or_create_user (
250234 get_django_user_lookup_attribute (get_saml_user_model ()),
251- 'john ' ,
235+ 'paul ' ,
252236 False ,
253237 )
254238
255- self .assertTrue (john is None )
239+ self .assertTrue (user is None )
256240 self .assertFalse (created )
257241 self .assertIn (
258- "ERROR:djangosaml2:The user does not exist, model: testprofiles.testuser, lookup: {'username': 'john '}" ,
242+ "ERROR:djangosaml2:The user does not exist, model: testprofiles.testuser, lookup: {'username': 'paul '}" ,
259243 logs .output ,
260244 )
261245
262246 def test_get_or_create_user_create (self ):
263- backend = Saml2Backend ()
264-
265247 with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
266248 with override_settings (SAML_USER_MODEL = 'testprofiles.TestUser' ):
267- john , created = backend .get_or_create_user (
249+ user , created = self . backend .get_or_create_user (
268250 get_django_user_lookup_attribute (get_saml_user_model ()),
269- 'john ' ,
251+ 'paul ' ,
270252 True ,
271253 )
272254
273- self .assertTrue (isinstance (john , TestUser ))
255+ self .assertTrue (isinstance (user , TestUser ))
274256 self .assertTrue (created )
275257 self .assertIn (
276- f"DEBUG:djangosaml2:New user created: { john } " ,
258+ f"DEBUG:djangosaml2:New user created: { user } " ,
277259 logs .output ,
278260 )
279261
0 commit comments