Просмотр исходного кода

Closes #23661: Fill missing SSO names in the default pipeline

social-auth-core 6.0 moved the conversion between full and first/last
names out of the backends and into the social_names pipeline step. Add
it to the default pipeline after social_details so users created or
updated by SSO keep getting their names filled in.
Jason Novinger 2 дней назад
Родитель
Сommit
55cd708a0e
2 измененных файлов с 49 добавлено и 0 удалено
  1. 1 0
      netbox/netbox/settings.py
  2. 48 0
      netbox/netbox/tests/test_authentication.py

+ 1 - 0
netbox/netbox/settings.py

@@ -752,6 +752,7 @@ NETBOX_COPILOT_URL = 'https://static.copilot.netboxlabs.ai/load.js'
 
 SOCIAL_AUTH_PIPELINE = (
     'social_core.pipeline.social_auth.social_details',
+    'social_core.pipeline.social_auth.social_names',
     'social_core.pipeline.social_auth.social_uid',
     'social_core.pipeline.social_auth.auth_allowed',
     'social_core.pipeline.social_auth.social_user',

+ 48 - 0
netbox/netbox/tests/test_authentication.py

@@ -1359,3 +1359,51 @@ class SocialAuthAllowlistTestCase(DjangoTestCase):
 
         self.assertIn('_auth_user_id', self.client.session)
         self.assertTrue(User.objects.filter(username='alice').exists())
+
+
+class FullNameTestBackend(BaseAuth):
+    """
+    Stands in for an identity provider which supplies a user's full name but not its components.
+    """
+    name = 'fullname-test'
+    ID_KEY = 'email'
+
+    def auth_complete(self, *args, **kwargs):
+        kwargs.update(response={'email': self.data['email'], 'name': self.data['name']}, backend=self)
+        return self.strategy.authenticate(*args, **kwargs)
+
+    def get_user_details(self, response):
+        return {
+            'username': response['email'].split('@')[0],
+            'email': response['email'],
+            'fullname': response['name'],
+        }
+
+
+@override_settings(
+    AUTHENTICATION_BACKENDS=[
+        'netbox.tests.test_authentication.FullNameTestBackend',
+        'netbox.authentication.ObjectPermissionBackend',
+    ],
+)
+class SocialAuthNamesTestCase(DjangoTestCase):
+    """
+    social-auth-core 6.0 moved name derivation out of the backends and into the social_names
+    pipeline step, which the default pipeline must therefore include (#23661).
+    """
+
+    def setUp(self):
+        # load_backends() caches the discovered backends in a module-level dict, so isolate the
+        # backend overridden above from the remainder of the test suite.
+        cache_patcher = patch.dict('social_core.backends.utils.BACKENDSCACHE', {}, clear=True)
+        cache_patcher.start()
+        self.addCleanup(cache_patcher.stop)
+
+        self.url = reverse('social:complete', args=['fullname-test'])
+
+    def test_first_and_last_name_derived_from_full_name(self):
+        self.client.get(self.url, {'email': 'alice@example.com', 'name': 'Alice Liddell'})
+
+        user = User.objects.get(username='alice')
+        self.assertEqual(user.first_name, 'Alice')
+        self.assertEqual(user.last_name, 'Liddell')