-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ improve error handling of pricing plans in webserver (#4980)
- Loading branch information
1 parent
5e3e596
commit d77e335
Showing
10 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
services/web/server/tests/unit/with_dbs/01/test_catalog_api__pricing_plan.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# pylint:disable=unused-variable | ||
# pylint:disable=unused-argument | ||
# pylint:disable=redefined-outer-name | ||
|
||
import re | ||
import urllib.parse | ||
|
||
import pytest | ||
from aiohttp import web | ||
from aiohttp.test_utils import TestClient | ||
from models_library.api_schemas_resource_usage_tracker.pricing_plans import ( | ||
ServicePricingPlanGet, | ||
) | ||
from models_library.utils.fastapi_encoders import jsonable_encoder | ||
from pydantic import parse_obj_as | ||
from pytest_simcore.aioresponses_mocker import AioResponsesMock | ||
from pytest_simcore.helpers.utils_assert import assert_status | ||
from pytest_simcore.helpers.utils_login import UserInfoDict | ||
from settings_library.resource_usage_tracker import ResourceUsageTrackerSettings | ||
from simcore_service_webserver.db.models import UserRole | ||
from simcore_service_webserver.resource_usage.settings import get_plugin_settings | ||
|
||
|
||
@pytest.fixture | ||
def mock_rut_api_responses( | ||
client: TestClient, aioresponses_mocker: AioResponsesMock | ||
) -> AioResponsesMock: | ||
assert client.app | ||
settings: ResourceUsageTrackerSettings = get_plugin_settings(client.app) | ||
|
||
service_pricing_plan_get = parse_obj_as( | ||
ServicePricingPlanGet, | ||
ServicePricingPlanGet.Config.schema_extra["examples"][0], | ||
) | ||
aioresponses_mocker.get( | ||
re.compile(f"^{settings.api_base_url}/services/+.+$"), | ||
payload=jsonable_encoder(service_pricing_plan_get), | ||
) | ||
|
||
return aioresponses_mocker | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"user_role,expected", | ||
[ | ||
(UserRole.ANONYMOUS, web.HTTPUnauthorized), | ||
(UserRole.GUEST, web.HTTPOk), | ||
(UserRole.USER, web.HTTPOk), | ||
(UserRole.TESTER, web.HTTPOk), | ||
], | ||
) | ||
async def test_get_service_pricinp_plan_role_access_rights( | ||
client: TestClient, | ||
logged_user: UserInfoDict, | ||
mock_rut_api_responses: AioResponsesMock, | ||
expected: type[web.HTTPException], | ||
): | ||
assert client.app | ||
assert client.app.router | ||
url = client.app.router["get_service_pricing_plan"].url_for( | ||
service_key=urllib.parse.quote("simcore/services/dynamic/someservice", safe=""), | ||
service_version="3.4.5", | ||
) | ||
response = await client.get(f"{url}") | ||
await assert_status(response, expected) | ||
|
||
|
||
@pytest.fixture | ||
def mock_catalog_get_service_pricing_plan_not_found( | ||
client: TestClient, aioresponses_mocker: AioResponsesMock | ||
) -> AioResponsesMock: | ||
assert client.app | ||
settings: ResourceUsageTrackerSettings = get_plugin_settings(client.app) | ||
url_pattern = re.compile(f"^{settings.base_url}+/.*$") | ||
|
||
aioresponses_mocker.get(url_pattern, exception=web.HTTPNotFound) | ||
return aioresponses_mocker | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"user_role,expected", | ||
[ | ||
(UserRole.TESTER, web.HTTPNotFound), | ||
], | ||
) | ||
async def test_get_service_pricing_plan_raises_not_found_error( | ||
client: TestClient, | ||
logged_user: UserInfoDict, | ||
mock_catalog_get_service_pricing_plan_not_found: AioResponsesMock, | ||
expected: type[web.HTTPException], | ||
): | ||
assert client.app | ||
assert client.app.router | ||
url = client.app.router["get_service_pricing_plan"].url_for( | ||
service_key="simcore%2Fservices%2Fdynamic%2Fsomeservice", | ||
service_version="3.4.5", | ||
) | ||
response = await client.get(f"{url}") | ||
await assert_status(response, expected) |