Sessions carry an owner; every chat route serves only the caller's own conversations (others are 404). Each turn and each approved held action acts as its owner's principal, checked against the access rules before judgement, so an operator's chat can't do what the operator can't. Chat is open to any signed-in person, and the stream sends chat updates only to the owner. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/internal/platform/hub"
|
|
"github.com/samcharles93/archie-core/modules/chat"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// TestStreamTellsOnlyTheConversationsOwner guards the live stream's chat
|
|
// boundary: an action settled in a conversation is announced to its owner
|
|
// and to nobody else, administrators included.
|
|
func TestStreamTellsOnlyTheConversationsOwner(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
reader string
|
|
hears bool
|
|
}{
|
|
{"alice", true},
|
|
{"bob", false},
|
|
{string(governance.SystemID), false},
|
|
} {
|
|
t.Run(tc.reader, func(t *testing.T) {
|
|
h := hub.New()
|
|
mux := http.NewServeMux()
|
|
Register(httpapi.New(mux, "test", headerAuth{}), h)
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(t.Context(), 300*time.Millisecond)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/api/stream", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("X-As", tc.reader)
|
|
res, err := srv.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = res.Body.Close() }()
|
|
h.Publish(hub.Note{Topic: chat.OwnerTopic("alice")})
|
|
|
|
heard := false
|
|
lines := bufio.NewScanner(res.Body)
|
|
for lines.Scan() {
|
|
if strings.Contains(lines.Text(), "chat_outcome") {
|
|
heard = true
|
|
break
|
|
}
|
|
}
|
|
if heard != tc.hears {
|
|
t.Fatalf("%s heard the update: %v, want %v", tc.reader, heard, tc.hears)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// headerAuth signs in whoever the X-As header names; the system identity is
|
|
// the instance administrator.
|
|
type headerAuth struct{}
|
|
|
|
func (headerAuth) Authenticate(ctx context.Context, r *http.Request) (context.Context, bool, error) {
|
|
id := governance.IdentityID(r.Header.Get("X-As"))
|
|
p := governance.Principal{IdentityID: id, Org: governance.DefaultOrgID}
|
|
if id == governance.SystemID {
|
|
p = governance.SharedTokenOwner()
|
|
}
|
|
return governance.WithPrincipal(ctx, p), p.InstanceAdmin(), nil
|
|
}
|