From d64cf27a668a216371a95f835cd37214bdee8aba Mon Sep 17 00:00:00 2001 From: Angel-Dijoux Date: Mon, 1 Jan 2024 01:36:56 +0100 Subject: [PATCH] test: write tests for a auth user --- .../unit/test_search_onisep_formations.py | 49 +++++++++++++++++-- src/tests/factories/factories.py | 17 ++++--- src/tests/fixtures/service.py | 18 +++++++ 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py b/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py index e8089b8..37c6aee 100644 --- a/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py +++ b/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py @@ -1,11 +1,23 @@ from unittest.mock import patch import pytest -from src.business_logic.formation.scrap.search_formation import search_formations +from src.business_logic.formation.scrap.search_formation import ( + auth_search_formations, + search_formations, +) from src.business_logic.formation.scrap.types import ( FormationsWithTotal, ) -from src.business_logic.formation.scrap.utils.format_formations import format_formations +from src.business_logic.formation.scrap.utils.format_formations import ( + format_formation_with_is_favorite, + format_formations, +) +from src.models.user import User +from src.models.user_favori import UserFavori +from src.tests.factories.factories import ( + UserFactory, + UserFavorisFactory, +) MOKED_RESEARCH = { @@ -39,7 +51,9 @@ def mock_search_formations(): yield mock_get_raw_data -def test_search_formations_successful(mock_search_formations): +def test_search_formations_should_return_formations_without_favorite( + mock_search_formations, +): # Arrange mock_search_formations.return_value = MOKED_RESEARCH @@ -55,3 +69,32 @@ def test_search_formations_successful(mock_search_formations): assert formations == waited_result mock_search_formations.assert_called_once_with("STHR", 1, None) + + +def test_authenticated_search_formations_should_return_formations_with_favorite( + mock_search_formations, db_session +): + # Arrange + mock_search_formations.return_value = MOKED_RESEARCH + + # Given + user: User = UserFactory() + db_session.add(user) + db_session.flush() + + user_favori: UserFavori = UserFavorisFactory(user_id=user.id) + db_session.add(user_favori) + db_session.commit() + + # Act + formations = auth_search_formations(user.id, "STHR", 1) + + # Assert + moked_formation = MOKED_RESEARCH["results"] + waited_formations = format_formation_with_is_favorite(user.id, moked_formation) + waited_result = FormationsWithTotal( + total=MOKED_RESEARCH["total"], formations=waited_formations + ) + + assert formations == waited_result + mock_search_formations.assert_called_once_with("STHR", 1, None) diff --git a/src/tests/factories/factories.py b/src/tests/factories/factories.py index ed1a1a5..1592437 100644 --- a/src/tests/factories/factories.py +++ b/src/tests/factories/factories.py @@ -6,13 +6,9 @@ class UserFactory(factory.Factory): class Meta: model = models.User - username = factory.Faker("uuidv4") - email = factory.Faker("uuidv4") - - -class UserFavorisFactory(factory.Factory): - class Meta: - model = models.UserFavori + username = factory.Faker("uuid4") + email = factory.Faker("uuid4") + password = factory.Faker("uuid4") class FormationFactory(factory.Factory): @@ -29,3 +25,10 @@ class Meta: domain = "hôtellerie-restauration, tourisme/hôtellerie | hôtellerie-restauration, tourisme/restauration" niveau_de_sortie = "Bac ou équivalent" duree = "1 an" + + +class UserFavorisFactory(factory.Factory): + class Meta: + model = models.UserFavori + + formation = factory.SubFactory(FormationFactory) diff --git a/src/tests/fixtures/service.py b/src/tests/fixtures/service.py index b38915d..61cab1d 100644 --- a/src/tests/fixtures/service.py +++ b/src/tests/fixtures/service.py @@ -4,9 +4,14 @@ from sqlalchemy.orm import Session from flask.testing import FlaskClient from flask_sqlalchemy import SQLAlchemy +from flask_jwt_extended import ( + create_access_token, +) from src import create_app from src import db as _db +from src.business_logic.formation.scrap.utils.get_onisep_token import BearerToken +from src.models.user import User @pytest.fixture(autouse=True, scope="session") @@ -34,3 +39,16 @@ def db_session(db: SQLAlchemy) -> Session: def client(app: Flask, db: SQLAlchemy) -> Generator[FlaskClient, None, None]: with app.test_client() as client: yield client + + +@pytest.fixture(scope="module") +def authenticated_user(db_session: Session) -> tuple[User, dict[str, BearerToken]]: + from src.tests.factories import UserFactory + + user: User = UserFactory() + db_session.add(user) + db_session.commit() + + headers = {"Authorization": f"Bearer {create_access_token(identity=user.id)}"} + + yield user, headers