Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 committed Aug 14, 2024
1 parent 203175d commit fc1e503
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# pylint: disable=redefined-outer-name
# pylint: disable=too-many-arguments
# pylint: disable=unused-argument
# pylint: disable=unused-variable
# pylint: disable=too-many-statements

from collections import UserDict
from pathlib import Path
from typing import Iterator

import pytest
import sqlalchemy as sa
from aiohttp.test_utils import TestClient
from models_library.folders import FolderID
from pytest_mock import MockerFixture
from pytest_simcore.helpers.typing_env import EnvVarsDict
from pytest_simcore.helpers.webserver_login import UserInfoDict
from pytest_simcore.helpers.webserver_parametrizations import (
ExpectedResponse,
standard_role_response,
)
from simcore_postgres_database.models.folders import folders, folders_to_projects
from simcore_service_webserver._meta import api_version_prefix
from simcore_service_webserver.db.models import UserRole
from simcore_service_webserver.projects.models import ProjectDict

# from .test_projects_crud_handlers__list_with_query_params import _assert_response_data


def standard_user_role() -> tuple[str, tuple[UserRole, ExpectedResponse]]:
all_roles = standard_role_response()

return (all_roles[0], [pytest.param(*all_roles[1][2], id="standard user role")])


@pytest.fixture
def mock_catalog_api_get_services_for_user_in_product(mocker: MockerFixture):
mocker.patch(
"simcore_service_webserver.projects._crud_api_read.get_services_for_user_in_product",
spec=True,
return_value=[],
)


@pytest.fixture()
def setup_folders_db(
postgres_db: sa.engine.Engine,
logged_user: UserInfoDict,
user_project: ProjectDict,
) -> Iterator[FolderID]:
with postgres_db.connect() as con:
result = con.execute(
folders.insert()
.values(
name="My Folder 1",
description="My Folder Decription",
product_name="osparc",
created_by=logged_user["primary_gid"],
)
.returning(folders.c.id)
)
_folder_id = result.fetchone()[0]

con.execute(
folders_to_projects.insert().values(
folder_id=_folder_id, project_uuid=user_project["uuid"]
)
)

yield FolderID(_folder_id)

con.execute(folders_to_projects.delete())
con.execute(folders.delete())


@pytest.fixture
def app_environment(
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
) -> EnvVarsDict:
# disable the webserver folders plugin
monkeypatch.setenv("WEBSERVER_FOLDERS", "0")
return app_environment | {"WEBSERVER_FOLDERS": "0"}


@pytest.mark.parametrize(*standard_user_role())
async def test_list_projects_with_disabled_project_folders_plugin(
client: TestClient,
app_environment: EnvVarsDict,
logged_user: UserDict,
expected: ExpectedResponse,
fake_project: ProjectDict,
tests_data_dir: Path,
osparc_product_name: str,
project_db_cleaner,
mock_catalog_api_get_services_for_user_in_product,
setup_folders_db,
):
"""
As the WEBSERVER_FOLDERS plugin is turned off, the project listing
should behave the same way as before, and therefore list all the projects
in the root directory, essentially ignoring the folders_to_projects table.
"""
base_url = client.app.router["list_projects"].url_for()
assert f"{base_url}" == f"/{api_version_prefix}/projects"

resp = await client.get(base_url)
data = await resp.json()

assert resp.status == 200
assert data["_meta"]["total"] == 1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from models_library.users import UserID
from pydantic import BaseModel, PositiveInt
from pytest_mock import MockerFixture
from pytest_simcore.helpers.typing_env import EnvVarsDict
from pytest_simcore.helpers.webserver_login import UserInfoDict
from pytest_simcore.helpers.webserver_parametrizations import (
ExpectedResponse,
Expand Down Expand Up @@ -480,40 +479,3 @@ async def test_list_projects_for_specific_folder_id(
_assert_response_data(
data, 1, 0, 1, f"/v0/projects?folder_id={setup_folders_db}&offset=0&limit=20", 1
)


@pytest.fixture
def app_environment(
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
) -> EnvVarsDict:
# disable the webserver folders plugin
monkeypatch.setenv("WEBSERVER_FOLDERS", "0")
return app_environment | {"WEBSERVER_FOLDERS": "0"}


@pytest.mark.parametrize(*standard_user_role())
async def test_list_projects_with_disabled_project_folders_plugin(
client: TestClient,
app_environment: EnvVarsDict,
logged_user: UserDict,
expected: ExpectedResponse,
fake_project: ProjectDict,
tests_data_dir: Path,
osparc_product_name: str,
project_db_cleaner,
mock_catalog_api_get_services_for_user_in_product,
setup_folders_db,
):
"""
As the WEBSERVER_FOLDERS plugin is turned off, the project listing
should behave the same way as before, and therefore list all the projects
in the root directory, essentially ignoring the folders_to_projects table.
"""
base_url = client.app.router["list_projects"].url_for()
assert f"{base_url}" == f"/{api_version_prefix}/projects"

resp = await client.get(base_url)
data = await resp.json()

assert resp.status == 200
_assert_response_data(data, 1, 0, 1, "/v0/projects?offset=0&limit=20", 1)

0 comments on commit fc1e503

Please sign in to comment.