Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SaboniAmine committed Aug 28, 2024
1 parent d734857 commit dced965
Show file tree
Hide file tree
Showing 24 changed files with 117 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def list_organizations(self, user: Optional[User] = None) -> List[Organization]:
)
return [self.map_sql_to_schema(org) for org in e]

def list_users(self, organization_id: UUID, user: User) -> List[OrganizationUser]:
def list_users(self, organization_id: UUID) -> List[OrganizationUser]:
with self.session_factory() as session:
e = (
session.query(SqlModelMembership, SqlModelUser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def subscribe_user_to_org(
)
session.add(db_membership)
session.commit()
return self.map_sql_to_schema(e)

def is_user_authorized_on_project(self, project_id, user_id: UUID):
with self.session_factory() as session:
Expand Down
2 changes: 1 addition & 1 deletion carbonserver/carbonserver/api/services/project_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ProjectService:
def __init__(
self, auth_context: AuthContext, project_repository: ProjectSqlRepository
self, project_repository: ProjectSqlRepository, auth_context: AuthContext
):
self._auth_context = auth_context
self._repository = project_repository
Expand Down
1 change: 0 additions & 1 deletion carbonserver/carbonserver/api/services/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def create_user(self, user: UserAutoCreate) -> User:
return created_user

def create_user_by_id(self, user: UserAutoCreate) -> User:
print("userservice", user)
created_user: User = self._repository.create_user(user)
return created_user

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from unittest import mock

from dependency_injector import providers
from dependency_injector.containers import Container
from dependency_injector import containers, providers

from carbonserver.api.infra.repositories.repository_projects import (
SqlAlchemyRepository as ProjectSqlRepository,
)
from carbonserver.carbonserver.api.services.project_service import ProjectService
from carbonserver.api.services.project_service import ProjectService


class DatabaseMock:
Expand All @@ -24,17 +21,17 @@ def isOperationAuthorizedOnProject():
return True


class TestContainer(Container):
class FakeContainer(containers.DeclarativeContainer):
db = providers.Singleton(
DatabaseMock,
db_url=None,
)
projects_repository = providers.Factory(
mock.Mock(spec=ProjectSqlRepository),
ProjectSqlRepository,
session_factory=db.provided.session,
)

test_project_service = providers.Factory(
project_service = providers.Factory(
ProjectService,
projects_repository=projects_repository,
auth_context=AuthContextMock(),
Expand Down
Empty file.
28 changes: 28 additions & 0 deletions carbonserver/tests/api/mocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from carbonserver.api.schemas import User

USER_ID_1 = "f52fe339-164d-4c2b-a8c0-f562dfce066d"

ORGANIZATION_ID = "c13e851f-5c2f-403d-98d0-51fe15df3bc3"
ORGANIZATION_ID_2 = "c13e851f-5c2f-403d-98d0-51fe15df3bc4"
DUMMY_USER = User(
id=USER_ID_1,
name="Gontran Bonheur",
email="gontran.bonheur@gmail.com",
organizations=[ORGANIZATION_ID, ORGANIZATION_ID_2],
is_active=True,
)


class FakeUserWithAuthDependency:
auth_user = {"sub": USER_ID_1}
db_user = DUMMY_USER


class FakeAuthContext:
@staticmethod
def isOperationAuthorizedOnOrg(*args, **kwargs):
return True

@staticmethod
def isOperationAuthorizedOnProject(*args, **kwargs):
return True
Empty file.
8 changes: 5 additions & 3 deletions carbonserver/tests/api/routers/test_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from unittest import mock

import pytest
from api.mocks import FakeAuthContext, FakeUserWithAuthDependency
from container import ServerContainer
from dependency_injector import providers
from fastapi import FastAPI
from fastapi_pagination import add_pagination
from starlette import status
Expand All @@ -13,6 +15,7 @@
)
from carbonserver.api.routers import experiments
from carbonserver.api.schemas import Experiment
from carbonserver.api.services.auth_service import UserWithAuthDependency

PROJECT_ID = "f52fe339-164d-4c2b-a8c0-f562dfce066d"

Expand Down Expand Up @@ -103,6 +106,8 @@ def custom_test_server():
app = FastAPI()
app.container = container
app.include_router(experiments.router)
app.dependency_overrides[UserWithAuthDependency] = FakeUserWithAuthDependency
app.container.auth_context.override(providers.Factory(FakeAuthContext))
add_pagination(app)
yield app

Expand All @@ -112,7 +117,6 @@ def client(custom_test_server):
yield TestClient(custom_test_server)


@pytest.mark.skip(reason="test server with no auth in dev")
def test_add_experiment(client, custom_test_server):
repository_mock = mock.Mock(spec=ExperimentRepository)
expected_expriment = EXPERIMENT_1
Expand All @@ -127,7 +131,6 @@ def test_add_experiment(client, custom_test_server):
assert actual_experiment == expected_expriment


@pytest.mark.skip(reason="test server with no auth in dev")
def test_get_experiment_by_id_returns_correct_experiment(client, custom_test_server):
# Prepare the test
repository_mock = mock.Mock(spec=ExperimentRepository)
Expand All @@ -143,7 +146,6 @@ def test_get_experiment_by_id_returns_correct_experiment(client, custom_test_ser
assert actual_experiment == expected_experiment


@pytest.mark.skip(reason="test server with no auth in dev")
def test_get_experiment_of_project_retrieves_all_experiments_of_project(
client, custom_test_server
):
Expand Down
16 changes: 6 additions & 10 deletions carbonserver/tests/api/routers/test_organizations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from unittest import mock

import pytest
from api.mocks import FakeAuthContext, FakeUserWithAuthDependency
from container import ServerContainer
from dependency_injector import providers
from fastapi import FastAPI, status
from fastapi.testclient import TestClient

Expand All @@ -10,16 +12,10 @@
)
from carbonserver.api.routers import organizations
from carbonserver.api.routers.authenticate import UserWithAuthDependency
from carbonserver.api.schemas import Organization, OrganizationUser, User
from carbonserver.api.schemas import Organization, OrganizationUser

USER_ID_1 = "f52fe339-164d-4c2b-a8c0-f562dfce066d"


class FakeUserWithAuthDependency:
db_user = User(id=USER_ID_1, name="user1", email="user1@local.com", is_active=True)
auth_user = {"sub": USER_ID_1}


ORG_ID_1 = "f52fe339-164d-4c2b-a8c0-f562dfce066d"
ORG_ID_2 = "e52fe339-164d-4c2b-a8c0-f562dfce066d"

Expand Down Expand Up @@ -57,6 +53,9 @@ def custom_test_server():
app = FastAPI()
app.container = container
app.include_router(organizations.router)
app.dependency_overrides[UserWithAuthDependency] = FakeUserWithAuthDependency
app.container.auth_context.override(providers.Factory(FakeAuthContext))

yield app


Expand All @@ -65,7 +64,6 @@ def client(custom_test_server):
yield TestClient(custom_test_server)


@pytest.mark.skip(reason="test server with no auth in dev")
def test_add_org(client, custom_test_server):
repository_mock = mock.Mock(spec=SqlAlchemyRepository)
expected_org = ORG_1
Expand All @@ -79,7 +77,6 @@ def test_add_org(client, custom_test_server):
assert actual_org == expected_org


@pytest.mark.skip(reason="test server with no auth in dev")
def test_get_organization_by_id_returns_correct_org(client, custom_test_server):
repository_mock = mock.Mock(spec=SqlAlchemyRepository)
expected_org = ORG_1
Expand Down Expand Up @@ -136,7 +133,6 @@ def test_list_organizations_returns_all_orgs_for_user(client, custom_test_server
assert actual_org_list == expected_org_list


@pytest.mark.skip(reason="test server with no auth in dev")
def test_patch_organization(client, custom_test_server):
repository_mock = mock.Mock(spec=SqlAlchemyRepository)
expected_org = ORG_1
Expand Down
21 changes: 12 additions & 9 deletions carbonserver/tests/api/routers/test_projects.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from unittest import mock

import pytest
from api.mocks import FakeAuthContext, FakeUserWithAuthDependency
from container import ServerContainer
from dependency_injector import providers
from fastapi import FastAPI, status
from fastapi.testclient import TestClient

from carbonserver.api.infra.repositories.repository_projects import SqlAlchemyRepository
from carbonserver.api.routers import projects
from carbonserver.api.schemas import Project
from carbonserver.api.services.auth_service import UserWithAuthDependency

PROJECT_ID = "f52fe339-164d-4c2b-a8c0-f562dfce066d"
PROJECT_ID_2 = "e52fe339-164d-4c2b-a8c0-f562dfce066d"
Expand Down Expand Up @@ -42,12 +45,15 @@


@pytest.fixture
def custom_test_server():
def custom_test_server() -> FastAPI:
container = ServerContainer()
container.wire(modules=[projects])

app = FastAPI()
app.container = container
app.include_router(projects.router)
app.dependency_overrides[UserWithAuthDependency] = FakeUserWithAuthDependency
app.container.auth_context.override(providers.Factory(FakeAuthContext))
yield app


Expand All @@ -56,21 +62,21 @@ def client(custom_test_server):
yield TestClient(custom_test_server)


@pytest.mark.skip(reason="test server with no auth in dev")
def test_add_project(client, custom_test_server):
repository_mock = mock.Mock(spec=SqlAlchemyRepository)
project_repository_mock = mock.Mock(spec=SqlAlchemyRepository)
expected_project = PROJECT_1
repository_mock.add_project.return_value = Project(**PROJECT_1)
project_repository_mock.add_project.return_value = Project(**PROJECT_1)

with custom_test_server.container.project_repository.override(repository_mock):
with custom_test_server.container.project_repository.override(
project_repository_mock
):
response = client.post("/projects", json=PROJECT_TO_CREATE)
actual_project = response.json()

assert response.status_code == status.HTTP_201_CREATED
assert actual_project == expected_project


@pytest.mark.skip(reason="test server with no auth in dev")
def test_delete_project(client, custom_test_server):
repository_mock = mock.Mock(spec=SqlAlchemyRepository)
repository_mock.delete_project.return_value = None
Expand All @@ -81,7 +87,6 @@ def test_delete_project(client, custom_test_server):
assert response.status_code == status.HTTP_204_NO_CONTENT


@pytest.mark.skip(reason="test server with no auth in dev")
def test_patch_project(client, custom_test_server):
repository_mock = mock.Mock(spec=SqlAlchemyRepository)
expected_project = PROJECT_1
Expand All @@ -95,7 +100,6 @@ def test_patch_project(client, custom_test_server):
assert actual_project == expected_project


@pytest.mark.skip(reason="test server with no auth in dev")
def test_get_project_by_id_returns_correct_project(client, custom_test_server):
repository_mock = mock.Mock(spec=SqlAlchemyRepository)
expected_project = PROJECT_1
Expand All @@ -109,7 +113,6 @@ def test_get_project_by_id_returns_correct_project(client, custom_test_server):
assert actual_project == expected_project


@pytest.mark.skip(reason="test server with no auth in dev")
def test_get_projects_from_organization_returns_correct_project(
client, custom_test_server
):
Expand Down
Empty file.
25 changes: 18 additions & 7 deletions carbonserver/tests/api/service/test_experiments_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from unittest import mock
from uuid import UUID

from api.mocks import DUMMY_USER, FakeAuthContext

from carbonserver.api.infra.repositories.repository_experiments import (
SqlAlchemyRepository,
)
Expand All @@ -12,7 +14,6 @@

PROJECT_ID = UUID("f52fe339-164d-4c2b-a8c0-f562dfce066d")


EXPERIMENT_1 = Experiment(
id=EXPERIMENT_ID,
name="Experiment",
Expand Down Expand Up @@ -59,26 +60,34 @@
def test_emission_service_creates_correct_emission(_):
repository_mock: SqlAlchemyRepository = mock.Mock(spec=SqlAlchemyRepository)
expected_id = EXPERIMENT_ID
experiment_service: ExperimentService = ExperimentService(repository_mock)
experiment_service: ExperimentService = ExperimentService(
repository_mock, auth_context=FakeAuthContext()
)
repository_mock.add_experiment.return_value = EXPERIMENT_ID

experiment_to_create = EXPERIMENT_CREATE

actual_saved_emission_id = experiment_service.add_experiment(experiment_to_create)
actual_saved_emission_id = experiment_service.add_experiment(
experiment_to_create, DUMMY_USER
)

assert actual_saved_emission_id == expected_id


def test_emission_service_retrieves_all_existing_emissions_for_one_run():
repository_mock: SqlAlchemyRepository = mock.Mock(spec=SqlAlchemyRepository)
expected_experiments_ids = [EXPERIMENT_ID, EXPERIMENT_ID_2]
emission_service: ExperimentService = ExperimentService(repository_mock)
emission_service: ExperimentService = ExperimentService(
repository_mock, auth_context=FakeAuthContext()
)
repository_mock.get_experiments_from_project.return_value = [
EXPERIMENT_1,
EXPERIMENT_2,
]

experiments_list = emission_service.get_experiments_from_project(PROJECT_ID)
experiments_list = emission_service.get_experiments_from_project(
PROJECT_ID, DUMMY_USER
)
actual_experiments_ids_list = map(lambda x: x.id, iter(experiments_list))
diff = set(actual_experiments_ids_list) ^ set(expected_experiments_ids)

Expand All @@ -91,9 +100,11 @@ def test_emission_service_retrieves_all_existing_emissions_for_one_run():
def test_emission_service_retrives_correct_emission_by_id():
repository_mock: SqlAlchemyRepository = mock.Mock(spec=SqlAlchemyRepository)
expected_emission_id = EXPERIMENT_ID
experiment_service: ExperimentService = ExperimentService(repository_mock)
experiment_service: ExperimentService = ExperimentService(
repository_mock, auth_context=FakeAuthContext()
)
repository_mock.get_one_experiment.return_value = EXPERIMENT_1

actual_emission = experiment_service.get_one_experiment(EXPERIMENT_ID)
actual_emission = experiment_service.get_one_experiment(EXPERIMENT_ID, DUMMY_USER)

assert actual_emission.id == expected_emission_id
Loading

0 comments on commit dced965

Please sign in to comment.