Skip to content

Commit

Permalink
adds integration test from ANE's PR#2777
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Apr 7, 2022
1 parent 3db78ae commit b30e67b
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 18 deletions.
29 changes: 12 additions & 17 deletions services/web/server/requirements/_test.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@
#
--constraint _base.txt

# testing

# fixtures
hypothesis
aioboto3
aioresponses
alembic
click
codecov
coverage
coveralls
docker
Faker
flaky
hypothesis
jsonschema
openapi-spec-validator
ptvsd
pylint
pytest
pytest-aiohttp # incompatible with pytest-asyncio. See https://github.com/pytest-dev/pytest-asyncio/issues/76
pytest-cov
Expand All @@ -23,21 +32,7 @@ pytest-instafail
pytest-mock
pytest-runner
pytest-sugar

# helpers
aioresponses
alembic
click
docker
jsonschema
openapi-spec-validator
python-dotenv
redis
tenacity
websockets

# tools
codecov
coveralls
ptvsd
pylint
27 changes: 26 additions & 1 deletion services/web/server/requirements/_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
#
# pip-compile --output-file=requirements/_test.txt --strip-extras requirements/_test.in
#
aioboto3==9.5.0
# via -r requirements/_test.in
aiobotocore==2.2.0
# via aioboto3
aiohttp==3.8.1
# via
# -c requirements/../../../../requirements/constraints.txt
# -c requirements/_base.txt
# aiobotocore
# aioresponses
# pytest-aiohttp
aioitertools==0.10.0
# via aiobotocore
aioresponses==0.7.3
# via -r requirements/_test.in
aiosignal==1.2.0
Expand Down Expand Up @@ -37,6 +44,13 @@ attrs==20.3.0
# pytest-docker
bcrypt==3.2.0
# via paramiko
boto3==1.21.21
# via aiobotocore
botocore==1.24.21
# via
# aiobotocore
# boto3
# s3transfer
certifi==2021.10.8
# via requests
cffi==1.15.0
Expand Down Expand Up @@ -124,6 +138,10 @@ iniconfig==1.1.1
# via pytest
isort==5.10.1
# via pylint
jmespath==1.0.0
# via
# boto3
# botocore
jsonschema==3.2.0
# via
# -c requirements/_base.txt
Expand Down Expand Up @@ -226,7 +244,9 @@ pytest-runner==6.0.0
pytest-sugar==0.9.4
# via -r requirements/_test.in
python-dateutil==2.8.2
# via faker
# via
# botocore
# faker
python-dotenv==0.20.0
# via
# -r requirements/_test.in
Expand All @@ -245,6 +265,8 @@ requests==2.27.1
# coveralls
# docker
# docker-compose
s3transfer==0.5.2
# via boto3
six==1.16.0
# via
# -c requirements/_base.txt
Expand Down Expand Up @@ -277,12 +299,14 @@ tomli==2.0.1
typing-extensions==4.1.1
# via
# -c requirements/_base.txt
# aioitertools
# astroid
# pylint
# redis
urllib3==1.26.9
# via
# -c requirements/../../../../requirements/constraints.txt
# botocore
# requests
websocket-client==0.59.0
# via
Expand All @@ -292,6 +316,7 @@ websockets==10.2
# via -r requirements/_test.in
wrapt==1.14.0
# via
# aiobotocore
# astroid
# deprecated
yarl==1.5.1
Expand Down
156 changes: 156 additions & 0 deletions services/web/server/tests/integration/01/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# pylint:disable=unused-argument
# pylint:disable=redefined-outer-name

import asyncio
from copy import deepcopy
from pathlib import Path
from typing import Any, AsyncIterable, Callable, Dict, Iterator, Tuple
from unittest import mock
from uuid import UUID

