Skip to content

Commit 1aa45f3

Browse files
committed
tests: no-error random exception raises correctly
1 parent c0b871f commit 1aa45f3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

tests/multi_tenant/conftest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def multi_tenant_app():
1515
yield
1616

1717

18+
@pytest.fixture
19+
def multi_tenant_app_auto_error_false():
20+
azure_scheme_overrides = generate_azure_scheme_multi_tenant_object(auto_error=False)
21+
app.dependency_overrides[azure_scheme] = azure_scheme_overrides
22+
yield
23+
24+
1825
@pytest.fixture
1926
def mock_openid(respx_mock):
2027
respx_mock.get(openid_config_url(multi_tenant=True)).respond(json=openid_configuration())
@@ -54,7 +61,7 @@ def mock_openid_and_no_valid_keys(respx_mock, mock_openid):
5461
yield
5562

5663

57-
def generate_azure_scheme_multi_tenant_object(issuer=None):
64+
def generate_azure_scheme_multi_tenant_object(issuer=None, auto_error=True):
5865
"""
5966
This method is used just to generate the Multi Tenant Obj
6067
"""
@@ -73,4 +80,5 @@ async def issuer_fetcher(tid):
7380
},
7481
validate_iss=True,
7582
iss_callable=current_issuer,
83+
auto_error=auto_error,
7684
)

tests/multi_tenant/test_websocket.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ async def websocket_endpoint_scope(
5858
await websocket.close()
5959

6060

61+
@app.websocket("/ws/no-error")
62+
async def websocket_endpoint_scope(websocket: WebSocket, no_user=Depends(azure_scheme)):
63+
await websocket.accept()
64+
await websocket.send_text("Hello. User will be None! Do not use this example for production!")
65+
await websocket.close()
66+
67+
6168
client = TestClient(app)
6269

6370

@@ -215,3 +222,14 @@ async def test_exception_raised_unknown(multi_tenant_app, mock_openid_and_keys,
215222
with client.websocket_connect("/ws", headers={'Authorization': 'Bearer ' + build_access_token()}):
216223
pass
217224
assert error.value.reason == 'Unable to validate token'
225+
226+
227+
@pytest.mark.anyio
228+
async def test_no_error_pass_through(multi_tenant_app_auto_error_false, mock_openid_and_keys, mocker):
229+
"""Has a auto_error_true in pytest param, to make any random exception just return None. Used with multi-auth"""
230+
mocker.patch.object(OpenIdConfig, 'load_config', side_effect=ValueError('lol'))
231+
with client.websocket_connect(
232+
"/ws/no-error", headers={'Authorization': 'Bearer ' + build_access_token()}
233+
) as websocket:
234+
data = websocket.receive_text()
235+
assert data == "Hello. User will be None! Do not use this example for production!"

0 commit comments

Comments
 (0)