83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
package archiemessaging
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/channels"
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/messagingrpc"
|
|
)
|
|
|
|
// sendingChannel records what it was asked to send.
|
|
type sendingChannel struct {
|
|
fakeChannel
|
|
sent []string
|
|
}
|
|
|
|
func (c *sendingChannel) Send(_ context.Context, chatID, text string) error {
|
|
c.sent = append(c.sent, chatID+":"+text)
|
|
return nil
|
|
}
|
|
|
|
var _ channels.Sender = (*sendingChannel)(nil)
|
|
|
|
func TestDeliverReachesOnlyTheOwnersChannel(t *testing.T) {
|
|
instance, acme := &sendingChannel{}, &sendingChannel{}
|
|
srv := &Service{channels: []*channelInstance{
|
|
{name: "telegram", kind: "telegram", owner: storecontract.DefaultOrgID, channel: instance},
|
|
{name: "acme/telegram", kind: "telegram", owner: "acme", channel: acme},
|
|
}}
|
|
tests := []struct {
|
|
name, org, channel string
|
|
want *sendingChannel
|
|
wantErr error
|
|
}{
|
|
{"the system org sends through the instance channel", storecontract.DefaultOrgID, "telegram", instance, nil},
|
|
{"an empty org is the system org", "", "telegram", instance, nil},
|
|
{"an org sends through its own channel", "acme", "telegram", acme, nil},
|
|
{"an org with no channel finds none", "beta", "telegram", nil, messagingrpc.ErrNoChannel},
|
|
{"an org cannot name another org's channel", "acme", "slack", nil, messagingrpc.ErrNoChannel},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
instance.sent, acme.sent = nil, nil
|
|
err := srv.Deliver(t.Context(), tt.org, tt.channel, "1", "hi")
|
|
if !errors.Is(err, tt.wantErr) {
|
|
t.Fatalf("err = %v, want %v", err, tt.wantErr)
|
|
}
|
|
for _, ch := range []*sendingChannel{instance, acme} {
|
|
if got := len(ch.sent) == 1; got != (ch == tt.want) {
|
|
t.Fatalf("channel %p sent %v, want delivery to %p only", ch, ch.sent, tt.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// recordingChat remembers the principal each call carried.
|
|
type recordingChat struct {
|
|
messaging.ChatContract
|
|
seen []access.Principal
|
|
}
|
|
|
|
func (c *recordingChat) Snapshot(ctx context.Context) (messaging.ChatSnapshot, error) {
|
|
p, _ := access.PrincipalFromContext(ctx)
|
|
c.seen = append(c.seen, p)
|
|
return messaging.ChatSnapshot{}, nil
|
|
}
|
|
|
|
func TestActingChatCarriesTheOrgsAgent(t *testing.T) {
|
|
inner := &recordingChat{}
|
|
chat := actingChat{ChatContract: inner, as: access.Principal{IdentityID: "agent-acme", Org: "acme"}}
|
|
if _, err := chat.Snapshot(t.Context()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(inner.seen) != 1 || inner.seen[0].Org != "acme" || inner.seen[0].IdentityID != "agent-acme" {
|
|
t.Fatalf("call carried %+v, want the acme agent", inner.seen)
|
|
}
|
|
}
|