diff --git a/lago_python_client/models/__init__.py b/lago_python_client/models/__init__.py index bf1c43a..71d9f75 100644 --- a/lago_python_client/models/__init__.py +++ b/lago_python_client/models/__init__.py @@ -354,3 +354,5 @@ ) from .wallet_transaction import WalletTransaction as WalletTransaction from .webhook_endpoint import WebhookEndpoint as WebhookEndpoint +from .webhook_endpoint import WebhookEndpointResponse as WebhookEndpointResponse +from .webhook_endpoint import WebhookEndpointSigningKeyResponse as WebhookEndpointSigningKeyResponse diff --git a/lago_python_client/models/webhook_endpoint.py b/lago_python_client/models/webhook_endpoint.py index a948ef8..cd80823 100644 --- a/lago_python_client/models/webhook_endpoint.py +++ b/lago_python_client/models/webhook_endpoint.py @@ -12,6 +12,12 @@ class WebhookEndpoint(BaseModel): event_types: Optional[List[str]] +class WebhookEndpointSigningKeyResponse(BaseModel): + lago_id: str + secret_key: str + created_at: str + + class WebhookEndpointResponse(BaseResponseModel): lago_id: str lago_organization_id: str @@ -20,3 +26,4 @@ class WebhookEndpointResponse(BaseResponseModel): name: Optional[str] event_types: Optional[List[str]] created_at: str + signing_keys: Optional[List[WebhookEndpointSigningKeyResponse]] diff --git a/tests/fixtures/webhook_endpoint_empty_signing_keys.json b/tests/fixtures/webhook_endpoint_empty_signing_keys.json new file mode 100644 index 0000000..60be2e4 --- /dev/null +++ b/tests/fixtures/webhook_endpoint_empty_signing_keys.json @@ -0,0 +1,12 @@ +{ + "webhook_endpoint": { + "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://foo.bar", + "signature_algo": "hmac", + "name": "My Webhook Endpoint", + "event_types": ["customer.created"], + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [] + } +} diff --git a/tests/fixtures/webhook_endpoint_index_standard_webhooks.json b/tests/fixtures/webhook_endpoint_index_standard_webhooks.json new file mode 100644 index 0000000..cdad055 --- /dev/null +++ b/tests/fixtures/webhook_endpoint_index_standard_webhooks.json @@ -0,0 +1,42 @@ +{ + "webhook_endpoints": [ + { + "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://foo.bar", + "signature_algo": "standard_webhooks", + "name": "My Webhook Endpoint", + "event_types": ["customer.created"], + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [ + { + "lago_id": "5f0b0a8e-3c39-4a2f-9a8d-1d0c6b1e2a01", + "secret_key": "whsec_fake_test_secret_1", + "created_at": "2022-04-29T08:59:51Z" + }, + { + "lago_id": "9c4e7d21-6b8f-4e0a-b5d3-2f1a7c9e3b02", + "secret_key": "whsec_fake_test_secret_2", + "created_at": "2022-05-03T10:15:00Z" + } + ] + }, + { + "lago_id": "3d6f1a2b-8c4e-4f7a-9b1d-5e2c7a9f0b13", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://bar.foo", + "signature_algo": "hmac", + "name": null, + "event_types": null, + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [] + } + ], + "meta": { + "current_page": 1, + "next_page": null, + "prev_page": null, + "total_pages": 1, + "total_count": 2 + } +} diff --git a/tests/fixtures/webhook_endpoint_standard_webhooks.json b/tests/fixtures/webhook_endpoint_standard_webhooks.json new file mode 100644 index 0000000..2a9bba9 --- /dev/null +++ b/tests/fixtures/webhook_endpoint_standard_webhooks.json @@ -0,0 +1,23 @@ +{ + "webhook_endpoint": { + "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://foo.bar", + "signature_algo": "standard_webhooks", + "name": "My Webhook Endpoint", + "event_types": ["customer.created"], + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [ + { + "lago_id": "5f0b0a8e-3c39-4a2f-9a8d-1d0c6b1e2a01", + "secret_key": "whsec_fake_test_secret_1", + "created_at": "2022-04-29T08:59:51Z" + }, + { + "lago_id": "9c4e7d21-6b8f-4e0a-b5d3-2f1a7c9e3b02", + "secret_key": "whsec_fake_test_secret_2", + "created_at": "2022-05-03T10:15:00Z" + } + ] + } +} diff --git a/tests/test_webhook_endpoint_client.py b/tests/test_webhook_endpoint_client.py index 7e74af7..d2bb06f 100644 --- a/tests/test_webhook_endpoint_client.py +++ b/tests/test_webhook_endpoint_client.py @@ -1,3 +1,4 @@ +import json import os import pytest @@ -5,7 +6,7 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models import WebhookEndpoint +from lago_python_client.models import WebhookEndpoint, WebhookEndpointSigningKeyResponse def webhook_endpoint_object(): @@ -25,6 +26,33 @@ def mock_response(): return webhook_endpoint_response.read() +def mock_standard_webhooks_response(): + this_dir = os.path.dirname(os.path.abspath(__file__)) + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_standard_webhooks.json") + + with open(data_path, "rb") as webhook_endpoint_response: + return webhook_endpoint_response.read() + + +def mock_empty_signing_keys_response(): + this_dir = os.path.dirname(os.path.abspath(__file__)) + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_empty_signing_keys.json") + + with open(data_path, "rb") as webhook_endpoint_response: + return webhook_endpoint_response.read() + + +def assert_standard_webhooks_signing_keys(signing_keys): + assert len(signing_keys) == 2 + assert all(isinstance(key, WebhookEndpointSigningKeyResponse) for key in signing_keys) + assert signing_keys[0].lago_id == "5f0b0a8e-3c39-4a2f-9a8d-1d0c6b1e2a01" + assert signing_keys[0].secret_key == "whsec_fake_test_secret_1" + assert signing_keys[0].created_at == "2022-04-29T08:59:51Z" + assert signing_keys[1].lago_id == "9c4e7d21-6b8f-4e0a-b5d3-2f1a7c9e3b02" + assert signing_keys[1].secret_key == "whsec_fake_test_secret_2" + assert signing_keys[1].created_at == "2022-05-03T10:15:00Z" + + def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_index.json") @@ -33,6 +61,14 @@ def mock_collection_response(): return webhook_endpoint_response.read() +def mock_collection_standard_webhooks_response(): + this_dir = os.path.dirname(os.path.abspath(__file__)) + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_index_standard_webhooks.json") + + with open(data_path, "rb") as webhook_endpoint_response: + return webhook_endpoint_response.read() + + def test_valid_create_webhook_endpoint_request(httpx_mock: HTTPXMock): client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") @@ -173,3 +209,115 @@ def test_invalid_find_all_webhook_endpoint_request(httpx_mock: HTTPXMock): with pytest.raises(LagoApiError): client.webhook_endpoints.find_all() + + +def test_valid_find_webhook_endpoint_request_with_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_standard_webhooks_response(), + ) + response = client.webhook_endpoints.find(arg) + + assert response.signature_algo == "standard_webhooks" + assert_standard_webhooks_signing_keys(response.signing_keys) + + +def test_valid_update_webhook_endpoint_request_with_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_standard_webhooks_response(), + ) + response = client.webhook_endpoints.update(webhook_endpoint_object(), arg) + + assert_standard_webhooks_signing_keys(response.signing_keys) + + +def test_valid_create_standard_webhooks_endpoint_request(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_standard_webhooks_response(), + ) + response = client.webhook_endpoints.create( + WebhookEndpoint( + webhook_url="https://foo.bar", + signature_algo="standard_webhooks", + name="My Webhook Endpoint", + event_types=["customer.created"], + ) + ) + + request_body = json.loads(httpx_mock.get_request().content) + assert request_body["webhook_endpoint"]["signature_algo"] == "standard_webhooks" + assert response.signature_algo == "standard_webhooks" + assert_standard_webhooks_signing_keys(response.signing_keys) + + +def test_valid_find_webhook_endpoint_request_with_empty_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_empty_signing_keys_response(), + ) + response = client.webhook_endpoints.find(arg) + + assert response.signing_keys == [] + + +def test_valid_find_webhook_endpoint_request_without_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_response(), + ) + response = client.webhook_endpoints.find(arg) + + assert response.signing_keys is None + + +def test_valid_find_all_webhook_endpoint_request_without_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_collection_response(), + ) + response = client.webhook_endpoints.find_all() + + assert len(response["webhook_endpoints"]) > 0 + assert all(endpoint.signing_keys is None for endpoint in response["webhook_endpoints"]) + + +def test_valid_find_all_webhook_endpoint_request_with_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_collection_standard_webhooks_response(), + ) + response = client.webhook_endpoints.find_all() + + endpoints = response["webhook_endpoints"] + assert len(endpoints) == 2 + assert endpoints[0].signature_algo == "standard_webhooks" + assert_standard_webhooks_signing_keys(endpoints[0].signing_keys) + assert endpoints[1].signing_keys == [] + assert response["meta"]["current_page"] == 1