176 lines
6.9 KiB
Go
176 lines
6.9 KiB
Go
package demoseed
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
seedusecase "lincodex/backend/internal/usecase/demoseed"
|
|
)
|
|
|
|
func TestParseAcceptsOnlyTheThreeSubcommands(t *testing.T) {
|
|
command, err := Parse([]string{"seed", "--actor", "operator:night"})
|
|
if err != nil || command.Subcommand != SubcommandSeed || command.Actor != "operator:night" {
|
|
t.Fatalf("Parse(seed) = %+v, %v", command, err)
|
|
}
|
|
for _, args := range [][]string{{"check-reset"}, {"inventory"}} {
|
|
if command, err := Parse(args); err != nil || string(command.Subcommand) != args[0] {
|
|
t.Errorf("Parse(%v) = %+v, %v", args, command, err)
|
|
}
|
|
}
|
|
for _, args := range [][]string{
|
|
nil, {"reset"}, {"seed"}, {"seed", "--actor", "alice"}, {"seed", "--actor", "system:demo-seed"},
|
|
{"seed", "--actor", "operator:night", "extra"}, {"check-reset", "--force"}, {"inventory", "all"},
|
|
} {
|
|
if _, err := Parse(args); err == nil {
|
|
t.Errorf("Parse(%v) accepted invalid arguments", args)
|
|
}
|
|
}
|
|
}
|
|
|
|
func lookupOf(values map[string]string) func(string) (string, bool) {
|
|
return func(key string) (string, bool) {
|
|
value, ok := values[key]
|
|
return value, ok
|
|
}
|
|
}
|
|
|
|
func TestRunRefusesWithoutTheIsolatedModeBeforeReadingAnythingElse(t *testing.T) {
|
|
for _, mode := range []string{"", "production", "ISOLATED", "isolated-ish"} {
|
|
values := map[string]string{"DATABASE_URL": "postgres://unreachable.invalid/none", PasswordName: "a long synthetic password"}
|
|
if mode != "" {
|
|
values[DemoModeName] = mode
|
|
}
|
|
var output bytes.Buffer
|
|
err := Run(context.Background(), []string{"check-reset"}, lookupOf(values), &output, time.Now)
|
|
if err == nil || !strings.Contains(err.Error(), "DEMO_MODE=isolated") || output.Len() != 0 {
|
|
t.Errorf("Run with DEMO_MODE=%q = %v, output %q; want a refusal before any output", mode, err, output.String())
|
|
}
|
|
}
|
|
err := Run(context.Background(), []string{"seed", "--actor", "operator:night"},
|
|
lookupOf(map[string]string{DemoModeName: IsolatedDemoMode, "DATABASE_URL": "postgres://unreachable.invalid/none", PasswordName: "short"}),
|
|
&bytes.Buffer{}, time.Now)
|
|
if err == nil || !strings.Contains(err.Error(), PasswordName) || strings.Contains(err.Error(), "short") {
|
|
t.Fatalf("Run(seed, short password) = %v, want a refusal that does not echo the value", err)
|
|
}
|
|
err = Run(context.Background(), []string{"inventory"}, lookupOf(map[string]string{DemoModeName: IsolatedDemoMode}), &bytes.Buffer{}, time.Now)
|
|
if err == nil || !strings.Contains(err.Error(), "DATABASE_URL") {
|
|
t.Fatalf("Run(inventory) without a database = %v", err)
|
|
}
|
|
}
|
|
|
|
type seederFake struct {
|
|
report seedusecase.Report
|
|
err error
|
|
}
|
|
|
|
func (fake seederFake) Seed(context.Context, seedusecase.Input) (seedusecase.Report, error) {
|
|
return fake.report, fake.err
|
|
}
|
|
|
|
func TestExecuteSeedPrintsLoginsAndAttemptsButNeverThePassword(t *testing.T) {
|
|
attemptID, err := domain.NewAssessmentAttemptID()
|
|
if err != nil {
|
|
t.Fatalf("NewAssessmentAttemptID() error = %v", err)
|
|
}
|
|
report := seedusecase.Report{
|
|
PolicyVersion: 2, Created: []string{"account candidate@demo.lincodex.test"},
|
|
Logins: []seedusecase.Login{{Email: "candidate@demo.lincodex.test", Role: "candidate"}},
|
|
PreparedAttempts: []seedusecase.PreparedAttempt{{Email: "prepared-1@demo.lincodex.test", AttemptID: attemptID}},
|
|
}
|
|
var output bytes.Buffer
|
|
password, err := domain.NewPassword("a long synthetic password")
|
|
if err != nil {
|
|
t.Fatalf("NewPassword() error = %v", err)
|
|
}
|
|
if err := ExecuteSeed(context.Background(), seederFake{report: report}, seedusecase.Input{Password: password}, &output); err != nil {
|
|
t.Fatalf("ExecuteSeed() error = %v", err)
|
|
}
|
|
text := output.String()
|
|
for _, want := range []string{
|
|
"created 1 records, found 0", "login: candidate@demo.lincodex.test", "prepared full attempt: " + attemptID.String(),
|
|
"no person took them", "never assigns a category",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Errorf("output lacks %q:\n%s", want, text)
|
|
}
|
|
}
|
|
if strings.Contains(text, "a long synthetic password") {
|
|
t.Fatal("the output contains the password")
|
|
}
|
|
for _, test := range []struct {
|
|
err error
|
|
want string
|
|
}{
|
|
{seedusecase.ErrStandNotFresh, "never seeded"},
|
|
{seedusecase.ErrForeignPolicy, "neither the initial demo version"},
|
|
{seedusecase.ErrAccountConflict, "did not create"},
|
|
{seedusecase.ErrPreparedAttemptFailed, "did not assign"},
|
|
{outbound.ErrPolicyVersionNotFound, "no version yet"},
|
|
{errors.New("pq: password authentication failed for user x"), "demo seed failed"},
|
|
} {
|
|
err := ExecuteSeed(context.Background(), seederFake{err: test.err}, seedusecase.Input{}, &bytes.Buffer{})
|
|
if err == nil || !strings.Contains(err.Error(), test.want) || strings.Contains(err.Error(), "authentication") {
|
|
t.Errorf("ExecuteSeed(%v) = %v, want a safe message containing %q", test.err, err, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
type inspectorFake struct {
|
|
census outbound.DemoAccountCensus
|
|
err error
|
|
}
|
|
|
|
func (fake inspectorFake) CheckReset(context.Context) (seedusecase.ResetCheck, error) {
|
|
return seedusecase.ResetCheck{Verdict: seedusecase.DecideReset(fake.census), Census: fake.census}, fake.err
|
|
}
|
|
|
|
func (fake inspectorFake) Inventory(context.Context) (outbound.DemoInventory, error) {
|
|
return outbound.DemoInventory{SyntheticAccounts: fake.census.Synthetic, OrdinaryAccounts: fake.census.Ordinary()}, fake.err
|
|
}
|
|
|
|
func TestExecuteCheckResetFailsOnOrdinaryAccounts(t *testing.T) {
|
|
for _, test := range []struct {
|
|
census outbound.DemoAccountCensus
|
|
want string
|
|
fails bool
|
|
}{
|
|
{outbound.DemoAccountCensus{}, "reset allowed (empty)", false},
|
|
{outbound.DemoAccountCensus{Total: 9, Synthetic: 9}, "reset allowed (synthetic): 9 accounts", false},
|
|
{outbound.DemoAccountCensus{Total: 10, Synthetic: 9}, "holds 1 accounts outside", true},
|
|
{outbound.DemoAccountCensus{Total: 1}, "holds 1 accounts outside", true},
|
|
} {
|
|
var output bytes.Buffer
|
|
err := ExecuteCheckReset(context.Background(), inspectorFake{census: test.census}, &output)
|
|
if test.fails != (err != nil) {
|
|
t.Fatalf("ExecuteCheckReset(%+v) = %v, want failure %v", test.census, err, test.fails)
|
|
}
|
|
text := output.String()
|
|
if err != nil {
|
|
text = err.Error()
|
|
}
|
|
if !strings.Contains(text, test.want) {
|
|
t.Errorf("ExecuteCheckReset(%+v) said %q, want %q", test.census, text, test.want)
|
|
}
|
|
}
|
|
if err := ExecuteCheckReset(context.Background(), inspectorFake{err: errors.New("connection reset")}, &bytes.Buffer{}); err == nil ||
|
|
err.Error() != "reset check failed" {
|
|
t.Fatalf("ExecuteCheckReset(store failure) = %v, want a refusal", err)
|
|
}
|
|
}
|
|
|
|
func TestExecuteInventoryPrintsStableCounts(t *testing.T) {
|
|
var output bytes.Buffer
|
|
if err := ExecuteInventory(context.Background(), inspectorFake{census: outbound.DemoAccountCensus{Total: 10, Synthetic: 9}}, &output); err != nil {
|
|
t.Fatalf("ExecuteInventory() error = %v", err)
|
|
}
|
|
if !strings.HasPrefix(output.String(), "synthetic_accounts=9\nverified_synthetic_accounts=0\nordinary_accounts=1\n") {
|
|
t.Fatalf("inventory = %q", output.String())
|
|
}
|
|
}
|