|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 | from aiohttp.test_utils import TestClient |
| 10 | +from models_library.api_schemas_resource_usage_tracker.pricing_plans import ( |
| 11 | + PricingUnitGet, |
| 12 | +) |
10 | 13 | from models_library.api_schemas_webserver.licensed_items import LicensedItemGet |
| 14 | +from models_library.api_schemas_webserver.wallets import WalletGetWithAvailableCredits |
11 | 15 | from models_library.licensed_items import LicensedResourceType |
| 16 | +from pytest_mock.plugin import MockerFixture |
12 | 17 | from pytest_simcore.helpers.assert_checks import assert_status |
13 | 18 | from pytest_simcore.helpers.webserver_login import UserInfoDict |
14 | 19 | from servicelib.aiohttp import status |
|
18 | 23 |
|
19 | 24 |
|
20 | 25 | @pytest.mark.parametrize("user_role,expected", [(UserRole.USER, status.HTTP_200_OK)]) |
21 | | -async def test_licensed_items_db_crud( |
| 26 | +async def test_licensed_items_listing( |
22 | 27 | client: TestClient, |
23 | 28 | logged_user: UserInfoDict, |
24 | 29 | user_project: ProjectDict, |
@@ -58,9 +63,77 @@ async def test_licensed_items_db_crud( |
58 | 63 | data, _ = await assert_status(resp, status.HTTP_200_OK) |
59 | 64 | assert LicensedItemGet(**data) |
60 | 65 |
|
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def mock_licensed_items_purchase_functions(mocker: MockerFixture) -> tuple: |
| 69 | + mock_wallet_credits = mocker.patch( |
| 70 | + "simcore_service_webserver.licenses._licensed_items_api.get_wallet_with_available_credits_by_user_and_wallet", |
| 71 | + spec=True, |
| 72 | + return_value=WalletGetWithAvailableCredits.model_validate( |
| 73 | + WalletGetWithAvailableCredits.model_config["json_schema_extra"]["examples"][ |
| 74 | + 0 |
| 75 | + ] |
| 76 | + ), |
| 77 | + ) |
| 78 | + mock_get_pricing_unit = mocker.patch( |
| 79 | + "simcore_service_webserver.licenses._licensed_items_api.get_pricing_plan_unit", |
| 80 | + spec=True, |
| 81 | + return_value=PricingUnitGet.model_validate( |
| 82 | + PricingUnitGet.model_config["json_schema_extra"]["examples"][0] |
| 83 | + ), |
| 84 | + ) |
| 85 | + mock_create_licensed_item_purchase = mocker.patch( |
| 86 | + "simcore_service_webserver.licenses._licensed_items_api.licensed_items_purchases.create_licensed_item_purchase", |
| 87 | + spec=True, |
| 88 | + ) |
| 89 | + |
| 90 | + return ( |
| 91 | + mock_wallet_credits, |
| 92 | + mock_get_pricing_unit, |
| 93 | + mock_create_licensed_item_purchase, |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize("user_role,expected", [(UserRole.USER, status.HTTP_200_OK)]) |
| 98 | +async def test_licensed_items_purchase( |
| 99 | + client: TestClient, |
| 100 | + logged_user: UserInfoDict, |
| 101 | + user_project: ProjectDict, |
| 102 | + osparc_product_name: str, |
| 103 | + expected: HTTPStatus, |
| 104 | + pricing_plan_id: int, |
| 105 | + mock_licensed_items_purchase_functions: tuple, |
| 106 | +): |
| 107 | + assert client.app |
| 108 | + |
| 109 | + licensed_item_db = await _licensed_items_db.create( |
| 110 | + client.app, |
| 111 | + product_name=osparc_product_name, |
| 112 | + name="Model A", |
| 113 | + licensed_resource_type=LicensedResourceType.VIP_MODEL, |
| 114 | + pricing_plan_id=pricing_plan_id, |
| 115 | + ) |
| 116 | + _licensed_item_id = licensed_item_db.licensed_item_id |
| 117 | + |
| 118 | + # get |
| 119 | + url = client.app.router["get_licensed_item"].url_for( |
| 120 | + licensed_item_id=f"{_licensed_item_id}" |
| 121 | + ) |
| 122 | + resp = await client.get(f"{url}") |
| 123 | + data, _ = await assert_status(resp, status.HTTP_200_OK) |
| 124 | + assert LicensedItemGet(**data) |
| 125 | + |
61 | 126 | # purchase |
62 | 127 | url = client.app.router["purchase_licensed_item"].url_for( |
63 | 128 | licensed_item_id=f"{_licensed_item_id}" |
64 | 129 | ) |
65 | | - resp = await client.post(f"{url}", json={"wallet_id": 1, "num_of_seats": 5}) |
66 | | - # NOTE: Not yet implemented |
| 130 | + resp = await client.post( |
| 131 | + f"{url}", |
| 132 | + json={ |
| 133 | + "wallet_id": 1, |
| 134 | + "num_of_seats": 5, |
| 135 | + "pricing_plan_id": pricing_plan_id, |
| 136 | + "pricing_unit_id": 1, |
| 137 | + }, |
| 138 | + ) |
| 139 | + await assert_status(resp, status.HTTP_204_NO_CONTENT) |
0 commit comments