From f81ba19da447542defc4e450bab168e3db4ce53b Mon Sep 17 00:00:00 2001 From: "MH.Dmitrii" Date: Tue, 28 Jul 2026 13:31:58 +0300 Subject: [PATCH] hash tests --- .coverage | Bin 0 -> 53248 bytes makefile | 4 +- pytest.ini | 1 + tests/integrated/conftest.py | 9 +++++ tests/unit/conftest.py | 17 +++++++-- tests/unit/{test_auth.py => test_jwt.py} | 45 ++++++++++++++++++----- 6 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 .coverage rename tests/unit/{test_auth.py => test_jwt.py} (69%) diff --git a/.coverage b/.coverage new file mode 100644 index 0000000000000000000000000000000000000000..e91ec91bd61dba6e6bb2c3769ec74b141f5ad15c GIT binary patch literal 53248 zcmeI4-;d)~702z^Nyhmxp54_dT1I6pRjb)(CKCceu%HT-?Mke=W#`v!Y5C!GoSTey z5<6=<^Wy;_mr4Z*9(hBQ2Y3PrsSkaie?Src0F^2gUaIy1ilDm^0v(tcI4!M zKE>4jtX)M|{rR7&>K&m{__m;azxo-qQR!9wS^h=k%gXJtRa{Y4=>#?q009sHfm1^u zSg#ao7cPh||G+oeedfD{$=v9@dTrzS)vfE=*41y^yskxM+U1f)Tce>})m&#!8!}hx zT79NjcE>Ue-?Dp}zs-``Bae0XoJD0cYw?P!VSB!(TODfUvmS{Uy4JvO540V2uo6~? zd-K`89~F=gX7y}dL%SR|uWK&rGMCvV^CB%STb+8T{-Yn3inVj+#8+8pli`~5SDvtj z3dW|~=#n7QaXVc5wrkktHuF|A~Qc`v~kOo&4qW0TWitakT<7erkTJnTpnne6IUcnpQyKSWl0CE z-FMoPz7~3n$0$H37bMlXBirPm;&B^HXP3D~kFCaMqqOj(9f@40r4(w{FV1qELl@9n zhr6ZVLZMh&TohkcLN|&>EB+`>xKJ{ZQ_^U1YQPL{Qod08 z>iJoUa~1R!e=oeUkPB7*u@urDP}1iwX+mXQGFJIHcF@>0`jkzEhUHSoQVx!00F77d z7*yG4J0m~zUw#<6C!c%9`bGlA#|e9?RS&CEc&|C0XVJ9Er!DU*NvXE9ZFoEjc}rT` zar(@#qq%`v-0-MR%hN&~j?YS88_VzxxQyQJQm9ihx$rc0ltm~YaVB!@=U)R&}3`3@gqy~W@8DtV4y z$^{q2Vr_X@3`TJTN6AU|=t^KUqP{~P8smZ^Ed~QLM7B9sA1MsEQ))!Yl zUm~k=>IZ`Qf%@=MX&{sa0T2KI5C8!X009sH0T2KI5C8!XI3!S!o)VL<0dgg2QHj3= z;P?N}Dt{2v->P@i-2AWS*QyVyzpOr2`Ka<*qNeAXO_vQFm(J?CWm~?k(=)egXGrL!EJC}+4ogeyERe+Kvq&5`9oF}n{2AP~ z;W0fvHcZnQ*}h@>q_?h8FV{|>m#{PHpgm6=+&F;_Qt_ z;8vMBcsdKEJ=RWp&qj#^*0TugIOYy>(=zWUB(s%8=D_N?^qVyFlM`vV>vJS`=`qP= z_Ac?UlanRo`hw8gJxDcBTP~WvAleyE4gq zCX2qzbsfr*+rB^4j_SF=o$n- z00ck)1V8`;KtK?-HDbl zznod)&wfB*=900@8p2!H?xfB*=900^8y0+Li!`2GKa`j(*nTm6Un zzWT2EXF9+J0w4eaAOHd&00JNY0w4eaAOHd&@JS>vUyw!RvpM^nH(q@B;3se7^LbfO z=5s}viWds6|Gps6&U`*s%<%&GUzZ9ZFOYKl{(nJzTTuU^zODX8{Yd?r`raqeKCurrentUser: + current_user_service=CurrentUser() + return current_user_service \ No newline at end of file diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index b4b5651..f13a959 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -1,9 +1,20 @@ 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 -def jwt_service(): +def jwt_service()->Jwt: jwt_service=Jwt() - return jwt_service \ No newline at end of file + 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 \ No newline at end of file diff --git a/tests/unit/test_auth.py b/tests/unit/test_jwt.py similarity index 69% rename from tests/unit/test_auth.py rename to tests/unit/test_jwt.py index 912b90a..92b33d4 100644 --- a/tests/unit/test_auth.py +++ b/tests/unit/test_jwt.py @@ -5,18 +5,20 @@ import pytest from fastapi import HTTPException from jose import jwt +from service.auth.auth import CurrentUser from src.models.configs_read.env import env_settings +from src.service.auth.jwt import Hashes, Jwt @pytest.mark.unit -class TestAuth: +class TestJwt: @pytest.mark.parametrize("data", [ ({"sub": "123"}), ({"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"): token = jwt_service.create_access_token(data) @@ -29,7 +31,7 @@ class TestAuth: @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): jwt_service.create_access_token(data) @@ -40,7 +42,7 @@ class TestAuth: ({"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"): token = jwt_service.create_refresh_token(data) @@ -53,7 +55,7 @@ class TestAuth: @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): jwt_service.create_refresh_token(data) @@ -61,7 +63,7 @@ class TestAuth: @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): + def test_jwt_decode_positive(self, data:dict, monkeypatch, jwt_service:Jwt)->None: with allure.step("patch a create token function"): def fake_create_access_token(data:dict)->str: @@ -82,7 +84,7 @@ class TestAuth: ({}, HTTPException), ("", 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"): def fake_create_access_token(data:dict)->str: return jwt.encode(data, env_settings.SECRET_KEY, env_settings.ALGORITHM) @@ -98,7 +100,7 @@ class TestAuth: (-15, "correct_key", "HS256"), (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"): def fake_create_access_token(data:dict, key:str, algorithm:str)->str: @@ -111,8 +113,33 @@ class TestAuth: 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 - \ No newline at end of file + 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") +