Skip to content

Commit 04388f8

Browse files
committed
test fix
1 parent 8082bd8 commit 04388f8

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

djangosaml2/backends.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ def set_attribute(obj: Any, attr: str, new_value: Any) -> bool:
6969
return False
7070

7171

72-
UserModel = get_saml_user_model()
73-
74-
7572
class Saml2Backend(ModelBackend):
7673
def is_authorized(self, attributes, attribute_mapping) -> bool:
7774
""" Hook to allow custom authorization policies based on SAML attributes. """
@@ -89,6 +86,8 @@ def _extract_user_identifier_params(self, session_info, attributes, attribute_ma
8986
""" Returns the attribute to perform a user lookup on, and the value to use for it.
9087
The value could be the name_id, or any other saml attribute from the request.
9188
"""
89+
UserModel = get_saml_user_model()
90+
9291
# Lookup key
9392
user_lookup_key = get_django_user_lookup_attribute(UserModel)
9493

@@ -124,6 +123,8 @@ def get_or_create_user(self, user_lookup_key, user_lookup_value, create_unknown_
124123
e.g. customize this per IdP. The kwargs contain these additional params: session_info, attribute_mapping, attributes, request.
125124
The identity provider id can be found in kwargs['session_info']['issuer]
126125
"""
126+
UserModel = get_saml_user_model()
127+
127128
# Construct query parameters to query the userModel with. An additional lookup modifier could be specified in the settings.
128129
user_query_args = {
129130
user_lookup_key + getattr(settings, 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP', ''): user_lookup_value

tests/testprofiles/tests.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
from django.contrib.auth import get_user_model
2020
from django.contrib.auth.models import User as DjangoUserModel
21-
from django.test import TestCase, override_settings
2221
from django.core.exceptions import ImproperlyConfigured
23-
from djangosaml2.backends import (Saml2Backend, get_model, get_saml_user_model, set_attribute,
24-
get_django_user_lookup_attribute, get_django_user_lookup_attribute,
25-
get_saml_user_model)
22+
from django.test import TestCase, override_settings
23+
24+
from djangosaml2.backends import (Saml2Backend,
25+
get_django_user_lookup_attribute, get_model,
26+
get_saml_user_model, set_attribute)
2627

2728
from .models import TestUser
2829

@@ -190,9 +191,8 @@ def test_invalid_model_attribute_log(self):
190191
logs.output,
191192
)
192193

193-
@override_settings(AUTH_USER_MODEL='testprofiles.RequiredFieldUser')
194+
@override_settings(SAML_USER_MODEL='testprofiles.RequiredFieldUser')
194195
def test_create_user_with_required_fields(self):
195-
backend = Saml2Backend()
196196
attribute_mapping = {
197197
'mail': ['email'],
198198
'mail_verified': ['email_verified']
@@ -202,12 +202,16 @@ def test_create_user_with_required_fields(self):
202202
'mail_verified': [True],
203203
}
204204
# User creation does not fail if several fields are required.
205-
user = backend._get_or_create_saml2_user(
205+
user, created = self.backend.get_or_create_user(
206+
get_django_user_lookup_attribute(get_saml_user_model()),
206207
207-
attributes,
208-
attribute_mapping,
208+
True
209209
)
210+
210211
self.assertEquals(user.email, '[email protected]')
212+
self.assertIs(user.email_verified, None)
213+
214+
user = self.backend._update_user(user, attributes, attribute_mapping, created)
211215
self.assertIs(user.email_verified, True)
212216

213217
def test_django_user_main_attribute(self):

0 commit comments

Comments
 (0)