109 lines
4.1 KiB
Go
109 lines
4.1 KiB
Go
package policypublish
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
"lincodex/backend/internal/usecase/policy"
|
|
)
|
|
|
|
type extenderFake struct {
|
|
result policy.MatchingResult
|
|
err error
|
|
author domain.PolicyAuthor
|
|
semantic bool
|
|
ensemble bool
|
|
}
|
|
|
|
func (extender *extenderFake) EnsureDemoMatchingSection(_ context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error) {
|
|
extender.author = author
|
|
return extender.result, extender.err
|
|
}
|
|
|
|
func (extender *extenderFake) EnsureDemoSemanticMatchingSection(_ context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error) {
|
|
extender.author = author
|
|
extender.semantic = true
|
|
return extender.result, extender.err
|
|
}
|
|
|
|
func (extender *extenderFake) EnsureDemoSemanticEnsembleSection(_ context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error) {
|
|
extender.author = author
|
|
extender.ensemble = true
|
|
return extender.result, extender.err
|
|
}
|
|
|
|
func TestParseRequiresTheMatchingTrackAndAnOperatorHandle(t *testing.T) {
|
|
command, err := Parse([]string{"matching", "--actor", "operator:night"})
|
|
if err != nil || command.Author != "operator:night" || command.Track != TrackMatching {
|
|
t.Fatalf("Parse(valid) = %+v, %v", command, err)
|
|
}
|
|
semantic, err := Parse([]string{"semantic", "--actor", "operator:night"})
|
|
if err != nil || semantic.Track != TrackSemantic {
|
|
t.Fatalf("Parse(semantic) = %+v, %v", semantic, err)
|
|
}
|
|
ensemble, err := Parse([]string{"semantic-ensemble", "--actor", "operator:night"})
|
|
if err != nil || ensemble.Track != TrackSemanticEnsemble {
|
|
t.Fatalf("Parse(semantic-ensemble) = %+v, %v", ensemble, err)
|
|
}
|
|
for _, args := range [][]string{
|
|
nil, {"list"}, {"matching"}, {"matching", "--actor", "alice"}, {"matching", "--actor", "system:demo-seed"},
|
|
{"matching", "--actor", "operator:night", "extra"},
|
|
} {
|
|
if _, err := Parse(args); err == nil {
|
|
t.Errorf("Parse(%v) accepted invalid arguments", args)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExecutePrintsOnlyTheVersionAndMapsFailuresToSafeMessages(t *testing.T) {
|
|
var output bytes.Buffer
|
|
extender := &extenderFake{}
|
|
if err := Execute(context.Background(), Command{Author: "operator:night"}, extender, &output); err != nil {
|
|
t.Fatalf("Execute() error = %v", err)
|
|
}
|
|
if !strings.HasPrefix(output.String(), "already present: product policy version 0") || extender.author != "operator:night" {
|
|
t.Fatalf("output = %q, author %q", output.String(), extender.author)
|
|
}
|
|
for _, test := range []struct {
|
|
err error
|
|
want string
|
|
}{
|
|
{policy.ErrNotDemoPolicy, "not a demonstration version"},
|
|
{outbound.ErrPolicyVersionNotFound, "no version yet"},
|
|
{policy.ErrPolicyChanged, "run the command again"},
|
|
{errors.New("pq: password authentication failed for user x"), "policy publication failed"},
|
|
} {
|
|
err := Execute(context.Background(), Command{Author: "operator:night"}, &extenderFake{err: test.err}, &bytes.Buffer{})
|
|
if err == nil || !strings.Contains(err.Error(), test.want) || strings.Contains(err.Error(), "password") {
|
|
t.Errorf("Execute(%v) error = %v, want a safe message containing %q", test.err, err, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExecuteSemanticTrackPublishesTheSemanticSection(t *testing.T) {
|
|
var output bytes.Buffer
|
|
extender := &extenderFake{}
|
|
if err := Execute(context.Background(), Command{Track: TrackSemantic, Author: "operator:night"}, extender, &output); err != nil {
|
|
t.Fatalf("Execute() error = %v", err)
|
|
}
|
|
if !extender.semantic || !strings.Contains(output.String(), "the semantic matching section") {
|
|
t.Fatalf("output = %q, semantic called %v", output.String(), extender.semantic)
|
|
}
|
|
}
|
|
|
|
func TestExecuteSemanticEnsembleTrackPublishesTheEnsembleSection(t *testing.T) {
|
|
var output bytes.Buffer
|
|
extender := &extenderFake{}
|
|
if err := Execute(context.Background(), Command{Track: TrackSemanticEnsemble, Author: "operator:night"}, extender, &output); err != nil {
|
|
t.Fatalf("Execute() error = %v", err)
|
|
}
|
|
if !extender.ensemble || extender.semantic || !strings.Contains(output.String(), "semantic ensemble section") {
|
|
t.Fatalf("output = %q, ensemble called %v", output.String(), extender.ensemble)
|
|
}
|
|
}
|