4b6817381b
Benchmark image — build + push to ECR (any adapter) / build + push (push) Waiting to run
CI / quality (ubuntu-latest) (push) Waiting to run
CI / test (tools-runtime) (push) Waiting to run
CI / test (e2e-general) (push) Waiting to run
CI / test (cli-runtime) (push) Waiting to run
CI / test (e2e-provider-and-openclaw) (push) Waiting to run
CI / test (integrations-and-misc) (push) Waiting to run
CI / coverage-report (push) Blocked by required conditions
CI / test-kubernetes (push) Waiting to run
CI / should-run-thorough (push) Waiting to run
CI / test-thorough (cloudwatch-demo) (push) Blocked by required conditions
CI / test-thorough (flink-ecs) (push) Blocked by required conditions
CI / test-thorough (upstream-lambda) (push) Blocked by required conditions
CI / test-thorough (prefect-ecs-fargate) (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Blocked by required conditions
Release / publish-release (push) Blocked by required conditions
Release / publish-main-release (push) Blocked by required conditions
Release / prepare (push) Waiting to run
Release / verify (push) Blocked by required conditions
Release / build-python-dist (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Blocked by required conditions
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Waiting to run
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
222 lines
6.9 KiB
Python
222 lines
6.9 KiB
Python
"""Tests for the Twilio SMS integration: config, catalog, verifier."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from integrations.config_models import TwilioIntegrationConfig, TwilioSMSChannelConfig
|
|
from integrations.twilio.verifier import verify_twilio as _verify_twilio
|
|
|
|
|
|
class _FakeResponse:
|
|
def __init__(self, payload: Any, status_code: int = 200) -> None:
|
|
self._payload = payload
|
|
self.status_code = status_code
|
|
|
|
def raise_for_status(self) -> None:
|
|
if self.status_code >= 400:
|
|
raise Exception(f"HTTP {self.status_code}")
|
|
|
|
def json(self) -> Any:
|
|
return self._payload
|
|
|
|
|
|
# ---- TwilioIntegrationConfig --------------------------------------------------
|
|
|
|
|
|
def test_config_accepts_sms_with_from_number() -> None:
|
|
config = TwilioIntegrationConfig(
|
|
account_sid="AC1",
|
|
auth_token="tok",
|
|
sms=TwilioSMSChannelConfig(enabled=True, from_number="+14155551111"),
|
|
)
|
|
assert config.configured_channels == ["sms"]
|
|
|
|
|
|
def test_config_accepts_sms_via_messaging_service_sid() -> None:
|
|
config = TwilioIntegrationConfig(
|
|
account_sid="AC1",
|
|
auth_token="tok",
|
|
sms=TwilioSMSChannelConfig(enabled=True, messaging_service_sid="MG1"),
|
|
)
|
|
assert config.configured_channels == ["sms"]
|
|
|
|
|
|
def test_config_rejects_missing_sms_channel() -> None:
|
|
with pytest.raises(ValueError, match="SMS channel configured"):
|
|
TwilioIntegrationConfig(
|
|
account_sid="AC1",
|
|
auth_token="tok",
|
|
sms=TwilioSMSChannelConfig(enabled=False),
|
|
)
|
|
|
|
|
|
def test_config_rejects_enabled_sms_without_sender() -> None:
|
|
with pytest.raises(ValueError, match="SMS channel configured"):
|
|
TwilioIntegrationConfig(
|
|
account_sid="AC1",
|
|
auth_token="tok",
|
|
sms=TwilioSMSChannelConfig(enabled=True, from_number=""),
|
|
)
|
|
|
|
|
|
def test_config_rejects_blank_account_sid() -> None:
|
|
with pytest.raises(ValueError, match="account_sid"):
|
|
TwilioIntegrationConfig(
|
|
account_sid=" ",
|
|
auth_token="tok",
|
|
sms=TwilioSMSChannelConfig(enabled=True, from_number="+1"),
|
|
)
|
|
|
|
|
|
def test_config_rejects_blank_auth_token() -> None:
|
|
with pytest.raises(ValueError, match="auth_token"):
|
|
TwilioIntegrationConfig(
|
|
account_sid="AC1",
|
|
auth_token=" ",
|
|
sms=TwilioSMSChannelConfig(enabled=True, from_number="+1"),
|
|
)
|
|
|
|
|
|
# ---- _verify_twilio -----------------------------------------------------------
|
|
|
|
|
|
def test_verify_missing_account_sid() -> None:
|
|
result = _verify_twilio("env", {"auth_token": "tok"})
|
|
assert result["status"] == "missing"
|
|
assert "account_sid" in result["detail"].lower()
|
|
|
|
|
|
def test_verify_missing_auth_token() -> None:
|
|
result = _verify_twilio("env", {"account_sid": "AC1"})
|
|
assert result["status"] == "missing"
|
|
assert "auth_token" in result["detail"].lower()
|
|
|
|
|
|
def test_verify_passed_when_sms_ready(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(
|
|
"integrations.twilio.verifier.requests.get",
|
|
lambda *_a, **_kw: _FakeResponse({"friendly_name": "Demo"}),
|
|
)
|
|
|
|
result = _verify_twilio(
|
|
"env",
|
|
{
|
|
"account_sid": "AC1",
|
|
"auth_token": "tok",
|
|
"sms": {"enabled": True, "from_number": "+14155551111"},
|
|
},
|
|
)
|
|
|
|
assert result["status"] == "passed"
|
|
assert "sms" in result["detail"].lower()
|
|
|
|
|
|
def test_verify_passed_with_messaging_service_sid(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(
|
|
"integrations.twilio.verifier.requests.get",
|
|
lambda *_a, **_kw: _FakeResponse({"friendly_name": "Demo"}),
|
|
)
|
|
|
|
result = _verify_twilio(
|
|
"env",
|
|
{
|
|
"account_sid": "AC1",
|
|
"auth_token": "tok",
|
|
"sms": {"enabled": True, "messaging_service_sid": "MG1"},
|
|
},
|
|
)
|
|
|
|
assert result["status"] == "passed"
|
|
|
|
|
|
def test_verify_failed_when_sms_not_ready(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(
|
|
"integrations.twilio.verifier.requests.get",
|
|
lambda *_a, **_kw: _FakeResponse({"friendly_name": "Demo"}),
|
|
)
|
|
|
|
result = _verify_twilio(
|
|
"env",
|
|
{
|
|
"account_sid": "AC1",
|
|
"auth_token": "tok",
|
|
"sms": {"enabled": False, "from_number": ""},
|
|
},
|
|
)
|
|
|
|
assert result["status"] == "failed"
|
|
assert "sms channel is not ready" in result["detail"].lower()
|
|
|
|
|
|
def test_verify_failed_when_api_errors(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
def _raise(*_a: Any, **_kw: Any) -> Any:
|
|
raise Exception("Connection timeout")
|
|
|
|
monkeypatch.setattr("integrations.twilio.verifier.requests.get", _raise)
|
|
|
|
result = _verify_twilio("env", {"account_sid": "AC1", "auth_token": "tok"})
|
|
|
|
assert result["status"] == "failed"
|
|
assert "Connection timeout" in result["detail"]
|
|
|
|
|
|
# ---- Catalog env-bootstrap ----------------------------------------------------
|
|
|
|
|
|
def test_catalog_bootstraps_twilio_from_env_with_sms(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setenv("TWILIO_ACCOUNT_SID", "AC1")
|
|
monkeypatch.setenv("TWILIO_AUTH_TOKEN", "tok")
|
|
monkeypatch.setenv("TWILIO_SMS_FROM", "+14155551111")
|
|
monkeypatch.setenv("TWILIO_SMS_DEFAULT_TO", "+14155550000")
|
|
|
|
from integrations.catalog import resolve_effective_integrations
|
|
|
|
effective = resolve_effective_integrations()
|
|
|
|
assert "twilio" in effective
|
|
twilio = effective["twilio"]["config"]
|
|
assert twilio["account_sid"] == "AC1"
|
|
assert twilio["sms"]["enabled"] is True
|
|
assert twilio["sms"]["from_number"] == "+14155551111"
|
|
assert twilio["sms"]["default_to"] == "+14155550000"
|
|
assert "whatsapp" not in twilio
|
|
|
|
|
|
def test_catalog_bootstraps_legacy_whatsapp_independently(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""The legacy ``whatsapp`` record is unaffected by the Twilio SMS integration."""
|
|
monkeypatch.setenv("TWILIO_ACCOUNT_SID", "AC1")
|
|
monkeypatch.setenv("TWILIO_AUTH_TOKEN", "tok")
|
|
monkeypatch.setenv("TWILIO_WHATSAPP_FROM", "whatsapp:+14155238886")
|
|
monkeypatch.delenv("TWILIO_SMS_FROM", raising=False)
|
|
monkeypatch.delenv("TWILIO_SMS_MESSAGING_SERVICE_SID", raising=False)
|
|
|
|
from integrations.catalog import resolve_effective_integrations
|
|
|
|
effective = resolve_effective_integrations()
|
|
|
|
assert "whatsapp" in effective
|
|
# WhatsApp-only env does NOT create a twilio record (SMS sender absent).
|
|
assert "twilio" not in effective
|
|
|
|
|
|
def test_catalog_skips_twilio_without_sms_env(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setenv("TWILIO_ACCOUNT_SID", "AC1")
|
|
monkeypatch.setenv("TWILIO_AUTH_TOKEN", "tok")
|
|
monkeypatch.delenv("TWILIO_SMS_FROM", raising=False)
|
|
monkeypatch.delenv("TWILIO_SMS_MESSAGING_SERVICE_SID", raising=False)
|
|
|
|
from integrations.catalog import resolve_effective_integrations
|
|
|
|
effective = resolve_effective_integrations()
|
|
|
|
assert "twilio" not in effective
|