-
Notifications
You must be signed in to change notification settings - Fork 32
🎨 PACT - add checkout release contract test #7303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
matusdrobuliak66
merged 32 commits into
ITISFoundation:master
from
matusdrobuliak66:pact-broker-experiment-2
Mar 4, 2025
Merged
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
051bd9f
introducing contract testing
matusdrobuliak66 d27aab3
Merge branch 'master' into pact-broker-experiment
matusdrobuliak66 4d80ec9
introduce pact testing
matusdrobuliak66 f5adb63
openapi specs
matusdrobuliak66 b2f764e
add pact-tests github job
matusdrobuliak66 e701dfa
add secrets
matusdrobuliak66 70df54f
Merge branch 'master' into pact-broker-experiment
matusdrobuliak66 ea04200
review @sanderegg
matusdrobuliak66 c785f5e
review @sanderegg
matusdrobuliak66 5557d72
Merge branch 'master' into pact-broker-experiment
matusdrobuliak66 883daa9
review @pcrespov
matusdrobuliak66 e88cf91
review
matusdrobuliak66 e84c8af
fix
matusdrobuliak66 e5e41fa
modify ci github actions
matusdrobuliak66 09de0ba
modify ci github actions
matusdrobuliak66 b782956
modify ci github actions
matusdrobuliak66 21d3eaf
modify ci github actions
matusdrobuliak66 b02e802
review @sanderegg
matusdrobuliak66 a66634d
review @pcrespov
matusdrobuliak66 ea88a5c
fix
matusdrobuliak66 e2043f7
Merge branch 'master' into pact-broker-experiment
matusdrobuliak66 38026a5
fix
matusdrobuliak66 dff6235
small refactor
matusdrobuliak66 196d0fb
Merge branch 'master' into pact-broker-experiment
matusdrobuliak66 806c923
fix
matusdrobuliak66 034063e
Merge branch 'master' into pact-broker-experiment
matusdrobuliak66 e63caf2
introduce checkout release contract
matusdrobuliak66 37c9016
introduce checkout release contract
matusdrobuliak66 ff45929
Merge branch 'master' into pact-broker-experiment-2
matusdrobuliak66 85262df
introduce checkout release contract
matusdrobuliak66 febb49e
review @pcrespov
matusdrobuliak66 3f2c25d
Merge branch 'master' into pact-broker-experiment-2
matusdrobuliak66 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -37,4 +37,5 @@ jobs: | |
| make devenv | ||
| source .venv/bin/activate | ||
| cd services/api-server | ||
| make install-ci | ||
| make test-pacts | ||
81 changes: 81 additions & 0 deletions
81
services/api-server/tests/unit/pact_broker/pacts/01_checkout_release.json
This file contains hidden or 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,81 @@ | ||
| { | ||
| "consumer": { | ||
| "name": "Sim4Life" | ||
| }, | ||
| "provider": { | ||
| "name": "OsparcApiServerCheckoutRelease" | ||
| }, | ||
| "interactions": [ | ||
| { | ||
| "description": "Checkout one license", | ||
| "request": { | ||
| "method": "POST", | ||
| "path": "/v0/wallets/35/licensed-items/99580844-77fa-41bb-ad70-02dfaf1e3965/checkout", | ||
| "headers": { | ||
| "Accept": "application/json", | ||
| "Content-Type": "application/json" | ||
| }, | ||
| "body": { | ||
| "number_of_seats": 1, | ||
| "service_run_id": "1740149365_21a9352a-1d46-41f9-9a9b-42ac888f5afb" | ||
| } | ||
| }, | ||
| "response": { | ||
| "status": 200, | ||
| "headers": { | ||
| "Content-Length": "294", | ||
| "Content-Type": "application/json", | ||
| "Server": "uvicorn" | ||
| }, | ||
| "body": { | ||
| "key": "MODEL_IX_HEAD", | ||
| "licensed_item_checkout_id": "25262183-392c-4268-9311-3c4256c46012", | ||
| "licensed_item_id": "99580844-77fa-41bb-ad70-02dfaf1e3965", | ||
| "num_of_seats": 1, | ||
| "product_name": "s4l", | ||
| "started_at": "2025-02-21T15:04:47.673828Z", | ||
| "stopped_at": null, | ||
| "user_id": 425, | ||
| "version": "1.0.0", | ||
| "wallet_id": 35 | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "description": "Release item", | ||
| "request": { | ||
| "method": "POST", | ||
| "path": "/v0/licensed-items/99580844-77fa-41bb-ad70-02dfaf1e3965/checked-out-items/25262183-392c-4268-9311-3c4256c46012/release", | ||
| "headers": { | ||
| "Accept": "application/json", | ||
| "Content-Type": "application/json" | ||
| } | ||
| }, | ||
| "response": { | ||
| "status": 200, | ||
| "headers": { | ||
| "Content-Length": "319", | ||
| "Content-Type": "application/json", | ||
| "Server": "uvicorn" | ||
| }, | ||
| "body": { | ||
| "key": "MODEL_IX_HEAD", | ||
| "licensed_item_checkout_id": "25262183-392c-4268-9311-3c4256c46012", | ||
| "licensed_item_id": "99580844-77fa-41bb-ad70-02dfaf1e3965", | ||
| "num_of_seats": 1, | ||
| "product_name": "s4l", | ||
| "started_at": "2025-02-21T15:04:47.673828Z", | ||
| "stopped_at": "2025-02-21T15:04:47.901169Z", | ||
| "user_id": 425, | ||
| "version": "1.0.0", | ||
| "wallet_id": 35 | ||
| } | ||
| } | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "pactSpecification": { | ||
| "version": "3.0.0" | ||
| } | ||
| } | ||
| } |
4 changes: 2 additions & 2 deletions
4
services/api-server/tests/unit/pact_broker/pacts/05_licensed_items.json
This file contains hidden or 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
144 changes: 144 additions & 0 deletions
144
services/api-server/tests/unit/pact_broker/test_pact_checkout_release.py
This file contains hidden or 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,144 @@ | ||
| # pylint: disable=redefined-outer-name | ||
| # pylint: disable=unused-argument | ||
| # pylint: disable=unused-variable | ||
| # pylint: disable=too-many-arguments | ||
|
|
||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
| from fastapi import FastAPI | ||
| from models_library.api_schemas_webserver.licensed_items_checkouts import ( | ||
| LicensedItemCheckoutRpcGet, | ||
| ) | ||
| from pact.v3 import Verifier | ||
| from pytest_mock import MockerFixture | ||
| from simcore_service_api_server._meta import API_VERSION | ||
| from simcore_service_api_server.api.dependencies.authentication import ( | ||
| Identity, | ||
| ) | ||
| from simcore_service_api_server.api.dependencies.resource_usage_tracker_rpc import ( | ||
| get_resource_usage_tracker_client, | ||
| ) | ||
| from simcore_service_api_server.api.dependencies.webserver_rpc import ( | ||
| get_wb_api_rpc_client, | ||
| ) | ||
| from simcore_service_api_server.services_rpc.resource_usage_tracker import ( | ||
| ResourceUsageTrackerClient, | ||
| ) | ||
| from simcore_service_api_server.services_rpc.wb_api_server import WbApiRpcClient | ||
|
|
||
|
|
||
| def mock_get_current_identity() -> Identity: | ||
| return Identity(user_id=1, product_name="osparc", email="[email protected]") | ||
|
|
||
|
|
||
| # Fake response based on values from 01_checkout_release.json | ||
| EXPECTED_CHECKOUT = LicensedItemCheckoutRpcGet.model_validate( | ||
| { | ||
matusdrobuliak66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "key": "MODEL_IX_HEAD", | ||
| "licensed_item_checkout_id": "25262183-392c-4268-9311-3c4256c46012", | ||
| "licensed_item_id": "99580844-77fa-41bb-ad70-02dfaf1e3965", | ||
| "num_of_seats": 1, | ||
| "product_name": "s4l", | ||
| "started_at": "2025-02-21T15:04:47.673828Z", | ||
| "stopped_at": None, | ||
| "user_id": 425, | ||
| "version": "1.0.0", | ||
| "wallet_id": 35, | ||
| } | ||
| ) | ||
| assert EXPECTED_CHECKOUT.stopped_at is None | ||
|
|
||
|
|
||
| EXPECTED_RELEASE = LicensedItemCheckoutRpcGet.model_validate( | ||
| { | ||
| "key": "MODEL_IX_HEAD", | ||
matusdrobuliak66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "licensed_item_checkout_id": "25262183-392c-4268-9311-3c4256c46012", | ||
| "licensed_item_id": "99580844-77fa-41bb-ad70-02dfaf1e3965", | ||
| "num_of_seats": 1, | ||
| "product_name": "s4l", | ||
| "started_at": "2025-02-21T15:04:47.673828Z", | ||
| "stopped_at": "2025-02-21T15:04:47.901169Z", | ||
| "user_id": 425, | ||
| "version": "1.0.0", | ||
| "wallet_id": 35, | ||
| } | ||
| ) | ||
| assert EXPECTED_RELEASE.stopped_at is not None | ||
|
|
||
|
|
||
| class DummyRpcClient: | ||
| pass | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def mock_wb_api_server_rpc(app: FastAPI, mocker: MockerFixture) -> MockerFixture: | ||
|
|
||
| app.dependency_overrides[get_wb_api_rpc_client] = lambda: WbApiRpcClient( | ||
| _client=DummyRpcClient() | ||
| ) | ||
|
|
||
| mocker.patch( | ||
| "simcore_service_api_server.services_rpc.wb_api_server._checkout_licensed_item_for_wallet", | ||
| return_value=EXPECTED_CHECKOUT, | ||
| ) | ||
|
|
||
| mocker.patch( | ||
| "simcore_service_api_server.services_rpc.wb_api_server._release_licensed_item_for_wallet", | ||
| return_value=EXPECTED_RELEASE, | ||
| ) | ||
|
|
||
| return mocker | ||
matusdrobuliak66 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def mock_rut_server_rpc(app: FastAPI, mocker: MockerFixture) -> MockerFixture: | ||
|
|
||
| app.dependency_overrides[get_resource_usage_tracker_client] = ( | ||
| lambda: ResourceUsageTrackerClient(_client=DummyRpcClient()) | ||
| ) | ||
|
|
||
| mocker.patch( | ||
| "simcore_service_api_server.services_rpc.resource_usage_tracker._get_licensed_item_checkout", | ||
| return_value=EXPECTED_CHECKOUT, | ||
| ) | ||
|
|
||
| return mocker | ||
matusdrobuliak66 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| not os.getenv("PACT_BROKER_URL"), | ||
| reason="This test runs only if PACT_BROKER_URL is provided", | ||
| ) | ||
| def test_provider_against_pact( | ||
| pact_broker_credentials: tuple[str, str, str], | ||
matusdrobuliak66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mock_wb_api_server_rpc: MockerFixture, | ||
| mock_rut_server_rpc: MockerFixture, | ||
| run_test_server: str, | ||
matusdrobuliak66 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> None: | ||
| """ | ||
| Use the Pact Verifier to check the real provider | ||
| against the generated contract. | ||
| """ | ||
| broker_url, broker_username, broker_password = pact_broker_credentials | ||
|
|
||
| broker_builder = ( | ||
| Verifier("OsparcApiServerCheckoutRelease") | ||
| .add_transport(url=run_test_server) | ||
| .broker_source( | ||
| broker_url, | ||
| username=broker_username, | ||
| password=broker_password, | ||
| selector=True, | ||
| ) | ||
| ) | ||
|
|
||
| # NOTE: If you want to filter/test against specific contract use tags | ||
| verifier = broker_builder.consumer_tags( | ||
| "checkout_release" # <-- Here you define which pact to verify | ||
matusdrobuliak66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ).build() | ||
|
|
||
| # Set API version and run verification | ||
| verifier.set_publish_options(version=API_VERSION, tags=None, branch=None) | ||
| verifier.verify() | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.