Files
pipelines/internal/server/chat_uploads_test.go
T

88 lines
3.0 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func postChatUpload(t *testing.T, name, body string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodPost, "/chat-uploads", strings.NewReader(body))
if name != "" {
req.Header.Set(chatUploadHeader, name)
}
req.Header.Set("Content-Type", "image/png")
rec := httptest.NewRecorder()
serveChatUpload(rec, req)
return rec
}
func TestChatUploadIsReferencedByAPrompt(t *testing.T) {
dir := filepath.Join(t.TempDir(), "uploads")
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
// Larger than the inline 1 MiB route allows.
body := strings.Repeat("x", 3<<20)
rec := postChatUpload(t, "big%20photo.png", body)
if rec.Code != http.StatusOK {
t.Fatalf("upload status = %d %s", rec.Code, rec.Body)
}
var reply chatUploadReply
if err := json.Unmarshal(rec.Body.Bytes(), &reply); err != nil {
t.Fatal(err)
}
if reply.Name != "big photo.png" || reply.Size != int64(len(body)) || reply.Mime != "image/png" || !chatUploadName.MatchString(reply.Upload) {
t.Fatalf("reply = %+v", reply)
}
info, err := os.Stat(filepath.Join(dir, reply.Upload))
if err != nil || info.Mode().Perm() != 0o600 || info.Size() != int64(len(body)) {
t.Fatalf("stored file = %v %v", info, err)
}
files, parts, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "big photo.png", Mime: "image/png", Upload: reply.Upload}})
if err != nil {
t.Fatal(err)
}
if len(files) != 1 || len(parts) != 1 || parts[0].Name != "big photo.png" || !strings.Contains(parts[0].URI, reply.Upload[:35]) {
t.Fatalf("files = %+v parts = %+v", files, parts)
}
releaseChatAttachments(files)
}
func TestChatUploadRefusals(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
if rec := postChatUpload(t, "", "x"); rec.Code != http.StatusBadRequest {
t.Fatalf("missing name status = %d", rec.Code)
}
if rec := postChatUpload(t, "empty.txt", ""); rec.Code != http.StatusBadRequest {
t.Fatalf("empty status = %d", rec.Code)
}
req := httptest.NewRequest(http.MethodPost, "/chat-uploads", strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "a.txt")
req.Header.Set("Sec-Fetch-Site", "cross-site")
rec := httptest.NewRecorder()
serveChatUpload(rec, req)
if rec.Code != http.StatusForbidden {
t.Fatalf("cross-site status = %d", rec.Code)
}
req = httptest.NewRequest(http.MethodPost, "/chat-uploads", strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "a.txt")
req.ContentLength = maxChatUploadBytes + 1
rec = httptest.NewRecorder()
serveChatUpload(rec, req)
if rec.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("oversized status = %d", rec.Code)
}
for _, upload := range []string{"../escape", "up-zz-x", "up-00000000000000000000000000000000-missing.txt"} {
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Upload: upload}}); err == nil {
t.Fatalf("reference %q accepted", upload)
}
}
}