diff --git a/backend/internal/httpapi/task_api.go b/backend/internal/httpapi/task_api.go index e31658d..f813d56 100644 --- a/backend/internal/httpapi/task_api.go +++ b/backend/internal/httpapi/task_api.go @@ -20,6 +20,10 @@ func (s *Server) issueDomainAsTask(c *gin.Context) { s.writeError(c, http.StatusNotImplemented, "证书申请接口尚未配置", errors.New("证书申请工作流未配置")) return } + if s.repository == nil || s.tasks == nil { + s.writeError(c, http.StatusNotImplemented, "证书任务接口尚未配置", errors.New("域名仓储或任务仓储未配置")) + return + } domainID := strings.TrimSpace(c.Param("id")) if _, err := s.repository.GetByID(c.Request.Context(), domainID); err != nil { if repository.IsNotFound(err) { diff --git a/backend/internal/httpapi/task_api_test.go b/backend/internal/httpapi/task_api_test.go index 9a65038..31e7528 100644 --- a/backend/internal/httpapi/task_api_test.go +++ b/backend/internal/httpapi/task_api_test.go @@ -224,3 +224,19 @@ func TestTaskAPIReplaysIdempotentIssue(t *testing.T) { t.Fatalf("幂等键没有正确保存:%+v %v", items, err) } } + +func TestTaskAPIReturnsNotImplementedWhenTaskDependenciesAreMissing(t *testing.T) { + cfg := readyTestConfig(t.TempDir()) + server := New(Dependencies{ + Config: cfg, + Tasks: (*repository.SQLTaskRepository)(nil), + IssueCertificate: func(context.Context, string) (certificate.IssueResult, deployment.Report, error) { + return certificate.IssueResult{}, deployment.Report{}, nil + }, + }) + response := httptest.NewRecorder() + serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil)) + if response.Code != http.StatusNotImplemented || !strings.Contains(response.Body.String(), "证书任务接口尚未配置") { + t.Fatalf("任务依赖缺失时应返回 501:%d %s", response.Code, response.Body.String()) + } +}