Files
archie/internal/infrastructure/staterpc/captures_workspace_test.go
T

69 lines
1.9 KiB
Go

package staterpc
import (
"context"
"testing"
"google.golang.org/grpc"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
type workspaceCaptures struct {
storecontract.CaptureStore
byWorkspace map[org.WorkspaceID][]storecontract.CapturedEvent
}
func (w workspaceCaptures) ListCaptures(_ context.Context, ws org.WorkspaceID, _ int) ([]storecontract.CapturedEvent, error) {
if ws != "" {
return w.byWorkspace[ws], nil
}
var all []storecontract.CapturedEvent
for _, list := range w.byWorkspace {
all = append(all, list...)
}
return all, nil
}
type captureStream struct {
grpc.ServerStream
sent []*pb.CapturedEvent
}
func (s *captureStream) Context() context.Context { return context.Background() }
func (s *captureStream) Send(r *pb.StreamCapturesResponse) error {
s.sent = append(s.sent, r.Capture)
return nil
}
// A capture list asked for one workspace carries that workspace to the store.
// Guards the dashboard's workspace-narrowed capture list showing another
// workspace's webhook bodies.
func TestStreamCapturesNarrowsToTheRequestedWorkspace(t *testing.T) {
store := workspaceCaptures{byWorkspace: map[org.WorkspaceID][]storecontract.CapturedEvent{
"ws-a": {{ID: "a1"}},
"ws-b": {{ID: "b1"}, {ID: "b2"}},
}}
s := &captureServer{server: &server{deps: Deps{Captures: store}}}
for _, tt := range []struct {
name, workspace string
want int
}{
{"one workspace", "ws-b", 2},
{"every workspace", "", 3},
} {
t.Run(tt.name, func(t *testing.T) {
stream := &captureStream{}
if err := s.StreamCaptures(&pb.StreamCapturesRequest{Limit: 10, WorkspaceId: tt.workspace}, stream); err != nil {
t.Fatal(err)
}
if len(stream.sent) != tt.want {
t.Fatalf("got %d captures, want %d", len(stream.sent), tt.want)
}
})
}
}