|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | +# pylint: disable=too-many-arguments |
| 5 | +# pylint: disable=too-many-statements |
| 6 | + |
| 7 | + |
| 8 | +from copy import deepcopy |
| 9 | +from http import HTTPStatus |
| 10 | + |
| 11 | +import pytest |
| 12 | +from aiohttp.test_utils import TestClient |
| 13 | +from pytest_mock import MockerFixture |
| 14 | +from pytest_simcore.helpers.assert_checks import assert_status |
| 15 | +from pytest_simcore.helpers.webserver_login import UserInfoDict |
| 16 | +from pytest_simcore.helpers.webserver_projects import create_project |
| 17 | +from servicelib.aiohttp import status |
| 18 | +from simcore_service_webserver.db.models import UserRole |
| 19 | +from simcore_service_webserver.projects.models import ProjectDict |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def mock_catalog_api_get_services_for_user_in_product(mocker: MockerFixture): |
| 24 | + mocker.patch( |
| 25 | + "simcore_service_webserver.projects._crud_api_read.get_services_for_user_in_product", |
| 26 | + spec=True, |
| 27 | + return_value=[], |
| 28 | + ) |
| 29 | + mocker.patch( |
| 30 | + "simcore_service_webserver.projects._crud_handlers.get_services_for_user_in_product", |
| 31 | + spec=True, |
| 32 | + return_value=[], |
| 33 | + ) |
| 34 | + mocker.patch( |
| 35 | + "simcore_service_webserver.projects._crud_handlers.project_uses_available_services", |
| 36 | + spec=True, |
| 37 | + return_value=True, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +_SEARCH_NAME_1 = "Quantum Solutions" |
| 42 | +_SEARCH_NAME_2 = "Orion solution" |
| 43 | +_SEARCH_NAME_3 = "Skyline solutions" |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("user_role,expected", [(UserRole.USER, status.HTTP_200_OK)]) |
| 47 | +async def test_workspaces__list_projects_full_search( |
| 48 | + client: TestClient, |
| 49 | + logged_user: UserInfoDict, |
| 50 | + user_project: ProjectDict, |
| 51 | + expected: HTTPStatus, |
| 52 | + mock_catalog_api_get_services_for_user_in_product: MockerFixture, |
| 53 | + fake_project: ProjectDict, |
| 54 | + workspaces_clean_db: None, |
| 55 | +): |
| 56 | + assert client.app |
| 57 | + |
| 58 | + # create a new workspace |
| 59 | + url = client.app.router["create_workspace"].url_for() |
| 60 | + resp = await client.post( |
| 61 | + url.path, |
| 62 | + json={ |
| 63 | + "name": "My first workspace", |
| 64 | + "description": "Custom description", |
| 65 | + "thumbnail": None, |
| 66 | + }, |
| 67 | + ) |
| 68 | + added_workspace, _ = await assert_status(resp, status.HTTP_201_CREATED) |
| 69 | + |
| 70 | + # Create project in shared workspace |
| 71 | + project_data = deepcopy(fake_project) |
| 72 | + project_data["workspace_id"] = f"{added_workspace['workspaceId']}" |
| 73 | + project_data["name"] = _SEARCH_NAME_1 |
| 74 | + project_1 = await create_project( |
| 75 | + client.app, |
| 76 | + project_data, |
| 77 | + user_id=logged_user["id"], |
| 78 | + product_name="osparc", |
| 79 | + ) |
| 80 | + |
| 81 | + # List project with full search |
| 82 | + base_url = client.app.router["list_projects_full_search"].url_for() |
| 83 | + url = base_url.with_query({"text": "solution"}) |
| 84 | + resp = await client.get(url) |
| 85 | + data, _ = await assert_status(resp, status.HTTP_200_OK) |
| 86 | + assert len(data) == 1 |
| 87 | + assert data[0]["uuid"] == project_1["uuid"] |
| 88 | + assert data[0]["workspaceId"] == added_workspace["workspaceId"] |
| 89 | + assert data[0]["folderId"] is None |
| 90 | + |
| 91 | + # Create projects in private workspace |
| 92 | + project_data = deepcopy(fake_project) |
| 93 | + project_data["name"] = _SEARCH_NAME_2 |
| 94 | + project_2 = await create_project( |
| 95 | + client.app, |
| 96 | + project_data, |
| 97 | + user_id=logged_user["id"], |
| 98 | + product_name="osparc", |
| 99 | + ) |
| 100 | + |
| 101 | + # List project with full search |
| 102 | + base_url = client.app.router["list_projects_full_search"].url_for() |
| 103 | + url = base_url.with_query({"text": "Orion"}) |
| 104 | + resp = await client.get(url) |
| 105 | + data, _ = await assert_status(resp, status.HTTP_200_OK) |
| 106 | + assert len(data) == 1 |
| 107 | + assert data[0]["uuid"] == project_2["uuid"] |
| 108 | + assert data[0]["workspaceId"] is None |
| 109 | + assert data[0]["folderId"] is None |
| 110 | + |
| 111 | + # Create projects in private workspace and move it to a folder |
| 112 | + project_data = deepcopy(fake_project) |
| 113 | + project_data["description"] = _SEARCH_NAME_3 |
| 114 | + project_3 = await create_project( |
| 115 | + client.app, |
| 116 | + project_data, |
| 117 | + user_id=logged_user["id"], |
| 118 | + product_name="osparc", |
| 119 | + ) |
| 120 | + |
| 121 | + # create a folder |
| 122 | + url = client.app.router["create_folder"].url_for() |
| 123 | + resp = await client.post(url.path, json={"name": "My first folder"}) |
| 124 | + root_folder, _ = await assert_status(resp, status.HTTP_201_CREATED) |
| 125 | + |
| 126 | + # add project to the folder |
| 127 | + url = client.app.router["replace_project_folder"].url_for( |
| 128 | + folder_id=f"{root_folder['folderId']}", |
| 129 | + project_id=f"{project_3['uuid']}", |
| 130 | + ) |
| 131 | + resp = await client.put(url.path) |
| 132 | + await assert_status(resp, status.HTTP_204_NO_CONTENT) |
| 133 | + |
| 134 | + # List project with full search |
| 135 | + base_url = client.app.router["list_projects_full_search"].url_for() |
| 136 | + url = base_url.with_query({"text": "Skyline"}) |
| 137 | + resp = await client.get(url) |
| 138 | + data, _ = await assert_status(resp, status.HTTP_200_OK) |
| 139 | + assert len(data) == 1 |
| 140 | + assert data[0]["uuid"] == project_3["uuid"] |
| 141 | + assert data[0]["workspaceId"] is None |
| 142 | + assert data[0]["folderId"] == root_folder["folderId"] |
| 143 | + |
| 144 | + # List project with full search (it should return data across all workspaces/folders) |
| 145 | + base_url = client.app.router["list_projects_full_search"].url_for() |
| 146 | + url = base_url.with_query({"text": "solution"}) |
| 147 | + resp = await client.get(url) |
| 148 | + data, _ = await assert_status(resp, status.HTTP_200_OK) |
| 149 | + sorted_data = sorted(data, key=lambda x: x["uuid"]) |
| 150 | + assert len(sorted_data) == 3 |
| 151 | + |
| 152 | + assert sorted_data[0]["uuid"] == project_1["uuid"] |
| 153 | + assert sorted_data[0]["workspaceId"] == added_workspace["workspaceId"] |
| 154 | + assert sorted_data[0]["folderId"] is None |
| 155 | + |
| 156 | + assert sorted_data[1]["uuid"] == project_2["uuid"] |
| 157 | + assert sorted_data[1]["workspaceId"] is None |
| 158 | + assert sorted_data[1]["folderId"] is None |
| 159 | + |
| 160 | + assert sorted_data[2]["uuid"] == project_3["uuid"] |
| 161 | + assert sorted_data[2]["workspaceId"] is None |
| 162 | + assert sorted_data[2]["folderId"] == root_folder["folderId"] |
0 commit comments