import aiopg
import aiopg.sa
import aioredis
import pytest
from aiohttp.test_utils import TestClient
from models_library.projects import ProjectID
from models_library.settings.redis import RedisConfig
from pytest_simcore.helpers.utils_environs import EnvVarsDict
from pytest_simcore.helpers.utils_login import AUserDict, log_client_in
from servicelib.aiohttp.application import create_safe_application
from simcore_service_webserver._meta import API_VTAG
from simcore_service_webserver.application import (
setup_director,
setup_director_v2,
setup_exporter,
setup_login,
setup_products,
setup_projects,
setup_resource_manager,
setup_rest,
setup_security,
setup_session,
setup_socketio,
setup_storage,
setup_users,
)
from simcore_service_webserver.application_settings import setup_settings
from simcore_service_webserver.catalog import setup_catalog
from simcore_service_webserver.db import setup_db
from simcore_service_webserver.scicrunch.submodule_setup import (
setup_scicrunch_submodule,
)
from simcore_service_webserver.security_roles import UserRole
from yarl import URL

# store only lowercase "v1", "v2", etc...
SUPPORTED_EXPORTER_VERSIONS = {"v1", "v2"}


@pytest.fixture
async def delete_all_redis_keys(redis_service: RedisConfig) -> AsyncIterable[None]:
client = await aioredis.create_redis_pool(redis_service.dsn, encoding="utf-8")
await client.flushall()
client.close()
await client.wait_closed()

yield
# do nothing on teadown


@pytest.fixture
def client(
loop: asyncio.AbstractEventLoop,
aiohttp_client: Callable,
app_config: Dict,
monkeypatch_setenv_from_app_config: Callable[[EnvVarsDict], Dict[str, Any]],
postgres_with_template_db: aiopg.sa.engine.Engine,
mock_orphaned_services: mock.Mock,
database_from_template_before_each_function: None,
delete_all_redis_keys: None,
) -> Iterator[TestClient]:

monkeypatch_setenv_from_app_config(app_config)
cfg = deepcopy(app_config)

assert cfg["rest"]["version"] == API_VTAG
assert cfg["rest"]["enabled"]
cfg["projects"]["enabled"] = True
cfg["director"]["enabled"] = True

# fake config
app = create_safe_application(cfg)
setup_settings(app)

# activates only security+restAPI sub-modules
setup_db(app)
setup_session(app)
setup_security(app)
setup_rest(app)
setup_login(app)
setup_users(app)
setup_socketio(app)
setup_projects(app)
setup_director(app)
setup_director_v2(app)
setup_exporter(app)
setup_storage(app)
setup_products(app)
setup_catalog(app)
setup_scicrunch_submodule(app)
assert setup_resource_manager(app)

yield loop.run_until_complete(
aiohttp_client(
app,
server_kwargs={"port": cfg["main"]["port"], "host": cfg["main"]["host"]},
)
)


@pytest.fixture(scope="session", params=["v1", "v2"])
def exporter_version(request) -> str:
return request.param


@pytest.fixture
def exported_project_file(tests_data_dir: Path, exporter_version: str) -> Path:
exporter_dir = tests_data_dir / "exporter"
assert exporter_dir.exists()
exported_files = {x for x in exporter_dir.glob("*.osparc")}

for exported_file in exported_files:
if exported_file.name.startswith(exporter_version):
return exported_file

raise FileNotFoundError(f"{exporter_version=} not found in {exported_files}")


@pytest.fixture
async def login_user_and_import_study(
client: TestClient,
exported_project_file: Path,
) -> Tuple[ProjectID, AUserDict]:

user = await log_client_in(client=client, user_data={"role": UserRole.USER.name})
export_file_name = exported_project_file.name
version_from_name = export_file_name.split("#")[0]

assert_error = (
f"The '{version_from_name}' version' is not present in the supported versions: "
f"{SUPPORTED_EXPORTER_VERSIONS}. If it's a new version please remember to add it."
)
assert version_from_name in SUPPORTED_EXPORTER_VERSIONS, assert_error

url_import = client.app.router["import_project"].url_for()
assert url_import == URL(f"/{API_VTAG}/projects:import")

data = {"fileName": open(exported_project_file, mode="rb")}
async with await client.post(url_import, data=data, timeout=10) as import_response:
assert import_response.status == 200, await import_response.text()
reply_data = await import_response.json()
assert reply_data.get("data") is not None

imported_project_uuid = reply_data["data"]["uuid"]

return UUID(imported_project_uuid), user
Loading

0 comments on commit b30e67b

Please sign in to comment.