Skip to content

Commit 4d7b8b3

Browse files
committed
Refactor _generate_jwt method in TestBase to streamline key selection logic for JWT encoding, improving readability and maintainability of unit tests.
1 parent 1ca4d28 commit 4d7b8b3

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

tests/unit/test_session_service.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,7 @@ def _provide_jwts(self):
204204
]
205205

206206
@classmethod
207-
def _generate_jwt(
208-
cls,
209-
iss: str,
210-
exp: int,
211-
nbf: int,
212-
valid_key: bool = True,
213-
) -> str:
207+
def _generate_jwt(cls, iss: str, exp: int, nbf: int, valid_key: bool = True) -> str:
214208
payload = {
215209
"iss": iss,
216210
"iat": int(time()),
@@ -220,15 +214,9 @@ def _generate_jwt(
220214
"name": TEST_NAME,
221215
}
222216

223-
key_to_use = cls.private_key if valid_key else cls.invalid_private_key
224-
225-
# signed JWT (RS256 by default)
226-
return encode(
227-
payload,
228-
key=key_to_use,
229-
algorithm="RS256",
230-
headers={"kid": "kid123"},
231-
)
217+
if valid_key:
218+
return encode(payload, key=cls.private_key, algorithm="RS256", headers={"kid": "kid123"})
219+
return encode(payload, key=cls.invalid_private_key, algorithm="RS256", headers={"kid": "kid123"})
232220

233221

234222
class TestSessionService(TestBase):

0 commit comments

Comments
 (0)