asyncio style refactoring
This commit is contained in:
+5
-5
@@ -1,14 +1,14 @@
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
|
||||
from src.service.auth.jwt import HashService, JwtService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def jwt_service()->JwtService:
|
||||
@pytest_asyncio.fixture
|
||||
async def jwt_service()->JwtService:
|
||||
jwt_service=JwtService()
|
||||
return jwt_service
|
||||
|
||||
@pytest.fixture
|
||||
def hash_service()->HashService:
|
||||
@pytest_asyncio.fixture
|
||||
async def hash_service()->HashService:
|
||||
hash_service=HashService()
|
||||
return hash_service
|
||||
@@ -1,16 +1,16 @@
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from fastapi import Request
|
||||
|
||||
from src.service.auth.auth import CurrentUserService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def current_user_service()->CurrentUserService:
|
||||
@pytest_asyncio.fixture
|
||||
async def current_user_service()->CurrentUserService:
|
||||
current_user_service=CurrentUserService()
|
||||
return current_user_service
|
||||
|
||||
@pytest.fixture
|
||||
def requests(mocker):
|
||||
@pytest_asyncio.fixture
|
||||
async def requests(mocker):
|
||||
fake_request = mocker.MagicMock(spec=Request)
|
||||
fake_request.headers = {"user-agent": "pytest-agent", "x-forwarded-for":"127.0.0.1"}
|
||||
return fake_request
|
||||
@@ -11,7 +11,9 @@ from pydantic import ValidationError
|
||||
from src.models.configs_read.env import env_settings
|
||||
from src.service.auth.auth import CurrentUserService
|
||||
from src.service.auth.jwt import HashService, JwtService
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
current_user_service=CurrentUserService()
|
||||
|
||||
@pytest.mark.integra
|
||||
class TestAuth:
|
||||
@@ -19,19 +21,19 @@ class TestAuth:
|
||||
@pytest.mark.parametrize("user_data",[
|
||||
pytest.param(SimpleNamespace(first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True), id="correct_data")
|
||||
])
|
||||
def test_get_current_user_positive(self,current_user_service:CurrentUserService, jwt_service:JwtService, monkeypatch, user_data:SimpleNamespace)->None:
|
||||
async def test_get_current_user_positive(self,current_user_service:CurrentUserService, jwt_service:JwtService, monkeypatch, user_data:SimpleNamespace)->None:
|
||||
|
||||
with allure.step("create token"):
|
||||
|
||||
token=jwt_service.create_access_token({"sub":str(uuid4())})
|
||||
token=await jwt_service.create_access_token({"sub":str(uuid4())})
|
||||
|
||||
with allure.step("patching db call functions"):
|
||||
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", lambda user:user_data)
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", AsyncMock(return_value=user_data))
|
||||
|
||||
with allure.step("test get_current_user_with_fake_token"):
|
||||
|
||||
test_result= current_user_service.get_current_user(token)
|
||||
test_result= await current_user_service.get_current_user(token)
|
||||
|
||||
assert test_result.first_name==user_data.first_name
|
||||
assert test_result.last_name==user_data.last_name
|
||||
@@ -46,37 +48,37 @@ class TestAuth:
|
||||
pytest.param(SimpleNamespace(first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True),1234, HTTPException, id="wrong_id"),
|
||||
pytest.param(SimpleNamespace(status=True),uuid4(), ValidationError,id="empty_model_data")
|
||||
])
|
||||
def test_get_current_user_negative(self,current_user_service:CurrentUserService, jwt_service:JwtService, monkeypatch, user_data:SimpleNamespace, expected_exception, uuid)->None:
|
||||
async def test_get_current_user_negative(self,current_user_service:CurrentUserService, jwt_service:JwtService, monkeypatch, user_data:SimpleNamespace, expected_exception, uuid)->None:
|
||||
|
||||
with allure.step("create token"):
|
||||
|
||||
token=jwt_service.create_access_token({"sub":str(uuid)})
|
||||
token=await jwt_service.create_access_token({"sub":str(uuid)})
|
||||
|
||||
with allure.step("patching db call functions"):
|
||||
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", lambda user:user_data)
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", AsyncMock(return_value=user_data))
|
||||
|
||||
with allure.step("test get_current_user_with_fake_token"), pytest.raises(expected_exception):
|
||||
|
||||
current_user_service.get_current_user(token)
|
||||
await current_user_service.get_current_user(token)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("user_data, form_data_email,form_data_password",[
|
||||
pytest.param(SimpleNamespace(id=uuid4(),hashed_password="1234", first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True), "d@d.d", "1234", id="correct_data"),
|
||||
])
|
||||
def test_login_positive(self, jwt_service:JwtService,current_user_service:CurrentUserService, monkeypatch, user_data:SimpleNamespace, hash_service:HashService, form_data_email:str, form_data_password:str, requests)->None:
|
||||
async def test_login_positive(self, jwt_service:JwtService,current_user_service:CurrentUserService, monkeypatch, user_data:SimpleNamespace, hash_service:HashService, form_data_email:str, form_data_password:str, requests)->None:
|
||||
|
||||
with allure.step("patching db call functions"):
|
||||
|
||||
user_data.hashed_password=hash_service.plain_to_hash(user_data.hashed_password)
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_email", lambda user:user_data)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", lambda user_id: True)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "create_token", lambda token: True )
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_email", AsyncMock(return_value=user_data))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", AsyncMock(return_value=True))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "create_token", AsyncMock(return_value=True))
|
||||
|
||||
fake_request = requests
|
||||
|
||||
with allure.step("test login_with_fake_data"):
|
||||
access, refresh=current_user_service.login(form_data_email, form_data_password,fake_request)
|
||||
access, refresh=await current_user_service.login(form_data_email, form_data_password,fake_request)
|
||||
parts_a=access.split(".")
|
||||
parts_b=refresh.split(".")
|
||||
assert isinstance(access, str)
|
||||
@@ -90,36 +92,36 @@ class TestAuth:
|
||||
pytest.param(SimpleNamespace(id=uuid4(),hashed_password="1234", first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=False), "d@d.d", "1234",HTTPException, id="false_status"),
|
||||
pytest.param(SimpleNamespace(id=1234,hashed_password="1234", first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True), "d@d.d", "1234",ValidationError, id="wrong_id"),
|
||||
])
|
||||
def test_login_negative(self, current_user_service:CurrentUserService, user_data:SimpleNamespace, jwt_service:JwtService, monkeypatch, requests, hash_service:HashService, form_data_email:str, form_data_password:str, expected_exception):
|
||||
async def test_login_negative(self, current_user_service:CurrentUserService, user_data:SimpleNamespace, jwt_service:JwtService, monkeypatch, requests, hash_service:HashService, form_data_email:str, form_data_password:str, expected_exception):
|
||||
|
||||
with allure.step("patching db call functions"):
|
||||
|
||||
user_data.hashed_password=hash_service.plain_to_hash(user_data.hashed_password)
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_email", lambda user:user_data)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", lambda user_id: True)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "create_token", lambda token: True )
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_email", AsyncMock(return_value=user_data))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", AsyncMock(return_value=True))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "create_token", AsyncMock(return_value=True))
|
||||
|
||||
fake_request = requests
|
||||
|
||||
with allure.step("test login_with_fake_data"), pytest.raises(expected_exception):
|
||||
|
||||
current_user_service.login(form_data_email, form_data_password,fake_request)
|
||||
await current_user_service.login(form_data_email, form_data_password,fake_request)
|
||||
|
||||
|
||||
|
||||
def test_logout_positive(self, jwt_service:JwtService, monkeypatch, current_user_service:CurrentUserService)->None:
|
||||
async def test_logout_positive(self, jwt_service:JwtService, monkeypatch, current_user_service:CurrentUserService)->None:
|
||||
|
||||
with allure.step("create fake refresh token"):
|
||||
|
||||
token=jwt_service.create_refresh_token({"sub":str(uuid4())})
|
||||
token=await jwt_service.create_refresh_token({"sub":str(uuid4())})
|
||||
|
||||
with allure.step("patching db call functions"):
|
||||
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "logout", lambda jti: True )
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "logout", AsyncMock(return_value=True))
|
||||
|
||||
with allure.step("test logout with fake data"):
|
||||
|
||||
status=current_user_service.logout(token[0])
|
||||
status=await current_user_service.logout(token[0])
|
||||
assert status is True
|
||||
|
||||
@pytest.mark.parametrize("jti,db_result, expected_exception",[
|
||||
@@ -127,13 +129,13 @@ class TestAuth:
|
||||
pytest.param(1234, True, HTTPException, id="jti_int"),
|
||||
pytest.param(str(uuid4()), False, HTTPException, id="db_result_none"),
|
||||
])
|
||||
def test_logout_negative(self, jwt_service:JwtService, monkeypatch, current_user_service:CurrentUserService, expected_exception, jti, db_result)->None:
|
||||
async def test_logout_negative(self, jwt_service:JwtService, monkeypatch, current_user_service:CurrentUserService, expected_exception, jti, db_result)->None:
|
||||
|
||||
with allure.step("patching db call functions"):
|
||||
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "logout", lambda jti: db_result )
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "logout", AsyncMock(return_value=db_result) )
|
||||
|
||||
def fake_create_refresh_token(data:dict)->str:
|
||||
async def fake_create_refresh_token(data:dict)->str:
|
||||
return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||
|
||||
monkeypatch.setattr(jwt_service, "create_refresh_token", fake_create_refresh_token)
|
||||
@@ -141,39 +143,39 @@ class TestAuth:
|
||||
|
||||
with allure.step("create fake refresh token"):
|
||||
|
||||
token=fake_create_refresh_token({"sub":str(uuid4()), "jti":jti, "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)})
|
||||
token=await fake_create_refresh_token({"sub":str(uuid4()), "jti":jti, "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)})
|
||||
|
||||
with allure.step("test logout with fake data"), pytest.raises(expected_exception):
|
||||
|
||||
current_user_service.logout(token)
|
||||
await current_user_service.logout(token)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("db_result_token, user_data_result_db", [
|
||||
pytest.param(SimpleNamespace(is_revoked=False, expires_at=datetime.now(UTC)+timedelta(days=15)),SimpleNamespace(status=True), id="correct_data")
|
||||
])
|
||||
def test_refresh_token_positive(self, monkeypatch, current_user_service:CurrentUserService, db_result_token, requests:Request, jwt_service:JwtService,user_data_result_db )->None:
|
||||
async def test_refresh_token_positive(self, monkeypatch, current_user_service:CurrentUserService, db_result_token, requests:Request, jwt_service:JwtService,user_data_result_db )->None:
|
||||
|
||||
with allure.step("patching db call functions"):
|
||||
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions,"get_token_by_id", lambda jti: db_result_token)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", lambda data:True)
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", lambda user:user_data_result_db)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions,"create_and_update_token", lambda data0, data1, data2: True)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions,"get_token_by_id", AsyncMock(return_value=db_result_token))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", AsyncMock(return_value=True))
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", AsyncMock(return_value=user_data_result_db))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions,"create_and_update_token", AsyncMock(return_value=True))
|
||||
|
||||
fake_request = requests
|
||||
|
||||
def fake_create_refresh_token(data:dict)->str:
|
||||
async def fake_create_refresh_token(data:dict)->str:
|
||||
return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||
|
||||
monkeypatch.setattr(jwt_service, "create_refresh_token", fake_create_refresh_token)
|
||||
|
||||
with allure.step("create fake refresh token"):
|
||||
|
||||
token=fake_create_refresh_token({"sub":str(uuid4()), "jti":str(uuid4()), "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)})
|
||||
token=await fake_create_refresh_token({"sub":str(uuid4()), "jti":str(uuid4()), "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)})
|
||||
|
||||
with allure.step("test refresh token with fake data"):
|
||||
|
||||
new_access_token, new_refresh_token = current_user_service.refresh_token(token, fake_request)
|
||||
new_access_token, new_refresh_token = await current_user_service.refresh_token(token, fake_request)
|
||||
parts_a=new_access_token.split(".")
|
||||
parts_b=new_refresh_token.split(".")
|
||||
assert isinstance(new_access_token, str)
|
||||
@@ -192,22 +194,22 @@ class TestAuth:
|
||||
pytest.param(SimpleNamespace(is_revoked=False, user_id="123",expires_at=datetime.now(UTC)+timedelta(days=15)),None,True,{"sub":str(uuid4()), "jti":str(uuid4()), "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)}, HTTPException,id="user_missing"),
|
||||
pytest.param(SimpleNamespace(is_revoked=False,user_id="123", expires_at=datetime.now(UTC)-timedelta(days=15)),SimpleNamespace(status=True),True,{"sub":str(uuid4()), "jti":str(uuid4()), "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)}, HTTPException,id="wrong_exp")
|
||||
])
|
||||
def test_refresh_token_negative(self, monkeypatch, current_user_service:CurrentUserService, db_result_token, requests, jwt_service:JwtService,user_data_result_db, expected_exception, fake_token_data, update_result)->None:
|
||||
async def test_refresh_token_negative(self, monkeypatch, current_user_service:CurrentUserService, db_result_token, requests, jwt_service:JwtService,user_data_result_db, expected_exception, fake_token_data, update_result)->None:
|
||||
with allure.step("patching db call functions"):
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions,"get_token_by_id", lambda jti: db_result_token)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", lambda user_id:True)
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", lambda user:user_data_result_db)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "create_and_update_token", lambda old_jti, new_jti, new_token_record:update_result)
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions,"get_token_by_id", AsyncMock(return_value=db_result_token))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "revoke_all", AsyncMock(return_value=True))
|
||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", AsyncMock(return_value=user_data_result_db))
|
||||
monkeypatch.setattr(current_user_service.jwt_db_actions, "create_and_update_token", AsyncMock(return_value=update_result))
|
||||
|
||||
fake_request = requests
|
||||
|
||||
def fake_create_refresh_token(data:dict)->str:
|
||||
async def fake_create_refresh_token(data:dict)->str:
|
||||
return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||
|
||||
monkeypatch.setattr(jwt_service, "create_refresh_token", fake_create_refresh_token)
|
||||
|
||||
with allure.step("create fake refresh token"):
|
||||
token=fake_create_refresh_token(fake_token_data)
|
||||
token=await fake_create_refresh_token(fake_token_data)
|
||||
|
||||
with allure.step("test refresh token with fake data"), pytest.raises(expected_exception):
|
||||
current_user_service.refresh_token(token, fake_request)
|
||||
await current_user_service.refresh_token(token, fake_request)
|
||||
+24
-24
@@ -15,10 +15,10 @@ class TestJwt:
|
||||
@pytest.mark.parametrize("data", [
|
||||
pytest.param({"sub": "123"}, id="full_sub")
|
||||
])
|
||||
def test_access_create_positive(self, jwt_service:JwtService, data:dict)->None:
|
||||
async def test_access_create_positive(self, jwt_service:JwtService, data:dict)->None:
|
||||
|
||||
with allure.step("create correct access token"):
|
||||
token = jwt_service.create_access_token(data)
|
||||
token = await jwt_service.create_access_token(data)
|
||||
parts=token.split(".")
|
||||
assert isinstance(token, str)
|
||||
assert len(parts)==3
|
||||
@@ -30,19 +30,19 @@ class TestJwt:
|
||||
pytest.param({"sub":""},HTTPException, id="empty_value"),
|
||||
pytest.param({"":""},HTTPException, id="empty_key_value")
|
||||
])
|
||||
def test_access_create_negative(self, jwt_service:JwtService, data:dict, expected_exception)->None:
|
||||
async def test_access_create_negative(self, jwt_service:JwtService, data:dict, expected_exception)->None:
|
||||
with allure.step("create invalid access token"),pytest.raises(expected_exception):
|
||||
jwt_service.create_access_token(data)
|
||||
await jwt_service.create_access_token(data)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.parametrize("data", [
|
||||
pytest.param({"sub": "123"}, id="full_sub")
|
||||
])
|
||||
def test_refresh_create_positive(self, jwt_service:JwtService, data:dict)->None:
|
||||
async def test_refresh_create_positive(self, jwt_service:JwtService, data:dict)->None:
|
||||
|
||||
with allure.step("create correct access token"):
|
||||
token = jwt_service.create_refresh_token(data)
|
||||
token = await jwt_service.create_refresh_token(data)
|
||||
parts=token[0].split(".")
|
||||
assert isinstance(token[0], str)
|
||||
assert isinstance(token[1], str)
|
||||
@@ -54,24 +54,24 @@ class TestJwt:
|
||||
pytest.param({"sub":""},HTTPException, id="empty_value"),
|
||||
pytest.param({"":""},HTTPException, id="empty_key_value")
|
||||
])
|
||||
def test_refresh_create_negative(self, jwt_service:JwtService, data, expected_exception)->None:
|
||||
async def test_refresh_create_negative(self, jwt_service:JwtService, data, expected_exception)->None:
|
||||
with allure.step("create invalid access token"), pytest.raises(expected_exception):
|
||||
jwt_service.create_refresh_token(data)
|
||||
await jwt_service.create_refresh_token(data)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("data", [
|
||||
pytest.param({"sub": "123", "exp":datetime.now(UTC)+timedelta(minutes=15), "token_type":"access"}, id="correct_data"),
|
||||
])
|
||||
def test_jwt_decode_positive(self, data:dict, monkeypatch, jwt_service:JwtService)->None:
|
||||
async def test_jwt_decode_positive(self, data:dict, monkeypatch, jwt_service:JwtService)->None:
|
||||
|
||||
with allure.step("patch a create token function"):
|
||||
def fake_create_access_token(data:dict)->str:
|
||||
async def fake_create_access_token(data:dict)->str:
|
||||
return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||
monkeypatch.setattr(jwt_service, "create_access_token", fake_create_access_token)
|
||||
|
||||
with allure.step("create and decode correct token"):
|
||||
fake_token = jwt_service.create_access_token(data)
|
||||
payload=jwt_service.jwt_decode(fake_token)
|
||||
fake_token = await jwt_service.create_access_token(data)
|
||||
payload=await jwt_service.jwt_decode(fake_token)
|
||||
assert payload.get("sub")
|
||||
assert payload.get("exp")
|
||||
assert payload.get("token_type")
|
||||
@@ -83,15 +83,15 @@ class TestJwt:
|
||||
pytest.param({}, HTTPException, id="empty_data"),
|
||||
pytest.param("", AttributeError, id="not_dict_data")
|
||||
])
|
||||
def test_jwt_decode_invalid(self,jwt_service:JwtService, expected_exception, data, monkeypatch)->None:
|
||||
async def test_jwt_decode_invalid(self,jwt_service:JwtService, expected_exception, data, monkeypatch)->None:
|
||||
with allure.step("patch a create token function"):
|
||||
def fake_create_access_token(data:dict)->str:
|
||||
async def fake_create_access_token(data:dict)->str:
|
||||
return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||
monkeypatch.setattr(jwt_service, "create_access_token", fake_create_access_token)
|
||||
|
||||
with allure.step("create and decode invalid token"), pytest.raises(expected_exception):
|
||||
fake_token=jwt_service.create_access_token(data)
|
||||
jwt_service.jwt_decode(fake_token)
|
||||
fake_token=await jwt_service.create_access_token(data)
|
||||
await jwt_service.jwt_decode(fake_token)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("time, key, algorithm", [
|
||||
@@ -99,23 +99,23 @@ class TestJwt:
|
||||
pytest.param(-15, "correct_key", "HS256", id="wrong_time"),
|
||||
pytest.param(15, "correct_key", "HS512", id="wrong_algorithm"),
|
||||
])
|
||||
def test_jwt_decode_wrong_env(self, monkeypatch, time:int, key:str, algorithm:str, jwt_service)->None:
|
||||
async def test_jwt_decode_wrong_env(self, monkeypatch, time:int, key:str, algorithm:str, jwt_service)->None:
|
||||
|
||||
with allure.step("patch a create token function"):
|
||||
def fake_create_access_token(data:dict, key:str, algorithm:str)->str:
|
||||
async def fake_create_access_token(data:dict, key:str, algorithm:str)->str:
|
||||
data.update({"exp":datetime.now(UTC)+timedelta(minutes=time)})
|
||||
return jwt.encode(data, key, algorithm)
|
||||
monkeypatch.setattr(jwt_service, "create_access_token", fake_create_access_token)
|
||||
|
||||
with allure.step("create and decode token with wrong data inside"), pytest.raises(HTTPException):
|
||||
fake_token=jwt_service.create_access_token({"sub": "123", "token_type":"access"}, key, algorithm)
|
||||
jwt_service.jwt_decode(fake_token)
|
||||
fake_token=await jwt_service.create_access_token({"sub": "123", "token_type":"access"}, key, algorithm)
|
||||
await jwt_service.jwt_decode(fake_token)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("password",[
|
||||
pytest.param("plain_password", id="correct_plain_password")
|
||||
])
|
||||
def test_hash_and_veryfy_positive(self, password:str, hash_service:HashService)->None:
|
||||
async def test_hash_and_veryfy_positive(self, password:str, hash_service:HashService)->None:
|
||||
|
||||
with allure.step("encode password"):
|
||||
encoded_password=hash_service.plain_to_hash(password)
|
||||
@@ -127,7 +127,7 @@ class TestJwt:
|
||||
|
||||
|
||||
|
||||
def test_verify_wrong_password(self, hash_service:HashService)->None:
|
||||
async def test_verify_wrong_password(self, hash_service:HashService)->None:
|
||||
|
||||
with allure.step("encode password"):
|
||||
encoded_password=hash_service.plain_to_hash("plain_password")
|
||||
@@ -136,9 +136,9 @@ class TestJwt:
|
||||
assert hash_service.verify_password("wrong_password", encoded_password) is False
|
||||
|
||||
|
||||
def test_token_to_hash_determistic(self, hash_service:HashService)->None:
|
||||
async def test_token_to_hash_determistic(self, hash_service:HashService)->None:
|
||||
assert hash_service.token_to_hash("abc")==hash_service.token_to_hash("abc")
|
||||
|
||||
def test_token_to_hash_different_input(self, hash_service:HashService)->None:
|
||||
async def test_token_to_hash_different_input(self, hash_service:HashService)->None:
|
||||
assert hash_service.token_to_hash("abc")!=hash_service.token_to_hash("xyz")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user