Skip to content

Commit 8ef26ee

Browse files
davidhuserJonasKs
authored andcommitted
feat: tests for backwards-compatible InvalidAuth
1 parent 7512a1c commit 8ef26ee

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/test_exception_compat.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
This module tests the exception handling and backwards compatibility of the exceptions module, introduced in
3+
issue https://github.com/intility/fastapi-azure-auth/issues/229.
4+
TODO: Remove this test module in v6.0.0
5+
"""
6+
import pytest
7+
from fastapi import HTTPException, WebSocketException, status
8+
9+
from fastapi_azure_auth.exceptions import (
10+
InvalidAuth,
11+
InvalidAuthHttp,
12+
InvalidAuthWebSocket,
13+
UnauthorizedHttp,
14+
UnauthorizedWebSocket,
15+
)
16+
17+
18+
def test_invalid_auth_backwards_compatibility():
19+
"""Test that InvalidAuth maps to correct exceptions and maintains format"""
20+
# Mock HTTP request scope
21+
http_conn = type('HTTPConnection', (), {'scope': {'type': 'http'}})()
22+
23+
# Mock WebSocket scope
24+
ws_conn = type('HTTPConnection', (), {'scope': {'type': 'websocket'}})()
25+
26+
# Test HTTP path
27+
http_exc = InvalidAuth("test message", http_conn)
28+
assert isinstance(http_exc, UnauthorizedHttp)
29+
assert isinstance(http_exc, HTTPException)
30+
assert http_exc.status_code == status.HTTP_401_UNAUTHORIZED
31+
assert http_exc.detail == {"error": "invalid_token", "message": "test message"}
32+
33+
# Test WebSocket path
34+
ws_exc = InvalidAuth("test message", ws_conn)
35+
assert isinstance(ws_exc, UnauthorizedWebSocket)
36+
assert isinstance(ws_exc, WebSocketException)
37+
assert ws_exc.code == status.WS_1008_POLICY_VIOLATION
38+
assert ws_exc.reason == str({"error": "invalid_token", "message": "test message"})
39+
40+
41+
def test_legacy_exception_catching():
42+
"""Test that old exception catching patterns still work"""
43+
# Test HTTP exceptions
44+
http_conn = type('HTTPConnection', (), {'scope': {'type': 'http'}})()
45+
46+
with pytest.raises((InvalidAuthHttp, UnauthorizedHttp)) as exc_info:
47+
raise InvalidAuth("test message", http_conn)
48+
49+
assert isinstance(exc_info.value, UnauthorizedHttp)
50+
assert exc_info.value.detail == {"error": "invalid_token", "message": "test message"}
51+
52+
# Test WebSocket exceptions
53+
ws_conn = type('HTTPConnection', (), {'scope': {'type': 'websocket'}})()
54+
55+
with pytest.raises((InvalidAuthWebSocket, UnauthorizedWebSocket)) as exc_info:
56+
raise InvalidAuth("test message", ws_conn)
57+
58+
assert isinstance(exc_info.value, UnauthorizedWebSocket)
59+
assert exc_info.value.reason == str({"error": "invalid_token", "message": "test message"})
60+
61+
62+
def test_new_exceptions_can_be_caught_as_legacy():
63+
"""Test that new exceptions can be caught with legacy catch blocks"""
64+
with pytest.raises((InvalidAuthHttp, UnauthorizedHttp)) as exc_info:
65+
raise UnauthorizedHttp("test message")
66+
67+
assert exc_info.value.detail == {"error": "invalid_token", "message": "test message"}
68+
69+
with pytest.raises((InvalidAuthWebSocket, UnauthorizedWebSocket)) as exc_info:
70+
raise UnauthorizedWebSocket("test message")
71+
72+
assert exc_info.value.reason == str({"error": "invalid_token", "message": "test message"})

0 commit comments

Comments
 (0)