From 2d3aac7909209c9bb955f90935b5af476e001430 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 16 Apr 2024 16:50:32 +0300 Subject: Create constructors for encoding and decoding a token. * gn2/tests/unit/wqflask/oauth2/(__init__.py, test_tokens.py): Test cases for jwt token creation. * gn2/wqflask/oauth2/tokens.py (JWTToken): New module for creating jwt tokens. Signed-off-by: Munyoki Kilyungi --- gn2/tests/unit/wqflask/oauth2/__init__.py | 0 gn2/tests/unit/wqflask/oauth2/test_tokens.py | 37 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 gn2/tests/unit/wqflask/oauth2/__init__.py create mode 100644 gn2/tests/unit/wqflask/oauth2/test_tokens.py (limited to 'gn2/tests/unit/wqflask') diff --git a/gn2/tests/unit/wqflask/oauth2/__init__.py b/gn2/tests/unit/wqflask/oauth2/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/gn2/tests/unit/wqflask/oauth2/test_tokens.py b/gn2/tests/unit/wqflask/oauth2/test_tokens.py new file mode 100644 index 00000000..ee527f51 --- /dev/null +++ b/gn2/tests/unit/wqflask/oauth2/test_tokens.py @@ -0,0 +1,37 @@ +"""Test oauth2 jwt tokens""" +from gn2.wqflask.oauth2.tokens import JWTToken + + +JWT_BEARER_TOKEN = b"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIs\ +ImN0eSI6Impzb24ifQ.eyJpc3MiOiJHTjIiLCJ\ +zdWIiOiIxMjM0IiwiYXVkIjoiR04yIiwiZXhwI\ +joiMTIzNDUifQ.ETSr_7O4ZWLac5l4pinO9Xeb\ +mzTO7xp_LvbgxjnskDc" + + +def test_encode_token(): + """Test encoding a jwt token.""" + token = JWTToken( + key="secret", + registered_claims={ + "iss": "GN2", + "sub": "1234", + "aud": "GN2", + "exp": "12345", + } + ) + assert token.encode() == JWT_BEARER_TOKEN + assert token.bearer_token == { + "Authorization": f"Bearer {JWT_BEARER_TOKEN}" + } + + +def test_decode_token(): + """Test decoding a jwt token.""" + claims = JWTToken.decode(JWT_BEARER_TOKEN, "secret") + assert claims == { + 'iss': 'GN2', + 'sub': '1234', + 'aud': 'GN2', + 'exp': '12345' + } -- cgit v1.2.3