halfapi/tests/test_jwt_middleware.py

108 lines
3.0 KiB
Python
Raw Normal View History

import os
import jwt
from requests import Request
import pytest
from unittest.mock import patch
import json
from json.decoder import JSONDecodeError
import sys
from hashlib import sha256
from base64 import b64decode
from uuid import uuid4, UUID
from starlette.testclient import TestClient
from starlette.authentication import (
AuthenticationBackend, AuthenticationError, BaseUser, AuthCredentials,
UnauthenticatedUser)
from starlette.responses import PlainTextResponse
from halfapi.lib.jwt_middleware import (
JWTUser, JWTAuthenticationBackend,
JWTWebSocketAuthenticationBackend)
def test_JWTUser():
uid = uuid4()
token = '{}'
payload = {}
user = JWTUser(uid, token, payload)
assert user.id == uid
assert user.token == token
assert user.payload == payload
assert user.is_authenticated == True
def test_jwt_NoToken(dummy_app):
async def test_route(request):
assert isinstance(request.user, UnauthenticatedUser)
return PlainTextResponse('ok')
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
[deps] Migration from starlette v0.18 to v0.23 Breaking : migrate your tests that use the TestDomain.client method following the instructions here https://github.com/Kludex/bump-testclient Squashed commit of the following: commit 0417f27b3f8de4334b2af3d9341288956cbc60fc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 11:08:44 2023 +0100 [deps] starlette 0.23 commit 552f00a65b263e7255944088f7e9aec9e9943515 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:59:42 2023 +0100 [deps] starlette 0.22 commit aefe448717717d36b536c9142b6b4a888950b819 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:45 2023 +0100 [tests][fix] compares the json interpreted value instead of the string commit 01333a200c98f05539ab6b26985c4207b7b03442 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:20 2023 +0100 [testing] changes from requests to httpx for Starlette TestClient (breaks) commit f3784fab7f1e5449d72ce69b6223768d579ffa28 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:54:10 2023 +0100 [deps][breaking] starlette 0.21 commit 717d3f8bd64bc654b150ae7294a298745acbd4b3 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:26:31 2023 +0100 [responses] use a wrapper function for exception handling (fix starlette 0.20) commit d0876e45dae235a49d4c4ea5df9edba884648d60 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:25:21 2023 +0100 [deps][breaking] starlette 0.20 commit 6504191c535da38b6cf7d7c717606660b8327795 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:12:51 2023 +0100 [deps] starlette 0.19 commit 7b639a8dc2a504cd31d4c3a5ab37ce20c5e1b6c5 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:11:14 2023 +0100 [deps] starlette 0.18 commit 20bd9077a474f634b619bc721af03901e4757237 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:07:48 2023 +0100 pipenv update
2023-01-14 11:10:13 +01:00
resp = test_client.request('get', '/test')
assert resp.status_code == 200
def test_jwt_Token(dummy_app, token_builder):
async def test_route(request):
assert isinstance(request.user, JWTUser)
print(request.scope['app'].debug)
return PlainTextResponse('ok')
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
[deps] Migration from starlette v0.18 to v0.23 Breaking : migrate your tests that use the TestDomain.client method following the instructions here https://github.com/Kludex/bump-testclient Squashed commit of the following: commit 0417f27b3f8de4334b2af3d9341288956cbc60fc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 11:08:44 2023 +0100 [deps] starlette 0.23 commit 552f00a65b263e7255944088f7e9aec9e9943515 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:59:42 2023 +0100 [deps] starlette 0.22 commit aefe448717717d36b536c9142b6b4a888950b819 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:45 2023 +0100 [tests][fix] compares the json interpreted value instead of the string commit 01333a200c98f05539ab6b26985c4207b7b03442 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:20 2023 +0100 [testing] changes from requests to httpx for Starlette TestClient (breaks) commit f3784fab7f1e5449d72ce69b6223768d579ffa28 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:54:10 2023 +0100 [deps][breaking] starlette 0.21 commit 717d3f8bd64bc654b150ae7294a298745acbd4b3 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:26:31 2023 +0100 [responses] use a wrapper function for exception handling (fix starlette 0.20) commit d0876e45dae235a49d4c4ea5df9edba884648d60 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:25:21 2023 +0100 [deps][breaking] starlette 0.20 commit 6504191c535da38b6cf7d7c717606660b8327795 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:12:51 2023 +0100 [deps] starlette 0.19 commit 7b639a8dc2a504cd31d4c3a5ab37ce20c5e1b6c5 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:11:14 2023 +0100 [deps] starlette 0.18 commit 20bd9077a474f634b619bc721af03901e4757237 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:07:48 2023 +0100 pipenv update
2023-01-14 11:10:13 +01:00
resp = test_client.request('get', '/test',
headers={
'Authorization': token_builder
})
assert resp.status_code == 200
def test_jwt_DebugFalse(dummy_app, token_debug_false_builder):
async def test_route(request):
assert isinstance(request.user, JWTUser)
return PlainTextResponse('ok')
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
[deps] Migration from starlette v0.18 to v0.23 Breaking : migrate your tests that use the TestDomain.client method following the instructions here https://github.com/Kludex/bump-testclient Squashed commit of the following: commit 0417f27b3f8de4334b2af3d9341288956cbc60fc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 11:08:44 2023 +0100 [deps] starlette 0.23 commit 552f00a65b263e7255944088f7e9aec9e9943515 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:59:42 2023 +0100 [deps] starlette 0.22 commit aefe448717717d36b536c9142b6b4a888950b819 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:45 2023 +0100 [tests][fix] compares the json interpreted value instead of the string commit 01333a200c98f05539ab6b26985c4207b7b03442 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:20 2023 +0100 [testing] changes from requests to httpx for Starlette TestClient (breaks) commit f3784fab7f1e5449d72ce69b6223768d579ffa28 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:54:10 2023 +0100 [deps][breaking] starlette 0.21 commit 717d3f8bd64bc654b150ae7294a298745acbd4b3 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:26:31 2023 +0100 [responses] use a wrapper function for exception handling (fix starlette 0.20) commit d0876e45dae235a49d4c4ea5df9edba884648d60 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:25:21 2023 +0100 [deps][breaking] starlette 0.20 commit 6504191c535da38b6cf7d7c717606660b8327795 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:12:51 2023 +0100 [deps] starlette 0.19 commit 7b639a8dc2a504cd31d4c3a5ab37ce20c5e1b6c5 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:11:14 2023 +0100 [deps] starlette 0.18 commit 20bd9077a474f634b619bc721af03901e4757237 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:07:48 2023 +0100 pipenv update
2023-01-14 11:10:13 +01:00
resp = test_client.request('get', '/test',
headers={
'Authorization': token_debug_false_builder
})
assert resp.status_code == 200
def test_jwt_DebugTrue(dummy_app, token_debug_true_builder):
"""
A debug token should return a 400 status code with a non debug app
"""
async def test_route(request):
return PlainTextResponse('ok')
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
[deps] Migration from starlette v0.18 to v0.23 Breaking : migrate your tests that use the TestDomain.client method following the instructions here https://github.com/Kludex/bump-testclient Squashed commit of the following: commit 0417f27b3f8de4334b2af3d9341288956cbc60fc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 11:08:44 2023 +0100 [deps] starlette 0.23 commit 552f00a65b263e7255944088f7e9aec9e9943515 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:59:42 2023 +0100 [deps] starlette 0.22 commit aefe448717717d36b536c9142b6b4a888950b819 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:45 2023 +0100 [tests][fix] compares the json interpreted value instead of the string commit 01333a200c98f05539ab6b26985c4207b7b03442 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:20 2023 +0100 [testing] changes from requests to httpx for Starlette TestClient (breaks) commit f3784fab7f1e5449d72ce69b6223768d579ffa28 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:54:10 2023 +0100 [deps][breaking] starlette 0.21 commit 717d3f8bd64bc654b150ae7294a298745acbd4b3 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:26:31 2023 +0100 [responses] use a wrapper function for exception handling (fix starlette 0.20) commit d0876e45dae235a49d4c4ea5df9edba884648d60 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:25:21 2023 +0100 [deps][breaking] starlette 0.20 commit 6504191c535da38b6cf7d7c717606660b8327795 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:12:51 2023 +0100 [deps] starlette 0.19 commit 7b639a8dc2a504cd31d4c3a5ab37ce20c5e1b6c5 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:11:14 2023 +0100 [deps] starlette 0.18 commit 20bd9077a474f634b619bc721af03901e4757237 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:07:48 2023 +0100 pipenv update
2023-01-14 11:10:13 +01:00
resp = test_client.request('get', '/test',
headers={
'Authorization': token_debug_true_builder
})
assert resp.status_code == 400
2021-05-28 12:41:23 +02:00
def test_jwt_DebugTrue_DebugApp(dummy_debug_app, token_debug_true_builder):
"""
A debug token should return a 200 status code with a debug app
"""
async def test_route(request):
assert isinstance(request.user, JWTUser)
return PlainTextResponse('ok')
2021-05-28 12:41:23 +02:00
dummy_debug_app.add_route('/test', test_route)
test_client = TestClient(dummy_debug_app)
2021-05-28 12:41:23 +02:00
[deps] Migration from starlette v0.18 to v0.23 Breaking : migrate your tests that use the TestDomain.client method following the instructions here https://github.com/Kludex/bump-testclient Squashed commit of the following: commit 0417f27b3f8de4334b2af3d9341288956cbc60fc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 11:08:44 2023 +0100 [deps] starlette 0.23 commit 552f00a65b263e7255944088f7e9aec9e9943515 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:59:42 2023 +0100 [deps] starlette 0.22 commit aefe448717717d36b536c9142b6b4a888950b819 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:45 2023 +0100 [tests][fix] compares the json interpreted value instead of the string commit 01333a200c98f05539ab6b26985c4207b7b03442 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:55:20 2023 +0100 [testing] changes from requests to httpx for Starlette TestClient (breaks) commit f3784fab7f1e5449d72ce69b6223768d579ffa28 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:54:10 2023 +0100 [deps][breaking] starlette 0.21 commit 717d3f8bd64bc654b150ae7294a298745acbd4b3 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:26:31 2023 +0100 [responses] use a wrapper function for exception handling (fix starlette 0.20) commit d0876e45dae235a49d4c4ea5df9edba884648d60 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:25:21 2023 +0100 [deps][breaking] starlette 0.20 commit 6504191c535da38b6cf7d7c717606660b8327795 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:12:51 2023 +0100 [deps] starlette 0.19 commit 7b639a8dc2a504cd31d4c3a5ab37ce20c5e1b6c5 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:11:14 2023 +0100 [deps] starlette 0.18 commit 20bd9077a474f634b619bc721af03901e4757237 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Sat Jan 14 10:07:48 2023 +0100 pipenv update
2023-01-14 11:10:13 +01:00
resp = test_client.request('get', '/test',
headers={
'Authorization': token_debug_true_builder
2021-05-28 12:41:23 +02:00
})
assert resp.status_code == 200