|
6 | 6 | import pytest |
7 | 7 | from freezegun import freeze_time |
8 | 8 | from unittest import mock |
| 9 | +from fastapi.params import Depends as DependsClass |
9 | 10 |
|
10 | 11 |
|
11 | 12 | from appointment.controller.auth import signed_url_by_subscriber |
@@ -167,6 +168,44 @@ def test_get_subscriber_with_invalid_token(self, with_db, with_l10n, make_pro_su |
167 | 168 | # Use a nonsense value, like the subscriber id! |
168 | 169 | get_subscriber(request, subscriber.id, db) |
169 | 170 |
|
| 171 | + def test_get_subscriber_handles_depends_object_for_redis( |
| 172 | + self, with_db, with_l10n, make_pro_subscriber, make_external_connections, monkeypatch |
| 173 | + ): |
| 174 | + """Test that get_subscriber gracefully handles when redis_instance is a Depends object.""" |
| 175 | + |
| 176 | + saved_scheme = os.environ.get('AUTH_SCHEME', 'password') |
| 177 | + os.environ['AUTH_SCHEME'] = 'oidc' |
| 178 | + |
| 179 | + try: |
| 180 | + subscriber = make_pro_subscriber() |
| 181 | + oidc_id = uuid.uuid4().hex |
| 182 | + access_token = uuid.uuid4().hex |
| 183 | + |
| 184 | + make_external_connections( |
| 185 | + subscriber_id=subscriber.id, type_id=oidc_id, type=ExternalConnectionType.oidc |
| 186 | + ) |
| 187 | + |
| 188 | + request = mock.MagicMock() |
| 189 | + |
| 190 | + with patch('appointment.controller.apis.oidc_client.OIDCClient.introspect_token') as introspect_mock: |
| 191 | + introspect_mock.return_value = { |
| 192 | + 'sub': oidc_id, |
| 193 | + 'username': subscriber.username, |
| 194 | + 'email': subscriber.email, |
| 195 | + } |
| 196 | + |
| 197 | + with with_db() as db: |
| 198 | + # Pass a Depends object as redis_instance (simulating direct call without DI resolution) |
| 199 | + depends_obj = DependsClass(lambda: None) |
| 200 | + retrieved_subscriber = get_subscriber(request, access_token, db, depends_obj) |
| 201 | + |
| 202 | + # Should succeed by treating Depends as None and falling back to OIDC introspection |
| 203 | + assert retrieved_subscriber is not None |
| 204 | + assert retrieved_subscriber.id == subscriber.id |
| 205 | + introspect_mock.assert_called_once_with(access_token) |
| 206 | + finally: |
| 207 | + os.environ['AUTH_SCHEME'] = saved_scheme |
| 208 | + |
170 | 209 | def test_get_admin_subscriber(self, with_db, with_l10n, make_pro_subscriber): |
171 | 210 | subscriber = make_pro_subscriber() |
172 | 211 | access_token = create_access_token(data={'sub': f'uid-{subscriber.id}'}) |
|
0 commit comments