60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package mq
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const currentTestDispatcherID = "c046b893-8628-4589-ae50-619d049248a6"
|
|
|
|
func currentMessage(t *testing.T, name string) []byte {
|
|
t.Helper()
|
|
body, err := os.ReadFile(filepath.Join("..", "..", "contracts", "local", "examples", name+".json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return body
|
|
}
|
|
|
|
func TestCurrentBrokerRejectsInvalidConfigBeforeConnecting(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
url, id string
|
|
prefetch int
|
|
}{
|
|
{"", currentTestDispatcherID, 1},
|
|
{"amqp://127.0.0.1:1", "not-a-uuid", 1},
|
|
{"amqp://127.0.0.1:1", currentTestDispatcherID, 0},
|
|
} {
|
|
if _, err := OpenCurrent(tc.url, tc.id, tc.prefetch); err == nil {
|
|
t.Fatalf("accepted invalid broker settings: %+v", tc)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCurrentPublishRejectsLegacyAndWrongOwnerBeforeNetwork(t *testing.T) {
|
|
b := &CurrentBroker{dispatcherID: currentTestDispatcherID, resultRoute: "d." + currentTestDispatcherID + ".out"}
|
|
for _, tc := range []struct {
|
|
name string
|
|
body []byte
|
|
}{
|
|
{"old version field", currentMessage(t, "invalid/mq-legacy-schema-version")},
|
|
{"wrong owner", []byte(strings.Replace(string(currentMessage(t, "mq-result-no-recording")), currentTestDispatcherID, "c046b893-8628-4589-ae50-619d049248a7", 1))},
|
|
{"unapproved event", currentMessage(t, "mq-sip-change")},
|
|
{"empty event identity", []byte(strings.Replace(string(currentMessage(t, "mq-execute-ack")), `"event_id":"call-example"`, `"event_id":""`, 1))},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := b.Publish(context.Background(), "agent-call.saas.v1", b.resultRoute, tc.body)
|
|
if err == nil || strings.Contains(err.Error(), "connection is closed") {
|
|
t.Fatalf("message reached network path instead of being rejected: %v", err)
|
|
}
|
|
})
|
|
}
|
|
valid := currentMessage(t, "mq-result-no-recording")
|
|
if err := b.Publish(context.Background(), "agent-call.saas.v1", b.resultRoute, valid); err == nil || !strings.Contains(err.Error(), "connection is closed") {
|
|
t.Fatalf("valid result did not reach network boundary: %v", err)
|
|
}
|
|
}
|