|
19 | 19 | from django.contrib.auth import get_user_model
|
20 | 20 | from django.contrib.auth.models import User as DjangoUserModel
|
21 | 21 | from django.test import TestCase, override_settings
|
| 22 | +from .models import TestUser |
22 | 23 | from djangosaml2.backends import (Saml2Backend,
|
23 | 24 | get_django_user_lookup_attribute,
|
24 | 25 | get_saml_user_model)
|
@@ -158,17 +159,77 @@ def test_django_user_main_attribute(self):
|
158 | 159 | with override_settings(SAML_DJANGO_USER_MAIN_ATTRIBUTE='foo'):
|
159 | 160 | self.assertEqual(get_django_user_lookup_attribute(get_saml_user_model()), 'foo')
|
160 | 161 |
|
161 |
| - def test_django_user_main_attribute_lookup(self): |
| 162 | + def test_get_or_create_user_existing(self): |
162 | 163 | backend = Saml2Backend()
|
163 | 164 |
|
164 |
| - # self.assertEqual(backend.get_django_user_main_attribute_lookup(), '') |
| 165 | + TestUser.objects.create(username='john') |
165 | 166 |
|
166 |
| - # TODO: test with acual user so he can be matched |
167 |
| - # with override_settings( |
168 |
| - # SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP='__iexact'): |
169 |
| - # self.assertEqual( |
170 |
| - # backend.get_django_user_main_attribute_lookup(), |
171 |
| - # '__iexact') |
| 167 | + with override_settings(SAML_USER_MODEL='testprofiles.TestUser'): |
| 168 | + john, created = backend.get_or_create_user( |
| 169 | + get_django_user_lookup_attribute(get_saml_user_model()), |
| 170 | + 'john', |
| 171 | + False, |
| 172 | + ) |
| 173 | + |
| 174 | + self.assertTrue(isinstance(john, TestUser)) |
| 175 | + self.assertFalse(created) |
| 176 | + |
| 177 | + def test_get_or_create_user_duplicates(self): |
| 178 | + backend = Saml2Backend() |
| 179 | + |
| 180 | + TestUser.objects.create(username='john', age=1) |
| 181 | + TestUser.objects.create(username='paul', age=1) |
| 182 | + |
| 183 | + with self.assertLogs('djangosaml2', level='DEBUG') as logs: |
| 184 | + with override_settings(SAML_USER_MODEL='testprofiles.TestUser'): |
| 185 | + john, created = backend.get_or_create_user( |
| 186 | + 'age', |
| 187 | + 1, |
| 188 | + False, |
| 189 | + ) |
| 190 | + |
| 191 | + self.assertTrue(john is None) |
| 192 | + self.assertFalse(created) |
| 193 | + self.assertIn( |
| 194 | + "ERROR:djangosaml2:Multiple users match, model: testprofiles.testuser, lookup: {'age': 1}", |
| 195 | + logs.output, |
| 196 | + ) |
| 197 | + |
| 198 | + def test_get_or_create_user_no_create(self): |
| 199 | + backend = Saml2Backend() |
| 200 | + |
| 201 | + with self.assertLogs('djangosaml2', level='DEBUG') as logs: |
| 202 | + with override_settings(SAML_USER_MODEL='testprofiles.TestUser'): |
| 203 | + john, created = backend.get_or_create_user( |
| 204 | + get_django_user_lookup_attribute(get_saml_user_model()), |
| 205 | + 'john', |
| 206 | + False, |
| 207 | + ) |
| 208 | + |
| 209 | + self.assertTrue(john is None) |
| 210 | + self.assertFalse(created) |
| 211 | + self.assertIn( |
| 212 | + "ERROR:djangosaml2:The user does not exist, model: testprofiles.testuser, lookup: {'username': 'john'}", |
| 213 | + logs.output, |
| 214 | + ) |
| 215 | + |
| 216 | + def test_get_or_create_user_create(self): |
| 217 | + backend = Saml2Backend() |
| 218 | + |
| 219 | + with self.assertLogs('djangosaml2', level='DEBUG') as logs: |
| 220 | + with override_settings(SAML_USER_MODEL='testprofiles.TestUser'): |
| 221 | + john, created = backend.get_or_create_user( |
| 222 | + get_django_user_lookup_attribute(get_saml_user_model()), |
| 223 | + 'john', |
| 224 | + True, |
| 225 | + ) |
| 226 | + |
| 227 | + self.assertTrue(isinstance(john, TestUser)) |
| 228 | + self.assertTrue(created) |
| 229 | + self.assertIn( |
| 230 | + f"DEBUG:djangosaml2:New user created: {john}", |
| 231 | + logs.output, |
| 232 | + ) |
172 | 233 |
|
173 | 234 |
|
174 | 235 | class LowerCaseSaml2Backend(Saml2Backend):
|
|
0 commit comments