[tests] jwt, use of TestClient instead of requests
This commit is contained in:
parent
a82fd6def0
commit
a0c41d7d78
|
@ -9,6 +9,7 @@ import tempfile
|
||||||
from typing import Dict, Tuple
|
from typing import Dict, Tuple
|
||||||
from uuid import uuid1, uuid4, UUID
|
from uuid import uuid1, uuid4, UUID
|
||||||
import click
|
import click
|
||||||
|
from click.testing import CliRunner
|
||||||
import jwt
|
import jwt
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
|
@ -31,92 +31,77 @@ def test_JWTUser():
|
||||||
assert user.payload == payload
|
assert user.payload == payload
|
||||||
assert user.is_authenticated == True
|
assert user.is_authenticated == True
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
def test_jwt_NoToken(dummy_app):
|
||||||
async def test_JWTAuthenticationBackend_NoToken(token_builder):
|
async def test_route(request):
|
||||||
backend = JWTAuthenticationBackend()
|
assert isinstance(request.user, UnauthenticatedUser)
|
||||||
assert backend.secret_key == SECRET
|
return PlainTextResponse('ok')
|
||||||
|
|
||||||
req = Request()
|
dummy_app.add_route('/test', test_route)
|
||||||
|
test_client = TestClient(dummy_app)
|
||||||
|
resp = test_client.get('/test')
|
||||||
|
assert resp.status_code == 200
|
||||||
|
|
||||||
credentials, user = await backend.authenticate(req)
|
def test_jwt_Token(dummy_app, token_builder):
|
||||||
assert isinstance(user, UnauthenticatedUser)
|
async def test_route(request):
|
||||||
assert isinstance(credentials, AuthCredentials)
|
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)
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
resp = test_client.get('/test',
|
||||||
async def test_JWTAuthenticationBackend_Token(token_builder):
|
|
||||||
backend = JWTAuthenticationBackend()
|
|
||||||
assert backend.secret_key == SECRET
|
|
||||||
|
|
||||||
req = Request(
|
|
||||||
headers={
|
headers={
|
||||||
'Authorization': token_builder
|
'Authorization': token_builder
|
||||||
})
|
})
|
||||||
|
assert resp.status_code == 200
|
||||||
credentials, user = await backend.authenticate(req)
|
|
||||||
assert isinstance(user, JWTUser)
|
|
||||||
assert isinstance(credentials, AuthCredentials)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
def test_jwt_DebugFalse(dummy_app, token_debug_false_builder):
|
||||||
async def test_JWTAuthenticationBackend_DebugFalse(token_debug_false_builder):
|
async def test_route(request):
|
||||||
backend = JWTAuthenticationBackend()
|
assert isinstance(request.user, JWTUser)
|
||||||
assert backend.secret_key == SECRET
|
return PlainTextResponse('ok')
|
||||||
|
|
||||||
req = Request(
|
dummy_app.add_route('/test', test_route)
|
||||||
|
test_client = TestClient(dummy_app)
|
||||||
|
|
||||||
|
resp = test_client.get('/test',
|
||||||
headers={
|
headers={
|
||||||
'Authorization': token_debug_false_builder
|
'Authorization': token_debug_false_builder
|
||||||
})
|
})
|
||||||
|
assert resp.status_code == 200
|
||||||
credentials, user = await backend.authenticate(req)
|
|
||||||
assert isinstance(user, JWTUser)
|
|
||||||
assert isinstance(credentials, AuthCredentials)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
def test_jwt_DebugTrue(dummy_app, token_debug_true_builder):
|
||||||
async def test_JWTAuthenticationBackend_DebugTrue(token_debug_true_builder):
|
"""
|
||||||
backend = JWTAuthenticationBackend()
|
A debug token should return a 400 status code with a non debug app
|
||||||
assert backend.secret_key == SECRET
|
"""
|
||||||
|
async def test_route(request):
|
||||||
|
return PlainTextResponse('ok')
|
||||||
|
|
||||||
req = Request(
|
dummy_app.add_route('/test', test_route)
|
||||||
|
test_client = TestClient(dummy_app)
|
||||||
|
|
||||||
|
resp = test_client.get('/test',
|
||||||
headers={
|
headers={
|
||||||
'Authorization': token_debug_true_builder
|
'Authorization': token_debug_true_builder
|
||||||
})
|
})
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
try:
|
|
||||||
await backend.authenticate(req)
|
|
||||||
except Exception as exc:
|
|
||||||
assert type(exc) == AuthenticationError
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
def test_jwt_DebugTrue_DebugApp(dummy_debug_app, token_debug_true_builder):
|
||||||
async def test_JWTAuthenticationBackend_Check(token_debug_false_builder):
|
"""
|
||||||
backend = JWTAuthenticationBackend()
|
A debug token should return a 200 status code with a debug app
|
||||||
assert backend.secret_key == SECRET
|
"""
|
||||||
|
async def test_route(request):
|
||||||
|
assert isinstance(request.user, JWTUser)
|
||||||
|
return PlainTextResponse('ok')
|
||||||
|
|
||||||
req = Request(
|
dummy_debug_app.add_route('/test', test_route)
|
||||||
params={
|
test_client = TestClient(dummy_debug_app)
|
||||||
'check':True,
|
|
||||||
|
resp = test_client.get('/test',
|
||||||
|
headers={
|
||||||
|
'Authorization': token_debug_true_builder
|
||||||
})
|
})
|
||||||
|
assert resp.status_code == 200
|
||||||
credentials, user = await backend.authenticate(req)
|
|
||||||
assert isinstance(user, UnauthenticatedUser)
|
|
||||||
assert isinstance(credentials, AuthCredentials)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_JWTAuthenticationBackend_CheckUserId(token_debug_false_builder):
|
|
||||||
backend = JWTAuthenticationBackend()
|
|
||||||
assert backend.secret_key == SECRET
|
|
||||||
|
|
||||||
tmp_user_id = str(uuid4())
|
|
||||||
|
|
||||||
req = Request(
|
|
||||||
params={
|
|
||||||
'check': True,
|
|
||||||
'user_id': tmp_user_id
|
|
||||||
})
|
|
||||||
|
|
||||||
credentials, user = await backend.authenticate(req)
|
|
||||||
assert isinstance(user, JWTUser)
|
|
||||||
assert user.__id == tmp_user_id
|
|
||||||
assert isinstance(credentials, AuthCredentials)
|
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
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 halfapi.app import app
|
|
||||||
os.environ['HALFAPI_PROD'] = ''
|
|
||||||
os.environ['HALFAPI_SECRET'] = 'randomsecret'
|
|
||||||
|
|
||||||
from halfapi.lib.jwt_middleware import (PRODUCTION, SECRET,
|
|
||||||
JWTUser, JWTAuthenticationBackend,
|
|
||||||
JWTWebSocketAuthenticationBackend)
|
|
||||||
|
|
||||||
def test_constants():
|
|
||||||
assert PRODUCTION == bool(os.environ['HALFAPI_PROD'])
|
|
||||||
#assert SECRET == os.environ['HALFAPI_SECRET']
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def token_debug_builder():
|
|
||||||
yield jwt.encode({
|
|
||||||
'name':'xxx',
|
|
||||||
'user_id': str(uuid4()),
|
|
||||||
'debug': True},
|
|
||||||
key=SECRET
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_JWTAuthenticationBackend_debug(token_debug_builder):
|
|
||||||
backend = JWTAuthenticationBackend()
|
|
||||||
|
|
||||||
req = Request(
|
|
||||||
headers={
|
|
||||||
'Authorization': token_debug_builder
|
|
||||||
})
|
|
||||||
|
|
||||||
auth = await backend.authenticate(req)
|
|
||||||
assert(len(auth) == 2)
|
|
||||||
assert type(auth[0]) == AuthCredentials
|
|
||||||
assert type(auth[1]) == JWTUser
|
|
Loading…
Reference in New Issue