Files
lincodex/backend/internal/adapter/primary/cli/mailretry/command_test.go
T

143 lines
5.8 KiB
Go

package mailretry
import (
"bytes"
"context"
"errors"
"strings"
"testing"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
"lincodex/backend/internal/usecase/notification"
)
const testDeliveryID = "2e203b62-3fb5-4ef7-b118-360bbff7f2aa"
func TestParseRequiresExplicitCommandFields(t *testing.T) {
tests := []struct {
name string
args []string
want string
}{
{name: "command", args: nil, want: "usage:"},
{name: "unknown command", args: []string{"send"}, want: "usage:"},
{name: "inspect delivery", args: []string{"inspect"}, want: "non-zero UUID"},
{name: "invalid delivery", args: []string{"inspect", "--delivery", "not-a-uuid"}, want: "non-zero UUID"},
{name: "retry delivery", args: []string{"retry", "--actor", "operator:mail-ops", "--acknowledge-duplicate-risk"}, want: "non-zero UUID"},
{name: "retry actor", args: []string{"retry", "--delivery", testDeliveryID, "--acknowledge-duplicate-risk"}, want: "actor must be"},
{name: "retry acknowledgement", args: []string{"retry", "--delivery", testDeliveryID, "--actor", "operator:mail-ops"}, want: "--acknowledge-duplicate-risk is required"},
{name: "invalid actor", args: []string{"retry", "--delivery", testDeliveryID, "--actor", "mail-ops", "--acknowledge-duplicate-risk"}, want: "actor must be"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := Parse(test.args)
if err == nil || !strings.Contains(err.Error(), test.want) {
t.Fatalf("Parse(%q) error = %v, want text %q", test.args, err, test.want)
}
})
}
}
func TestRunValidatesArgumentsBeforeReadingEnvironment(t *testing.T) {
var reads int
err := Run(context.Background(), []string{"retry", "--delivery", testDeliveryID, "--actor", "operator:mail-ops"}, func(string) (string, bool) {
reads++
return "", false
}, &bytes.Buffer{})
if err == nil || reads != 0 {
t.Fatalf("Run() error = %v, environment reads = %d; want argument error before config access", err, reads)
}
}
func TestRunDoesNotPrintDatabaseURL(t *testing.T) {
const secretURL = "postgres://operator:secret-password@127.0.0.1:1/mail"
lookup := func(key string) (string, bool) {
switch key {
case "DATABASE_URL":
return secretURL, true
case "EMAIL_PROVIDER":
return "resend", true
default:
return "", false
}
}
var output bytes.Buffer
err := Run(context.Background(), []string{"inspect", "--delivery", testDeliveryID}, lookup, &output)
if err == nil || strings.Contains(err.Error(), "secret-password") || strings.Contains(output.String(), "secret-password") {
t.Fatalf("Run() error/output leaked connection details: error %v, output %q", err, output.String())
}
}
func TestExecuteWarnsBeforeSchedulingAndRedactsMetadata(t *testing.T) {
deliveryID := uuid.MustParse(testDeliveryID)
var output bytes.Buffer
operator := &testOperator{
inspection: notification.DeliveryRetryInspection{
DeliveryID: deliveryID,
Provider: "resend",
ProviderAccount: "sensitive-account-label",
Status: domain.DeliveryStatusUnknown,
AttemptCount: 2,
},
output: &output,
}
command := Command{Action: retryAction, DeliveryID: deliveryID, Actor: "operator:mail-ops", AcknowledgeDuplicateRisk: true}
if err := Execute(context.Background(), command, operator, &output); err != nil {
t.Fatalf("Execute() error = %v", err)
}
if !operator.retryCalled || !strings.Contains(output.String(), notification.DuplicateRiskWarning) ||
strings.Contains(output.String(), "sensitive-account-label") {
t.Fatalf("Execute() output/calls = %q, %t; want warning before retry and redacted account", output.String(), operator.retryCalled)
}
}
func TestExecuteDoesNotScheduleWhenWarningCannotBeWritten(t *testing.T) {
deliveryID := uuid.MustParse(testDeliveryID)
operator := &testOperator{inspection: notification.DeliveryRetryInspection{DeliveryID: deliveryID, Status: domain.DeliveryStatusUnknown}}
command := Command{Action: retryAction, DeliveryID: deliveryID, Actor: "operator:mail-ops", AcknowledgeDuplicateRisk: true}
if err := Execute(context.Background(), command, operator, errorWriter{}); err == nil || operator.retryCalled {
t.Fatalf("Execute() error = %v, retry called = %t; want output error before mutation", err, operator.retryCalled)
}
}
func TestExecuteReturnsSafeStateError(t *testing.T) {
deliveryID := uuid.MustParse(testDeliveryID)
operator := &testOperator{
inspection: notification.DeliveryRetryInspection{DeliveryID: deliveryID, Status: domain.DeliveryStatusUnknown},
retryErr: errors.Join(outbound.ErrDeliveryRetryNotAllowed, errors.New("recipient private@example.invalid")),
}
var output bytes.Buffer
command := Command{Action: retryAction, DeliveryID: deliveryID, Actor: "operator:mail-ops", AcknowledgeDuplicateRisk: true}
err := Execute(context.Background(), command, operator, &output)
if err == nil || err.Error() != "delivery is not eligible for manual retry" || strings.Contains(err.Error(), "private@example.invalid") {
t.Fatalf("Execute() error = %v; want a safe state error", err)
}
}
type testOperator struct {
inspection notification.DeliveryRetryInspection
inspectErr error
retryErr error
output *bytes.Buffer
retryCalled bool
}
func (operator *testOperator) Inspect(context.Context, uuid.UUID) (notification.DeliveryRetryInspection, error) {
return operator.inspection, operator.inspectErr
}
func (operator *testOperator) RetryUnknown(context.Context, uuid.UUID, string, bool) (string, error) {
operator.retryCalled = true
if operator.output != nil && !strings.Contains(operator.output.String(), notification.DuplicateRiskWarning) {
return "", errors.New("retry called before warning")
}
return "", operator.retryErr
}
type errorWriter struct{}
func (errorWriter) Write([]byte) (int, error) { return 0, errors.New("output unavailable") }