api/ui: Allow per-scratch TTL down to 1m
Format / gofmt (push) Successful in 13s
CI / Build (push) Successful in 22s
CI / Go Tests (push) Successful in 23s
Format / gofmt (pull_request) Successful in 12s
CI / Build (pull_request) Successful in 20s
CI / Go Tests (pull_request) Successful in 20s

#4
This commit is contained in:
s1d3sw1ped_bot
2026-09-01 21:16:36 +00:00
parent 1d301d29b6
commit de465d6818
6 changed files with 439 additions and 12 deletions
+68 -1
View File
@@ -7,9 +7,11 @@ import (
"net/http"
"net/http/httptest"
"net/netip"
"net/url"
"path/filepath"
"strings"
"testing"
"time"
"scratchbox/internal/config"
"scratchbox/internal/storage"
@@ -88,11 +90,76 @@ func TestGetUIConfig(t *testing.T) {
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
if got := rec.Body.String(); got == "" || !containsAll(got, "max_upload_size_bytes", "2048", `"upload_allowed":true`) {
if got := rec.Body.String(); got == "" || !containsAll(got, "max_upload_size_bytes", "2048", `"upload_allowed":true`, `"max_ttl":"`+config.MaxScratchTTL.String()+`"`, "default_ttl") {
t.Fatalf("unexpected body: %q", got)
}
}
func TestResolveTTL(t *testing.T) {
t.Parallel()
s := &Server{
cfg: config.Config{
Limits: config.LimitsConfig{
DefaultTTLDuration: 15 * time.Minute,
},
},
}
cases := []struct {
name string
url string
formTTL string
multipart bool
want time.Duration
wantErr bool
}{
{name: "omit raw uses default", url: "/api/scratch", want: 15 * time.Minute},
{name: "empty query uses default", url: "/api/scratch?ttl=", want: 15 * time.Minute},
{name: "empty form uses default", url: "/api/scratch", multipart: true, formTTL: " ", want: 15 * time.Minute},
{name: "query min", url: "/api/scratch?ttl=1m", want: time.Minute},
{name: "query max", url: "/api/scratch?ttl=24h", want: config.MaxScratchTTL},
{name: "query default value", url: "/api/scratch?ttl=15m", want: 15 * time.Minute},
{name: "form min", url: "/api/scratch", multipart: true, formTTL: "1m", want: time.Minute},
{name: "form max", url: "/api/scratch", multipart: true, formTTL: "24h", want: config.MaxScratchTTL},
{name: "form default value", url: "/api/scratch", multipart: true, formTTL: "15m", want: 15 * time.Minute},
{name: "invalid duration", url: "/api/scratch?ttl=nope", wantErr: true},
{name: "below min", url: "/api/scratch?ttl=59s", wantErr: true},
{name: "above max", url: "/api/scratch?ttl=24h1s", wantErr: true},
{name: "zero", url: "/api/scratch?ttl=0", wantErr: true},
{name: "negative", url: "/api/scratch?ttl=-1m", wantErr: true},
{name: "form invalid", url: "/api/scratch", multipart: true, formTTL: "forever", wantErr: true},
{name: "form below min", url: "/api/scratch", multipart: true, formTTL: "30s", wantErr: true},
{name: "form above max", url: "/api/scratch", multipart: true, formTTL: "25h", wantErr: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(http.MethodPost, tc.url, nil)
if tc.multipart {
req.Form = url.Values{}
if tc.formTTL != "" {
req.Form.Set("ttl", tc.formTTL)
}
}
got, err := s.resolveTTL(req, tc.multipart)
if tc.wantErr {
if err == nil {
t.Fatalf("resolveTTL() error = nil, want error (got %s)", got)
}
return
}
if err != nil {
t.Fatalf("resolveTTL() error = %v", err)
}
if got != tc.want {
t.Fatalf("resolveTTL() = %s, want %s", got, tc.want)
}
})
}
}
func TestGetUIConfigUploadDenied(t *testing.T) {
t.Parallel()