73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
type bootstrapProbe struct {
|
|
calls []string
|
|
org *domain.OrganizationRecord
|
|
err error
|
|
}
|
|
|
|
func (p *bootstrapProbe) RunMigrations(context.Context) error {
|
|
p.calls = append(p.calls, "migrate")
|
|
return p.err
|
|
}
|
|
func (p *bootstrapProbe) EnsureIndexes(context.Context) error {
|
|
p.calls = append(p.calls, "indexes")
|
|
return p.err
|
|
}
|
|
func (p *bootstrapProbe) EnsureDefaultOrganization(context.Context) (*domain.OrganizationRecord, error) {
|
|
p.calls = append(p.calls, "initialize")
|
|
return p.org, p.err
|
|
}
|
|
func (p *bootstrapProbe) GetOrganization(_ context.Context, id string) (*domain.OrganizationRecord, error) {
|
|
p.calls = append(p.calls, "read:"+id)
|
|
return p.org, p.err
|
|
}
|
|
|
|
func TestVerifyExistingBootstrapNeverMutates(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
org *domain.OrganizationRecord
|
|
err error
|
|
wantError bool
|
|
}{
|
|
{"initialized", &domain.OrganizationRecord{ID: domain.DefaultOrganizationID}, nil, false},
|
|
{"empty", nil, nil, true},
|
|
{"unavailable", nil, errors.New("unavailable"), true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
p := &bootstrapProbe{org: tc.org, err: tc.err}
|
|
err := bootstrapAPI(context.Background(), p, "verify-existing")
|
|
if (err != nil) != tc.wantError {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(p.calls, []string{"read:" + domain.DefaultOrganizationID}) {
|
|
t.Fatalf("unexpected writes: %v", p.calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestBootstrapLegacyAndInvalidModes(t *testing.T) {
|
|
for _, mode := range []string{"", "migrate"} {
|
|
p := &bootstrapProbe{}
|
|
if err := bootstrapAPI(context.Background(), p, mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(p.calls, []string{"migrate", "indexes", "initialize"}) {
|
|
t.Fatal(p.calls)
|
|
}
|
|
}
|
|
p := &bootstrapProbe{}
|
|
if err := bootstrapAPI(context.Background(), p, "typo"); err == nil || len(p.calls) != 0 {
|
|
t.Fatal("invalid mode accessed database")
|
|
}
|
|
}
|