Files
stpweb/backend/services/services_misc_test.go
adamfandClaude Opus 5.5 18265871e1
Test and deploy / test (push) Successful in 16s
Test and deploy / deploy (push) Successful in 30s
Harden the chat hub, evict idle rate-limit keys, isolate Dozzle
- Chat: cap frame, message and file URL sizes, put a write deadline on
  every send so a stalled client can't hold the hub lock, and check the
  save, prune and history errors instead of broadcasting an unsaved
  message. ChatPanel's maxlength stays under the server cap.
- RateLimiter: sweep keys idle past the window, at most once a window.
- Dozzle: move to an internal dozzle-net shared only with Caddy, so the
  admin gate is the only way to reach the Docker socket.
- Drop the GraphQL WebSocket transport: the route is POST-only and the
  schema has no subscriptions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 15:15:31 +00:00

201 lines
5.9 KiB
Go

package services
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
// Expiry uses the real clock, hence the tiny window.
func TestRateLimiterAllow(t *testing.T) {
t.Run("calls up to max are allowed", func(t *testing.T) {
rl := NewRateLimiter(3, time.Minute)
for i := 1; i <= 3; i++ {
if !rl.Allow("ip") {
t.Fatalf("call %d denied, want allowed", i)
}
}
})
t.Run("the call after max is denied", func(t *testing.T) {
rl := NewRateLimiter(2, time.Minute)
rl.Allow("ip")
rl.Allow("ip")
if rl.Allow("ip") {
t.Fatal("third call allowed, want denied")
}
})
t.Run("a denied attempt is not recorded, so it cannot extend the lockout", func(t *testing.T) {
rl := NewRateLimiter(1, time.Minute)
rl.Allow("ip")
for i := 0; i < 5; i++ {
rl.Allow("ip")
}
// One timestamp only: the rejected hammering must not have been stored.
if got := len(rl.attempts["ip"]); got != 1 {
t.Fatalf("stored %d attempts, want 1", got)
}
})
t.Run("keys are limited independently", func(t *testing.T) {
rl := NewRateLimiter(1, time.Minute)
rl.Allow("a")
if !rl.Allow("b") {
t.Fatal("second key denied, want allowed")
}
})
t.Run("attempts older than the window stop counting", func(t *testing.T) {
rl := NewRateLimiter(1, 5*time.Millisecond)
rl.Allow("ip")
time.Sleep(10 * time.Millisecond)
if !rl.Allow("ip") {
t.Fatal("denied after the window elapsed, want allowed")
}
})
t.Run("keys idle past the window are evicted", func(t *testing.T) {
rl := NewRateLimiter(1, 5*time.Millisecond)
rl.Allow("gone")
time.Sleep(10 * time.Millisecond)
rl.Allow("other")
if _, ok := rl.attempts["gone"]; ok {
t.Fatal("idle key still stored, want evicted")
}
})
t.Run("max of zero denies everything", func(t *testing.T) {
rl := NewRateLimiter(0, time.Minute)
if rl.Allow("ip") {
t.Fatal("call allowed with max 0, want denied")
}
})
}
func TestNotesParsePath(t *testing.T) {
base := t.TempDir()
notes := InitNotes(&NotesConfig{Dir: base})
cases := []struct {
name string
in string
want string // relative to base; ignored when wantErr
wantErr bool
}{
{"empty path is the wiki index", "", "Index.md", false},
{"bare slash is the wiki index", "/", "Index.md", false},
{"a plain file resolves inside the dir", "/Notes.md", "Notes.md", false},
{"a nested file resolves inside the dir", "/private/Secret.md", "private/Secret.md", false},
{"embedded .. that stays inside is fine", "/a/../b.md", "b.md", false},
{"traversal above the base is rejected", "/../escape.md", "", true},
{"deep traversal is rejected", "/a/../../escape.md", "", true},
{"the base dir itself is rejected, only children are servable", "/.", "", true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := notes.ParsePath(c.in)
if c.wantErr {
if err == nil {
t.Fatalf("ParsePath(%q) = %q, want error", c.in, got)
}
return
}
if err != nil {
t.Fatalf("ParsePath(%q) errored: %v", c.in, err)
}
if want := filepath.Join(base, c.want); got != want {
t.Fatalf("ParsePath(%q) = %q, want %q", c.in, got, want)
}
})
}
}
// The sibling-directory case is the reason ParsePath appends a separator
// before the prefix check, so it gets its own test with real neighbouring dirs.
func TestNotesParsePathRejectsSiblingPrefix(t *testing.T) {
parent := t.TempDir()
base := filepath.Join(parent, "notes")
if err := os.Mkdir(base, 0o755); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(parent, "notes-private"), 0o755); err != nil {
t.Fatal(err)
}
notes := InitNotes(&NotesConfig{Dir: base})
if got, err := notes.ParsePath("/../notes-private/Secret.md"); err == nil {
t.Fatalf("ParsePath resolved into a sibling dir: %q", got)
}
}
func TestParseCommitMessage(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{
"first commit message of a push event",
`{"Commits":[{"Message":"fix nginx template\n"},{"Message":"second"}]}`,
"fix nginx template\n",
},
{"push event with no commits", `{"Commits":[]}`, ""},
{"non-push event whose Content is a different shape", `"some string"`, ""},
{"empty Content, as sent for events with no payload", "", ""},
{"malformed JSON is swallowed rather than surfaced", `{"Commits":`, ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := ParseCommitMessage(c.in); got != c.want {
t.Fatalf("ParseCommitMessage(%q) = %q, want %q", c.in, got, c.want)
}
})
}
}
// Guards the struct tags against a rename: Gitea's feed uses capitalised keys
// inside Content but snake_case at the top level, which is easy to "tidy" wrongly.
func TestGiteaFeedResponseDecoding(t *testing.T) {
const fixture = `[{
"act_user": {"avatar_url": "http://gitea:3000/avatars/1", "login": "adamf"},
"repo": {"full_name": "adamf/stpweb", "html_url": "http://gitea:3000/adamf/stpweb"},
"op_type": "commit_repo",
"content": "{\"Commits\":[{\"Message\":\"add tests\"}]}",
"created": "2026-09-22T10:30:00Z",
"unknown_field": 1
}]`
var items []GiteaFeedResponse
if err := json.Unmarshal([]byte(fixture), &items); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if len(items) != 1 {
t.Fatalf("got %d entries, want 1", len(items))
}
got := items[0]
if got.ActUser.AvatarURL != "http://gitea:3000/avatars/1" {
t.Errorf("AvatarURL = %q", got.ActUser.AvatarURL)
}
if got.Repo.FullName != "adamf/stpweb" {
t.Errorf("FullName = %q", got.Repo.FullName)
}
if got.Repo.HTMLURL != "http://gitea:3000/adamf/stpweb" {
t.Errorf("HTMLURL = %q", got.Repo.HTMLURL)
}
if got.OpType != "commit_repo" {
t.Errorf("OpType = %q", got.OpType)
}
if !got.Created.Equal(time.Date(2026, 9, 22, 10, 30, 0, 0, time.UTC)) {
t.Errorf("Created = %v", got.Created)
}
// Content is the nested JSON string ParseCommitMessage then decodes.
if msg := ParseCommitMessage(got.Content); msg != "add tests" {
t.Errorf("ParseCommitMessage(Content) = %q, want %q", msg, "add tests")
}
}