asyncio style refactoring

This commit is contained in:
2026-08-05 14:46:10 +03:00
parent 2da58c7483
commit 5ec033a21d
16 changed files with 280 additions and 237 deletions
+24 -24
View File
@@ -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")