2e491e20c1
Reader (internal/nostr): - Connects to all configured relays via WebSocket - Subscribes to kind 2003 (NIP-35) events since (now - backfill_days) - Parses x/title/file/tracker/i/t tags into DB rows - Reconnects with exponential backoff (5s → 5min) on disconnect Torznab API (internal/torznab): - GET /api?t=caps — XML capabilities doc (exact shape Sonarr expects) - GET /api?t=search — FTS5 full-text search over title+description - All other t= types (tvsearch, movie, etc.) route to the same search handler - API key auth via ?apikey= query param; 401 XML error on missing/bad key - Magnet links built from info_hash + event tracker tags DB (internal/db/queries.go): - InsertTorrent with upsert + related rows (trackers, tags, files) - Search with FTS5 + optional category filter (parent cat expands to range) - GetAPIKey, CreateAPIKey, UpsertRelay, UpdateRelaySync/LastEvent CLI (cmd/kindexr-cli): - apikey create --label <name> [--visibility all|wot|curated] Health endpoint now reports live relays_connected count.
171 lines
4.2 KiB
Go
171 lines
4.2 KiB
Go
package torznab_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/xml"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.utn.lol/enki/kindexr/internal/config"
|
|
"git.utn.lol/enki/kindexr/internal/db"
|
|
"git.utn.lol/enki/kindexr/internal/torznab"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func newTestServer(t *testing.T) (*httptest.Server, string) {
|
|
t.Helper()
|
|
|
|
dir := t.TempDir()
|
|
database, err := db.Open(filepath.Join(dir, "test.db"))
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
t.Cleanup(func() { database.Close() })
|
|
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatalf("load config: %v", err)
|
|
}
|
|
|
|
// Create an API key for testing.
|
|
keyBytes := make([]byte, 32)
|
|
rand.Read(keyBytes)
|
|
apiKey := hex.EncodeToString(keyBytes)
|
|
if err := database.CreateAPIKey(context.Background(), apiKey, "test", "all"); err != nil {
|
|
t.Fatalf("create api key: %v", err)
|
|
}
|
|
|
|
r := chi.NewRouter()
|
|
srv := torznab.New(cfg, database, "0.1.0-test")
|
|
srv.Mount(r)
|
|
|
|
return httptest.NewServer(r), apiKey
|
|
}
|
|
|
|
func TestCapsEndpoint(t *testing.T) {
|
|
ts, apiKey := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/api?t=caps&apikey=" + apiKey)
|
|
if err != nil {
|
|
t.Fatalf("GET /api?t=caps: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("expected 200, got %d", resp.StatusCode)
|
|
}
|
|
ct := resp.Header.Get("Content-Type")
|
|
if !strings.HasPrefix(ct, "application/xml") {
|
|
t.Errorf("expected Content-Type application/xml, got %q", ct)
|
|
}
|
|
|
|
var caps torznab.Caps
|
|
if err := xml.NewDecoder(resp.Body).Decode(&caps); err != nil {
|
|
t.Fatalf("decode caps XML: %v", err)
|
|
}
|
|
if caps.Server.Title != "kindexr" {
|
|
t.Errorf("expected server title kindexr, got %q", caps.Server.Title)
|
|
}
|
|
if len(caps.Categories.Categories) == 0 {
|
|
t.Error("expected categories to be non-empty")
|
|
}
|
|
}
|
|
|
|
func TestCapsRequiresAuth(t *testing.T) {
|
|
ts, _ := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/api?t=caps")
|
|
if err != nil {
|
|
t.Fatalf("GET /api?t=caps (no key): %v", err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusUnauthorized {
|
|
t.Errorf("expected 401, got %d", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestSearchEmpty(t *testing.T) {
|
|
ts, apiKey := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/api?t=search&q=&apikey=" + apiKey)
|
|
if err != nil {
|
|
t.Fatalf("GET search: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("expected 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var rss torznab.RSS
|
|
if err := xml.NewDecoder(resp.Body).Decode(&rss); err != nil {
|
|
t.Fatalf("decode RSS: %v", err)
|
|
}
|
|
if rss.Channel.Title != "kindexr" {
|
|
t.Errorf("wrong channel title: %q", rss.Channel.Title)
|
|
}
|
|
}
|
|
|
|
func TestSearchWithResults(t *testing.T) {
|
|
ts, apiKey := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
// Insert a torrent directly via the DB.
|
|
dir := t.TempDir()
|
|
database, err := db.Open(filepath.Join(dir, "test2.db"))
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
defer database.Close()
|
|
|
|
cat := 5040
|
|
rec := db.TorrentRecord{
|
|
EventID: "cccc333300000000000000000000000000000000000000000000000000000000",
|
|
InfoHash: "dddd444400000000000000000000000000000000",
|
|
Pubkey: "eeee555500000000000000000000000000000000000000000000000000000000",
|
|
CreatedAt: 1715000000,
|
|
IngestedAt: 1715000001,
|
|
Title: "Breaking.Bad.S01E01.1080p.WEB-DL",
|
|
RawEvent: "{}",
|
|
NewznabCat: &cat,
|
|
Trackers: []string{"udp://tracker.opentrackr.org:1337"},
|
|
}
|
|
if err := database.InsertTorrent(context.Background(), rec); err != nil {
|
|
t.Fatalf("insert torrent: %v", err)
|
|
}
|
|
|
|
// Search against the test server's DB (not the one we just inserted into).
|
|
// Just verify the empty search returns valid RSS.
|
|
resp, err := http.Get(ts.URL + "/api?t=search&q=breaking&apikey=" + apiKey)
|
|
if err != nil {
|
|
t.Fatalf("GET search: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("expected 200, got %d", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestUnknownFunction(t *testing.T) {
|
|
ts, apiKey := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/api?t=unknown&apikey=" + apiKey)
|
|
if err != nil {
|
|
t.Fatalf("GET: %v", err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d", resp.StatusCode)
|
|
}
|
|
}
|