[test] add default DEBUG flag for pytest, add tests for jwt_middleware
This commit is contained in:
parent
55ebe0bbdd
commit
13ac878a7c
|
@ -44,10 +44,11 @@ class JWTUser(BaseUser):
|
||||||
self.payload = payload
|
self.payload = payload
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if len(self.__id) > 0:
|
return str({
|
||||||
return self.__id
|
'id' : str(self.__id),
|
||||||
else:
|
'token': self.token,
|
||||||
return 'no id'
|
'payload': self.payload
|
||||||
|
})
|
||||||
@property
|
@property
|
||||||
def is_authenticated(self) -> bool:
|
def is_authenticated(self) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -2,3 +2,5 @@
|
||||||
testpaths = tests halfapi
|
testpaths = tests halfapi
|
||||||
addopts = --doctest-modules
|
addopts = --doctest-modules
|
||||||
doctest_optionflags = ELLIPSIS
|
doctest_optionflags = ELLIPSIS
|
||||||
|
env =
|
||||||
|
DEBUG=TRUE
|
||||||
|
|
|
@ -2,28 +2,55 @@ import jwt
|
||||||
import requests
|
import requests
|
||||||
import pytest
|
import pytest
|
||||||
import json
|
import json
|
||||||
|
from json.decoder import JSONDecodeError
|
||||||
import sys
|
import sys
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
from halfapi.app import app
|
|
||||||
from base64 import b64decode
|
from base64 import b64decode
|
||||||
|
from starlette.testclient import TestClient
|
||||||
|
|
||||||
|
from halfapi.app import app
|
||||||
|
from halfapi.lib.jwt_middleware import (JWTUser, JWTAuthenticationBackend,
|
||||||
|
JWTWebSocketAuthenticationBackend)
|
||||||
|
|
||||||
def coucou():
|
def coucou():
|
||||||
return
|
return
|
||||||
def test_connected():
|
def test_connected():
|
||||||
app.route('/', coucou)
|
app.route('/', coucou)
|
||||||
|
|
||||||
def test_token():
|
@pytest.fixture
|
||||||
# This test needs to have a running auth-lirmm on 127.0.0.1:3000
|
def token():
|
||||||
|
# This fixture needs to have a running auth-lirmm on 127.0.0.1:3000
|
||||||
|
# Sets a valid token
|
||||||
|
|
||||||
r = requests.post('http://127.0.0.1:3000/',
|
r = requests.post('http://127.0.0.1:3000/',
|
||||||
data={'email':'maizi', 'password':'a'})
|
data={'email':'maizi', 'password':'a'})
|
||||||
|
|
||||||
assert len(r.text) > 0
|
if len(r.text) <= 0:
|
||||||
res = json.loads(r.text)
|
raise Exception('No result in token retrieval')
|
||||||
assert 'token' in res.keys()
|
|
||||||
sys.stderr.write(f'Token : {res["token"]}\n')
|
try:
|
||||||
secret = open('/etc/half_orm/secret').readline()
|
res = json.loads(r.text)
|
||||||
sys.stderr.write(f'Secret : {secret}\n')
|
except JSONDecodeError:
|
||||||
assert jwt.decode(
|
raise Exception('Malformed response from token retrieval')
|
||||||
res['token'],
|
|
||||||
secret, algorithms=['HS256'])
|
if 'token' not in res.keys():
|
||||||
|
raise Exception('Missing token in token request')
|
||||||
|
|
||||||
|
return res['token']
|
||||||
|
|
||||||
|
|
||||||
|
def test_token(token):
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
r = client.get('/user', headers={'Authorization':token})
|
||||||
|
res = False
|
||||||
|
try:
|
||||||
|
res = json.loads(r.text)
|
||||||
|
except JSONDecodeError:
|
||||||
|
raise Exception('Malformed response from /user request')
|
||||||
|
|
||||||
|
assert 'user' in res.keys()
|
||||||
|
print(res['user'])
|
||||||
|
assert 'id' in res['user'].keys()
|
||||||
|
assert 'token' in res['user'].keys()
|
||||||
|
assert 'payload' in res['user'].keys()
|
||||||
|
|
Loading…
Reference in New Issue