authentik.providers.scim.tests.test_client
SCIM Client tests
1"""SCIM Client tests""" 2 3from unittest.mock import MagicMock, patch 4 5from django.core.cache import cache 6from django.test import TestCase 7from requests_mock import Mocker 8 9from authentik.blueprints.tests import apply_blueprint 10from authentik.core.models import Application, Group, User 11from authentik.lib.generators import generate_id 12from authentik.providers.scim.clients.base import SCIMClient 13from authentik.providers.scim.models import SCIMMapping, SCIMProvider 14from authentik.providers.scim.tasks import scim_sync 15 16 17@patch("authentik.providers.scim.clients.base.SCIMClient.can_discover", False) 18class SCIMClientTests(TestCase): 19 """SCIM Client tests""" 20 21 @apply_blueprint("system/providers-scim.yaml") 22 def setUp(self) -> None: 23 # Clear cache before each test 24 cache.clear() 25 self.provider: SCIMProvider = SCIMProvider.objects.create( 26 name=generate_id(), 27 url="https://localhost", 28 token=generate_id(), 29 ) 30 self.app: Application = Application.objects.create( 31 name=generate_id(), 32 slug=generate_id(), 33 ) 34 self.app.backchannel_providers.add(self.provider) 35 self.provider.property_mappings.add( 36 SCIMMapping.objects.get(managed="goauthentik.io/providers/scim/user") 37 ) 38 self.provider.property_mappings_group.add( 39 SCIMMapping.objects.get(managed="goauthentik.io/providers/scim/group") 40 ) 41 42 def test_config(self): 43 """Test valid config: 44 https://docs.aws.amazon.com/singlesignon/latest/developerguide/serviceproviderconfig.html""" 45 with Mocker() as mock: 46 mock: Mocker 47 mock.get( 48 "https://localhost/ServiceProviderConfig", 49 json={ 50 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 51 "documentationUri": ( 52 "https://docs.aws.amazon.com/singlesignon/latest/" 53 "userguide/manage-your-identity-source-idp.html" 54 ), 55 "authenticationSchemes": [ 56 { 57 "type": "oauthbearertoken", 58 "name": "OAuth Bearer Token", 59 "description": ( 60 "Authentication scheme using the OAuth Bearer Token Standard" 61 ), 62 "specUri": "https://www.rfc-editor.org/info/rfc6750", 63 "documentationUri": ( 64 "https://docs.aws.amazon.com/singlesignon/latest/" 65 "userguide/provision-automatically.html" 66 ), 67 "primary": True, 68 } 69 ], 70 "patch": {"supported": True}, 71 "bulk": {"supported": False}, 72 "filter": {"supported": True, "maxResults": 50}, 73 "changePassword": {"supported": False}, 74 "sort": {"supported": False}, 75 "etag": {"supported": False}, 76 }, 77 ) 78 client = SCIMClient(self.provider) 79 self.assertEqual(mock.call_count, 1) 80 self.assertEqual(mock.request_history[0].method, "GET") 81 self.assertFalse(client._config.is_fallback) 82 83 def test_config_bulk_unsupported_without_max_operations(self): 84 """A provider that reports bulk as unsupported may omit the bulk limits. 85 86 RFC 7644 Section 5 only defines maxOperations alongside bulk support, so 87 such a response is conforming and must not push the client onto its 88 fallback config -- doing so reports patch and filter as unsupported 89 regardless of what the provider advertised, which silently disables 90 PATCH updates and the filtered lookup that adopts existing objects.""" 91 with Mocker() as mock: 92 mock: Mocker 93 mock.get( 94 "https://localhost/ServiceProviderConfig", 95 json={ 96 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 97 "authenticationSchemes": [], 98 "patch": {"supported": True}, 99 "bulk": {"supported": False}, 100 "filter": {"supported": True, "maxResults": 50}, 101 "changePassword": {"supported": False}, 102 "sort": {"supported": False}, 103 "etag": {"supported": False}, 104 }, 105 ) 106 client = SCIMClient(self.provider) 107 self.assertFalse(client._config.is_fallback) 108 self.assertTrue(client._config.patch.supported) 109 self.assertTrue(client._config.filter.supported) 110 self.assertEqual(client._config.bulk.maxOperations, 0) 111 112 def test_config_invalid(self): 113 """Test invalid config""" 114 with Mocker() as mock: 115 mock: Mocker 116 mock.get( 117 "https://localhost/ServiceProviderConfig", 118 json={}, 119 ) 120 client = SCIMClient(self.provider) 121 self.assertEqual(mock.call_count, 1) 122 self.assertEqual(mock.request_history[0].method, "GET") 123 self.assertTrue(client._config.is_fallback) 124 125 def test_config_non_json_response(self): 126 """Test non-JSON config response falls back to defaults""" 127 with Mocker() as mock: 128 mock.get( 129 "https://localhost/ServiceProviderConfig", 130 text="<html></html>", 131 headers={"Content-Type": "text/html"}, 132 ) 133 client = SCIMClient(self.provider) 134 self.assertEqual(mock.call_count, 1) 135 self.assertEqual(mock.request_history[0].method, "GET") 136 self.assertTrue(client._config.is_fallback) 137 138 def test_scim_sync(self): 139 """test scim_sync task""" 140 scim_sync.send(self.provider.pk).get_result() 141 142 def test_full_sync_discovers_once_per_object_type(self): 143 """Full sync runs discovery once, not once per object page.""" 144 SCIMProvider.objects.filter(pk=self.provider.pk).update(sync_page_size=1) 145 User.objects.bulk_create([User(username=generate_id()), User(username=generate_id())]) 146 Group.objects.bulk_create([Group(name=generate_id()), Group(name=generate_id())]) 147 user_client = MagicMock(can_discover=True) 148 group_client = MagicMock(can_discover=True) 149 150 def client_for_model(_provider, model): 151 if model is User: 152 return user_client 153 self.assertIs(model, Group) 154 return group_client 155 156 with patch.object(SCIMProvider, "client_for_model", autospec=True) as client_factory: 157 client_factory.side_effect = client_for_model 158 scim_sync.send(self.provider.pk).get_result() 159 160 self.assertGreater(user_client.write_locked.call_count, 1) 161 self.assertGreater(group_client.write_locked.call_count, 1) 162 user_client.discover.assert_called_once_with() 163 group_client.discover.assert_called_once_with() 164 165 def test_config_caching(self): 166 """Test that ServiceProviderConfig is cached after first successful fetch""" 167 config_json = { 168 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 169 "documentationUri": "https://example.com/docs", 170 "authenticationSchemes": [ 171 { 172 "type": "oauthbearertoken", 173 "name": "OAuth Bearer Token", 174 "description": "OAuth Bearer Token", 175 "specUri": "https://www.rfc-editor.org/info/rfc6750", 176 "documentationUri": "https://example.com/auth", 177 "primary": True, 178 } 179 ], 180 "patch": {"supported": True}, 181 "bulk": {"supported": False, "maxOperations": 1, "maxPayloadSize": 1048576}, 182 "filter": {"supported": True, "maxResults": 50}, 183 "changePassword": {"supported": False}, 184 "sort": {"supported": False}, 185 "etag": {"supported": False}, 186 } 187 188 with Mocker() as mock: 189 mock.get("https://localhost/ServiceProviderConfig", json=config_json) 190 191 client = SCIMClient(self.provider) 192 193 # First call should hit the API 194 config1 = client.get_service_provider_config() 195 self.assertEqual(mock.call_count, 1) 196 197 # Second call should use cache, no additional API call 198 config2 = client.get_service_provider_config() 199 self.assertEqual(mock.call_count, 1) # Still 1, not 2 200 201 # Verify both configs are the same 202 self.assertEqual(config1, config2) 203 204 def test_config_caching_invalid(self): 205 """Test that default config is cached when remote config is invalid""" 206 with Mocker() as mock: 207 mock.get("https://localhost/ServiceProviderConfig", json={}) 208 209 client = SCIMClient(self.provider) 210 211 # First call should hit the API and get invalid response 212 config1 = client.get_service_provider_config() 213 self.assertEqual(mock.call_count, 1) 214 215 # Second call should use cached default config, no additional API call 216 config2 = client.get_service_provider_config() 217 self.assertEqual(mock.call_count, 1) # Still 1, not 2 218 219 # Verify both configs are the same default 220 self.assertEqual(config1, config2) 221 222 def test_config_caching_error(self): 223 """Test that default config is cached when remote request fails""" 224 with Mocker() as mock: 225 mock.get("https://localhost/ServiceProviderConfig", status_code=500) 226 227 client = SCIMClient(self.provider) 228 229 # First call should hit the API and fail 230 config1 = client.get_service_provider_config() 231 self.assertEqual(mock.call_count, 1) 232 233 # Second call should use cached default config, no additional API call 234 config2 = client.get_service_provider_config() 235 self.assertEqual(mock.call_count, 1) # Still 1, not 2 236 237 # Verify both configs are the same default 238 self.assertEqual(config1, config2) 239 240 def test_config_cache_invalidation_on_save(self): 241 """Test that ServiceProviderConfig cache is cleared when provider is saved""" 242 config_json = { 243 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 244 "documentationUri": "https://example.com/docs", 245 "authenticationSchemes": [ 246 { 247 "type": "oauthbearertoken", 248 "name": "OAuth Bearer Token", 249 "description": "OAuth Bearer Token", 250 "specUri": "https://www.rfc-editor.org/info/rfc6750", 251 "documentationUri": "https://example.com/auth", 252 "primary": True, 253 } 254 ], 255 "patch": {"supported": True}, 256 "bulk": {"supported": False, "maxOperations": 1, "maxPayloadSize": 1048576}, 257 "filter": {"supported": True, "maxResults": 50}, 258 "changePassword": {"supported": False}, 259 "sort": {"supported": False}, 260 "etag": {"supported": False}, 261 } 262 263 with Mocker() as mock: 264 mock.get("https://localhost/ServiceProviderConfig", json=config_json) 265 266 # First client instantiation caches the config 267 SCIMClient(self.provider) 268 self.assertEqual(mock.call_count, 1) 269 270 # Creating another client should use cached config 271 SCIMClient(self.provider) 272 self.assertEqual(mock.call_count, 1) # Still 1, using cache 273 274 # Save the provider (e.g., changing compatibility mode) 275 self.provider.compatibility_mode = "aws" 276 self.provider.save() 277 278 # Creating a new client should now hit the API again since cache was cleared 279 SCIMClient(self.provider) 280 self.assertEqual(mock.call_count, 2) # New API call after cache invalidation
18@patch("authentik.providers.scim.clients.base.SCIMClient.can_discover", False) 19class SCIMClientTests(TestCase): 20 """SCIM Client tests""" 21 22 @apply_blueprint("system/providers-scim.yaml") 23 def setUp(self) -> None: 24 # Clear cache before each test 25 cache.clear() 26 self.provider: SCIMProvider = SCIMProvider.objects.create( 27 name=generate_id(), 28 url="https://localhost", 29 token=generate_id(), 30 ) 31 self.app: Application = Application.objects.create( 32 name=generate_id(), 33 slug=generate_id(), 34 ) 35 self.app.backchannel_providers.add(self.provider) 36 self.provider.property_mappings.add( 37 SCIMMapping.objects.get(managed="goauthentik.io/providers/scim/user") 38 ) 39 self.provider.property_mappings_group.add( 40 SCIMMapping.objects.get(managed="goauthentik.io/providers/scim/group") 41 ) 42 43 def test_config(self): 44 """Test valid config: 45 https://docs.aws.amazon.com/singlesignon/latest/developerguide/serviceproviderconfig.html""" 46 with Mocker() as mock: 47 mock: Mocker 48 mock.get( 49 "https://localhost/ServiceProviderConfig", 50 json={ 51 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 52 "documentationUri": ( 53 "https://docs.aws.amazon.com/singlesignon/latest/" 54 "userguide/manage-your-identity-source-idp.html" 55 ), 56 "authenticationSchemes": [ 57 { 58 "type": "oauthbearertoken", 59 "name": "OAuth Bearer Token", 60 "description": ( 61 "Authentication scheme using the OAuth Bearer Token Standard" 62 ), 63 "specUri": "https://www.rfc-editor.org/info/rfc6750", 64 "documentationUri": ( 65 "https://docs.aws.amazon.com/singlesignon/latest/" 66 "userguide/provision-automatically.html" 67 ), 68 "primary": True, 69 } 70 ], 71 "patch": {"supported": True}, 72 "bulk": {"supported": False}, 73 "filter": {"supported": True, "maxResults": 50}, 74 "changePassword": {"supported": False}, 75 "sort": {"supported": False}, 76 "etag": {"supported": False}, 77 }, 78 ) 79 client = SCIMClient(self.provider) 80 self.assertEqual(mock.call_count, 1) 81 self.assertEqual(mock.request_history[0].method, "GET") 82 self.assertFalse(client._config.is_fallback) 83 84 def test_config_bulk_unsupported_without_max_operations(self): 85 """A provider that reports bulk as unsupported may omit the bulk limits. 86 87 RFC 7644 Section 5 only defines maxOperations alongside bulk support, so 88 such a response is conforming and must not push the client onto its 89 fallback config -- doing so reports patch and filter as unsupported 90 regardless of what the provider advertised, which silently disables 91 PATCH updates and the filtered lookup that adopts existing objects.""" 92 with Mocker() as mock: 93 mock: Mocker 94 mock.get( 95 "https://localhost/ServiceProviderConfig", 96 json={ 97 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 98 "authenticationSchemes": [], 99 "patch": {"supported": True}, 100 "bulk": {"supported": False}, 101 "filter": {"supported": True, "maxResults": 50}, 102 "changePassword": {"supported": False}, 103 "sort": {"supported": False}, 104 "etag": {"supported": False}, 105 }, 106 ) 107 client = SCIMClient(self.provider) 108 self.assertFalse(client._config.is_fallback) 109 self.assertTrue(client._config.patch.supported) 110 self.assertTrue(client._config.filter.supported) 111 self.assertEqual(client._config.bulk.maxOperations, 0) 112 113 def test_config_invalid(self): 114 """Test invalid config""" 115 with Mocker() as mock: 116 mock: Mocker 117 mock.get( 118 "https://localhost/ServiceProviderConfig", 119 json={}, 120 ) 121 client = SCIMClient(self.provider) 122 self.assertEqual(mock.call_count, 1) 123 self.assertEqual(mock.request_history[0].method, "GET") 124 self.assertTrue(client._config.is_fallback) 125 126 def test_config_non_json_response(self): 127 """Test non-JSON config response falls back to defaults""" 128 with Mocker() as mock: 129 mock.get( 130 "https://localhost/ServiceProviderConfig", 131 text="<html></html>", 132 headers={"Content-Type": "text/html"}, 133 ) 134 client = SCIMClient(self.provider) 135 self.assertEqual(mock.call_count, 1) 136 self.assertEqual(mock.request_history[0].method, "GET") 137 self.assertTrue(client._config.is_fallback) 138 139 def test_scim_sync(self): 140 """test scim_sync task""" 141 scim_sync.send(self.provider.pk).get_result() 142 143 def test_full_sync_discovers_once_per_object_type(self): 144 """Full sync runs discovery once, not once per object page.""" 145 SCIMProvider.objects.filter(pk=self.provider.pk).update(sync_page_size=1) 146 User.objects.bulk_create([User(username=generate_id()), User(username=generate_id())]) 147 Group.objects.bulk_create([Group(name=generate_id()), Group(name=generate_id())]) 148 user_client = MagicMock(can_discover=True) 149 group_client = MagicMock(can_discover=True) 150 151 def client_for_model(_provider, model): 152 if model is User: 153 return user_client 154 self.assertIs(model, Group) 155 return group_client 156 157 with patch.object(SCIMProvider, "client_for_model", autospec=True) as client_factory: 158 client_factory.side_effect = client_for_model 159 scim_sync.send(self.provider.pk).get_result() 160 161 self.assertGreater(user_client.write_locked.call_count, 1) 162 self.assertGreater(group_client.write_locked.call_count, 1) 163 user_client.discover.assert_called_once_with() 164 group_client.discover.assert_called_once_with() 165 166 def test_config_caching(self): 167 """Test that ServiceProviderConfig is cached after first successful fetch""" 168 config_json = { 169 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 170 "documentationUri": "https://example.com/docs", 171 "authenticationSchemes": [ 172 { 173 "type": "oauthbearertoken", 174 "name": "OAuth Bearer Token", 175 "description": "OAuth Bearer Token", 176 "specUri": "https://www.rfc-editor.org/info/rfc6750", 177 "documentationUri": "https://example.com/auth", 178 "primary": True, 179 } 180 ], 181 "patch": {"supported": True}, 182 "bulk": {"supported": False, "maxOperations": 1, "maxPayloadSize": 1048576}, 183 "filter": {"supported": True, "maxResults": 50}, 184 "changePassword": {"supported": False}, 185 "sort": {"supported": False}, 186 "etag": {"supported": False}, 187 } 188 189 with Mocker() as mock: 190 mock.get("https://localhost/ServiceProviderConfig", json=config_json) 191 192 client = SCIMClient(self.provider) 193 194 # First call should hit the API 195 config1 = client.get_service_provider_config() 196 self.assertEqual(mock.call_count, 1) 197 198 # Second call should use cache, no additional API call 199 config2 = client.get_service_provider_config() 200 self.assertEqual(mock.call_count, 1) # Still 1, not 2 201 202 # Verify both configs are the same 203 self.assertEqual(config1, config2) 204 205 def test_config_caching_invalid(self): 206 """Test that default config is cached when remote config is invalid""" 207 with Mocker() as mock: 208 mock.get("https://localhost/ServiceProviderConfig", json={}) 209 210 client = SCIMClient(self.provider) 211 212 # First call should hit the API and get invalid response 213 config1 = client.get_service_provider_config() 214 self.assertEqual(mock.call_count, 1) 215 216 # Second call should use cached default config, no additional API call 217 config2 = client.get_service_provider_config() 218 self.assertEqual(mock.call_count, 1) # Still 1, not 2 219 220 # Verify both configs are the same default 221 self.assertEqual(config1, config2) 222 223 def test_config_caching_error(self): 224 """Test that default config is cached when remote request fails""" 225 with Mocker() as mock: 226 mock.get("https://localhost/ServiceProviderConfig", status_code=500) 227 228 client = SCIMClient(self.provider) 229 230 # First call should hit the API and fail 231 config1 = client.get_service_provider_config() 232 self.assertEqual(mock.call_count, 1) 233 234 # Second call should use cached default config, no additional API call 235 config2 = client.get_service_provider_config() 236 self.assertEqual(mock.call_count, 1) # Still 1, not 2 237 238 # Verify both configs are the same default 239 self.assertEqual(config1, config2) 240 241 def test_config_cache_invalidation_on_save(self): 242 """Test that ServiceProviderConfig cache is cleared when provider is saved""" 243 config_json = { 244 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 245 "documentationUri": "https://example.com/docs", 246 "authenticationSchemes": [ 247 { 248 "type": "oauthbearertoken", 249 "name": "OAuth Bearer Token", 250 "description": "OAuth Bearer Token", 251 "specUri": "https://www.rfc-editor.org/info/rfc6750", 252 "documentationUri": "https://example.com/auth", 253 "primary": True, 254 } 255 ], 256 "patch": {"supported": True}, 257 "bulk": {"supported": False, "maxOperations": 1, "maxPayloadSize": 1048576}, 258 "filter": {"supported": True, "maxResults": 50}, 259 "changePassword": {"supported": False}, 260 "sort": {"supported": False}, 261 "etag": {"supported": False}, 262 } 263 264 with Mocker() as mock: 265 mock.get("https://localhost/ServiceProviderConfig", json=config_json) 266 267 # First client instantiation caches the config 268 SCIMClient(self.provider) 269 self.assertEqual(mock.call_count, 1) 270 271 # Creating another client should use cached config 272 SCIMClient(self.provider) 273 self.assertEqual(mock.call_count, 1) # Still 1, using cache 274 275 # Save the provider (e.g., changing compatibility mode) 276 self.provider.compatibility_mode = "aws" 277 self.provider.save() 278 279 # Creating a new client should now hit the API again since cache was cleared 280 SCIMClient(self.provider) 281 self.assertEqual(mock.call_count, 2) # New API call after cache invalidation
SCIM Client tests
22 @apply_blueprint("system/providers-scim.yaml") 23 def setUp(self) -> None: 24 # Clear cache before each test 25 cache.clear() 26 self.provider: SCIMProvider = SCIMProvider.objects.create( 27 name=generate_id(), 28 url="https://localhost", 29 token=generate_id(), 30 ) 31 self.app: Application = Application.objects.create( 32 name=generate_id(), 33 slug=generate_id(), 34 ) 35 self.app.backchannel_providers.add(self.provider) 36 self.provider.property_mappings.add( 37 SCIMMapping.objects.get(managed="goauthentik.io/providers/scim/user") 38 ) 39 self.provider.property_mappings_group.add( 40 SCIMMapping.objects.get(managed="goauthentik.io/providers/scim/group") 41 )
Hook method for setting up the test fixture before exercising it.
43 def test_config(self): 44 """Test valid config: 45 https://docs.aws.amazon.com/singlesignon/latest/developerguide/serviceproviderconfig.html""" 46 with Mocker() as mock: 47 mock: Mocker 48 mock.get( 49 "https://localhost/ServiceProviderConfig", 50 json={ 51 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 52 "documentationUri": ( 53 "https://docs.aws.amazon.com/singlesignon/latest/" 54 "userguide/manage-your-identity-source-idp.html" 55 ), 56 "authenticationSchemes": [ 57 { 58 "type": "oauthbearertoken", 59 "name": "OAuth Bearer Token", 60 "description": ( 61 "Authentication scheme using the OAuth Bearer Token Standard" 62 ), 63 "specUri": "https://www.rfc-editor.org/info/rfc6750", 64 "documentationUri": ( 65 "https://docs.aws.amazon.com/singlesignon/latest/" 66 "userguide/provision-automatically.html" 67 ), 68 "primary": True, 69 } 70 ], 71 "patch": {"supported": True}, 72 "bulk": {"supported": False}, 73 "filter": {"supported": True, "maxResults": 50}, 74 "changePassword": {"supported": False}, 75 "sort": {"supported": False}, 76 "etag": {"supported": False}, 77 }, 78 ) 79 client = SCIMClient(self.provider) 80 self.assertEqual(mock.call_count, 1) 81 self.assertEqual(mock.request_history[0].method, "GET") 82 self.assertFalse(client._config.is_fallback)
84 def test_config_bulk_unsupported_without_max_operations(self): 85 """A provider that reports bulk as unsupported may omit the bulk limits. 86 87 RFC 7644 Section 5 only defines maxOperations alongside bulk support, so 88 such a response is conforming and must not push the client onto its 89 fallback config -- doing so reports patch and filter as unsupported 90 regardless of what the provider advertised, which silently disables 91 PATCH updates and the filtered lookup that adopts existing objects.""" 92 with Mocker() as mock: 93 mock: Mocker 94 mock.get( 95 "https://localhost/ServiceProviderConfig", 96 json={ 97 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 98 "authenticationSchemes": [], 99 "patch": {"supported": True}, 100 "bulk": {"supported": False}, 101 "filter": {"supported": True, "maxResults": 50}, 102 "changePassword": {"supported": False}, 103 "sort": {"supported": False}, 104 "etag": {"supported": False}, 105 }, 106 ) 107 client = SCIMClient(self.provider) 108 self.assertFalse(client._config.is_fallback) 109 self.assertTrue(client._config.patch.supported) 110 self.assertTrue(client._config.filter.supported) 111 self.assertEqual(client._config.bulk.maxOperations, 0)
A provider that reports bulk as unsupported may omit the bulk limits.
RFC 7644 Section 5 only defines maxOperations alongside bulk support, so such a response is conforming and must not push the client onto its fallback config -- doing so reports patch and filter as unsupported regardless of what the provider advertised, which silently disables PATCH updates and the filtered lookup that adopts existing objects.
113 def test_config_invalid(self): 114 """Test invalid config""" 115 with Mocker() as mock: 116 mock: Mocker 117 mock.get( 118 "https://localhost/ServiceProviderConfig", 119 json={}, 120 ) 121 client = SCIMClient(self.provider) 122 self.assertEqual(mock.call_count, 1) 123 self.assertEqual(mock.request_history[0].method, "GET") 124 self.assertTrue(client._config.is_fallback)
Test invalid config
126 def test_config_non_json_response(self): 127 """Test non-JSON config response falls back to defaults""" 128 with Mocker() as mock: 129 mock.get( 130 "https://localhost/ServiceProviderConfig", 131 text="<html></html>", 132 headers={"Content-Type": "text/html"}, 133 ) 134 client = SCIMClient(self.provider) 135 self.assertEqual(mock.call_count, 1) 136 self.assertEqual(mock.request_history[0].method, "GET") 137 self.assertTrue(client._config.is_fallback)
Test non-JSON config response falls back to defaults
139 def test_scim_sync(self): 140 """test scim_sync task""" 141 scim_sync.send(self.provider.pk).get_result()
test scim_sync task
143 def test_full_sync_discovers_once_per_object_type(self): 144 """Full sync runs discovery once, not once per object page.""" 145 SCIMProvider.objects.filter(pk=self.provider.pk).update(sync_page_size=1) 146 User.objects.bulk_create([User(username=generate_id()), User(username=generate_id())]) 147 Group.objects.bulk_create([Group(name=generate_id()), Group(name=generate_id())]) 148 user_client = MagicMock(can_discover=True) 149 group_client = MagicMock(can_discover=True) 150 151 def client_for_model(_provider, model): 152 if model is User: 153 return user_client 154 self.assertIs(model, Group) 155 return group_client 156 157 with patch.object(SCIMProvider, "client_for_model", autospec=True) as client_factory: 158 client_factory.side_effect = client_for_model 159 scim_sync.send(self.provider.pk).get_result() 160 161 self.assertGreater(user_client.write_locked.call_count, 1) 162 self.assertGreater(group_client.write_locked.call_count, 1) 163 user_client.discover.assert_called_once_with() 164 group_client.discover.assert_called_once_with()
Full sync runs discovery once, not once per object page.
166 def test_config_caching(self): 167 """Test that ServiceProviderConfig is cached after first successful fetch""" 168 config_json = { 169 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 170 "documentationUri": "https://example.com/docs", 171 "authenticationSchemes": [ 172 { 173 "type": "oauthbearertoken", 174 "name": "OAuth Bearer Token", 175 "description": "OAuth Bearer Token", 176 "specUri": "https://www.rfc-editor.org/info/rfc6750", 177 "documentationUri": "https://example.com/auth", 178 "primary": True, 179 } 180 ], 181 "patch": {"supported": True}, 182 "bulk": {"supported": False, "maxOperations": 1, "maxPayloadSize": 1048576}, 183 "filter": {"supported": True, "maxResults": 50}, 184 "changePassword": {"supported": False}, 185 "sort": {"supported": False}, 186 "etag": {"supported": False}, 187 } 188 189 with Mocker() as mock: 190 mock.get("https://localhost/ServiceProviderConfig", json=config_json) 191 192 client = SCIMClient(self.provider) 193 194 # First call should hit the API 195 config1 = client.get_service_provider_config() 196 self.assertEqual(mock.call_count, 1) 197 198 # Second call should use cache, no additional API call 199 config2 = client.get_service_provider_config() 200 self.assertEqual(mock.call_count, 1) # Still 1, not 2 201 202 # Verify both configs are the same 203 self.assertEqual(config1, config2)
Test that ServiceProviderConfig is cached after first successful fetch
205 def test_config_caching_invalid(self): 206 """Test that default config is cached when remote config is invalid""" 207 with Mocker() as mock: 208 mock.get("https://localhost/ServiceProviderConfig", json={}) 209 210 client = SCIMClient(self.provider) 211 212 # First call should hit the API and get invalid response 213 config1 = client.get_service_provider_config() 214 self.assertEqual(mock.call_count, 1) 215 216 # Second call should use cached default config, no additional API call 217 config2 = client.get_service_provider_config() 218 self.assertEqual(mock.call_count, 1) # Still 1, not 2 219 220 # Verify both configs are the same default 221 self.assertEqual(config1, config2)
Test that default config is cached when remote config is invalid
223 def test_config_caching_error(self): 224 """Test that default config is cached when remote request fails""" 225 with Mocker() as mock: 226 mock.get("https://localhost/ServiceProviderConfig", status_code=500) 227 228 client = SCIMClient(self.provider) 229 230 # First call should hit the API and fail 231 config1 = client.get_service_provider_config() 232 self.assertEqual(mock.call_count, 1) 233 234 # Second call should use cached default config, no additional API call 235 config2 = client.get_service_provider_config() 236 self.assertEqual(mock.call_count, 1) # Still 1, not 2 237 238 # Verify both configs are the same default 239 self.assertEqual(config1, config2)
Test that default config is cached when remote request fails
241 def test_config_cache_invalidation_on_save(self): 242 """Test that ServiceProviderConfig cache is cleared when provider is saved""" 243 config_json = { 244 "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"], 245 "documentationUri": "https://example.com/docs", 246 "authenticationSchemes": [ 247 { 248 "type": "oauthbearertoken", 249 "name": "OAuth Bearer Token", 250 "description": "OAuth Bearer Token", 251 "specUri": "https://www.rfc-editor.org/info/rfc6750", 252 "documentationUri": "https://example.com/auth", 253 "primary": True, 254 } 255 ], 256 "patch": {"supported": True}, 257 "bulk": {"supported": False, "maxOperations": 1, "maxPayloadSize": 1048576}, 258 "filter": {"supported": True, "maxResults": 50}, 259 "changePassword": {"supported": False}, 260 "sort": {"supported": False}, 261 "etag": {"supported": False}, 262 } 263 264 with Mocker() as mock: 265 mock.get("https://localhost/ServiceProviderConfig", json=config_json) 266 267 # First client instantiation caches the config 268 SCIMClient(self.provider) 269 self.assertEqual(mock.call_count, 1) 270 271 # Creating another client should use cached config 272 SCIMClient(self.provider) 273 self.assertEqual(mock.call_count, 1) # Still 1, using cache 274 275 # Save the provider (e.g., changing compatibility mode) 276 self.provider.compatibility_mode = "aws" 277 self.provider.save() 278 279 # Creating a new client should now hit the API again since cache was cleared 280 SCIMClient(self.provider) 281 self.assertEqual(mock.call_count, 2) # New API call after cache invalidation
Test that ServiceProviderConfig cache is cleared when provider is saved