Files
orbit/weave/instance/service_test.go

60 lines
2.1 KiB
Go

package instance
import (
"context"
"encoding/json"
"strings"
"testing"
core "git.yuntuo.hk/k2safe/orbit/model"
"git.yuntuo.hk/k2safe/orbit/platform/auth"
)
type captureStore struct {
calls int
input StartInput
}
func (s *captureStore) Start(_ context.Context, _ auth.Token, _ string, in StartInput) (StartResult, error) {
s.calls++
s.input = in
return StartResult{}, nil
}
func (s *captureStore) Get(context.Context, auth.Token, core.ID) (View, error) {
s.calls++
return View{}, nil
}
func (s *captureStore) Steps(context.Context, auth.Token, core.ID) (StepsView, error) {
s.calls++
return StepsView{}, nil
}
func TestAdmissionBoundaryPreservesExactInput(t *testing.T) {
store := &captureStore{}
api := New(store)
base := StartInput{DefinitionID: "11111111111111111111111111111111", Version: "v1", Input: json.RawMessage(`{"n":9007199254740993}`), BusinessKey: "关联 "}
for _, raw := range []string{`{"n":9007199254740993}`, `null`} {
in := base
in.Input = json.RawMessage(raw)
if _, err := api.Start(context.Background(), auth.Token{}, "identity", in); err != nil || string(store.input.Input) != raw || store.input.BusinessKey != base.BusinessKey {
t.Fatal("input changed", err)
}
}
for _, change := range []func(*StartInput){
func(in *StartInput) { in.Input = nil }, func(in *StartInput) { in.Input = json.RawMessage(`{"n":1,"n":2}`) },
func(in *StartInput) { in.Input = json.RawMessage(`"\ud800"`) }, func(in *StartInput) { in.Input = json.RawMessage(`"` + strings.Repeat("a", 256<<10) + `"`) },
func(in *StartInput) { in.Version = "../v1" }, func(in *StartInput) { in.Version = strings.Repeat("v", 65) },
func(in *StartInput) { in.BusinessKey = strings.Repeat("中", 192) }, func(in *StartInput) { in.BusinessKey = "a\x00b" },
func(in *StartInput) { in.DefinitionID = "bad" },
} {
in := base
change(&in)
before := store.calls
if _, err := api.Start(context.Background(), auth.Token{}, "identity", in); err == nil || store.calls != before {
t.Fatal("invalid input reached store")
}
}
if _, err := api.Start(context.Background(), auth.Token{}, "", base); err == nil {
t.Fatal("missing identity accepted")
}
}