Files
kindexr/internal/torznab/server_test.go
T
enki 1933306392 cleanup: pre-phase-2 fixes
- fix two stale nzbstr comment refs
- migration 002: drop api_keys.visibility and curation_set
- remove Visibility from APIKey struct, GetAPIKey, CreateAPIKey, CLI
- omit torznab size/seeders/peers attrs when data is absent
- reset relay backoff on successful connection (>= 30s uptime)
- use last_event from relays table as since on reconnect
- fix TestSearchWithResults to actually query the test server DB
2026-05-16 23:21:22 -07:00

176 lines
4.4 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 newTestServerWithDB(t *testing.T) (*httptest.Server, string, *db.DB) {
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)
}
keyBytes := make([]byte, 32)
rand.Read(keyBytes)
apiKey := hex.EncodeToString(keyBytes)
if err := database.CreateAPIKey(context.Background(), apiKey, "test"); 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, database
}
func newTestServer(t *testing.T) (*httptest.Server, string) {
ts, apiKey, _ := newTestServerWithDB(t)
return ts, 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, database := newTestServerWithDB(t)
defer ts.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)
}
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)
}
var rss torznab.RSS
if err := xml.NewDecoder(resp.Body).Decode(&rss); err != nil {
t.Fatalf("decode RSS: %v", err)
}
if len(rss.Channel.Items) == 0 {
t.Error("expected at least one search result")
}
if rss.Channel.Items[0].Title != "Breaking.Bad.S01E01.1080p.WEB-DL" {
t.Errorf("unexpected first result title: %q", rss.Channel.Items[0].Title)
}
}
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)
}
}