43 lines
1.6 KiB
Go
43 lines
1.6 KiB
Go
package gatewayrpc
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
)
|
|
|
|
// TestChatWireKeepsGatewayInputs pins the fields the Gateway decides on
|
|
// across the messaging-to-gateway hop.
|
|
func TestChatWireKeepsGatewayInputs(t *testing.T) {
|
|
in := messaging.Inbound{Message: messaging.Message{SenderID: "42"}, Platform: "telegram"}
|
|
got := inboundValue(inboundProto(in))
|
|
if got.Message.SenderID != "42" || got.Platform != "telegram" {
|
|
t.Fatalf("inbound round trip lost a field: %+v", got)
|
|
}
|
|
}
|
|
|
|
// TestCheckInboundRefusesContradictoryMedia pins that a channel extension
|
|
// cannot deliver an attachment whose declared type its bytes contradict.
|
|
func TestCheckInboundRefusesContradictoryMedia(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
att messaging.MediaAttachment
|
|
want codes.Code
|
|
}{
|
|
{"a png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", Data: []byte("\x89PNG\r\n\x1a\n")}, codes.OK},
|
|
{"html declared as png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", Data: []byte("<!doctype html><script>")}, codes.InvalidArgument},
|
|
{"an executable declared as png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", Data: []byte("\x7fELF\x02\x01\x01\x00")}, codes.InvalidArgument},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := status.Code(checkInbound(messaging.Inbound{Media: []messaging.MediaAttachment{tt.att}}))
|
|
if got != tt.want {
|
|
t.Fatalf("code = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|