|
2 | 2 | Unit tests for the conditional authentication decorator. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import pytest |
5 | 6 | from unittest.mock import Mock |
6 | 7 | from policyengine_household_api.decorators.auth import ( |
7 | 8 | NoOpDecorator, |
8 | 9 | ConditionalAuthDecorator, |
| 10 | + AuthConfigurationError, |
9 | 11 | create_auth_decorator, |
| 12 | + StaticBearerTokenValidator, |
10 | 13 | ) |
11 | 14 | from tests.fixtures.decorators.auth import ( |
12 | 15 | AUTH0_CONFIG_DATA, |
13 | 16 | auth_enabled_environment, |
| 17 | + auth_test_environment, |
14 | 18 | auth_disabled_environment, |
15 | 19 | auth_enabled_missing_config_environment, |
16 | 20 | auth_backward_compat_environment, |
@@ -66,6 +70,29 @@ def test__given_multiple_functions__each_passes_through_unchanged(self): |
66 | 70 | class TestConditionalAuthDecoratorWithAuthEnabled: |
67 | 71 | """Test ConditionalAuthDecorator with authentication enabled.""" |
68 | 72 |
|
| 73 | + def test__given_test_auth_environment__uses_static_token_validator( |
| 74 | + self, |
| 75 | + auth_test_environment, |
| 76 | + mock_resource_protector, |
| 77 | + mock_auth0_validator, |
| 78 | + ): |
| 79 | + _, mock_protector_instance = mock_resource_protector |
| 80 | + mock_validator_class, _ = mock_auth0_validator |
| 81 | + |
| 82 | + decorator = ConditionalAuthDecorator() |
| 83 | + |
| 84 | + mock_validator_class.assert_not_called() |
| 85 | + registered_validator = ( |
| 86 | + mock_protector_instance.register_token_validator.call_args[0][0] |
| 87 | + ) |
| 88 | + assert isinstance(registered_validator, StaticBearerTokenValidator) |
| 89 | + assert registered_validator.expected_token == "test-jwt-token" |
| 90 | + assert decorator.get_decorator() is mock_protector_instance |
| 91 | + assert decorator.is_enabled is True |
| 92 | + |
| 93 | + auth_test_environment.assert_any_call("app.environment", "") |
| 94 | + auth_test_environment.assert_any_call("auth.auth0.test_token", "") |
| 95 | + |
69 | 96 | def test__given_auth_enabled_with_valid_config__auth0_is_configured( |
70 | 97 | self, |
71 | 98 | auth_enabled_environment, |
@@ -97,26 +124,26 @@ def test__given_auth_enabled_with_valid_config__auth0_is_configured( |
97 | 124 | auth_enabled_environment.assert_any_call("auth.auth0.address", "") |
98 | 125 | auth_enabled_environment.assert_any_call("auth.auth0.audience", "") |
99 | 126 |
|
100 | | - def test__given_auth_enabled_missing_config__falls_back_to_noop( |
| 127 | + def test__given_auth_enabled_missing_config__raises_configuration_error( |
101 | 128 | self, |
102 | 129 | auth_enabled_missing_config_environment, |
103 | 130 | mock_resource_protector, |
104 | 131 | mock_auth0_validator, |
105 | 132 | ): |
106 | | - """Test fallback to NoOp when auth is enabled but config is missing.""" |
| 133 | + """Test auth fails closed when auth is enabled but config is missing.""" |
107 | 134 | mock_protector_class, _ = mock_resource_protector |
108 | 135 | mock_validator_class, _ = mock_auth0_validator |
109 | 136 |
|
110 | | - decorator = ConditionalAuthDecorator() |
| 137 | + with pytest.raises( |
| 138 | + AuthConfigurationError, |
| 139 | + match="Auth enabled but Auth0 configuration missing", |
| 140 | + ): |
| 141 | + ConditionalAuthDecorator() |
111 | 142 |
|
112 | 143 | # Verify Auth0 components were not created |
113 | 144 | mock_validator_class.assert_not_called() |
114 | 145 | mock_protector_class.assert_not_called() |
115 | 146 |
|
116 | | - # Verify we get a NoOpDecorator |
117 | | - assert isinstance(decorator.get_decorator(), NoOpDecorator) |
118 | | - assert decorator.is_enabled is False |
119 | | - |
120 | 147 | # Verify configuration was checked |
121 | 148 | auth_enabled_missing_config_environment.assert_any_call( |
122 | 149 | "auth.enabled", False |
@@ -182,3 +209,16 @@ def test__given_auth_disabled__returns_noop_decorator( |
182 | 209 | decorator = create_auth_decorator() |
183 | 210 |
|
184 | 211 | assert isinstance(decorator, NoOpDecorator) |
| 212 | + |
| 213 | + def test__given_auth_enabled_missing_config__raises_configuration_error( |
| 214 | + self, |
| 215 | + auth_enabled_missing_config_environment, |
| 216 | + mock_resource_protector, |
| 217 | + mock_auth0_validator, |
| 218 | + ): |
| 219 | + """Test that factory raises when auth is enabled but misconfigured.""" |
| 220 | + with pytest.raises( |
| 221 | + AuthConfigurationError, |
| 222 | + match="Auth enabled but Auth0 configuration missing", |
| 223 | + ): |
| 224 | + create_auth_decorator() |
0 commit comments