chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

@@ -0,0 +1,215 @@
from collections.abc import Iterator
from inspect import unwrap
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from flask import Flask
from controllers.console.billing.billing import Invoices, Subscription, SubscriptionQuery
from controllers.console.billing.error import (
BillingOperationFailedError,
BillingUnavailableError,
)
from enums import CloudPlan
from machinery.context import RequestContext
from services.billing_client import billing
from services.errors.billing import (
UnknownErrorReason,
)
class TestBillingPortal:
@pytest.fixture
def app(self) -> Flask:
app = Flask(__name__)
app.config["TESTING"] = True
return app
@pytest.fixture
def request_context(self) -> RequestContext:
return RequestContext(
request_id="request-1",
trace_id="trace-1",
account_id="account-1",
active_workspace_id="tenant-1",
)
@pytest.fixture
def billing_portal(self) -> MagicMock:
return MagicMock()
@pytest.fixture(autouse=True)
def mock_application_services(self, billing_portal: MagicMock) -> Iterator[None]:
with patch(
"controllers.console.billing.billing.application_services",
return_value=SimpleNamespace(billing_portal=billing_portal),
):
yield
def test_get_subscription_uses_admission_context_and_response_contract(
self,
app: Flask,
request_context: RequestContext,
billing_portal: MagicMock,
) -> None:
resource = Subscription()
method = unwrap(resource.get)
query = SubscriptionQuery(plan=CloudPlan.PROFESSIONAL, interval="month")
billing_portal.get_subscription.return_value = billing.GetSubscriptionPaymentLinkReply(
url="https://billing.example.com/checkout"
)
with app.test_request_context("/billing/subscription"):
result = method(resource, query, request_context)
billing_portal.get_subscription.assert_called_once_with(
request_context,
plan=CloudPlan.PROFESSIONAL,
interval="month",
)
assert result == {"url": "https://billing.example.com/checkout"}
def test_get_invoices_uses_admission_context_and_response_contract(
self,
app: Flask,
request_context: RequestContext,
billing_portal: MagicMock,
) -> None:
resource = Invoices()
method = unwrap(resource.get)
billing_portal.get_invoices.return_value = billing.GetInvoicesReply(url="https://billing.example.com/portal")
with app.test_request_context("/billing/invoices"):
result = method(resource, request_context)
billing_portal.get_invoices.assert_called_once_with(request_context)
assert result == {"url": "https://billing.example.com/portal"}
def test_get_invoices_translates_unavailable_operation(
self,
app: Flask,
request_context: RequestContext,
billing_portal: MagicMock,
) -> None:
resource = Invoices()
method = unwrap(resource.get)
billing_portal.get_invoices.side_effect = billing.BillingInvoiceUpstreamUnavailableError
with app.test_request_context("/billing/invoices"):
with pytest.raises(BillingUnavailableError) as exc_info:
method(resource, request_context)
assert exc_info.value.data == {
"code": "billing_unavailable",
"message": "This operation is temporarily unavailable. Please try again later.",
"status": 503,
}
def test_get_subscription_translates_invalid_upstream_response(
self,
app: Flask,
request_context: RequestContext,
billing_portal: MagicMock,
) -> None:
resource = Subscription()
method = unwrap(resource.get)
query = SubscriptionQuery(plan=CloudPlan.PROFESSIONAL, interval="month")
billing_portal.get_subscription.side_effect = billing.BillingSubscriptionUpstreamInvalidResponseError
with app.test_request_context("/billing/subscription"):
with pytest.raises(BillingOperationFailedError) as exc_info:
method(resource, query, request_context)
assert exc_info.value.data == {
"code": "billing_operation_failed",
"message": "We couldn't complete this request. Please try again. If the problem persists, contact support.",
"status": 502,
}
@pytest.mark.parametrize(
("error", "expected"),
[
(
billing.BillingSubscriptionUpstreamInvalidResponseError(status_code=400, reason="MISSING_PLAN"),
BillingOperationFailedError,
),
(
billing.BillingSubscriptionUpstreamUnavailableError(
status_code=500, reason="CREATE_SUBSCRIPTION_PAYMENT_LINK_FAILED"
),
BillingUnavailableError,
),
(
billing.BillingSubscriptionUpstreamUnavailableError(status_code=429, reason="REQUEST_IN_PROGRESS"),
BillingUnavailableError,
),
(
billing.BillingSubscriptionUpstreamInvalidResponseError(
status_code=409, reason=UnknownErrorReason("PLAN_SHIPPED_LATER")
),
BillingOperationFailedError,
),
(
billing.BillingSubscriptionUpstreamUnavailableError(
status_code=503, reason=UnknownErrorReason("PLAN_SHIPPED_LATER")
),
BillingUnavailableError,
),
(billing.BillingSubscriptionUpstreamInvalidResponseError(status_code=400), BillingOperationFailedError),
(billing.BillingSubscriptionUpstreamUnavailableError(), BillingUnavailableError),
],
)
def test_get_subscription_maps_every_reason_to_a_generic_billing_error(
self,
app: Flask,
request_context: RequestContext,
billing_portal: MagicMock,
error: billing.BillingSubscriptionUpstreamError,
expected: type[Exception],
) -> None:
resource = Subscription()
billing_portal.get_subscription.side_effect = error
with app.test_request_context("/billing/subscription"), pytest.raises(expected) as exc_info:
unwrap(resource.get)(
resource, SubscriptionQuery(plan=CloudPlan.PROFESSIONAL, interval="month"), request_context
)
assert exc_info.value.__cause__ is error
@pytest.mark.parametrize(
("error", "expected"),
[
(
billing.BillingInvoiceUpstreamInvalidResponseError(status_code=400, reason="INVALID_INVOICE_REQUEST"),
BillingOperationFailedError,
),
(
billing.BillingInvoiceUpstreamUnavailableError(status_code=500, reason="CREATE_INVOICE_PORTAL_FAILED"),
BillingUnavailableError,
),
(
billing.BillingInvoiceUpstreamInvalidResponseError(
status_code=409, reason=UnknownErrorReason("INVOICE_SHIPPED_LATER")
),
BillingOperationFailedError,
),
(billing.BillingInvoiceUpstreamUnavailableError(status_code=502), BillingUnavailableError),
],
)
def test_get_invoices_maps_every_reason_to_a_generic_billing_error(
self,
app: Flask,
request_context: RequestContext,
billing_portal: MagicMock,
error: billing.BillingInvoiceUpstreamError,
expected: type[Exception],
) -> None:
resource = Invoices()
billing_portal.get_invoices.side_effect = error
with app.test_request_context("/billing/invoices"), pytest.raises(expected) as exc_info:
unwrap(resource.get)(resource, request_context)
assert exc_info.value.__cause__ is error
@@ -0,0 +1,172 @@
from collections.abc import Iterator
from inspect import unwrap
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from flask import Flask
from controllers.console.billing.compliance import ComplianceApi, ComplianceDownloadQuery
from controllers.console.billing.error import ComplianceRateLimitError
from libs.exception import BaseHTTPException
from machinery.context import RequestContext
from services.billing_client import billing
from services.errors.billing import (
ComplianceRateLimitExceededError,
UnknownErrorReason,
)
@pytest.fixture
def app() -> Flask:
app = Flask(__name__)
app.config["TESTING"] = True
return app
@pytest.fixture
def request_context() -> RequestContext:
return RequestContext(
request_id="request-1",
trace_id="trace-1",
account_id="account-1",
active_workspace_id="tenant-1",
)
@pytest.fixture
def compliance_downloads() -> MagicMock:
return MagicMock()
@pytest.fixture(autouse=True)
def mock_application_services(compliance_downloads: MagicMock) -> Iterator[None]:
with patch(
"controllers.console.billing.compliance.application_services",
return_value=SimpleNamespace(compliance_downloads=compliance_downloads),
):
yield
def test_get_compliance_download_link(
app: Flask,
request_context: RequestContext,
compliance_downloads: MagicMock,
) -> None:
resource = ComplianceApi()
method = unwrap(resource.get)
query = ComplianceDownloadQuery(doc_name="SOC2_Type_II")
compliance_downloads.get_link.return_value = billing.DownloadComplianceDocumentReply(
url="https://example.com/report"
)
with (
app.test_request_context("/compliance/download", headers={"User-Agent": "test-agent"}),
patch("controllers.console.billing.compliance.extract_remote_ip", return_value="127.0.0.1"),
):
result = method(resource, query, request_context)
assert result == {"url": "https://example.com/report"}
compliance_downloads.get_link.assert_called_once_with(
request_context=request_context,
document_name="SOC2_Type_II",
ip_address="127.0.0.1",
device_info="test-agent",
)
def test_get_compliance_download_translates_rate_limit(
app: Flask,
request_context: RequestContext,
compliance_downloads: MagicMock,
) -> None:
resource = ComplianceApi()
method = unwrap(resource.get)
query = ComplianceDownloadQuery(doc_name="SOC2_Type_II")
compliance_downloads.get_link.side_effect = ComplianceRateLimitExceededError
with (
app.test_request_context("/compliance/download"),
patch("controllers.console.billing.compliance.extract_remote_ip", return_value="127.0.0.1"),
):
with pytest.raises(ComplianceRateLimitError) as exc_info:
method(resource, query, request_context)
assert exc_info.value.data == {
"code": "compliance_rate_limit",
"message": "Rate limit exceeded for downloading compliance report.",
"status": 429,
}
@pytest.mark.parametrize(
("error", "expected_code", "expected_status"),
[
(
billing.BillingComplianceUpstreamInvalidResponseError(
status_code=404, reason="COMPLIANCE_DOCUMENT_NOT_FOUND"
),
"compliance_document_not_found",
404,
),
(
billing.BillingComplianceUpstreamInvalidResponseError(
status_code=403, reason="COMPLIANCE_DOWNLOAD_FORBIDDEN"
),
"compliance_download_forbidden",
403,
),
(
billing.BillingComplianceUpstreamUnavailableError(status_code=429, reason="REQUEST_IN_PROGRESS"),
"billing_request_in_progress",
429,
),
(
billing.BillingComplianceUpstreamUnavailableError(status_code=500, reason="COMPLIANCE_DOWNLOAD_FAILED"),
"billing_unavailable",
503,
),
(
billing.BillingComplianceUpstreamInvalidResponseError(status_code=400, reason="MISSING_DOC_NAME"),
"billing_operation_failed",
502,
),
(
billing.BillingComplianceUpstreamInvalidResponseError(
status_code=403, reason=UnknownErrorReason("COMPLIANCE_SHIPPED_LATER")
),
"billing_operation_failed",
502,
),
(
billing.BillingComplianceUpstreamUnavailableError(
status_code=503, reason=UnknownErrorReason("COMPLIANCE_SHIPPED_LATER")
),
"billing_unavailable",
503,
),
(billing.BillingComplianceUpstreamInvalidResponseError(status_code=404), "billing_operation_failed", 502),
(billing.BillingComplianceUpstreamUnavailableError(), "billing_unavailable", 503),
],
)
def test_get_compliance_download_maps_billing_reason(
app: Flask,
request_context: RequestContext,
compliance_downloads: MagicMock,
error: billing.BillingComplianceUpstreamError,
expected_code: str,
expected_status: int,
) -> None:
resource = ComplianceApi()
compliance_downloads.get_link.side_effect = error
with (
app.test_request_context("/compliance/download"),
patch("controllers.console.billing.compliance.extract_remote_ip", return_value="127.0.0.1"),
pytest.raises(BaseHTTPException) as exc_info,
):
unwrap(resource.get)(resource, ComplianceDownloadQuery(doc_name="SOC2_Type_II"), request_context)
assert exc_info.value.error_code == expected_code
assert exc_info.value.code == expected_status
assert exc_info.value.__cause__ is error
@@ -0,0 +1,9 @@
import pytest
from controllers.console.billing.error import to_billing_request_error
from services.errors.billing import BillingError
def test_to_billing_request_error_rejects_unknown_error() -> None:
with pytest.raises(TypeError, match="Unsupported billing error"):
to_billing_request_error(BillingError())