Files
The_Great_Excel_Project/tests/integrated/test_auth.py
2026-07-29 00:44:14 +03:00

67 lines
2.7 KiB
Python

from types import SimpleNamespace
from uuid import UUID, uuid4
import allure
import pytest
from fastapi import HTTPException
from pydantic import ValidationError
from src.service.auth.auth import CurrentUser
from src.service.auth.jwt import Jwt
@pytest.mark.integra
class TestAuth:
'''
create token
decode it
mock db with fake data
check output
'''
@pytest.mark.parametrize("user_data",[
(SimpleNamespace(first_name="test",
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:
with allure.step("create token"):
token=jwt_service.create_access_token({"sub":str(uuid4())})
with allure.step("test get_current_user_with_fake_token"):
monkeypatch.setattr(current_user_service.crud_db_actions, "get_user_by_id", lambda user:user_data)
test_result= current_user_service.get_current_user(token)
assert test_result.first_name==user_data.first_name
assert test_result.last_name==user_data.last_name
assert test_result.middle_name==user_data.middle_name
assert test_result.email==user_data.email
assert test_result.direct_permissions==user_data.direct_permissions
assert test_result.group==user_data.group
@pytest.mark.parametrize("user_data, uuid, expected_exception",[
(SimpleNamespace(first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=False), uuid4(), HTTPException),
(SimpleNamespace(first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True),1234, HTTPException),
(SimpleNamespace(status=True),uuid4(), ValidationError)
])
def test_get_current_user_negative(self,current_user_service:CurrentUser, jwt_service:Jwt, monkeypatch, user_data:SimpleNamespace, expected_exception, uuid)->None:
with allure.step("create token"):
token=jwt_service.create_access_token({"sub":str(uuid)})
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)