mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 23:51:41 +00:00
ec21c9bb29
M28: ACME Renewal Information (RFC 9702) — CA-directed renewal timing with cert ID computation, directory endpoint discovery, graceful degradation for non-ARI CAs. 19 tests. M29: Email notifier wiring + scheduled certificate digest — SMTP connector bridged to service layer via NotifierAdapter, DigestService with HTML email template, 7th scheduler loop (24h), digest preview/send API endpoints and GUI card. 21 tests. M30: Production-ready Helm chart — server Deployment, PostgreSQL StatefulSet, agent DaemonSet, ConfigMaps, Secrets, Ingress, security contexts, health probes, example values for dev/prod/ACME scenarios. Also: OpenAPI spec updates, MCP tool additions, CI helm-lint job, documentation updates across 5 doc files and README. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
158 lines
3.7 KiB
Go
158 lines
3.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// mockDigestService implements DigestServicer for testing.
|
|
type mockDigestService struct {
|
|
previewHTML string
|
|
previewErr error
|
|
sendErr error
|
|
sendCalled bool
|
|
}
|
|
|
|
func (m *mockDigestService) PreviewDigest(ctx context.Context) (string, error) {
|
|
if m.previewErr != nil {
|
|
return "", m.previewErr
|
|
}
|
|
return m.previewHTML, nil
|
|
}
|
|
|
|
func (m *mockDigestService) SendDigest(ctx context.Context) error {
|
|
m.sendCalled = true
|
|
return m.sendErr
|
|
}
|
|
|
|
func TestDigestHandler_PreviewDigest_Success(t *testing.T) {
|
|
svc := &mockDigestService{
|
|
previewHTML: "<html><body>Digest Preview</body></html>",
|
|
}
|
|
h := NewDigestHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/digest/preview", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.PreviewDigest(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("expected status 200, got %d", w.Code)
|
|
}
|
|
|
|
if w.Header().Get("Content-Type") != "text/html; charset=utf-8" {
|
|
t.Errorf("expected Content-Type text/html, got %s", w.Header().Get("Content-Type"))
|
|
}
|
|
|
|
if w.Body.String() != "<html><body>Digest Preview</body></html>" {
|
|
t.Errorf("unexpected body: %s", w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestDigestHandler_PreviewDigest_MethodNotAllowed(t *testing.T) {
|
|
svc := &mockDigestService{}
|
|
h := NewDigestHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/digest/preview", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.PreviewDigest(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("expected status 405, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDigestHandler_PreviewDigest_ServiceError(t *testing.T) {
|
|
svc := &mockDigestService{
|
|
previewErr: errors.New("stats unavailable"),
|
|
}
|
|
h := NewDigestHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/digest/preview", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.PreviewDigest(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Errorf("expected status 500, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDigestHandler_PreviewDigest_NotConfigured(t *testing.T) {
|
|
h := NewDigestHandler(nil)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/digest/preview", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.PreviewDigest(w, req)
|
|
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Errorf("expected status 503, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDigestHandler_SendDigest_Success(t *testing.T) {
|
|
svc := &mockDigestService{}
|
|
h := NewDigestHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/digest/send", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.SendDigest(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("expected status 200, got %d", w.Code)
|
|
}
|
|
|
|
if !svc.sendCalled {
|
|
t.Error("expected SendDigest to be called")
|
|
}
|
|
}
|
|
|
|
func TestDigestHandler_SendDigest_MethodNotAllowed(t *testing.T) {
|
|
svc := &mockDigestService{}
|
|
h := NewDigestHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/digest/send", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.SendDigest(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("expected status 405, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDigestHandler_SendDigest_ServiceError(t *testing.T) {
|
|
svc := &mockDigestService{
|
|
sendErr: errors.New("SMTP connection refused"),
|
|
}
|
|
h := NewDigestHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/digest/send", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.SendDigest(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Errorf("expected status 500, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDigestHandler_SendDigest_NotConfigured(t *testing.T) {
|
|
h := NewDigestHandler(nil)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/digest/send", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
h.SendDigest(w, req)
|
|
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Errorf("expected status 503, got %d", w.Code)
|
|
}
|
|
}
|