26
26
27
27
from .models import TestUser
28
28
29
- User = get_user_model ()
29
+ User = get_user_model () # = TestUser
30
30
31
31
32
- class BackendUtilsTests (TestCase ):
32
+ class BackendUtilMethodsTests (TestCase ):
33
33
def test_get_model_ok (self ):
34
34
user_model = get_model ('testprofiles.TestUser' )
35
35
self .assertEqual (user_model , TestUser )
@@ -84,13 +84,14 @@ def test_set_attribute(self):
84
84
self .assertEqual (u .custom_attribute , 'new_value' )
85
85
86
86
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 ):
91
88
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')
93
93
94
+ def test_update_user (self ):
94
95
attribute_mapping = {
95
96
'uid' : ('username' , ),
96
97
'mail' : ('email' , ),
@@ -103,20 +104,17 @@ def test_update_user(self):
103
104
'cn' : ('John' , ),
104
105
'sn' : ('Doe' , ),
105
106
}
106
- backend ._update_user (user , attributes , attribute_mapping )
107
- self .
assertEqual (
user .
email ,
'[email protected] ' )
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 ,
'[email protected] ' )
109
+ self .assertEqual (self . user .first_name , 'John' )
110
+ self .assertEqual (self . user .last_name , 'Doe' )
110
111
111
112
attribute_mapping ['saml_age' ] = ('age' , )
112
113
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' )
115
116
116
117
def test_update_user_callable_attributes (self ):
117
- user = User .objects .create (username = 'john' )
118
-
119
- backend = Saml2Backend ()
120
118
attribute_mapping = {
121
119
'uid' : ('username' , ),
122
120
'mail' : ('email' , ),
@@ -129,15 +127,15 @@ def test_update_user_callable_attributes(self):
129
127
'cn' : ('John' , ),
130
128
'sn' : ('Doe' , ),
131
129
}
132
- backend ._update_user (user , attributes , attribute_mapping )
133
- self .
assertEqual (
user .
email ,
'[email protected] ' )
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 ,
'[email protected] ' )
132
+ self .assertEqual (self . user .first_name , 'John' )
133
+ self .assertEqual (self . user .last_name , 'Doe' )
136
134
137
135
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 ()
139
138
140
- backend = Saml2Backend ()
141
139
attribute_mapping = {
142
140
'uid' : ('username' , ),
143
141
'mail' : ('email' , ),
@@ -151,20 +149,17 @@ def test_update_user_empty_attribute(self):
151
149
'sn' : (),
152
150
}
153
151
with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
154
- backend ._update_user (user , attributes , attribute_mapping )
155
- self .
assertEqual (
user .
email ,
'[email protected] ' )
156
- self .assertEqual (user .first_name , 'John' )
152
+ self . backend ._update_user (self . user , attributes , attribute_mapping )
153
+ self .
assertEqual (
self . user .
email ,
'[email protected] ' )
154
+ self .assertEqual (self . user .first_name , 'John' )
157
155
# empty attribute list: no update
158
- self .assertEqual (user .last_name , 'Smith' )
156
+ self .assertEqual (self . user .last_name , 'Smith' )
159
157
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\' ,)"' ,
162
159
logs .output ,
163
160
)
164
161
165
162
def test_invalid_model_attribute_log (self ):
166
- backend = Saml2Backend ()
167
-
168
163
attribute_mapping = {
169
164
'uid' : ['username' ],
170
165
'cn' : ['nonexistent' ],
@@ -175,17 +170,15 @@ def test_invalid_model_attribute_log(self):
175
170
}
176
171
177
172
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 )
180
175
181
176
self .assertIn (
182
177
'DEBUG:djangosaml2:Could not find attribute "nonexistent" on user "john"' ,
183
178
logs .output ,
184
179
)
185
180
186
181
def test_django_user_main_attribute (self ):
187
- backend = Saml2Backend ()
188
-
189
182
old_username_field = User .USERNAME_FIELD
190
183
User .USERNAME_FIELD = 'slug'
191
184
self .assertEqual (get_django_user_lookup_attribute (get_saml_user_model ()), 'slug' )
@@ -206,74 +199,63 @@ def test_django_user_main_attribute(self):
206
199
self .assertEqual (get_django_user_lookup_attribute (get_saml_user_model ()), 'foo' )
207
200
208
201
def test_get_or_create_user_existing (self ):
209
- backend = Saml2Backend ()
210
-
211
- TestUser .objects .create (username = 'john' )
212
-
213
202
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 (
215
204
get_django_user_lookup_attribute (get_saml_user_model ()),
216
205
'john' ,
217
206
False ,
218
207
)
219
208
220
- self .assertTrue (isinstance (john , TestUser ))
209
+ self .assertTrue (isinstance (user , TestUser ))
221
210
self .assertFalse (created )
222
211
223
212
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' )
228
214
229
215
with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
230
216
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 (
232
218
'age' ,
233
- 1 ,
219
+ '' ,
234
220
False ,
235
221
)
236
222
237
- self .assertTrue (john is None )
223
+ self .assertTrue (user is None )
238
224
self .assertFalse (created )
239
225
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': '' }" ,
241
227
logs .output ,
242
228
)
243
229
244
230
def test_get_or_create_user_no_create (self ):
245
- backend = Saml2Backend ()
246
-
247
231
with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
248
232
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 (
250
234
get_django_user_lookup_attribute (get_saml_user_model ()),
251
- 'john ' ,
235
+ 'paul ' ,
252
236
False ,
253
237
)
254
238
255
- self .assertTrue (john is None )
239
+ self .assertTrue (user is None )
256
240
self .assertFalse (created )
257
241
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 '}" ,
259
243
logs .output ,
260
244
)
261
245
262
246
def test_get_or_create_user_create (self ):
263
- backend = Saml2Backend ()
264
-
265
247
with self .assertLogs ('djangosaml2' , level = 'DEBUG' ) as logs :
266
248
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 (
268
250
get_django_user_lookup_attribute (get_saml_user_model ()),
269
- 'john ' ,
251
+ 'paul ' ,
270
252
True ,
271
253
)
272
254
273
- self .assertTrue (isinstance (john , TestUser ))
255
+ self .assertTrue (isinstance (user , TestUser ))
274
256
self .assertTrue (created )
275
257
self .assertIn (
276
- f"DEBUG:djangosaml2:New user created: { john } " ,
258
+ f"DEBUG:djangosaml2:New user created: { user } " ,
277
259
logs .output ,
278
260
)
279
261
0 commit comments