|
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 |
23 |
| -from djangosaml2.backends import (Saml2Backend, |
24 |
| - get_django_user_lookup_attribute, |
| 22 | +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 | 25 | get_saml_user_model)
|
26 | 26 |
|
| 27 | +from .models import TestUser |
| 28 | + |
27 | 29 | User = get_user_model()
|
28 | 30 |
|
29 |
| -if sys.version_info < (3, 4): |
30 |
| - # Monkey-patch TestCase to add the assertLogs method introduced in |
31 |
| - # Python 3.4 |
32 |
| - from unittest2.case import _AssertLogsContext |
33 | 31 |
|
34 |
| - class LoggerTestCase(TestCase): |
35 |
| - def assertLogs(self, logger=None, level=None): |
36 |
| - return _AssertLogsContext(self, logger, level) |
| 32 | +class BackendUtilsTests(TestCase): |
| 33 | + def test_get_model_ok(self): |
| 34 | + user_model = get_model('testprofiles.TestUser') |
| 35 | + self.assertEqual(user_model, TestUser) |
| 36 | + |
| 37 | + def test_get_model_nonexisting(self): |
| 38 | + nonexisting_model = 'testprofiles.NonExisting' |
37 | 39 |
|
38 |
| - TestCase = LoggerTestCase |
| 40 | + with self.assertRaisesMessage(ImproperlyConfigured, f"SAML_USER_MODEL refers to model '{nonexisting_model}' that has not been installed"): |
| 41 | + get_model(nonexisting_model) |
| 42 | + |
| 43 | + def test_get_model_invalid_specifier(self): |
| 44 | + nonexisting_model = 'random_package.specifier.testprofiles.NonExisting' |
| 45 | + |
| 46 | + with self.assertRaisesMessage(ImproperlyConfigured, "SAML_USER_MODEL must be of the form 'app_label.model_name'"): |
| 47 | + get_model(nonexisting_model) |
| 48 | + |
| 49 | + def test_get_saml_user_model_specified(self): |
| 50 | + with override_settings(AUTH_USER_MODEL='auth.User'): |
| 51 | + with override_settings(SAML_USER_MODEL='testprofiles.TestUser'): |
| 52 | + self.assertEqual(get_saml_user_model(), TestUser) |
| 53 | + |
| 54 | + def test_get_saml_user_model_default(self): |
| 55 | + with override_settings(AUTH_USER_MODEL='auth.User'): |
| 56 | + self.assertEqual(get_saml_user_model(), DjangoUserModel) |
| 57 | + |
| 58 | + def test_get_django_user_lookup_attribute_specified(self): |
| 59 | + with override_settings(SAML_USER_MODEL='testprofiles.TestUser'): |
| 60 | + with override_settings(SAML_DJANGO_USER_MAIN_ATTRIBUTE='age'): |
| 61 | + self.assertEqual(get_django_user_lookup_attribute(TestUser), 'age') |
| 62 | + |
| 63 | + def test_get_django_user_lookup_attribute_default(self): |
| 64 | + with override_settings(SAML_USER_MODEL='testprofiles.TestUser'): |
| 65 | + self.assertEqual(get_django_user_lookup_attribute(TestUser), 'username') |
| 66 | + |
| 67 | + def test_set_attribute(self): |
| 68 | + u = TestUser() |
| 69 | + self.assertFalse(hasattr(u, 'custom_attribute')) |
| 70 | + |
| 71 | + # Set attribute initially |
| 72 | + changed = set_attribute(u, 'custom_attribute', 'value') |
| 73 | + self.assertTrue(changed) |
| 74 | + self.assertEqual(u.custom_attribute, 'value') |
| 75 | + |
| 76 | + # 'Update' to the same value again |
| 77 | + changed_same = set_attribute(u, 'custom_attribute', 'value') |
| 78 | + self.assertFalse(changed_same) |
| 79 | + self.assertEqual(u.custom_attribute, 'value') |
| 80 | + |
| 81 | + # Update to a different value |
| 82 | + changed_different = set_attribute(u, 'custom_attribute', 'new_value') |
| 83 | + self.assertTrue(changed_different) |
| 84 | + self.assertEqual(u.custom_attribute, 'new_value') |
39 | 85 |
|
40 | 86 |
|
41 | 87 | class Saml2BackendTests(TestCase):
|
|
0 commit comments