338 lines
11 KiB
Python
338 lines
11 KiB
Python
"""
|
|
Тесты для покрытия модуля auth
|
|
"""
|
|
import pytest
|
|
from unittest.mock import Mock, patch, MagicMock, AsyncMock
|
|
from datetime import datetime, timedelta
|
|
|
|
# Импортируем модули auth для покрытия
|
|
import auth.__init__
|
|
import auth.permissions
|
|
import auth.decorators
|
|
import auth.oauth
|
|
import auth.state
|
|
import auth.middleware
|
|
import auth.identity
|
|
import auth.jwtcodec
|
|
import auth.email
|
|
import auth.exceptions
|
|
import auth.validations
|
|
import auth.orm
|
|
import auth.credentials
|
|
import auth.handler
|
|
import auth.internal
|
|
|
|
|
|
class TestAuthInit:
|
|
"""Тесты для auth.__init__"""
|
|
|
|
def test_auth_init_import(self):
|
|
"""Тест импорта auth"""
|
|
import auth
|
|
assert auth is not None
|
|
|
|
def test_auth_functions_exist(self):
|
|
"""Тест существования основных функций auth"""
|
|
from auth import logout, refresh_token
|
|
assert logout is not None
|
|
assert refresh_token is not None
|
|
|
|
|
|
class TestAuthPermissions:
|
|
"""Тесты для auth.permissions"""
|
|
|
|
def test_permissions_import(self):
|
|
"""Тест импорта permissions"""
|
|
import auth.permissions
|
|
assert auth.permissions is not None
|
|
|
|
def test_permissions_functions_exist(self):
|
|
"""Тест существования функций permissions"""
|
|
import auth.permissions
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.permissions is not None
|
|
|
|
|
|
class TestAuthDecorators:
|
|
"""Тесты для auth.decorators"""
|
|
|
|
def test_decorators_import(self):
|
|
"""Тест импорта decorators"""
|
|
import auth.decorators
|
|
assert auth.decorators is not None
|
|
|
|
def test_decorators_functions_exist(self):
|
|
"""Тест существования функций decorators"""
|
|
import auth.decorators
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.decorators is not None
|
|
|
|
|
|
class TestAuthOAuth:
|
|
"""Тесты для auth.oauth"""
|
|
|
|
def test_oauth_import(self):
|
|
"""Тест импорта oauth"""
|
|
import auth.oauth
|
|
assert auth.oauth is not None
|
|
|
|
def test_oauth_functions_exist(self):
|
|
"""Тест существования функций oauth"""
|
|
import auth.oauth
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.oauth is not None
|
|
|
|
|
|
class TestAuthState:
|
|
"""Тесты для auth.state"""
|
|
|
|
def test_state_import(self):
|
|
"""Тест импорта state"""
|
|
import auth.state
|
|
assert auth.state is not None
|
|
|
|
def test_state_functions_exist(self):
|
|
"""Тест существования функций state"""
|
|
import auth.state
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.state is not None
|
|
|
|
|
|
class TestAuthMiddleware:
|
|
"""Тесты для auth.middleware"""
|
|
|
|
def test_middleware_import(self):
|
|
"""Тест импорта middleware"""
|
|
import auth.middleware
|
|
assert auth.middleware is not None
|
|
|
|
def test_middleware_functions_exist(self):
|
|
"""Тест существования функций middleware"""
|
|
import auth.middleware
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.middleware is not None
|
|
|
|
|
|
class TestAuthIdentity:
|
|
"""Тесты для auth.identity"""
|
|
|
|
def test_identity_import(self):
|
|
"""Тест импорта identity"""
|
|
import auth.identity
|
|
assert auth.identity is not None
|
|
|
|
def test_identity_functions_exist(self):
|
|
"""Тест существования функций identity"""
|
|
import auth.identity
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.identity is not None
|
|
|
|
|
|
class TestAuthJWTCodec:
|
|
"""Тесты для auth.jwtcodec"""
|
|
|
|
def test_jwtcodec_import(self):
|
|
"""Тест импорта jwtcodec"""
|
|
import auth.jwtcodec
|
|
assert auth.jwtcodec is not None
|
|
|
|
def test_jwtcodec_functions_exist(self):
|
|
"""Тест существования функций jwtcodec"""
|
|
import auth.jwtcodec
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.jwtcodec is not None
|
|
|
|
|
|
class TestAuthEmail:
|
|
"""Тесты для auth.email"""
|
|
|
|
def test_email_import(self):
|
|
"""Тест импорта email"""
|
|
import auth.email
|
|
assert auth.email is not None
|
|
|
|
def test_email_functions_exist(self):
|
|
"""Тест существования функций email"""
|
|
import auth.email
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.email is not None
|
|
|
|
|
|
class TestAuthExceptions:
|
|
"""Тесты для auth.exceptions"""
|
|
|
|
def test_exceptions_import(self):
|
|
"""Тест импорта exceptions"""
|
|
import auth.exceptions
|
|
assert auth.exceptions is not None
|
|
|
|
def test_exceptions_classes_exist(self):
|
|
"""Тест существования классов exceptions"""
|
|
import auth.exceptions
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.exceptions is not None
|
|
|
|
|
|
class TestAuthValidations:
|
|
"""Тесты для auth.validations"""
|
|
|
|
def test_validations_import(self):
|
|
"""Тест импорта validations"""
|
|
import auth.validations
|
|
assert auth.validations is not None
|
|
|
|
def test_validations_functions_exist(self):
|
|
"""Тест существования функций validations"""
|
|
import auth.validations
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.validations is not None
|
|
|
|
|
|
class TestAuthORM:
|
|
"""Тесты для auth.orm"""
|
|
|
|
def test_orm_import(self):
|
|
"""Тест импорта orm"""
|
|
from auth.orm import Author
|
|
assert Author is not None
|
|
|
|
def test_orm_functions_exist(self):
|
|
"""Тест существования функций orm"""
|
|
from auth.orm import Author
|
|
# Проверяем что модель Author существует
|
|
assert Author is not None
|
|
assert hasattr(Author, 'id')
|
|
assert hasattr(Author, 'email')
|
|
assert hasattr(Author, 'name')
|
|
assert hasattr(Author, 'slug')
|
|
|
|
|
|
class TestAuthCredentials:
|
|
"""Тесты для auth.credentials"""
|
|
|
|
def test_credentials_import(self):
|
|
"""Тест импорта credentials"""
|
|
import auth.credentials
|
|
assert auth.credentials is not None
|
|
|
|
def test_credentials_functions_exist(self):
|
|
"""Тест существования функций credentials"""
|
|
import auth.credentials
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.credentials is not None
|
|
|
|
|
|
class TestAuthHandler:
|
|
"""Тесты для auth.handler"""
|
|
|
|
def test_handler_import(self):
|
|
"""Тест импорта handler"""
|
|
import auth.handler
|
|
assert auth.handler is not None
|
|
|
|
def test_handler_functions_exist(self):
|
|
"""Тест существования функций handler"""
|
|
import auth.handler
|
|
# Проверяем что модуль импортируется без ошибок
|
|
assert auth.handler is not None
|
|
|
|
|
|
class TestAuthInternal:
|
|
"""Тесты для auth.internal"""
|
|
|
|
def test_internal_import(self):
|
|
"""Тест импорта internal"""
|
|
from auth.internal import verify_internal_auth
|
|
assert verify_internal_auth is not None
|
|
|
|
def test_internal_functions_exist(self):
|
|
"""Тест существования функций internal"""
|
|
from auth.internal import verify_internal_auth
|
|
assert verify_internal_auth is not None
|
|
|
|
|
|
class TestAuthTokens:
|
|
"""Тесты для auth.tokens"""
|
|
|
|
def test_tokens_import(self):
|
|
"""Тест импорта tokens"""
|
|
from auth.tokens.storage import TokenStorage
|
|
assert TokenStorage is not None
|
|
|
|
def test_tokens_functions_exist(self):
|
|
"""Тест существования функций tokens"""
|
|
from auth.tokens.storage import TokenStorage
|
|
assert TokenStorage is not None
|
|
assert hasattr(TokenStorage, 'revoke_session')
|
|
assert hasattr(TokenStorage, 'refresh_session')
|
|
|
|
|
|
class TestAuthCommon:
|
|
"""Тесты общих функций auth"""
|
|
|
|
def test_auth_config(self):
|
|
"""Тест конфигурации auth"""
|
|
from settings import (
|
|
SESSION_COOKIE_HTTPONLY,
|
|
SESSION_COOKIE_MAX_AGE,
|
|
SESSION_COOKIE_NAME,
|
|
SESSION_COOKIE_SAMESITE,
|
|
SESSION_COOKIE_SECURE,
|
|
SESSION_TOKEN_HEADER,
|
|
)
|
|
assert all([
|
|
SESSION_COOKIE_HTTPONLY,
|
|
SESSION_COOKIE_MAX_AGE,
|
|
SESSION_COOKIE_NAME,
|
|
SESSION_COOKIE_SAMESITE,
|
|
SESSION_COOKIE_SECURE,
|
|
SESSION_TOKEN_HEADER,
|
|
])
|
|
|
|
def test_auth_utils(self):
|
|
"""Тест утилит auth"""
|
|
from utils.logger import root_logger
|
|
assert root_logger is not None
|
|
|
|
|
|
class TestAuthIntegration:
|
|
"""Интеграционные тесты auth"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_logout_function(self):
|
|
"""Тест функции logout"""
|
|
from auth import logout
|
|
from starlette.requests import Request
|
|
from starlette.responses import Response
|
|
|
|
# Создаем мок запроса
|
|
mock_request = Mock(spec=Request)
|
|
mock_request.cookies = {}
|
|
mock_request.headers = {}
|
|
mock_request.client = None
|
|
|
|
# Патчим зависимости
|
|
with patch('auth.verify_internal_auth', return_value=(None, None, None)):
|
|
with patch('auth.TokenStorage.revoke_session'):
|
|
result = await logout(mock_request)
|
|
assert isinstance(result, Response)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_refresh_token_function(self):
|
|
"""Тест функции refresh_token"""
|
|
from auth import refresh_token
|
|
from starlette.requests import Request
|
|
from starlette.responses import JSONResponse
|
|
|
|
# Создаем мок запроса
|
|
mock_request = Mock(spec=Request)
|
|
mock_request.cookies = {}
|
|
mock_request.headers = {}
|
|
mock_request.client = None
|
|
|
|
# Патчим зависимости
|
|
with patch('auth.verify_internal_auth', return_value=(None, None, None)):
|
|
result = await refresh_token(mock_request)
|
|
assert isinstance(result, JSONResponse)
|
|
assert result.status_code == 401
|