Authorisation: @admin and @signedIn schema directives replace the 25 hand-copied resolver checks. A test fails if a mutation has neither and is not on the public allow-list, so a new mutation cannot ship unguarded. IDs: uint model IDs bind through graphql.UintID, removing the ten int(obj.ID) resolvers. Job status is a GraphQL enum, tied to services.statusOrder by a test. Input is strict, so the mutations no longer coerce an unknown status to "applied"; output is lenient so one legacy row cannot fail the list. deleteJobApplication and deleteJobAppReference now error on a missing id like the other deletes. The rowing reader, the job extractor and now the email classifier reach the Python service through one services.CallPython helper. The email pipeline's prompt and fence stripping moved to Python, and anthropic-sdk-go is gone from go.mod. Fix the auth test fixture, whose 11-byte secret had been panicking the services test package since InitAuth began requiring 32 bytes. Add a test that keeps the python service's compose environment list in step with the env registry. Co-Authored-By: Claude Opus 5.5 <[email protected]>
202 lines
6.4 KiB
Go
202 lines
6.4 KiB
Go
package services
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Covered here: the pure logic in ratelimit.go, notes.go and gitea.go —
|
|
// sliding-window arithmetic, the path-traversal guard, and the two-stage JSON
|
|
// decoding of a Gitea feed entry (from a hand-written fixture).
|
|
//
|
|
// Deliberately not covered:
|
|
// - FetchLatestFeed: it is an HTTP call to the Gitea container. Its only
|
|
// logic beyond the request is the empty-feed nil,nil case, and testing it
|
|
// would mean either a network or an injected client that does not exist.
|
|
// - The 1-minute Gitea cache TTL: it lives in handlers.Store, not here.
|
|
// - RateLimiter's window expiry uses the real clock (Allow calls time.Now
|
|
// directly and there is no injectable clock), so the expiry case below
|
|
// uses a deliberately tiny window rather than a long sleep.
|
|
|
|
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("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/web_server", "html_url": "http://gitea:3000/adamf/web_server"},
|
|
"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/web_server" {
|
|
t.Errorf("FullName = %q", got.Repo.FullName)
|
|
}
|
|
if got.Repo.HTMLURL != "http://gitea:3000/adamf/web_server" {
|
|
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")
|
|
}
|
|
}
|