Files
archie/internal/infrastructure/gatewayrpc/wire_test.go
T
sam cc8428643a
deploy / gate (push) Successful in 4m41s
Quality / check (push) Successful in 7m2s
deploy / archied-image (push) Successful in 3m37s
deploy / agent-image (push) Successful in 12s
deploy / dist-zip (push) Skipped
fix(gatewayrpc): sniff the head of URL-only attachments (#1672)
2026-11-08 05:40:27 +11:00

64 lines
2.9 KiB
Go

package gatewayrpc
import (
"context"
"net/http"
"net/http/httptest"
"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) {
// The URL-only rows are served from a local host, which the production
// client refuses, so the test client is a plain one.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/png":
_, _ = w.Write([]byte("\x89PNG\r\n\x1a\n"))
case "/html":
_, _ = w.Write([]byte("<!doctype html><script>"))
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
tests := []struct {
name string
att messaging.MediaAttachment
want codes.Code
}{
{"a url-only png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", URL: srv.URL + "/png"}, codes.OK},
{"a url-only png that serves html", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", URL: srv.URL + "/html"}, codes.InvalidArgument},
{"a url-only voice note that serves html", messaging.MediaAttachment{Type: messaging.MediaTypeVoice, MIMEType: "audio/ogg", URL: srv.URL + "/html"}, codes.InvalidArgument},
{"a url-only image that cannot be read", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", URL: srv.URL + "/gone"}, codes.InvalidArgument},
{"a url-only document is not fetched", messaging.MediaAttachment{Type: messaging.MediaTypeDocument, MIMEType: "application/pdf", URL: srv.URL + "/html"}, codes.OK},
{"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(context.Background(), srv.Client(), messaging.Inbound{Media: []messaging.MediaAttachment{tt.att}}))
if got != tt.want {
t.Fatalf("code = %v, want %v", got, tt.want)
}
})
}
}