|
6 | 6 | from lago_python_client.client import Client |
7 | 7 | from lago_python_client.exceptions import LagoApiError |
8 | 8 | from lago_python_client.models import Subscription |
| 9 | +from lago_python_client.models.alert import Alert, AlertThreshold, AlertsList |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def create_subscription(): |
@@ -400,3 +401,95 @@ def test_fixed_charge_response_model_parsing(): |
400 | 401 | assert response.properties.graduated_ranges[0].from_value == 0 |
401 | 402 | assert response.properties.graduated_ranges[0].per_unit_amount == "100" |
402 | 403 | assert response.properties.graduated_ranges[1].to_value is None |
| 404 | + |
| 405 | + |
| 406 | +def mock_subscription_alerts_response(): |
| 407 | + this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 408 | + my_data_path = os.path.join(this_dir, "fixtures/subscription_alerts.json") |
| 409 | + |
| 410 | + with open(my_data_path, "rb") as alerts_response: |
| 411 | + return alerts_response.read() |
| 412 | + |
| 413 | + |
| 414 | +def test_valid_create_alerts_request(httpx_mock: HTTPXMock): |
| 415 | + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") |
| 416 | + external_id = "sub_1234" |
| 417 | + |
| 418 | + httpx_mock.add_response( |
| 419 | + method="POST", |
| 420 | + url="https://api.getlago.com/api/v1/subscriptions/" + external_id + "/alerts", |
| 421 | + content=mock_subscription_alerts_response(), |
| 422 | + ) |
| 423 | + |
| 424 | + input_object = AlertsList( |
| 425 | + alerts=[ |
| 426 | + Alert( |
| 427 | + alert_type="current_usage_amount", |
| 428 | + code="alert1", |
| 429 | + name="First Alert", |
| 430 | + thresholds=[AlertThreshold(code="warn", value="1000")], |
| 431 | + ), |
| 432 | + Alert( |
| 433 | + alert_type="billable_metric_current_usage_amount", |
| 434 | + code="alert2", |
| 435 | + billable_metric_code="storage", |
| 436 | + thresholds=[AlertThreshold(value="2000")], |
| 437 | + ), |
| 438 | + ] |
| 439 | + ) |
| 440 | + |
| 441 | + response = client.subscriptions.create_alerts(external_id, input_object) |
| 442 | + |
| 443 | + assert len(response["alerts"]) == 2 |
| 444 | + assert response["alerts"][0].lago_id == "1a901a90-1a90-1a90-1a90-1a901a901a90" |
| 445 | + assert response["alerts"][0].code == "alert1" |
| 446 | + assert response["alerts"][0].alert_type == "current_usage_amount" |
| 447 | + assert response["alerts"][1].lago_id == "3c903c90-3c90-3c90-3c90-3c903c903c90" |
| 448 | + assert response["alerts"][1].code == "alert2" |
| 449 | + |
| 450 | + |
| 451 | +def test_invalid_create_alerts_request(httpx_mock: HTTPXMock): |
| 452 | + client = Client(api_key="invalid") |
| 453 | + external_id = "sub_1234" |
| 454 | + |
| 455 | + httpx_mock.add_response( |
| 456 | + method="POST", |
| 457 | + url="https://api.getlago.com/api/v1/subscriptions/" + external_id + "/alerts", |
| 458 | + status_code=422, |
| 459 | + content=b"", |
| 460 | + ) |
| 461 | + |
| 462 | + input_object = AlertsList(alerts=[]) |
| 463 | + |
| 464 | + with pytest.raises(LagoApiError): |
| 465 | + client.subscriptions.create_alerts(external_id, input_object) |
| 466 | + |
| 467 | + |
| 468 | +def test_valid_delete_alerts_request(httpx_mock: HTTPXMock): |
| 469 | + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") |
| 470 | + external_id = "sub_1234" |
| 471 | + |
| 472 | + httpx_mock.add_response( |
| 473 | + method="DELETE", |
| 474 | + url="https://api.getlago.com/api/v1/subscriptions/" + external_id + "/alerts", |
| 475 | + status_code=200, |
| 476 | + content=b"", |
| 477 | + ) |
| 478 | + |
| 479 | + result = client.subscriptions.delete_alerts(external_id) |
| 480 | + assert result is None |
| 481 | + |
| 482 | + |
| 483 | +def test_invalid_delete_alerts_request(httpx_mock: HTTPXMock): |
| 484 | + client = Client(api_key="invalid") |
| 485 | + external_id = "invalid_sub" |
| 486 | + |
| 487 | + httpx_mock.add_response( |
| 488 | + method="DELETE", |
| 489 | + url="https://api.getlago.com/api/v1/subscriptions/" + external_id + "/alerts", |
| 490 | + status_code=404, |
| 491 | + content=b"", |
| 492 | + ) |
| 493 | + |
| 494 | + with pytest.raises(LagoApiError): |
| 495 | + client.subscriptions.delete_alerts(external_id) |
0 commit comments