Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

6.6 KB
Raw Blame
"""Resend management adapter tests without live provider credentials."""

from __future__ import annotations

from dataclasses import dataclass, replace

import httpx
import pytest

from core.human_input_v2.shared import NormalizedEmail
from repositories.human_input_v2.email_channel import (
    EmailProviderOperationError,
    EmailProviderValidationError,
    ResendCandidate,
)
from repositories.human_input_v2.email_channel.entities import ResendCredentials
from services.human_input_v2.resend_channel import ResendProviderGateway


@dataclass
class FakeResponse:
    status_code: int
    body: object

    def json(self) -> object:
        if isinstance(self.body, Exception):
            raise self.body
        return self.body


class FakeHTTPClient:
    def __init__(self, *responses: FakeResponse | Exception) -> None:
        self.responses = list(responses)
        self.calls: list[tuple[str, str, dict[str, object]]] = []

    def post(self, url: str, **kwargs: object) -> FakeResponse:
        return self._request("POST", url, kwargs)

    def _request(self, method: str, url: str, kwargs: dict[str, object]) -> FakeResponse:
        self.calls.append((method, url, kwargs))
        result = self.responses.pop(0)
        if isinstance(result, Exception):
            raise result
        return result


def _candidate() -> ResendCandidate:
    return ResendCandidate(
        sender_email=NormalizedEmail("[email protected]"),
        sender_name="Approvals",
        credentials=ResendCredentials(api_key="re_secret"),
    )


def test_send_test_uses_resend_test_recipient_once_with_unique_idempotency_key() -> None:
    http_client = FakeHTTPClient(FakeResponse(200, {"id": "email-1"}))
    gateway = ResendProviderGateway(
        http_client=http_client,
        id_factory=lambda: "test-1",
    )

    gateway.send_test(_candidate())

    assert len(http_client.calls) == 1
    method, url, kwargs = http_client.calls[0]
    assert method == "POST"
    assert url == "https://api.resend.com/emails"
    headers = kwargs["headers"]
    assert isinstance(headers, dict)
    assert headers["Idempotency-Key"] == "human-input-channel-test/test-1"
    assert headers["Authorization"] == "Bearer re_secret"
    assert headers["User-Agent"] == "Dify-Human-Input/1.0"
    assert kwargs["max_retries"] == 0
    assert kwargs["json"] == {
        "from": "Approvals <[email protected]>",
        "to": ["[email protected]"],
        "subject": "Dify Human Input channel test",
        "html": (
            "<p>Your Resend channel is connected to Dify Human Input.</p>"
            "<p>You can close this email after confirming delivery.</p>"
        ),
    }


def test_send_test_rejects_invalid_credentials_after_a_successful_test() -> None:
    http_client = FakeHTTPClient(
        FakeResponse(200, {"id": "email-1"}),
        FakeResponse(403, {"name": "invalid_api_key"}),
    )
    gateway = ResendProviderGateway(http_client=http_client)

    gateway.send_test(_candidate())
    with pytest.raises(EmailProviderValidationError):
        gateway.send_test(replace(_candidate(), credentials=ResendCredentials(api_key="re_invalid")))

    for (_, _, kwargs), api_key in zip(http_client.calls, ("re_secret", "re_invalid"), strict=True):
        headers = kwargs["headers"]
        assert isinstance(headers, dict)
        assert headers["Authorization"] == f"Bearer {api_key}"


@pytest.mark.parametrize(
    ("response", "exception_type", "code"),
    [
        (FakeResponse(403, {"name": "invalid_api_key"}), EmailProviderValidationError, "invalid_api_key"),
        (FakeResponse(403, {"name": "restricted_api_key"}), EmailProviderValidationError, "invalid_api_key"),
        (
            FakeResponse(422, {"name": "invalid_from_address"}),
            EmailProviderValidationError,
            "invalid_sender",
        ),
        (
            FakeResponse(429, {"name": "monthly_quota_exceeded"}),
            EmailProviderOperationError,
            "provider_quota_exhausted",
        ),
        (FakeResponse(503, {}), EmailProviderOperationError, "provider_unavailable"),
        (FakeResponse(200, {}), EmailProviderOperationError, "provider_response_malformed"),
    ],
)
def test_send_test_maps_provider_responses_to_safe_failures(
    response: FakeResponse,
    exception_type: type[EmailProviderValidationError] | type[EmailProviderOperationError],
    code: str,
) -> None:
    gateway = ResendProviderGateway(http_client=FakeHTTPClient(response))

    with pytest.raises(exception_type) as raised:
        gateway.send_test(_candidate())

    assert raised.value.code == code
    assert "re_secret" not in repr(raised.value)


def test_transport_failure_is_classified_without_credential_material() -> None:
    request = httpx.Request("POST", "https://api.resend.com/emails")
    gateway = ResendProviderGateway(http_client=FakeHTTPClient(httpx.ReadTimeout("request timed out", request=request)))

    with pytest.raises(EmailProviderOperationError) as raised:
        gateway.send_test(_candidate())

    assert raised.value.code == "provider_timeout"
    assert "re_secret" not in repr(raised.value)


@pytest.mark.parametrize(
    "name", ["restricted_api_key", "validation_error", "monthly_quota_exceeded", "new_provider_error"]
)
def test_provider_error_preserves_original_resend_code_without_raw_message(name: str) -> None:
    gateway = ResendProviderGateway(
        http_client=FakeHTTPClient(
            FakeResponse(
                403,
                {
                    "name": name,
                    "message": "raw response containing re_secret",
                },
            )
        )
    )

    with pytest.raises((EmailProviderValidationError, EmailProviderOperationError)) as captured:
        gateway.send_test(_candidate())

    assert captured.value.provider_error_code == name
    assert "re_secret" not in repr(captured.value)


def test_send_message_uses_rendered_content_and_selected_channel_credentials() -> None:
    http_client = FakeHTTPClient(FakeResponse(200, {"id": "email-1"}))
    gateway = ResendProviderGateway(http_client=http_client, id_factory=lambda: "message-1")

    gateway.send_message(_candidate(), to="[email protected]", subject="Rendered subject", html="<p>Rendered body</p>")

    assert len(http_client.calls) == 1
    _, _, kwargs = http_client.calls[0]
    assert kwargs["json"] == {
        "from": "Approvals <[email protected]>",
        "to": ["[email protected]"],
        "subject": "Rendered subject",
        "html": "<p>Rendered body</p>",
    }
    headers = kwargs["headers"]
    assert isinstance(headers, dict)
    assert headers["Idempotency-Key"] == "human-input-message/message-1"
    assert kwargs["max_retries"] == 0