[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:
|
||||||
|
raise Exception('No result in token retrieval')
|
||||||
|
|
||||||
|
try:
|
||||||
res = json.loads(r.text)
|
res = json.loads(r.text)
|
||||||
assert 'token' in res.keys()
|
except JSONDecodeError:
|
||||||
sys.stderr.write(f'Token : {res["token"]}\n')
|
raise Exception('Malformed response from token retrieval')
|
||||||
secret = open('/etc/half_orm/secret').readline()
|
|
||||||
sys.stderr.write(f'Secret : {secret}\n')
|
if 'token' not in res.keys():
|
||||||
assert jwt.decode(
|
raise Exception('Missing token in token request')
|
||||||
res['token'],
|
|
||||||
secret, algorithms=['HS256'])
|
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