Files
sam c5f24ec914 chore: delete code no binary reaches
Removes the Kit task-tool result shim (and with it the MCP go-sdk
dependency), the messaging enable/disable/restart helpers and channel
rebuild closures nothing calls since channels became extensions, the
unused messaging capability, ephemeral and text-fallback types, and
single stragglers in telemetry, releaseupdate, skill, storepkg and
webui. The egress sandbox relay stays for #1867.
2026-11-13 03:33:32 +11:00

62 lines
2.1 KiB
Go

package webui
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
// TestChatFileDownload: only a file a turn offered is served, and only until
// its token expires.
func TestChatFileDownload(t *testing.T) {
path := filepath.Join(t.TempDir(), "report.txt")
if err := os.WriteFile(path, []byte("contents"), 0o600); err != nil {
t.Fatal(err)
}
now := time.Now()
srv := newChatAPI(&Server{})
srv.files.now = func() time.Time { return now }
token := srv.files.offerFor(path, "", "")
private := srv.files.offerFor(path, "", "alice")
tests := []struct {
name string
user string
token string
after time.Duration
want int
}{
{name: "an offered file is served", token: token, want: http.StatusOK},
{name: "a token nobody was given is refused", token: "0123456789abcdef0123456789abcdef", want: http.StatusNotFound},
{name: "a path is not a token", token: "..%2f..%2fetc%2fpasswd", want: http.StatusNotFound}, //nolint:gosec // G101: a fake file token.
{name: "owner downloads private file", user: "alice", token: private, want: http.StatusOK},
{name: "another person cannot download", user: "bob", token: private, want: http.StatusNotFound},
{name: "unidentified caller cannot download private file", token: private, want: http.StatusNotFound},
{name: "an expired token is refused", token: token, after: chatFileTTL + time.Second, want: http.StatusNotFound},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv.files.now = func() time.Time { return now.Add(tt.after) }
ctx := t.Context()
if tt.user != "" {
ctx = WithActingIdentity(ctx, identity.Identity{ID: identity.IdentityID(tt.user)})
}
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/chat/file/x", nil)
req.SetPathValue("token", tt.token)
rec := httptest.NewRecorder()
srv.handleChatFile(rec, req)
if rec.Code != tt.want {
t.Fatalf("status = %d, want %d", rec.Code, tt.want)
}
if tt.want == http.StatusOK && rec.Body.String() != "contents" {
t.Fatalf("body = %q", rec.Body.String())
}
})
}
}