|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | +# pylint: disable=too-many-arguments |
| 5 | + |
| 6 | + |
| 7 | +import pytest |
| 8 | +from aiohttp import web |
| 9 | +from aiohttp.test_utils import TestClient, make_mocked_request |
| 10 | +from common_library.json_serialization import json_dumps |
| 11 | +from models_library.products import ProductName |
| 12 | +from pytest_mock import MockerFixture, MockType |
| 13 | +from servicelib.rest_constants import X_PRODUCT_NAME_HEADER |
| 14 | +from simcore_service_webserver._meta import API_VTAG |
| 15 | +from simcore_service_webserver.products import products_web |
| 16 | +from simcore_service_webserver.products.plugin import setup_products |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def setup_products_mocked(mocker: MockerFixture) -> MockType: |
| 21 | + def _wrap(app: web.Application): |
| 22 | + setup_products(app) |
| 23 | + |
| 24 | + # register test handlers |
| 25 | + app.router.add_get( |
| 26 | + f"/{API_VTAG}/test-helpers", |
| 27 | + _test_helpers_handler, |
| 28 | + name=_test_helpers_handler.__name__, |
| 29 | + ) |
| 30 | + app.router.add_get( |
| 31 | + f"/{API_VTAG}/test-product-template-helpers", |
| 32 | + _test_product_template_handler, |
| 33 | + name=_test_product_template_handler.__name__, |
| 34 | + ) |
| 35 | + |
| 36 | + return True |
| 37 | + |
| 38 | + return mocker.patch( |
| 39 | + "simcore_service_webserver.application.setup_products", |
| 40 | + autospec=True, |
| 41 | + side_effect=_wrap, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def client( |
| 47 | + setup_products_mocked: MockType, # keep before client fixture! |
| 48 | + client: TestClient, |
| 49 | +) -> TestClient: |
| 50 | + assert setup_products_mocked.called |
| 51 | + |
| 52 | + assert client.app |
| 53 | + assert client.app.router |
| 54 | + |
| 55 | + registered_routes = { |
| 56 | + route.resource.canonical |
| 57 | + for route in client.app.router.routes() |
| 58 | + if route.resource |
| 59 | + } |
| 60 | + assert f"/{API_VTAG}/test-helpers" in registered_routes |
| 61 | + |
| 62 | + return client |
| 63 | + |
| 64 | + |
| 65 | +async def _test_helpers_handler(request: web.Request): |
| 66 | + product_name = products_web.get_product_name(request) |
| 67 | + current_product = products_web.get_current_product(request) |
| 68 | + |
| 69 | + assert current_product.name == product_name |
| 70 | + |
| 71 | + credit_price_info = await products_web.get_current_product_credit_price_info( |
| 72 | + request |
| 73 | + ) |
| 74 | + assert credit_price_info is None |
| 75 | + |
| 76 | + return web.json_response( |
| 77 | + json_dumps( |
| 78 | + { |
| 79 | + "current_product": current_product, |
| 80 | + "get_producproduct_namet_name": product_name, |
| 81 | + "credit_price_info": credit_price_info, |
| 82 | + } |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +async def test_request_helpers(client: TestClient, default_product_name: ProductName): |
| 88 | + |
| 89 | + resp = await client.get( |
| 90 | + f"/{API_VTAG}/test-helpers", |
| 91 | + headers={X_PRODUCT_NAME_HEADER: default_product_name}, |
| 92 | + ) |
| 93 | + |
| 94 | + assert resp.ok, f"Got {await resp.text()}" |
| 95 | + |
| 96 | + got = await resp.json() |
| 97 | + assert got["current_product"] == default_product_name |
| 98 | + |
| 99 | + |
| 100 | +async def _test_product_template_handler(request: web.Request): |
| 101 | + product_name = products_web.get_product_name(request) |
| 102 | + |
| 103 | + # if no product, it should return common |
| 104 | + |
| 105 | + # if no template for product, it should return common |
| 106 | + # template/common/close_account.jinja2" |
| 107 | + template_path = await products_web.get_product_template_path( |
| 108 | + request, filename="close_account.jinja2" |
| 109 | + ) |
| 110 | + assert template_path.exists() |
| 111 | + assert template_path.name == "close_account.jinja2" |
| 112 | + assert "common/" in f"{template_path.resolve().absolute()}" |
| 113 | + |
| 114 | + # if specific template, it gets and caches in file |
| 115 | + # "templates/osparc/registration_email.jinja2" |
| 116 | + template_path = await products_web.get_product_template_path( |
| 117 | + request, filename="registration_email.jinja2" |
| 118 | + ) |
| 119 | + assert template_path.exists() |
| 120 | + assert template_path.name == "registration_email.jinja2" |
| 121 | + assert f"{product_name}/" in f"{template_path.resolve().absolute()}" |
| 122 | + |
| 123 | + # get again and should use file |
| 124 | + |
| 125 | + for _ in range(2): |
| 126 | + got = await products_web.get_product_template_path( |
| 127 | + request, filename="registration_email.jinja2" |
| 128 | + ) |
| 129 | + assert got == template_path |
| 130 | + |
| 131 | + path = await products_web.get_product_template_path( |
| 132 | + request, filename="invalid-template-name.jinja" |
| 133 | + ) |
| 134 | + assert path |
| 135 | + return web.json_response() |
| 136 | + |
| 137 | + |
| 138 | +async def test_product_template_helpers( |
| 139 | + client: TestClient, default_product_name: ProductName |
| 140 | +): |
| 141 | + |
| 142 | + resp = await client.get( |
| 143 | + f"/{API_VTAG}/test-product-template-helpers", |
| 144 | + headers={X_PRODUCT_NAME_HEADER: default_product_name}, |
| 145 | + ) |
| 146 | + |
| 147 | + assert resp.ok, f"Got {await resp.text()}" |
| 148 | + |
| 149 | + |
| 150 | +async def test_get_product_template_path_without_product(): |
| 151 | + fake_request = make_mocked_request("GET", "/fake", app=web.Application()) |
| 152 | + |
| 153 | + # if no product, it should return common |
| 154 | + template_path = await products_web.get_product_template_path( |
| 155 | + fake_request, filename="close_account.jinja2" |
| 156 | + ) |
| 157 | + |
| 158 | + assert template_path.exists() |
| 159 | + assert template_path.name == "close_account.jinja2" |
| 160 | + assert "common/" in f"{template_path.resolve().absolute()}" |
0 commit comments