hash tests
This commit is contained in:
4
makefile
4
makefile
@@ -3,7 +3,7 @@ ALLURE=.venv/allure-2.44.0/bin/allure #linux&macos
|
|||||||
#ALLURE=.venv\allure-2.44.0\bin\allure #Windows
|
#ALLURE=.venv\allure-2.44.0\bin\allure #Windows
|
||||||
|
|
||||||
.PHONY:
|
.PHONY:
|
||||||
run, m_gen, m_up, allure, coverage
|
run, m_gen, m_up, allure, coverage, pytest
|
||||||
|
|
||||||
run:
|
run:
|
||||||
${VENV} python3 main.py
|
${VENV} python3 main.py
|
||||||
@@ -11,6 +11,8 @@ m_gen:
|
|||||||
${VENV} alembic revision --autogenerate
|
${VENV} alembic revision --autogenerate
|
||||||
m_up:
|
m_up:
|
||||||
${VENV} alembic upgrade head
|
${VENV} alembic upgrade head
|
||||||
|
pytest:
|
||||||
|
${VENV} pytest
|
||||||
allure:
|
allure:
|
||||||
${VENV} ${ALLURE} generate tests/allure-results/reports --single-file -o tests/allure-results/html --clean
|
${VENV} ${ALLURE} generate tests/allure-results/reports --single-file -o tests/allure-results/html --clean
|
||||||
coverage:
|
coverage:
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
[pytest]
|
[pytest]
|
||||||
addopts =
|
addopts =
|
||||||
-l
|
-l
|
||||||
|
-v
|
||||||
--alluredir=tests/allure-results/reports/
|
--alluredir=tests/allure-results/reports/
|
||||||
testpaths =
|
testpaths =
|
||||||
tests
|
tests
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from service.auth.auth import CurrentUser
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def current_user_service()->CurrentUser:
|
||||||
|
current_user_service=CurrentUser()
|
||||||
|
return current_user_service
|
||||||
@@ -1,9 +1,20 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from src.service.auth.jwt import Jwt
|
from service.auth.auth import CurrentUser
|
||||||
|
from src.service.auth.jwt import Hashes, Jwt
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def jwt_service():
|
def jwt_service()->Jwt:
|
||||||
jwt_service=Jwt()
|
jwt_service=Jwt()
|
||||||
return jwt_service
|
return jwt_service
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def hash_service()->Hashes:
|
||||||
|
hash_service=Hashes()
|
||||||
|
return hash_service
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def current_user_service()->CurrentUser:
|
||||||
|
current_user_service=CurrentUser()
|
||||||
|
return current_user_service
|
||||||
@@ -5,18 +5,20 @@ import pytest
|
|||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from jose import jwt
|
from jose import jwt
|
||||||
|
|
||||||
|
from service.auth.auth import CurrentUser
|
||||||
from src.models.configs_read.env import env_settings
|
from src.models.configs_read.env import env_settings
|
||||||
|
from src.service.auth.jwt import Hashes, Jwt
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
class TestAuth:
|
class TestJwt:
|
||||||
|
|
||||||
@pytest.mark.parametrize("data", [
|
@pytest.mark.parametrize("data", [
|
||||||
({"sub": "123"}),
|
({"sub": "123"}),
|
||||||
({"sub":""}),
|
({"sub":""}),
|
||||||
({"":""})
|
({"":""})
|
||||||
])
|
])
|
||||||
def test_access_create_positive(self, jwt_service, data:dict):
|
def test_access_create_positive(self, jwt_service:Jwt, data:dict)->None:
|
||||||
|
|
||||||
with allure.step("create correct access token"):
|
with allure.step("create correct access token"):
|
||||||
token = jwt_service.create_access_token(data)
|
token = jwt_service.create_access_token(data)
|
||||||
@@ -29,7 +31,7 @@ class TestAuth:
|
|||||||
@pytest.mark.parametrize("data",[
|
@pytest.mark.parametrize("data",[
|
||||||
("")
|
("")
|
||||||
])
|
])
|
||||||
def test_access_create_negative(self, jwt_service, data:dict):
|
def test_access_create_negative(self, jwt_service:Jwt, data:dict)->None:
|
||||||
with allure.step("create invalid access token"),pytest.raises(AttributeError):
|
with allure.step("create invalid access token"),pytest.raises(AttributeError):
|
||||||
jwt_service.create_access_token(data)
|
jwt_service.create_access_token(data)
|
||||||
|
|
||||||
@@ -40,7 +42,7 @@ class TestAuth:
|
|||||||
({"sub":""}),
|
({"sub":""}),
|
||||||
({"":""})
|
({"":""})
|
||||||
])
|
])
|
||||||
def test_refresh_create_positive(self, jwt_service, data:dict):
|
def test_refresh_create_positive(self, jwt_service:Jwt, data:dict)->None:
|
||||||
|
|
||||||
with allure.step("create correct access token"):
|
with allure.step("create correct access token"):
|
||||||
token = jwt_service.create_refresh_token(data)
|
token = jwt_service.create_refresh_token(data)
|
||||||
@@ -53,7 +55,7 @@ class TestAuth:
|
|||||||
@pytest.mark.parametrize("data",[
|
@pytest.mark.parametrize("data",[
|
||||||
("")
|
("")
|
||||||
])
|
])
|
||||||
def test_refresh_create_negative(self, jwt_service, data):
|
def test_refresh_create_negative(self, jwt_service:Jwt, data)->None:
|
||||||
with allure.step("create invalid access token"), pytest.raises(AttributeError):
|
with allure.step("create invalid access token"), pytest.raises(AttributeError):
|
||||||
jwt_service.create_refresh_token(data)
|
jwt_service.create_refresh_token(data)
|
||||||
|
|
||||||
@@ -61,7 +63,7 @@ class TestAuth:
|
|||||||
@pytest.mark.parametrize("data", [
|
@pytest.mark.parametrize("data", [
|
||||||
({"sub": "123", "exp":datetime.now(UTC)+timedelta(minutes=15), "token_type":"access"}),
|
({"sub": "123", "exp":datetime.now(UTC)+timedelta(minutes=15), "token_type":"access"}),
|
||||||
])
|
])
|
||||||
def test_jwt_decode_positive(self, data, monkeypatch, jwt_service):
|
def test_jwt_decode_positive(self, data:dict, monkeypatch, jwt_service:Jwt)->None:
|
||||||
|
|
||||||
with allure.step("patch a create token function"):
|
with allure.step("patch a create token function"):
|
||||||
def fake_create_access_token(data:dict)->str:
|
def fake_create_access_token(data:dict)->str:
|
||||||
@@ -82,7 +84,7 @@ class TestAuth:
|
|||||||
({}, HTTPException),
|
({}, HTTPException),
|
||||||
("", AttributeError)
|
("", AttributeError)
|
||||||
])
|
])
|
||||||
def test_jwt_decode_invalid(self,jwt_service, expected_exception, data, monkeypatch):
|
def test_jwt_decode_invalid(self,jwt_service:Jwt, expected_exception, data, monkeypatch)->None:
|
||||||
with allure.step("patch a create token function"):
|
with allure.step("patch a create token function"):
|
||||||
def fake_create_access_token(data:dict)->str:
|
def fake_create_access_token(data:dict)->str:
|
||||||
return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||||
@@ -98,7 +100,7 @@ class TestAuth:
|
|||||||
(-15, "correct_key", "HS256"),
|
(-15, "correct_key", "HS256"),
|
||||||
(15, "correct_key", "HS512"),
|
(15, "correct_key", "HS512"),
|
||||||
])
|
])
|
||||||
def test_jwt_decode_wrong_env(self, monkeypatch, time:int, key:str, algorithm:str, jwt_service):
|
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"):
|
with allure.step("patch a create token function"):
|
||||||
def fake_create_access_token(data:dict, key:str, algorithm:str)->str:
|
def fake_create_access_token(data:dict, key:str, algorithm:str)->str:
|
||||||
@@ -111,8 +113,33 @@ class TestAuth:
|
|||||||
jwt_service.jwt_decode(fake_token)
|
jwt_service.jwt_decode(fake_token)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("password",[
|
||||||
|
("plain_password")
|
||||||
|
])
|
||||||
|
def test_hash_and_veryfy_positive(self, password:str, hash_service:Hashes)->None:
|
||||||
|
|
||||||
|
with allure.step("encode password"):
|
||||||
|
encoded_password=hash_service.plain_to_hash(password)
|
||||||
|
assert isinstance(encoded_password, str)
|
||||||
|
assert encoded_password!=password
|
||||||
|
|
||||||
|
with allure.step("decode password"):
|
||||||
|
assert hash_service.verify_password(password, encoded_password) is True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_verify_wrong_password(self, hash_service:Hashes)->None:
|
||||||
|
|
||||||
|
with allure.step("encode password"):
|
||||||
|
encoded_password=hash_service.plain_to_hash("plain_password")
|
||||||
|
|
||||||
|
with allure.step("decode password"):
|
||||||
|
assert hash_service.verify_password("wrong_password", encoded_password) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_token_to_hash_determistic(self, hash_service:Hashes)->None:
|
||||||
|
assert hash_service.token_to_hash("abc")==hash_service.token_to_hash("abc")
|
||||||
|
|
||||||
|
def test_token_to_hash_different_input(self, hash_service:Hashes)->None:
|
||||||
|
assert hash_service.token_to_hash("abc")!=hash_service.token_to_hash("xyz")
|
||||||
|
|
||||||
Reference in New Issue
Block a user