tests logout
This commit is contained in:
20
poetry.lock
generated
20
poetry.lock
generated
@@ -1704,6 +1704,24 @@ pytest = ">=7"
|
|||||||
[package.extras]
|
[package.extras]
|
||||||
testing = ["process-tests", "pytest-xdist", "virtualenv"]
|
testing = ["process-tests", "pytest-xdist", "virtualenv"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pytest-mock"
|
||||||
|
version = "3.15.1"
|
||||||
|
description = "Thin-wrapper around the mock package for easier use with pytest"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.9"
|
||||||
|
groups = ["main"]
|
||||||
|
files = [
|
||||||
|
{file = "pytest_mock-3.15.1-py3-none-any.whl", hash = "sha256:0a25e2eb88fe5168d535041d09a4529a188176ae608a6d249ee65abc0949630d"},
|
||||||
|
{file = "pytest_mock-3.15.1.tar.gz", hash = "sha256:1849a238f6f396da19762269de72cb1814ab44416fa73a8686deac10b0d87a0f"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
pytest = ">=6.2.5"
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
dev = ["pre-commit", "pytest-asyncio", "tox"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "python-dateutil"
|
name = "python-dateutil"
|
||||||
version = "2.9.0.post0"
|
version = "2.9.0.post0"
|
||||||
@@ -2133,4 +2151,4 @@ files = [
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.1"
|
lock-version = "2.1"
|
||||||
python-versions = ">=3.13"
|
python-versions = ">=3.13"
|
||||||
content-hash = "472523567b581bc4046c5b926ff842e7ade6b230dc96649cf6899f59841d38c1"
|
content-hash = "ebbba0a60f3f121f79117d05b5a6dbd8914ca1d971776808c7d1cfd146b562bc"
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ dependencies = [
|
|||||||
"ipython (>=9.15.0,<10.0.0)",
|
"ipython (>=9.15.0,<10.0.0)",
|
||||||
"httpie (>=3.2.4,<4.0.0)",
|
"httpie (>=3.2.4,<4.0.0)",
|
||||||
"pytest-cov (>=7.1.0,<8.0.0)",
|
"pytest-cov (>=7.1.0,<8.0.0)",
|
||||||
"allure-pytest (>=2.16.0,<3.0.0)"
|
"allure-pytest (>=2.16.0,<3.0.0)",
|
||||||
|
"pytest-mock (>=3.15.1,<4.0.0)"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
from fastapi import Request
|
||||||
|
|
||||||
from src.service.auth.auth import CurrentUser
|
from src.service.auth.auth import CurrentUser
|
||||||
from src.service.auth.jwt import Jwt
|
from src.service.auth.jwt import Hashes, Jwt
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -13,3 +14,14 @@ def current_user_service()->CurrentUser:
|
|||||||
def jwt_service()->Jwt:
|
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 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
|
||||||
@@ -1,32 +1,23 @@
|
|||||||
|
from datetime import UTC, datetime, timedelta
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
from uuid import UUID, uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import allure
|
import allure
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
from jose import jwt
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
|
|
||||||
|
from src.models.configs_read.env import env_settings
|
||||||
from src.service.auth.auth import CurrentUser
|
from src.service.auth.auth import CurrentUser
|
||||||
from src.service.auth.jwt import Jwt
|
from src.service.auth.jwt import Hashes, Jwt
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.integra
|
@pytest.mark.integra
|
||||||
class TestAuth:
|
class TestAuth:
|
||||||
'''
|
|
||||||
create token
|
|
||||||
decode it
|
|
||||||
mock db with fake data
|
|
||||||
check output
|
|
||||||
'''
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("user_data",[
|
@pytest.mark.parametrize("user_data",[
|
||||||
(SimpleNamespace(first_name="test",
|
(SimpleNamespace(first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True))
|
||||||
last_name="test",
|
|
||||||
middle_name="test",
|
|
||||||
email="d@d.d",
|
|
||||||
direct_permissions=[],
|
|
||||||
group=[],
|
|
||||||
status=True))
|
|
||||||
])
|
])
|
||||||
def test_get_current_user_positive(self,current_user_service:CurrentUser, jwt_service:Jwt, monkeypatch, user_data:SimpleNamespace)->None:
|
def test_get_current_user_positive(self,current_user_service:CurrentUser, jwt_service:Jwt, monkeypatch, user_data:SimpleNamespace)->None:
|
||||||
|
|
||||||
@@ -34,10 +25,12 @@ class TestAuth:
|
|||||||
|
|
||||||
token=jwt_service.create_access_token({"sub":str(uuid4())})
|
token=jwt_service.create_access_token({"sub":str(uuid4())})
|
||||||
|
|
||||||
with allure.step("test get_current_user_with_fake_token"):
|
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", lambda user:user_data)
|
||||||
|
|
||||||
|
with allure.step("test get_current_user_with_fake_token"):
|
||||||
|
|
||||||
test_result= current_user_service.get_current_user(token)
|
test_result= current_user_service.get_current_user(token)
|
||||||
|
|
||||||
assert test_result.first_name==user_data.first_name
|
assert test_result.first_name==user_data.first_name
|
||||||
@@ -59,8 +52,97 @@ class TestAuth:
|
|||||||
|
|
||||||
token=jwt_service.create_access_token({"sub":str(uuid)})
|
token=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)
|
||||||
|
|
||||||
with allure.step("test get_current_user_with_fake_token"), pytest.raises(expected_exception):
|
with allure.step("test get_current_user_with_fake_token"), pytest.raises(expected_exception):
|
||||||
|
|
||||||
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", lambda user:user_data)
|
|
||||||
current_user_service.get_current_user(token)
|
current_user_service.get_current_user(token)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("user_data, form_data_email,form_data_password",[
|
||||||
|
(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"),
|
||||||
|
])
|
||||||
|
def test_login_positive(self, jwt_service:Jwt,current_user_service:CurrentUser, monkeypatch, user_data:SimpleNamespace, hash_service:Hashes, 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 )
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
assert isinstance(access, str)
|
||||||
|
assert isinstance(refresh, str)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("user_data, form_data_email,form_data_password, expected_exception",[
|
||||||
|
(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", "wrong_password", HTTPException),
|
||||||
|
(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),
|
||||||
|
(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),
|
||||||
|
])
|
||||||
|
def test_login_negative(self, current_user_service:CurrentUser, user_data:SimpleNamespace, jwt_service:Jwt, monkeypatch, requests, hash_service:Hashes, 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 )
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_logout_positive(self, jwt_service:Jwt, monkeypatch, current_user_service:CurrentUser)->None:
|
||||||
|
|
||||||
|
with allure.step("create fake refresh token"):
|
||||||
|
|
||||||
|
token=jwt_service.create_refresh_token({"sub":str(uuid4())})
|
||||||
|
|
||||||
|
with allure.step("patching db call functions"):
|
||||||
|
|
||||||
|
monkeypatch.setattr(current_user_service.jwt_db_actions, "get_token_by_id", lambda jti: "test")
|
||||||
|
monkeypatch.setattr(current_user_service.jwt_db_actions, "logout", lambda jti: True )
|
||||||
|
|
||||||
|
with allure.step("test logout with fake data"):
|
||||||
|
|
||||||
|
current_user_service.logout(token[0])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("jti,db_result, expected_exception",[
|
||||||
|
pytest.param(None, True, HTTPException, id="jti_none"),
|
||||||
|
pytest.param(1234, True, HTTPException, id="jti_int"),
|
||||||
|
pytest.param(str(uuid4()), None, HTTPException, id="db_result_none"),
|
||||||
|
])
|
||||||
|
def test_logout_negative(self, jwt_service:Jwt, monkeypatch, current_user_service:CurrentUser, expected_exception, jti, db_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)
|
||||||
|
monkeypatch.setattr(current_user_service.jwt_db_actions, "logout", lambda jti: True )
|
||||||
|
|
||||||
|
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":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)
|
||||||
|
|
||||||
Reference in New Issue
Block a user