Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lago_python_client/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lago_python_client/models/webhook_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,3 +26,4 @@ class WebhookEndpointResponse(BaseResponseModel):
name: Optional[str]
event_types: Optional[List[str]]
created_at: str
signing_keys: Optional[List[WebhookEndpointSigningKeyResponse]]
12 changes: 12 additions & 0 deletions tests/fixtures/webhook_endpoint_empty_signing_keys.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
42 changes: 42 additions & 0 deletions tests/fixtures/webhook_endpoint_index_standard_webhooks.json
Original file line number Diff line number Diff line change
@@ -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
}
}
23 changes: 23 additions & 0 deletions tests/fixtures/webhook_endpoint_standard_webhooks.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
150 changes: 149 additions & 1 deletion tests/test_webhook_endpoint_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import json
import os

import pytest
from pytest_httpx import HTTPXMock

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():
Expand All @@ -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")
Expand All @@ -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")

Expand Down Expand Up @@ -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
Loading