From f983817731065750a9b0e10533d596a83000d5a6 Mon Sep 17 00:00:00 2001 From: Angel-Dijoux Date: Wed, 3 Apr 2024 21:41:57 +0200 Subject: [PATCH] test: fix errors on multiple tests --- .../services/test_jwt_authentification.py | 30 ++++++++------- .../user/tests/units/test_validate_user.py | 37 ++++++++++++------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/blueprints/tests/services/test_jwt_authentification.py b/src/blueprints/tests/services/test_jwt_authentification.py index 3439da5..8ff58fd 100644 --- a/src/blueprints/tests/services/test_jwt_authentification.py +++ b/src/blueprints/tests/services/test_jwt_authentification.py @@ -1,22 +1,19 @@ -import pytest +from werkzeug.security import generate_password_hash + from src.models.user import User from src.tests.factories.factories import UserFactory -from werkzeug.security import generate_password_hash -@pytest.fixture -def user_in_db(db_session) -> User: +def test_jwt_authentification_should_return_user_payload_with_tokens( + client, db_session +): + # Given user: User = UserFactory( email="test@example.com", password=generate_password_hash("password123") ) db_session.add(user) db_session.commit() - return user - -def test_jwt_authentification_should_return_user_payload_with_tokens( - client, user_in_db -): # When response = client.post( "/api/v1/auth/login", @@ -26,14 +23,21 @@ def test_jwt_authentification_should_return_user_payload_with_tokens( result = response.json # Then - assert result["user"]["id"] == user_in_db.id - assert result["user"]["email"] == user_in_db.email - assert result["user"]["username"] == user_in_db.username + assert result["user"]["id"] == user.id + assert result["user"]["email"] == user.email + assert result["user"]["username"] == user.username assert result["user"]["access"] != "" assert result["user"]["refresh"] != "" -def test_jwt_authentification_should_return_401_unauthorized(client, user_in_db): +def test_jwt_authentification_should_return_401_unauthorized(client, db_session): + # Given + user: User = UserFactory( + email="test1@example.com", password=generate_password_hash("password123") + ) + db_session.add(user) + db_session.commit() + # When response = client.post( "/api/v1/auth/login", diff --git a/src/business_logic/user/tests/units/test_validate_user.py b/src/business_logic/user/tests/units/test_validate_user.py index df89b16..305394b 100644 --- a/src/business_logic/user/tests/units/test_validate_user.py +++ b/src/business_logic/user/tests/units/test_validate_user.py @@ -7,38 +7,49 @@ from werkzeug.security import generate_password_hash -@pytest.fixture -def user_in_db(db_session) -> User: +def test_validate_user_should_return_user(db_session): + # Given user: User = UserFactory( - email="test@example.com", password=generate_password_hash("password123") + email="test1@example.com", password=generate_password_hash("password123") ) db_session.add(user) db_session.commit() - return user - -def test_validate_user_should_return_user(user_in_db): # Act - result = validate_user("test@example.com", "password123") + result = validate_user("test1@example.com", "password123") # Assert - assert result == user_in_db + assert result == user + +def test_validate_user_should_create_email_exception(db_session): + # Given + user: User = UserFactory( + email="test2@example.com", password=generate_password_hash("password123") + ) + db_session.add(user) + db_session.commit() -def test_validate_user_should_create_email_exception(user_in_db): # Act & Assert with pytest.raises(AuthenticationException) as exc_info: - validate_user("bad.email@gmail.com", "password123") + validate_user("bad.email2@gmail.com", "password123") assert str(exc_info.value) == "User not found" -def test_validate_user_should_create_password_exception(user_in_db): +def test_validate_user_should_create_password_exception(db_session): + # Given + user: User = UserFactory( + email="test3@example.com", password=generate_password_hash("password123") + ) + db_session.add(user) + db_session.commit() + with pytest.raises(AuthenticationException) as exc_info: - validate_user("test@example.com", "badPassword") + validate_user("test3@example.com", "badPassword") assert str(exc_info.value) == "Incorrect password" -def test_validate_user_should_create_missing_email_or_password_exception(user_in_db): +def test_validate_user_should_create_missing_email_or_password_exception(): # Act & Assert with pytest.raises(AuthenticationException) as exc_info: validate_user("", "password")