auth unit tests & ruff visual fix

This commit is contained in:
2026-07-28 01:42:29 +03:00
parent 1d25b0edc3
commit 5522447b07
22 changed files with 411 additions and 54 deletions

View File

@@ -0,0 +1,7 @@
import pytest
from src.service.auth.jwt import Jwt
@pytest.fixture
def jwt_service():
jwt_service=Jwt()
return jwt_service

118
tests/unit/test_auth.py Normal file
View File

@@ -0,0 +1,118 @@
from datetime import UTC, datetime, timedelta
import allure
import pytest
from fastapi import HTTPException
from jose import jwt
from src.models.configs_read.env import env_settings
@pytest.mark.unit
class TestAuth:
@pytest.mark.parametrize("data", [
({"sub": "123"}),
({"sub":""}),
({"":""})
])
def test_access_create_positive(self, jwt_service, data:dict):
with allure.step("create correct access token"):
token = jwt_service.create_access_token(data)
parts=token.split(".")
assert isinstance(token, str)
assert len(parts)==3
@pytest.mark.parametrize("data",[
("")
])
def test_access_create_negative(self, jwt_service, data:dict):
with allure.step("create invalid access token"),pytest.raises(AttributeError):
jwt_service.create_access_token(data)
@pytest.mark.parametrize("data", [
({"sub": "123"}),
({"sub":""}),
({"":""})
])
def test_refresh_create_positive(self, jwt_service, data:dict):
with allure.step("create correct access token"):
token = jwt_service.create_refresh_token(data)
parts=token[0].split(".")
assert isinstance(token[0], str)
assert isinstance(token[1], str)
assert len(parts)==3
@pytest.mark.parametrize("data",[
("")
])
def test_refresh_create_negative(self, jwt_service, data):
with allure.step("create invalid access token"), pytest.raises(AttributeError):
jwt_service.create_refresh_token(data)
@pytest.mark.parametrize("data", [
({"sub": "123", "exp":datetime.now(UTC)+timedelta(minutes=15), "token_type":"access"}),
])
def test_jwt_decode_positive(self, data, monkeypatch, jwt_service):
with allure.step("patch a create token function"):
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)
assert payload.get("sub")
assert payload.get("exp")
assert payload.get("token_type")
@pytest.mark.parametrize("data, expected_exception", [
({"sub": "123", "exp":datetime.now(UTC)-timedelta(minutes=15), "token_type":"access"}, HTTPException),
({"sub": "123", "exp":datetime.now(UTC)+timedelta(minutes=15)}, HTTPException),
({"sub": "123", "token_type":"access"}, HTTPException),
({}, HTTPException),
("", AttributeError)
])
def test_jwt_decode_invalid(self,jwt_service, expected_exception, data, monkeypatch):
with allure.step("patch a create token function"):
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)
@pytest.mark.parametrize("time, key, algorithm", [
(15, "wrong_key", "HS256"),
(-15, "correct_key", "HS256"),
(15, "correct_key", "HS512"),
])
def test_jwt_decode_wrong_env(self, monkeypatch, time:int, key:str, algorithm:str, jwt_service):
with allure.step("patch a create token function"):
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)