Skip to content

Commit

Permalink
test: fix errors on multiple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Apr 3, 2024
1 parent f184e96 commit f983817
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
30 changes: 17 additions & 13 deletions src/blueprints/tests/services/test_jwt_authentification.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]", 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",
Expand All @@ -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="[email protected]", password=generate_password_hash("password123")
)
db_session.add(user)
db_session.commit()

# When
response = client.post(
"/api/v1/auth/login",
Expand Down
37 changes: 24 additions & 13 deletions src/business_logic/user/tests/units/test_validate_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]", 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="[email protected]", 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")
Expand Down

0 comments on commit f983817

Please sign in to comment.