* docs(autoscan): add arr webhook intake spec and implementation plan Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add webhook intake schema migration Adds delivery_mode to autoscan_sources, the autoscan_webhook_endpoints table, and delivery_mode/provider_event_type on autoscan_events. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add built-in arr-webhook source identity Host-discovered scan-source entry so webhook-mode sources need no plugin installation; composite lister appends it to plugin discovery. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): persist delivery mode, webhook endpoints, event metadata Sources carry delivery_mode; autoscan_webhook_endpoints CRUD with SHA-256 token lookup and AAD-bound encrypted redisplay; events record delivery_mode/provider_event_type; CreateEvent gains SkipRunningCheck so webhook deliveries are never dropped by the poll exclusion. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): share the consume path and add webhook IngestChanges Extracts consumeSourceChanges from PollOnce (marker semantics preserved, existing poll tests unchanged); PollOnce skips webhook sources; IngestChanges feeds deliveries through the shared pipeline without markers and without the running-event exclusion. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add Sonarr/Radarr webhook payload parser Host-side arrwebhook package: provider inference, import/rename/delete path extraction with vanished-path-friendly previous paths, subtree fallback, exact-path dedupe, and no-op unknown events. Fixture-backed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add public webhook delivery route and admin endpoint management Public POST /api/v1/autoscan/webhooks/{token} with per-IP rate limiting, 256KiB body cap, 202-for-noop semantics, and token/body kept out of logs; admin create/rotate/delete endpoint routes; source responses carry delivery mode + webhook status/URL; create/update validate delivery mode against source identity. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): add webhook delivery mode to Autoscan admin UI Webhook sources get a generate/copy/rotate webhook URL section, provider selector, delivery status, and a connection-free Add-source flow; activity rows badge webhook deliveries with the arr event type. Path rewrites stay editable in both modes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): redact secret path params from request and activity logs The request logger and activity-log middleware recorded raw URLs, so bearer credentials in secret path segments (autoscan webhook {token}, webhook-sync {secret}) were persisted to app logs and activity_log. Redact the secret segment via the chi route params in both sinks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(autoscan): make webhook delivery reliable --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
246 lines
7.4 KiB
Go
246 lines
7.4 KiB
Go
package arrwebhook
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/autoscan"
|
|
)
|
|
|
|
func fixture(t *testing.T, name string) []byte {
|
|
t.Helper()
|
|
body, err := os.ReadFile(filepath.Join("testdata", name))
|
|
if err != nil {
|
|
t.Fatalf("read fixture %s: %v", name, err)
|
|
}
|
|
return body
|
|
}
|
|
|
|
func paths(changes []autoscan.Change) []string {
|
|
out := make([]string, 0, len(changes))
|
|
for _, c := range changes {
|
|
out = append(out, c.SourcePath)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestParseSonarrDownload(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "sonarr_download.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if parsed.Provider != ProviderSonarr || parsed.EventType != "Download" || parsed.Test {
|
|
t.Fatalf("unexpected classification: %+v", parsed)
|
|
}
|
|
want := "/data/tv/Example Show/Season 02/Example Show - S02E01 - The One That Imports.mkv"
|
|
if len(parsed.Changes) != 1 || parsed.Changes[0].SourcePath != want || parsed.Changes[0].Scope != autoscan.ChangeScopeFile {
|
|
t.Fatalf("changes = %+v", parsed.Changes)
|
|
}
|
|
}
|
|
|
|
func TestParseSonarrRenameIncludesPreviousPaths(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "sonarr_rename.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
got := paths(parsed.Changes)
|
|
want := []string{
|
|
"/data/tv/Example Show/Season 02/Example Show - S02E01.mkv",
|
|
"/data/tv/Example Show/Season 02/old-name-e01.mkv",
|
|
"/data/tv/Example Show/Season 02/Example Show - S02E02.mkv",
|
|
"/data/tv/Example Show/Season 02/old-name-e02.mkv",
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("paths = %v, want %v", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("paths[%d] = %q, want %q", i, got[i], want[i])
|
|
}
|
|
}
|
|
for _, c := range parsed.Changes {
|
|
if c.Scope != autoscan.ChangeScopeFile {
|
|
t.Fatalf("rename changes must be file scope, got %+v", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseSonarrEpisodeFileDelete(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "sonarr_episodefile_delete.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if parsed.EventType != "EpisodeFileDelete" {
|
|
t.Fatalf("event type = %q", parsed.EventType)
|
|
}
|
|
if len(parsed.Changes) != 1 || parsed.Changes[0].SourcePath != "/data/tv/Example Show/Season 02/Example Show - S02E01.mkv" {
|
|
t.Fatalf("changes = %+v", parsed.Changes)
|
|
}
|
|
}
|
|
|
|
func TestParseSonarrTest(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "sonarr_test.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if !parsed.Test || len(parsed.Changes) != 0 {
|
|
t.Fatalf("test event must be a no-op: %+v", parsed)
|
|
}
|
|
if parsed.Provider != ProviderSonarr {
|
|
t.Fatalf("provider = %q", parsed.Provider)
|
|
}
|
|
}
|
|
|
|
func TestParseRadarrDownload(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "radarr_download.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if parsed.Provider != ProviderRadarr || parsed.EventType != "Download" {
|
|
t.Fatalf("unexpected classification: %+v", parsed)
|
|
}
|
|
want := "/data/movies/Example Movie (2024)/Example Movie (2024) Bluray-1080p.mkv"
|
|
if len(parsed.Changes) != 1 || parsed.Changes[0].SourcePath != want {
|
|
t.Fatalf("changes = %+v", parsed.Changes)
|
|
}
|
|
}
|
|
|
|
func TestParseUpgradeIncludesReplacedFiles(t *testing.T) {
|
|
for name, body := range map[string]string{
|
|
"sonarr": `{
|
|
"eventType": "Download",
|
|
"series": {"path": "/data/tv/Show"},
|
|
"episodeFile": {"path": "/data/tv/Show/new.mkv"},
|
|
"deletedFiles": [{"path": "/data/tv/Show/old.mkv"}],
|
|
"isUpgrade": true
|
|
}`,
|
|
"radarr": `{
|
|
"eventType": "Download",
|
|
"movie": {"folderPath": "/data/movies/Movie"},
|
|
"movieFile": {"path": "/data/movies/Movie/new.mkv"},
|
|
"deletedFiles": [{"path": "/data/movies/Movie/old.mkv"}],
|
|
"isUpgrade": true
|
|
}`,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, []byte(body))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
got := paths(parsed.Changes)
|
|
if len(got) != 2 || got[1] == "" || got[1] == got[0] {
|
|
t.Fatalf("upgrade paths = %v, want new and replaced file", got)
|
|
}
|
|
for _, change := range parsed.Changes {
|
|
if change.Scope != autoscan.ChangeScopeFile {
|
|
t.Fatalf("upgrade change = %+v, want file scope", change)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseRadarrRename(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "radarr_rename.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
got := paths(parsed.Changes)
|
|
want := []string{
|
|
"/data/movies/Example Movie (2024)/Example Movie (2024) Bluray-1080p.mkv",
|
|
"/data/movies/Example Movie (2024)/old-release-name.mkv",
|
|
}
|
|
if len(got) != 2 || got[0] != want[0] || got[1] != want[1] {
|
|
t.Fatalf("paths = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParseRadarrMovieFileDelete(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "radarr_moviefile_delete.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if parsed.EventType != "MovieFileDelete" || len(parsed.Changes) != 1 {
|
|
t.Fatalf("unexpected parse: %+v", parsed)
|
|
}
|
|
}
|
|
|
|
func TestParseRadarrTest(t *testing.T) {
|
|
parsed, err := Parse(ProviderAuto, fixture(t, "radarr_test.json"))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if !parsed.Test || parsed.Provider != ProviderRadarr {
|
|
t.Fatalf("unexpected parse: %+v", parsed)
|
|
}
|
|
}
|
|
|
|
func TestParseUnknownEventIsNoOpNotError(t *testing.T) {
|
|
parsed, err := Parse(ProviderSonarr, fixture(t, "unknown_event.json"))
|
|
if err != nil {
|
|
t.Fatalf("unknown event types must parse: %v", err)
|
|
}
|
|
if parsed.EventType != "Health" || parsed.Test || len(parsed.Changes) != 0 {
|
|
t.Fatalf("unknown event must be a no-op: %+v", parsed)
|
|
}
|
|
}
|
|
|
|
func TestParseMalformedBody(t *testing.T) {
|
|
if _, err := Parse(ProviderAuto, []byte("{not json")); !errors.Is(err, ErrMalformedPayload) {
|
|
t.Fatalf("want ErrMalformedPayload, got %v", err)
|
|
}
|
|
if _, err := Parse(ProviderAuto, []byte(`{"noEventType": true}`)); !errors.Is(err, ErrMalformedPayload) {
|
|
t.Fatalf("missing eventType must be malformed, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseAutoInferenceFailure(t *testing.T) {
|
|
// A work-producing event with neither series nor movie shape cannot be
|
|
// attributed in auto mode.
|
|
body := []byte(`{"eventType": "Download"}`)
|
|
if _, err := Parse(ProviderAuto, body); !errors.Is(err, ErrUnknownProvider) {
|
|
t.Fatalf("want ErrUnknownProvider, got %v", err)
|
|
}
|
|
// The same payload with an explicit provider parses (zero changes).
|
|
parsed, err := Parse(ProviderSonarr, body)
|
|
if err != nil {
|
|
t.Fatalf("explicit provider must parse: %v", err)
|
|
}
|
|
if len(parsed.Changes) != 0 {
|
|
t.Fatalf("changes = %+v, want none", parsed.Changes)
|
|
}
|
|
}
|
|
|
|
func TestParseImportFallsBackToSubtree(t *testing.T) {
|
|
body := []byte(`{
|
|
"eventType": "Download",
|
|
"series": {"path": "/data/tv/Example Show"}
|
|
}`)
|
|
parsed, err := Parse(ProviderAuto, body)
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if len(parsed.Changes) != 1 ||
|
|
parsed.Changes[0].SourcePath != "/data/tv/Example Show" ||
|
|
parsed.Changes[0].Scope != autoscan.ChangeScopeSubtree {
|
|
t.Fatalf("changes = %+v, want one subtree fallback", parsed.Changes)
|
|
}
|
|
}
|
|
|
|
func TestParseDedupesExactPaths(t *testing.T) {
|
|
body := []byte(`{
|
|
"eventType": "Download",
|
|
"episodeFile": {"path": "/data/tv/S/e1.mkv"},
|
|
"episodeFiles": [{"path": "/data/tv/S/e1.mkv"}, {"path": "/data/tv/S/e2.mkv"}]
|
|
}`)
|
|
parsed, err := Parse(ProviderAuto, body)
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if got := paths(parsed.Changes); len(got) != 2 || got[0] != "/data/tv/S/e1.mkv" || got[1] != "/data/tv/S/e2.mkv" {
|
|
t.Fatalf("paths = %v, want deduped pair", got)
|
|
}
|
|
}
|