84 lines
3.0 KiB
Go
84 lines
3.0 KiB
Go
package controlplanestore
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
func TestChannelSettingsWritesStayWithinTheirOrg(t *testing.T) {
|
|
tests := []struct {
|
|
name, org, value string
|
|
refused bool
|
|
}{
|
|
{"system org sets the instance channel", storecontract.DefaultOrgID, `{"operator":"sam","workspace":"/srv"}`, false},
|
|
{"system org names bot users", storecontract.DefaultOrgID, `{"allowed_user_ids":[1]}`, true},
|
|
{"system org names an agent", storecontract.DefaultOrgID, `{"agent":"a1"}`, true},
|
|
{"another org sets its users and agent", "acme", `{"agent":"a1","show_tool_calls":true,"allowed_user_ids":[7]}`, false},
|
|
{"another org sets the host workspace", "acme", `{"workspace":"/srv"}`, true},
|
|
{"another org lifts the filesystem limit", "acme", `{"unrestricted_filesystem":true}`, true},
|
|
{"another org sets the instance rate limit", "acme", `{"rate_limit":{"max_requests":5}}`, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := (&Server{}).checkOrgWrite(t.Context(), tt.org, ChannelSettingsKind, []byte(tt.value))
|
|
if refused := errors.Is(err, ErrValidation); refused != tt.refused {
|
|
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type listingStore struct {
|
|
ResourceStore
|
|
resources []storecontract.Resource
|
|
}
|
|
|
|
func (s listingStore) ListResources(context.Context) ([]storecontract.Resource, error) {
|
|
return s.resources, nil
|
|
}
|
|
|
|
func TestChannelOrgsServesOnlyMessagingAndOnlyOtherOrgs(t *testing.T) {
|
|
server := &Server{store: listingStore{resources: []storecontract.Resource{
|
|
{OrgID: storecontract.DefaultOrgID, Kind: ChannelSettingsKind},
|
|
{OrgID: "acme", Kind: ChannelSettingsKind},
|
|
{OrgID: "acme", Kind: SoulKind},
|
|
}}}
|
|
tests := []struct {
|
|
name, caller string
|
|
wantOrgs []string
|
|
wantCode codes.Code
|
|
}{
|
|
{"messaging lists other orgs' channel settings", presence.Messaging, []string{"acme"}, codes.OK},
|
|
{"the gateway is refused", presence.Gateway, nil, codes.PermissionDenied},
|
|
{"an unattributed caller is refused", "", nil, codes.PermissionDenied},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx := t.Context()
|
|
if tt.caller != "" {
|
|
ctx = access.WithSource(ctx, tt.caller)
|
|
}
|
|
response, err := server.ChannelOrgs(ctx, &pb.ChannelOrgsRequest{})
|
|
if status.Code(err) != tt.wantCode {
|
|
t.Fatalf("code = %v, want %v (err %v)", status.Code(err), tt.wantCode, err)
|
|
}
|
|
var got []string
|
|
for _, resource := range response.GetResources() {
|
|
got = append(got, resource.GetOrgId())
|
|
}
|
|
if len(got) != len(tt.wantOrgs) || (len(got) == 1 && got[0] != tt.wantOrgs[0]) {
|
|
t.Fatalf("orgs = %v, want %v", got, tt.wantOrgs)
|
|
}
|
|
})
|
|
}
|
|
}
|