|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 |
|
| 17 | +import sys |
| 18 | + |
17 | 19 | from django.contrib.auth import get_user_model |
18 | 20 | from django.contrib.auth.models import User as DjangoUserModel |
19 | 21 | from django.test import TestCase, override_settings |
|
22 | 24 |
|
23 | 25 | User = get_user_model() |
24 | 26 |
|
| 27 | +if sys.version_info < (3, 4): |
| 28 | + # Monkey-patch TestCase to add the assertLogs method introduced in |
| 29 | + # Python 3.4 |
| 30 | + from unittest2.case import _AssertLogsContext |
| 31 | + |
| 32 | + class LoggerTestCase(TestCase): |
| 33 | + def assertLogs(self, logger=None, level=None): |
| 34 | + return _AssertLogsContext(self, logger, level) |
| 35 | + |
| 36 | + TestCase = LoggerTestCase |
| 37 | + |
25 | 38 |
|
26 | 39 | class Saml2BackendTests(TestCase): |
27 | 40 | def test_update_user(self): |
@@ -89,11 +102,37 @@ def test_update_user_empty_attribute(self): |
89 | 102 | 'cn': ('John', ), |
90 | 103 | 'sn': (), |
91 | 104 | } |
92 | | - backend.update_user(user, attributes, attribute_mapping) |
| 105 | + with self.assertLogs('djangosaml2', level='DEBUG') as logs: |
| 106 | + backend.update_user(user, attributes, attribute_mapping) |
93 | 107 | self.assertEqual(user.email, 'john@example.com') |
94 | 108 | self.assertEqual(user.first_name, 'John') |
95 | 109 | # empty attribute list: no update |
96 | 110 | self.assertEqual(user.last_name, 'Smith') |
| 111 | + self.assertIn( |
| 112 | + 'DEBUG:djangosaml2:Could not find value for "sn", not ' |
| 113 | + 'updating fields "(\'last_name\',)"', |
| 114 | + logs.output, |
| 115 | + ) |
| 116 | + |
| 117 | + def test_invalid_model_attribute_log(self): |
| 118 | + backend = Saml2Backend() |
| 119 | + |
| 120 | + attribute_mapping = { |
| 121 | + 'uid': ['username'], |
| 122 | + 'cn': ['nonexistent'], |
| 123 | + } |
| 124 | + attributes = { |
| 125 | + 'uid': ['john'], |
| 126 | + 'cn': ['John'], |
| 127 | + } |
| 128 | + |
| 129 | + with self.assertLogs('djangosaml2', level='DEBUG') as logs: |
| 130 | + backend.get_saml2_user(True, 'john', attributes, attribute_mapping) |
| 131 | + |
| 132 | + self.assertIn( |
| 133 | + 'DEBUG:djangosaml2:Could not find attribute "nonexistent" on user "john"', |
| 134 | + logs.output, |
| 135 | + ) |
97 | 136 |
|
98 | 137 | def test_django_user_main_attribute(self): |
99 | 138 | backend = Saml2Backend() |
|
0 commit comments