Advertisement
SportyScripter

DLD_unit_test/utils

Mar 27th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. import pytest
  2. from datetime import datetime, timedelta, timezone
  3. from auth import utils
  4. from unittest.mock import patch
  5.  
  6. class MockUser:
  7.     def __init__(self, is_active=True, role="user", id="123"):
  8.         self.is_active = is_active
  9.         self.role = role
  10.         self.id = id
  11.  
  12. @pytest.fixture
  13. def mock_user():
  14.     return MockUser()
  15.  
  16. @pytest.fixture
  17. def mock_inactive_user():
  18.     return MockUser(is_active=False)
  19.  
  20. # Usunąłem niepotrzebną globalną deklarację password_context
  21. def test_get_hashed_password():
  22.     test_password = "superSecret123"
  23.     hashed_password = utils.get_hashed_password(test_password)
  24.     assert utils.password_context.verify(test_password, hashed_password)
  25.  
  26. def test_verify_password():
  27.     password = "correct_password"
  28.     hashed_password = utils.get_hashed_password(password)
  29.     assert utils.verify_password(password, hashed_password)
  30.     assert not utils.verify_password("incorrect_password", hashed_password)
  31.  
  32. def test_create_access_token():
  33.     subject = "test_subject"
  34.     expires_delta = timedelta(minutes=60)  # 1 hour expiry
  35.     token = utils.create_access_token(subject, expires_delta)
  36.     decoded_token = utils.jwt.decode(token, utils.JWT_SECRET_KEY, algorithms=[utils.ALGORITHM])
  37.     assert decoded_token['sub'] == subject
  38.  
  39. def test_create_refresh_token():
  40.     subject = "test_subject"
  41.     expires_delta = timedelta(minutes=60)  # 1 hour expiry
  42.     token = utils.create_refresh_token(subject, expires_delta)
  43.     decoded_token = utils.jwt.decode(token, utils.JWT_REFRESH_SECRET_KEY, algorithms=[utils.ALGORITHM])
  44.     assert decoded_token['sub'] == subject
  45.  
  46. @pytest.mark.asyncio
  47. @patch("auth.utils.jwt.decode")
  48. @patch("auth.utils.get_user")
  49. async def test_get_current_user_active(mock_get_user, mock_jwt_decode, mock_user):
  50.     mock_jwt_decode.return_value = {"sub": mock_user.id}
  51.     mock_get_user.return_value = mock_user
  52.     result = await utils.get_current_user("valid_token")
  53.     assert result == mock_user
  54.  
  55. @pytest.mark.asyncio
  56. @patch("auth.utils.jwt.decode")
  57. @patch("auth.utils.get_user")
  58. async def test_get_current_user_inactive(mock_get_user, mock_jwt_decode, mock_inactive_user):
  59.     mock_jwt_decode.return_value = {"sub": mock_inactive_user.id}
  60.     mock_get_user.return_value = mock_inactive_user
  61.     with pytest.raises(utils.HTTPException) as exc_info:
  62.         await utils.get_current_user("invalid_token")
  63.     assert exc_info.value.status_code == 401
  64.  
  65. @pytest.mark.asyncio
  66. async def test_get_current_active_user_active(mock_user):
  67.     result = await utils.get_current_active_user(mock_user)
  68.     assert result == mock_user
  69.  
  70. @pytest.mark.asyncio
  71. async def test_get_current_active_user_inactive(mock_inactive_user):
  72.     with pytest.raises(utils.HTTPException) as exc_info:
  73.         await utils.get_current_active_user(mock_inactive_user)
  74.     assert exc_info.value.status_code == 400
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement