2020-08-27 17:45:23 +02:00
|
|
|
import os
|
2020-07-03 11:42:10 +02:00
|
|
|
import jwt
|
2020-08-27 17:45:23 +02:00
|
|
|
from requests import Request
|
2020-07-08 12:41:24 +02:00
|
|
|
import pytest
|
2020-08-27 17:45:23 +02:00
|
|
|
from unittest.mock import patch
|
2020-07-08 12:41:24 +02:00
|
|
|
import json
|
2020-07-09 10:27:45 +02:00
|
|
|
from json.decoder import JSONDecodeError
|
2020-07-08 12:41:24 +02:00
|
|
|
import sys
|
|
|
|
from hashlib import sha256
|
|
|
|
from base64 import b64decode
|
2020-08-27 17:45:23 +02:00
|
|
|
from uuid import uuid4, UUID
|
|
|
|
|
2020-07-09 10:27:45 +02:00
|
|
|
from starlette.testclient import TestClient
|
2020-08-27 17:45:23 +02:00
|
|
|
from starlette.authentication import (
|
|
|
|
AuthenticationBackend, AuthenticationError, BaseUser, AuthCredentials,
|
|
|
|
UnauthenticatedUser)
|
2021-05-28 21:54:45 +02:00
|
|
|
from starlette.responses import PlainTextResponse
|
2020-08-27 17:45:23 +02:00
|
|
|
|
2021-05-28 21:54:45 +02:00
|
|
|
from halfapi.lib.jwt_middleware import (
|
2020-08-27 17:45:23 +02:00
|
|
|
JWTUser, JWTAuthenticationBackend,
|
2020-07-09 10:27:45 +02:00
|
|
|
JWTWebSocketAuthenticationBackend)
|
2020-07-03 11:42:10 +02:00
|
|
|
|
2020-07-10 12:58:53 +02:00
|
|
|
|
2020-08-27 17:45:23 +02:00
|
|
|
def test_JWTUser():
|
|
|
|
uid = uuid4()
|
|
|
|
token = '{}'
|
|
|
|
payload = {}
|
|
|
|
user = JWTUser(uid, token, payload)
|
2021-05-28 22:02:35 +02:00
|
|
|
assert user.id == uid
|
2020-08-27 17:45:23 +02:00
|
|
|
assert user.token == token
|
|
|
|
assert user.payload == payload
|
|
|
|
assert user.is_authenticated == True
|
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
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)
|
|
|
|
resp = test_client.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)
|
|
|
|
|
|
|
|
resp = test_client.get('/test',
|
2020-08-27 17:45:23 +02:00
|
|
|
headers={
|
|
|
|
'Authorization': token_builder
|
|
|
|
})
|
2021-05-28 22:12:53 +02:00
|
|
|
assert resp.status_code == 200
|
2020-08-27 17:45:23 +02:00
|
|
|
|
2020-08-27 18:09:48 +02:00
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
def test_jwt_DebugFalse(dummy_app, token_debug_false_builder):
|
|
|
|
async def test_route(request):
|
|
|
|
assert isinstance(request.user, JWTUser)
|
|
|
|
return PlainTextResponse('ok')
|
2020-08-27 18:09:48 +02:00
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
dummy_app.add_route('/test', test_route)
|
|
|
|
test_client = TestClient(dummy_app)
|
2020-08-27 18:09:48 +02:00
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
resp = test_client.get('/test',
|
2020-08-27 18:09:48 +02:00
|
|
|
headers={
|
|
|
|
'Authorization': token_debug_false_builder
|
|
|
|
})
|
2021-05-28 22:12:53 +02:00
|
|
|
assert resp.status_code == 200
|
2020-08-27 18:09:48 +02:00
|
|
|
|
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
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')
|
2020-08-27 18:09:48 +02:00
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
dummy_app.add_route('/test', test_route)
|
|
|
|
test_client = TestClient(dummy_app)
|
2020-08-27 18:09:48 +02:00
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
resp = test_client.get('/test',
|
2020-08-27 18:09:48 +02:00
|
|
|
headers={
|
|
|
|
'Authorization': token_debug_true_builder
|
|
|
|
})
|
2021-05-28 22:12:53 +02:00
|
|
|
assert resp.status_code == 400
|
2020-08-27 18:09:48 +02:00
|
|
|
|
2021-05-28 12:41:23 +02:00
|
|
|
|
2021-05-28 22:12:53 +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
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
dummy_debug_app.add_route('/test', test_route)
|
|
|
|
test_client = TestClient(dummy_debug_app)
|
2021-05-28 12:41:23 +02:00
|
|
|
|
2021-05-28 22:12:53 +02:00
|
|
|
resp = test_client.get('/test',
|
|
|
|
headers={
|
|
|
|
'Authorization': token_debug_true_builder
|
2021-05-28 12:41:23 +02:00
|
|
|
})
|
2021-05-28 22:12:53 +02:00
|
|
|
assert resp.status_code == 200
|