mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 17:02:43 +00:00
8b75e0311b
Mechanical sed across the main go.mod's module declaration, the f5-mock-icontrol
sub-module's go.mod, every Go file's import path (361 files), and a rebuild of
the checked-in f5-mock-icontrol binary so its embedded build-info reflects the
new module path. No behavior change.
Choice B from cowork/transfer-certctl-to-org.md, executed 2026-05-04. Choice A
(keep module path declared as github.com/shankar0123/certctl regardless of
repo URL) shipped on the day of the org transfer (2026-05-03) since we had no
external Go consumers; this commit closes that deferral.
Backward-compat: GitHub HTTP redirects continue to forward
github.com/shankar0123/certctl → github.com/certctl-io/certctl at the URL
level, but Go's module proxy uses the path declared in go.mod as the
canonical name. Pre-fix, anyone trying `go get github.com/certctl-io/certctl/...`
hit a "module path mismatch" error because go.mod said
github.com/shankar0123/certctl and the URL they fetched it from said
certctl-io/certctl. Post-fix, the canonical name and the URL agree, so
go get / go install / external Go consumers / Go-tooling integrations
work cleanly via either the new path (preferred) or the old path (which
redirects and Go follows the redirect for source fetch).
Anyone still importing the old path inside their own code keeps working
provided they update their go.mod's `require` line to match — the module
path declared in their consumer's go.sum / go.mod is the authoritative
import name, so a mass sed across their import statements is the migration
on the consumer side. No external consumers exist today.
Diff shape:
361 *.go files — import path replacement only
2 go.mod — module declaration replacement only
1 binary — deploy/test/f5-mock-icontrol/f5-mock-icontrol rebuilt
so embedded build-info reflects the new path (8618965 vs
8618933 bytes; 32-byte diff is the build-info change)
Total: 364 files, 730 insertions / 730 deletions, net-zero size, pure
mechanical substitution.
Verification:
gofmt: 17 files needed re-alignment after sed (the new path is one char
shorter than the old, so column-aligned import groups drifted). Applied
`gofmt -w` to fix.
go mod tidy: clean exit on both modules.
go vet ./...: clean exit.
go build ./...: clean exit.
go test -short -count=1 on representative packages: all green
(internal/domain, internal/validation, internal/crypto, internal/crypto/signer,
cmd/agent). Test output now reads `ok github.com/certctl-io/certctl/...`
confirming the module path resolves correctly.
binary: f5-mock-icontrol rebuilt; `strings | grep shankar0123` returns
nothing; `strings | grep certctl-io/certctl` shows the new module path
embedded in build-info.
Files intentionally NOT touched in this commit:
README.md / CHANGELOG.md / docs/ / etc. — already swept to certctl-io
URLs in commit 0729ee4 (the post-transfer URL refresh). This commit is
purely the Go-tooling layer.
Scarf pixels (`shankar0123.docker.scarf.sh/...`) — Scarf-account
namespace, not a Go import or GitHub repo URL. Stays.
This is a non-blocking, non-customer-impacting change. Operators pulling
container images, running `make verify`, hitting the API, or installing the
agent see no functional difference. Only Go-tooling consumers (none today)
are affected, and they're enabled — not broken — by this commit.
435 lines
13 KiB
Go
435 lines
13 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
"github.com/certctl-io/certctl/internal/service"
|
|
)
|
|
|
|
// G-1 red tests: lock in the HTTP surface of /api/v1/renewal-policies before
|
|
// the production code exists. Every subtest here references a symbol that
|
|
// Phase 2b must introduce:
|
|
//
|
|
// - NewRenewalPolicyHandler(svc) (constructor)
|
|
// - RenewalPolicyService (service-layer interface, in this package)
|
|
// - handler.ListRenewalPolicies / GetRenewalPolicy / CreateRenewalPolicy /
|
|
// UpdateRenewalPolicy / DeleteRenewalPolicy
|
|
// - service.ErrRenewalPolicyDuplicateName (pg 23505 → 409 mapping)
|
|
// - service.ErrRenewalPolicyInUse (pg 23503 → 409 mapping)
|
|
|
|
// MockRenewalPolicyService is a mock implementation of RenewalPolicyService.
|
|
type MockRenewalPolicyService struct {
|
|
ListRenewalPoliciesFn func(page, perPage int) ([]domain.RenewalPolicy, int64, error)
|
|
GetRenewalPolicyFn func(id string) (*domain.RenewalPolicy, error)
|
|
CreateRenewalPolicyFn func(rp domain.RenewalPolicy) (*domain.RenewalPolicy, error)
|
|
UpdateRenewalPolicyFn func(id string, rp domain.RenewalPolicy) (*domain.RenewalPolicy, error)
|
|
DeleteRenewalPolicyFn func(id string) error
|
|
}
|
|
|
|
func (m *MockRenewalPolicyService) ListRenewalPolicies(_ context.Context, page, perPage int) ([]domain.RenewalPolicy, int64, error) {
|
|
if m.ListRenewalPoliciesFn != nil {
|
|
return m.ListRenewalPoliciesFn(page, perPage)
|
|
}
|
|
return nil, 0, nil
|
|
}
|
|
|
|
func (m *MockRenewalPolicyService) GetRenewalPolicy(_ context.Context, id string) (*domain.RenewalPolicy, error) {
|
|
if m.GetRenewalPolicyFn != nil {
|
|
return m.GetRenewalPolicyFn(id)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *MockRenewalPolicyService) CreateRenewalPolicy(_ context.Context, rp domain.RenewalPolicy) (*domain.RenewalPolicy, error) {
|
|
if m.CreateRenewalPolicyFn != nil {
|
|
return m.CreateRenewalPolicyFn(rp)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *MockRenewalPolicyService) UpdateRenewalPolicy(_ context.Context, id string, rp domain.RenewalPolicy) (*domain.RenewalPolicy, error) {
|
|
if m.UpdateRenewalPolicyFn != nil {
|
|
return m.UpdateRenewalPolicyFn(id, rp)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *MockRenewalPolicyService) DeleteRenewalPolicy(_ context.Context, id string) error {
|
|
if m.DeleteRenewalPolicyFn != nil {
|
|
return m.DeleteRenewalPolicyFn(id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ----- List -----
|
|
|
|
func TestListRenewalPolicies_Success(t *testing.T) {
|
|
now := time.Now()
|
|
rp1 := domain.RenewalPolicy{
|
|
ID: "rp-default", Name: "Default", RenewalWindowDays: 30,
|
|
MaxRetries: 3, RetryInterval: 3600, AutoRenew: true,
|
|
CreatedAt: now, UpdatedAt: now,
|
|
}
|
|
rp2 := domain.RenewalPolicy{
|
|
ID: "rp-urgent", Name: "Urgent", RenewalWindowDays: 7,
|
|
MaxRetries: 5, RetryInterval: 600, AutoRenew: true,
|
|
CreatedAt: now, UpdatedAt: now,
|
|
}
|
|
|
|
mock := &MockRenewalPolicyService{
|
|
ListRenewalPoliciesFn: func(page, perPage int) ([]domain.RenewalPolicy, int64, error) {
|
|
return []domain.RenewalPolicy{rp1, rp2}, 2, nil
|
|
},
|
|
}
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/renewal-policies", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.ListRenewalPolicies(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected status 200, got %d", w.Code)
|
|
}
|
|
|
|
var resp PagedResponse
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("failed to decode response: %v", err)
|
|
}
|
|
if resp.Total != 2 {
|
|
t.Errorf("expected total 2, got %d", resp.Total)
|
|
}
|
|
}
|
|
|
|
func TestListRenewalPolicies_ServiceError(t *testing.T) {
|
|
mock := &MockRenewalPolicyService{
|
|
ListRenewalPoliciesFn: func(page, perPage int) ([]domain.RenewalPolicy, int64, error) {
|
|
return nil, 0, ErrMockServiceFailed
|
|
},
|
|
}
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/renewal-policies", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.ListRenewalPolicies(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Fatalf("expected status 500, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestListRenewalPolicies_MethodNotAllowed(t *testing.T) {
|
|
handler := NewRenewalPolicyHandler(&MockRenewalPolicyService{})
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/renewal-policies", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.ListRenewalPolicies(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("expected status 405, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// ----- Get -----
|
|
|
|
func TestGetRenewalPolicy_Success(t *testing.T) {
|
|
now := time.Now()
|
|
mock := &MockRenewalPolicyService{
|
|
GetRenewalPolicyFn: func(id string) (*domain.RenewalPolicy, error) {
|
|
return &domain.RenewalPolicy{
|
|
ID: id, Name: "Default", RenewalWindowDays: 30,
|
|
MaxRetries: 3, RetryInterval: 3600, AutoRenew: true,
|
|
CreatedAt: now, UpdatedAt: now,
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/renewal-policies/rp-default", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.GetRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected status 200, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestGetRenewalPolicy_NotFound(t *testing.T) {
|
|
mock := &MockRenewalPolicyService{
|
|
GetRenewalPolicyFn: func(id string) (*domain.RenewalPolicy, error) {
|
|
return nil, ErrMockNotFound
|
|
},
|
|
}
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/renewal-policies/nonexistent", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.GetRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("expected status 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// ----- Create -----
|
|
|
|
func TestCreateRenewalPolicy_Success(t *testing.T) {
|
|
now := time.Now()
|
|
mock := &MockRenewalPolicyService{
|
|
CreateRenewalPolicyFn: func(rp domain.RenewalPolicy) (*domain.RenewalPolicy, error) {
|
|
rp.ID = "rp-new"
|
|
rp.CreatedAt = now
|
|
rp.UpdatedAt = now
|
|
return &rp, nil
|
|
},
|
|
}
|
|
|
|
body := map[string]interface{}{
|
|
"name": "New Policy",
|
|
"renewal_window_days": 30,
|
|
"max_retries": 3,
|
|
"retry_interval_seconds": 3600,
|
|
"auto_renew": true,
|
|
}
|
|
bodyBytes, _ := json.Marshal(body)
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/renewal-policies", bytes.NewReader(bodyBytes))
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.CreateRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("expected status 201, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateRenewalPolicy_MissingName(t *testing.T) {
|
|
body := map[string]interface{}{
|
|
"renewal_window_days": 30,
|
|
"max_retries": 3,
|
|
"retry_interval_seconds": 3600,
|
|
}
|
|
bodyBytes, _ := json.Marshal(body)
|
|
|
|
handler := NewRenewalPolicyHandler(&MockRenewalPolicyService{})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/renewal-policies", bytes.NewReader(bodyBytes))
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.CreateRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected status 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateRenewalPolicy_InvalidJSON(t *testing.T) {
|
|
handler := NewRenewalPolicyHandler(&MockRenewalPolicyService{})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/renewal-policies", bytes.NewReader([]byte("not json")))
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.CreateRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected status 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateRenewalPolicy_DuplicateName(t *testing.T) {
|
|
// Service bubbles up ErrRenewalPolicyDuplicateName (pg 23505) → handler maps to 409.
|
|
mock := &MockRenewalPolicyService{
|
|
CreateRenewalPolicyFn: func(rp domain.RenewalPolicy) (*domain.RenewalPolicy, error) {
|
|
return nil, service.ErrRenewalPolicyDuplicateName
|
|
},
|
|
}
|
|
|
|
body := map[string]interface{}{
|
|
"name": "Duplicate",
|
|
"renewal_window_days": 30,
|
|
"max_retries": 3,
|
|
"retry_interval_seconds": 3600,
|
|
}
|
|
bodyBytes, _ := json.Marshal(body)
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/renewal-policies", bytes.NewReader(bodyBytes))
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.CreateRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusConflict {
|
|
t.Fatalf("expected status 409 on duplicate name, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateRenewalPolicy_MethodNotAllowed(t *testing.T) {
|
|
handler := NewRenewalPolicyHandler(&MockRenewalPolicyService{})
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/renewal-policies", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.CreateRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("expected status 405, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// ----- Update -----
|
|
|
|
func TestUpdateRenewalPolicy_Success(t *testing.T) {
|
|
now := time.Now()
|
|
mock := &MockRenewalPolicyService{
|
|
UpdateRenewalPolicyFn: func(id string, rp domain.RenewalPolicy) (*domain.RenewalPolicy, error) {
|
|
return &domain.RenewalPolicy{
|
|
ID: id, Name: rp.Name, RenewalWindowDays: rp.RenewalWindowDays,
|
|
MaxRetries: rp.MaxRetries, RetryInterval: rp.RetryInterval,
|
|
AutoRenew: rp.AutoRenew,
|
|
CreatedAt: now, UpdatedAt: now,
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
body := map[string]interface{}{
|
|
"name": "Updated Policy",
|
|
"renewal_window_days": 45,
|
|
"max_retries": 5,
|
|
"retry_interval_seconds": 1800,
|
|
"auto_renew": true,
|
|
}
|
|
bodyBytes, _ := json.Marshal(body)
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodPut, "/api/v1/renewal-policies/rp-default", bytes.NewReader(bodyBytes))
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.UpdateRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected status 200, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestUpdateRenewalPolicy_NotFound(t *testing.T) {
|
|
mock := &MockRenewalPolicyService{
|
|
UpdateRenewalPolicyFn: func(id string, rp domain.RenewalPolicy) (*domain.RenewalPolicy, error) {
|
|
return nil, ErrMockNotFound
|
|
},
|
|
}
|
|
|
|
body := map[string]interface{}{
|
|
"name": "Updated",
|
|
"renewal_window_days": 30,
|
|
"max_retries": 3,
|
|
"retry_interval_seconds": 3600,
|
|
}
|
|
bodyBytes, _ := json.Marshal(body)
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodPut, "/api/v1/renewal-policies/rp-missing", bytes.NewReader(bodyBytes))
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.UpdateRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("expected status 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// ----- Delete -----
|
|
|
|
func TestDeleteRenewalPolicy_Success(t *testing.T) {
|
|
var deletedID string
|
|
mock := &MockRenewalPolicyService{
|
|
DeleteRenewalPolicyFn: func(id string) error {
|
|
deletedID = id
|
|
return nil
|
|
},
|
|
}
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/renewal-policies/rp-default", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.DeleteRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("expected status 204, got %d", w.Code)
|
|
}
|
|
if deletedID != "rp-default" {
|
|
t.Errorf("expected deleted ID 'rp-default', got '%s'", deletedID)
|
|
}
|
|
}
|
|
|
|
func TestDeleteRenewalPolicy_NotFound(t *testing.T) {
|
|
mock := &MockRenewalPolicyService{
|
|
DeleteRenewalPolicyFn: func(id string) error {
|
|
return ErrMockNotFound
|
|
},
|
|
}
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/renewal-policies/rp-missing", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.DeleteRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("expected status 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDeleteRenewalPolicy_InUseConflict(t *testing.T) {
|
|
// Service bubbles up ErrRenewalPolicyInUse (pg 23503 FK-RESTRICT) → handler maps to 409.
|
|
mock := &MockRenewalPolicyService{
|
|
DeleteRenewalPolicyFn: func(id string) error {
|
|
return service.ErrRenewalPolicyInUse
|
|
},
|
|
}
|
|
|
|
handler := NewRenewalPolicyHandler(mock)
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/renewal-policies/rp-active", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.DeleteRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusConflict {
|
|
t.Fatalf("expected status 409 on in-use conflict, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDeleteRenewalPolicy_EmptyID(t *testing.T) {
|
|
handler := NewRenewalPolicyHandler(&MockRenewalPolicyService{})
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/renewal-policies/", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.DeleteRenewalPolicy(w, req)
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected status 400, got %d", w.Code)
|
|
}
|
|
}
|