Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from auth.utils import (
- get_hashed_password,
- password_context)
- import unittest
- import pytest
- import asyncio
- from unittest.mock import Mock, patch , MagicMock
- from fastapi import HTTPException, status
- from auth.utils import verify_password, get_hashed_password, create_access_token, get_user, get_current_user
- from db.session import SessionLocal
- from auth.models import User
- from sqlalchemy.orm import Session
- from jose import JWTError
- def test_get_hashed_password():
- test_password = "superSecret123"
- hashed_password = get_hashed_password(test_password)
- assert password_context.verify(test_password, hashed_password), "The hashed password does not match the original password."
- user_data = {
- "id": "123",
- "username": "testuser",
- "hashed_password": get_hashed_password("test123"),
- "is_active": True,
- "role": "user"
- }
- @pytest.fixture
- def test_user():
- return User(**user_data)
- @pytest.fixture
- def db_session_mock(test_user):
- db_session = MagicMock()
- db_session.query.return_value.filter.return_value.first.return_value = test_user
- return db_session
- @pytest.fixture
- def credentials():
- return Mock(credentials="mocked_token")
- def test_verify_password():
- password = "test123"
- hashed_password = get_hashed_password(password)
- assert verify_password(password, hashed_password) == True
- assert verify_password("wrongpassword", hashed_password) == False
- def test_create_access_token():
- token = create_access_token("user_id")
- assert isinstance(token, str) and len(token) > 0
- def test_get_user_found(db_session_mock):
- user_id = "123"
- user = get_user(user_id, db_session_mock)
- assert user.id == user_id
- def test_get_user_not_found(db_session_mock):
- db_session_mock.query.return_value.filter.return_value.first.return_value = None
- with pytest.raises(HTTPException) as excinfo:
- get_user("not_existing_id", db_session_mock)
- assert excinfo.value.status_code == 404
- @pytest.fixture
- def test_user():
- return User(
- id="123",
- username="test_username",
- hashed_password="fake_hashed_password",
- is_active=True,
- role="user"
- )
- # Testowanie get_current_user
- @pytest.mark.asyncio
- async def test_get_current_user_success(mocker, test_user):
- # Mockowanie zależności
- mock_jwt_decode = mocker.patch("jose.jwt.decode", return_value={"sub": test_user.id})
- mock_get_user = mocker.patch("auth.utils.get_user", return_value=test_user)
- # Mockowanie zależności HTTPBearer i Depends
- mock_credentials = MagicMock()
- mock_credentials.credentials = "fake_token"
- mock_db_session = MagicMock(spec=Session)
- # Wywołanie testowanej funkcji
- result_user = await get_current_user(credentials=mock_credentials, db=mock_db_session)
- # Asercje
- assert result_user == test_user
- mock_jwt_decode.assert_called_once()
- mock_get_user.assert_called_once_with(test_user.id, mock_db_session)
- # Testowanie get_current_user z błędnym tokenem
- @pytest.mark.asyncio
- @pytest.mark.asyncio
- async def test_get_current_user_invalid_token(mocker):
- mocker.patch("jose.jwt.decode", side_effect=JWTError("Token is invalid"))
- # Przygotowanie danych wejściowych
- mock_credentials = MagicMock()
- mock_credentials.credentials = "invalid_token"
- mock_db_session = MagicMock(spec=Session)
- # Oczekiwanie na wyjątek HTTPException
- with pytest.raises(HTTPException) as exc_info:
- await get_current_user(credentials=mock_credentials, db=mock_db_session)
- assert exc_info.value.status_code == 401
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement