60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
)
|
|
|
|
func TestExecutionServerRefusesPublicationByDefault(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "definitions.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
handler := Handler(ctx, store)
|
|
defer func() { cancel(); WaitRuns(handler) }()
|
|
srv := httptest.NewServer(handler)
|
|
defer srv.Close()
|
|
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.CloseNow()
|
|
var reply protocol.Message
|
|
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
source := pipeline.Template([]string{"work"}, true)
|
|
// This is the same publication envelope used by submit, import, templates,
|
|
// and raw RPC; no client-selected flag can enable the server capability.
|
|
for _, kind := range []string{protocol.Validate, protocol.Submit} {
|
|
if err := wsjson.Write(ctx, conn, protocol.Message{ID: kind, Type: kind, Name: "unauthorized", Source: source}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if kind == protocol.Validate && reply.Type != protocol.Validated {
|
|
t.Fatalf("validation should remain read-only: %+v", reply)
|
|
}
|
|
if kind == protocol.Submit && (reply.Type != protocol.Error || !strings.Contains(reply.Error, "authoring is disabled")) {
|
|
t.Fatalf("publication bypassed gate: %+v", reply)
|
|
}
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil || len(items) != 0 {
|
|
t.Fatalf("disabled authoring changed definitions: %+v %v", items, err)
|
|
}
|
|
}
|