From 13ac878a7c7a9159787700bbef498bceaaa6dcf7 Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Thu, 9 Jul 2020 10:27:45 +0200 Subject: [PATCH] [test] add default DEBUG flag for pytest, add tests for jwt_middleware --- halfapi/lib/jwt_middleware.py | 9 ++++--- pytest.ini | 2 ++ tests/test_jwt_middleware.py | 51 ++++++++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/halfapi/lib/jwt_middleware.py b/halfapi/lib/jwt_middleware.py index 104f9c7..f980b33 100644 --- a/halfapi/lib/jwt_middleware.py +++ b/halfapi/lib/jwt_middleware.py @@ -44,10 +44,11 @@ class JWTUser(BaseUser): self.payload = payload def __str__(self): - if len(self.__id) > 0: - return self.__id - else: - return 'no id' + return str({ + 'id' : str(self.__id), + 'token': self.token, + 'payload': self.payload + }) @property def is_authenticated(self) -> bool: return True diff --git a/pytest.ini b/pytest.ini index 25c7847..d70ffb1 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,3 +2,5 @@ testpaths = tests halfapi addopts = --doctest-modules doctest_optionflags = ELLIPSIS +env = + DEBUG=TRUE diff --git a/tests/test_jwt_middleware.py b/tests/test_jwt_middleware.py index 19485d1..4e49bbf 100644 --- a/tests/test_jwt_middleware.py +++ b/tests/test_jwt_middleware.py @@ -2,28 +2,55 @@ import jwt import requests import pytest import json +from json.decoder import JSONDecodeError import sys from hashlib import sha256 -from halfapi.app import app 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(): return def test_connected(): app.route('/', coucou) -def test_token(): - # This test needs to have a running auth-lirmm on 127.0.0.1:3000 +@pytest.fixture +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/', data={'email':'maizi', 'password':'a'}) - assert len(r.text) > 0 - res = json.loads(r.text) - assert 'token' in res.keys() - sys.stderr.write(f'Token : {res["token"]}\n') - secret = open('/etc/half_orm/secret').readline() - sys.stderr.write(f'Secret : {secret}\n') - assert jwt.decode( - res['token'], - secret, algorithms=['HS256']) + if len(r.text) <= 0: + raise Exception('No result in token retrieval') + + try: + res = json.loads(r.text) + except JSONDecodeError: + raise Exception('Malformed response from token retrieval') + + 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()