Skip to content

Commit

Permalink
test: write tests for a auth user
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Jan 1, 2024
1 parent 02d9a86 commit d64cf27
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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

Expand All @@ -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)
17 changes: 10 additions & 7 deletions src/tests/factories/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
18 changes: 18 additions & 0 deletions src/tests/fixtures/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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

0 comments on commit d64cf27

Please sign in to comment.