Skip to content

Commit a6efd73

Browse files
committed
Simplify InvalidLogoutToken exception
Remove the init overriding and use standard methods on the exception object to examine it in the DecodeLogoutToken service unit tests.
1 parent df87721 commit a6efd73

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

manage_breast_screening/auth/services.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@
1515
class InvalidLogoutToken(Exception):
1616
"""Raised when a CIS2 back-channel logout token is invalid."""
1717

18-
def __init__(self, cause=None):
19-
"""
20-
Initialize with optional cause.
21-
22-
Args:
23-
cause: The original exception that caused this error
24-
"""
25-
self.cause = cause
26-
super().__init__(str(cause) if cause else "Invalid logout token")
27-
2818

2919
class DecodeLogoutToken:
3020
def call(
@@ -68,4 +58,4 @@ def call(
6858
MissingClaimError,
6959
) as e:
7060
logger.exception("Invalid logout token")
71-
raise InvalidLogoutToken(cause=e) from e
61+
raise InvalidLogoutToken() from e

manage_breast_screening/auth/tests/test_services.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ def test_invalid_claims_raise_error(
124124
key_loader=self._key_loader(public_jwk),
125125
)
126126

127-
# Assert on the cause type and error message content
128-
assert isinstance(excinfo.value.cause, expected_error_type)
129-
assert expected_error_text in str(excinfo.value.cause)
127+
assert isinstance(excinfo.value.__cause__, expected_error_type)
128+
assert expected_error_text in str(excinfo.value.__cause__)
130129

131130
def test_invalid_signature_raises_error(self):
132131
kid = "k1"
@@ -147,9 +146,8 @@ def test_invalid_signature_raises_error(self):
147146
key_loader=self._key_loader(public_jwk_1),
148147
)
149148

150-
# Invalid signature should raise a JoseError
151-
assert isinstance(excinfo.value.cause, JoseError)
152-
assert "signature" in str(excinfo.value.cause).lower()
149+
assert isinstance(excinfo.value.__cause__, JoseError)
150+
assert "signature" in str(excinfo.value.__cause__)
153151

154152
def test_expired_token_raises_error(self):
155153
kid = "k1"
@@ -173,10 +171,8 @@ def test_expired_token_raises_error(self):
173171
client_id=client_id,
174172
key_loader=self._key_loader(public_jwk),
175173
)
176-
177-
# Expired token should raise an ExpiredTokenError
178-
assert isinstance(excinfo.value.cause, ExpiredTokenError)
179-
assert "expired" in str(excinfo.value.cause).lower()
174+
assert isinstance(excinfo.value.__cause__, ExpiredTokenError)
175+
assert "expired" in str(excinfo.value.__cause__)
180176

181177
def test_missing_iat_raises_error(self):
182178
kid = "k1"
@@ -197,6 +193,5 @@ def test_missing_iat_raises_error(self):
197193
key_loader=self._key_loader(public_jwk),
198194
)
199195

200-
# Missing iat should raise a MissingClaimError
201-
assert isinstance(excinfo.value.cause, MissingClaimError)
202-
assert "iat" in str(excinfo.value.cause).lower()
196+
assert isinstance(excinfo.value.__cause__, MissingClaimError)
197+
assert "iat" in str(excinfo.value.__cause__)

0 commit comments

Comments
 (0)