55import ddt
66from unittest import mock
77from unittest .mock import call
8- from django .test import TestCase , override_settings
8+ from django .test import TestCase
99from django .contrib .sites .models import Site
1010from common .djangoapps .third_party_auth .tests .factories import SAMLConfigurationFactory , SAMLProviderConfigFactory
1111from common .djangoapps .third_party_auth .models import SAMLProviderConfig
@@ -34,64 +34,42 @@ def setUp(self):
3434 )
3535
3636 @mock .patch ('common.djangoapps.third_party_auth.signals.handlers.set_custom_attribute' )
37- def test_saml_config_signal_handlers_disabled (self , mock_set_custom_attribute ):
37+ def test_saml_config_signal_handlers_with_error (self , mock_set_custom_attribute ):
3838 """
39- Test behavior when SAML config signal handlers are disabled .
39+ Test error handling when signal handlers encounter an exception .
4040
41- Verifies that basic attributes are set but no provider updates are attempted .
41+ Verifies that error information is properly captured when provider updates fail .
4242 """
43- with override_settings (ENABLE_SAML_CONFIG_SIGNAL_HANDLERS = False ):
43+ error_message = "Test error"
44+ # Simulate an exception in the provider config update logic
45+ with mock .patch (
46+ 'common.djangoapps.third_party_auth.models.SAMLProviderConfig.objects.current_set' ,
47+ side_effect = Exception (error_message )
48+ ):
4449 self .saml_config .entity_id = 'https://updated.example.com'
4550 self .saml_config .save ()
4651
47- expected_calls = [
48- call ('saml_config_signal.enabled' , False ),
49- call ('saml_config_signal.new_config_id' , self .saml_config .id ),
50- call ('saml_config_signal.slug' , 'test-config' ),
51- ]
52-
53- mock_set_custom_attribute .assert_has_calls (expected_calls , any_order = False )
54- assert mock_set_custom_attribute .call_count == 3
52+ expected_calls = [
53+ call ('saml_config_signal.new_config_id' , self .saml_config .id ),
54+ call ('saml_config_signal.slug' , 'test-config' ),
55+ ]
5556
56- @mock .patch ('common.djangoapps.third_party_auth.signals.handlers.set_custom_attribute' )
57- def test_saml_config_signal_handlers_with_error (self , mock_set_custom_attribute ):
58- """
59- Test error handling when signal handlers encounter an exception.
57+ mock_set_custom_attribute .assert_has_calls (expected_calls , any_order = False )
58+ assert mock_set_custom_attribute .call_count == 3
6059
61- Verifies that error information is properly captured when provider updates fail.
62- """
63- error_message = "Test error"
64- with override_settings (ENABLE_SAML_CONFIG_SIGNAL_HANDLERS = True ):
65- # Simulate an exception in the provider config update logic
66- with mock .patch (
67- 'common.djangoapps.third_party_auth.models.SAMLProviderConfig.objects.current_set' ,
68- side_effect = Exception (error_message )
69- ):
70- self .saml_config .entity_id = 'https://updated.example.com'
71- self .saml_config .save ()
72-
73- expected_calls = [
74- call ('saml_config_signal.enabled' , True ),
75- call ('saml_config_signal.new_config_id' , self .saml_config .id ),
76- call ('saml_config_signal.slug' , 'test-config' ),
77- ]
78-
79- mock_set_custom_attribute .assert_has_calls (expected_calls , any_order = False )
80- assert mock_set_custom_attribute .call_count == 4
81-
82- # Verify error message was logged
83- mock_set_custom_attribute .assert_any_call (
84- 'saml_config_signal.error_message' ,
85- mock .ANY
86- )
87- error_calls = [
88- call for call in mock_set_custom_attribute .mock_calls
89- if call [1 ][0 ] == 'saml_config_signal.error_message'
90- ]
91- assert error_message in error_calls [0 ][1 ][1 ], (
92- f"Expected '{ error_message } ' in error message, "
93- f"got: { error_calls [0 ][1 ][1 ]} "
94- )
60+ # Verify error message was logged
61+ mock_set_custom_attribute .assert_any_call (
62+ 'saml_config_signal.error_message' ,
63+ mock .ANY
64+ )
65+ error_calls = [
66+ call for call in mock_set_custom_attribute .mock_calls
67+ if call [1 ][0 ] == 'saml_config_signal.error_message'
68+ ]
69+ assert error_message in error_calls [0 ][1 ][1 ], (
70+ f"Expected '{ error_message } ' in error message, "
71+ f"got: { error_calls [0 ][1 ][1 ]} "
72+ )
9573
9674 def _get_current_provider (self , slug ):
9775 """
@@ -125,7 +103,6 @@ def _get_site(self, site_id):
125103 )
126104 @ddt .unpack
127105 @mock .patch ('common.djangoapps.third_party_auth.signals.handlers.set_custom_attribute' )
128- @override_settings (ENABLE_SAML_CONFIG_SIGNAL_HANDLERS = True )
129106 def test_saml_provider_config_updates (self , provider_site_id , provider_slug ,
130107 signal_saml_site_id , signal_saml_slug , is_provider_updated ,
131108 mock_set_custom_attribute ):
@@ -154,7 +131,6 @@ def test_saml_provider_config_updates(self, provider_site_id, provider_slug,
154131 current_provider = self ._get_current_provider (provider_slug )
155132
156133 expected_calls = [
157- call ('saml_config_signal.enabled' , True ),
158134 call ('saml_config_signal.new_config_id' , new_saml_config .id ),
159135 call ('saml_config_signal.slug' , signal_saml_slug ),
160136 ]
@@ -177,7 +153,6 @@ def test_saml_provider_config_updates(self, provider_site_id, provider_slug,
177153 (2 , 'slug' , 1 , 'default' ),
178154 )
179155 @ddt .unpack
180- @override_settings (ENABLE_SAML_CONFIG_SIGNAL_HANDLERS = True )
181156 def test_saml_provider_with_null_config_not_updated (self , provider_site_id , provider_slug ,
182157 signal_saml_site_id , signal_saml_slug ):
183158 """
0 commit comments