Changed module name from my_auth to myauth
Changed encryption algorithm to argon2 Added unit tests
This commit is contained in:
@@ -7,11 +7,11 @@ from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from my_auth.core.password import PasswordManager
|
||||
from my_auth.core.token import TokenManager
|
||||
from src.my_auth.core.auth import AuthService
|
||||
from src.my_auth.models.user import UserCreate, UserInDB
|
||||
from src.my_auth.persistence.sqlite import SQLiteUserRepository, SQLiteTokenRepository
|
||||
from myauth.core.password import PasswordManager
|
||||
from myauth.core.token import TokenManager
|
||||
from myauth.core.auth import AuthService
|
||||
from myauth.models.user import UserCreate, UserInDB
|
||||
from myauth.persistence.sqlite import SQLiteUserRepository, SQLiteTokenRepository
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
# tests/core/test_auth_service.py
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from unittest.mock import MagicMock, patch
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from src.my_auth.core.auth import AuthService
|
||||
from src.my_auth.exceptions import UserAlreadyExistsError, InvalidCredentialsError, InvalidTokenError, \
|
||||
from myauth.core.auth import AuthService
|
||||
from myauth.exceptions import UserAlreadyExistsError, InvalidCredentialsError, InvalidTokenError, \
|
||||
ExpiredTokenError, RevokedTokenError
|
||||
from src.my_auth.models.token import TokenData, TokenPayload
|
||||
from src.my_auth.models.user import UserCreate, UserUpdate
|
||||
from myauth.models.token import TokenPayload
|
||||
from myauth.models.user import UserCreate
|
||||
|
||||
|
||||
class TestAuthServiceRegisterLogin(object):
|
||||
@@ -171,7 +171,6 @@ class TestAuthServiceTokenManagement(object):
|
||||
with pytest.raises(ExpiredTokenError):
|
||||
auth_service.get_current_user("expired_access_jwt")
|
||||
|
||||
|
||||
# class TestAuthServiceResetVerification(object):
|
||||
# """Tests for password reset and email verification flows."""
|
||||
#
|
||||
@@ -190,7 +189,7 @@ class TestAuthServiceTokenManagement(object):
|
||||
# # Restore hash mock
|
||||
# pm.hash_password.return_value = original_hash
|
||||
#
|
||||
# @patch('src.my_auth.core.email.send_email')
|
||||
# @patch('myauth.core.email.send_email')
|
||||
# def test_request_password_reset_success(self, mock_send_email: MagicMock, auth_service: AuthService):
|
||||
# """Success: Requesting a password reset generates a token and sends an email."""
|
||||
#
|
||||
@@ -226,7 +225,7 @@ class TestAuthServiceTokenManagement(object):
|
||||
# updated_user = auth_service.user_repository.get_user_by_id(self.user.id)
|
||||
# assert updated_user.hashed_password == "NEW_HASHED_PASSWORD_FOR_RESET"
|
||||
#
|
||||
# @patch('src.my_auth.core.email.send_email')
|
||||
# @patch('myauth.core.email.send_email')
|
||||
# def test_request_email_verification_success(self, mock_send_email: MagicMock, auth_service: AuthService):
|
||||
# """Success: Requesting verification generates a token and sends an email."""
|
||||
#
|
||||
|
||||
25
tests/core/test_password.py
Normal file
25
tests/core/test_password.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
from myauth.core import PasswordManager
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def password_manager():
|
||||
return PasswordManager()
|
||||
|
||||
|
||||
def test_i_can_hash_password(password_manager):
|
||||
hashed_password = password_manager.hash_password("password")
|
||||
assert hashed_password is not None
|
||||
assert hashed_password != "password"
|
||||
|
||||
|
||||
def test_i_can_verify_password(password_manager):
|
||||
password = "password"
|
||||
hashed_password = password_manager.hash_password(password)
|
||||
assert password_manager.verify_password(password, hashed_password)
|
||||
|
||||
def test_i_cannot_verify_invalid_password(password_manager):
|
||||
password = "password"
|
||||
hashed_password = password_manager.hash_password(password)
|
||||
assert not password_manager.verify_password("invalid_password", hashed_password)
|
||||
@@ -6,9 +6,9 @@ from unittest.mock import MagicMock, patch
|
||||
import pytest
|
||||
from jose import jwt
|
||||
|
||||
from src.my_auth.core.token import TokenManager
|
||||
from src.my_auth.exceptions import InvalidTokenError, ExpiredTokenError
|
||||
from src.my_auth.models.user import UserInDB # Assuming you have a fixture for this
|
||||
from myauth.core.token import TokenManager
|
||||
from myauth.exceptions import InvalidTokenError, ExpiredTokenError
|
||||
from myauth.models.user import UserInDB # Assuming you have a fixture for this
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -99,7 +99,7 @@ class TestTokenExpirationCalculations:
|
||||
"""Tests for token expiration date methods."""
|
||||
|
||||
# We patch datetime.now() to ensure stable calculations
|
||||
@patch('src.my_auth.core.token.datetime')
|
||||
@patch('myauth.core.token.datetime')
|
||||
def test_get_refresh_token_expiration(self, mock_datetime, token_manager: TokenManager):
|
||||
"""Should calculate refresh token expiration correctly."""
|
||||
|
||||
@@ -112,7 +112,7 @@ class TestTokenExpirationCalculations:
|
||||
|
||||
assert actual_exp == expected_exp
|
||||
|
||||
@patch('src.my_auth.core.token.datetime')
|
||||
@patch('myauth.core.token.datetime')
|
||||
def test_get_password_reset_token_expiration(self, mock_datetime, token_manager: TokenManager):
|
||||
"""Should calculate password reset token expiration correctly."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user