Files
vHost/backend/internal/service/dns_certificates_test.go
T

106 lines
3.7 KiB
Go

package service
import (
"context"
"errors"
"testing"
"sea-vhost/internal/store"
)
type dnsCredentialTestProvider struct {
presentName, presentValue string
cleanedID string
cleanupCalls int
cleanupFailures int
cleanupErr error
}
func (p *dnsCredentialTestProvider) Present(_ context.Context, name, value string) (string, error) {
p.presentName, p.presentValue = name, value
return "provider-record-42", nil
}
func (p *dnsCredentialTestProvider) CreateRecord(ctx context.Context, name, _ string, value string) (string, error) {
return p.Present(ctx, name, value)
}
func (p *dnsCredentialTestProvider) Cleanup(_ context.Context, id string) error {
p.cleanedID = id
p.cleanupCalls++
if p.cleanupFailures > 0 {
p.cleanupFailures--
return errors.New("temporary provider failure")
}
return p.cleanupErr
}
func TestRouteDNSCredentialsSeparatesPlatformAndTenantZones(t *testing.T) {
tenantID := uint64(8)
platform := store.DNSCredential{ID: 1, ZoneName: "example.net"}
tenant := store.DNSCredential{ID: 2, TenantID: &tenantID, ZoneName: "customer.example"}
routes, err := routeDNSCredentials([]string{"sv1.hosts.example.net", "www.customer.example"}, "hosts.example.net", []store.DNSCredential{tenant, platform})
if err != nil {
t.Fatal(err)
}
if routes["sv1.hosts.example.net"].ID != platform.ID {
t.Fatal("assigned domain did not select platform credential")
}
if routes["www.customer.example"].ID != tenant.ID {
t.Fatal("custom domain did not select tenant credential")
}
}
func TestRouteDNSCredentialsUsesLongestZoneSuffix(t *testing.T) {
tenantID := uint64(8)
credentials := []store.DNSCredential{
{ID: 1, TenantID: &tenantID, ZoneName: "example.com"},
{ID: 2, TenantID: &tenantID, ZoneName: "app.example.com"},
}
routes, err := routeDNSCredentials([]string{"www.app.example.com"}, "hosts.platform.test", credentials)
if err != nil {
t.Fatal(err)
}
if routes["www.app.example.com"].ID != 2 {
t.Fatal("did not select longest matching DNS zone")
}
}
func TestRouteDNSCredentialsWillNotUseTenantKeyForPlatformDomain(t *testing.T) {
tenantID := uint64(8)
_, err := routeDNSCredentials([]string{"sv1.hosts.example.net"}, "hosts.example.net", []store.DNSCredential{{ID: 2, TenantID: &tenantID, ZoneName: "example.net"}})
if err == nil {
t.Fatal("tenant credential must not manage the platform root domain")
}
}
func TestVerifyDNSCredentialCreatesAndDeletesExactRecord(t *testing.T) {
provider := &dnsCredentialTestProvider{}
if err := verifyDNSCredential(context.Background(), provider, "_svh-check-a.example.com", "verification-value"); err != nil {
t.Fatal(err)
}
if provider.presentName != "_svh-check-a.example.com" || provider.presentValue != "verification-value" {
t.Fatalf("unexpected TXT record: %q %q", provider.presentName, provider.presentValue)
}
if provider.cleanedID != "provider-record-42" {
t.Fatalf("cleanup used %q instead of exact provider record id", provider.cleanedID)
}
}
func TestVerifyDNSCredentialReportsCleanupFailure(t *testing.T) {
provider := &dnsCredentialTestProvider{cleanupErr: errors.New("delete denied")}
err := verifyDNSCredential(context.Background(), provider, "_svh-check-a.example.com", "verification-value")
if err == nil || provider.cleanedID != "provider-record-42" || provider.cleanupCalls != dnsCleanupAttempts {
t.Fatal("cleanup permission failure was not reported")
}
}
func TestVerifyDNSCredentialRetriesTransientCleanupFailure(t *testing.T) {
provider := &dnsCredentialTestProvider{cleanupFailures: 1}
if err := verifyDNSCredential(context.Background(), provider, "_svh-check-a.example.com", "verification-value"); err != nil {
t.Fatal(err)
}
if provider.cleanupCalls != 2 {
t.Fatalf("cleanup calls = %d, want 2", provider.cleanupCalls)
}
}