feat(indices): add authenticated OpenFIGI search
Add the bounded OpenFIGI v3 adapter, authenticated search route, and server-side configuration. Refs #3 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
82
backend/internal/api/index_search_http_test.go
Normal file
82
backend/internal/api/index_search_http_test.go
Normal file
@ -0,0 +1,82 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tradarr/backend/internal/api"
|
||||
"github.com/tradarr/backend/internal/api/handlers"
|
||||
"github.com/tradarr/backend/internal/auth"
|
||||
"github.com/tradarr/backend/internal/config"
|
||||
"github.com/tradarr/backend/internal/indices"
|
||||
)
|
||||
|
||||
type fakeIndexSearcher struct {
|
||||
calls int
|
||||
query string
|
||||
limit int
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeIndexSearcher) Search(_ context.Context, q string, limit int) ([]indices.Index, error) {
|
||||
f.calls++
|
||||
f.query = q
|
||||
f.limit = limit
|
||||
return []indices.Index{{Name: "NASDAQ", Symbol: "IXIC", FIGI: "figi"}}, f.err
|
||||
}
|
||||
func TestIndexSearchHTTPInjectionValidationAndErrors(t *testing.T) {
|
||||
token, err := auth.GenerateToken("owner", "owner@example.test", "user", ticket3JWTSecret)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, q, limit string
|
||||
err error
|
||||
want, calls int
|
||||
}{
|
||||
{"success", " NASDAQ ", "", nil, 200, 1},
|
||||
{"unicode boundary", strings.Repeat("界", 100), "20", nil, 200, 1},
|
||||
{"blank", " ", "", nil, 400, 0},
|
||||
{"long", strings.Repeat("界", 101), "", nil, 400, 0},
|
||||
{"empty limit", "NASDAQ", "empty", nil, 400, 0},
|
||||
{"bad limit", "NASDAQ", "21", nil, 400, 0},
|
||||
{"quota", "NASDAQ", "", indices.ErrRateLimited, 429, 1},
|
||||
{"timeout", "NASDAQ", "", context.DeadlineExceeded, 504, 1},
|
||||
{"provider", "NASDAQ", "", errors.New("private-key-and-provider-body"), 502, 1},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fake := &fakeIndexSearcher{err: tc.err}
|
||||
h := handlers.New(nil, &config.Config{}, nil, nil, nil, nil)
|
||||
h.SetIndexSearcher(fake)
|
||||
r := api.SetupRouter(h, ticket3JWTSecret)
|
||||
path := "/api/indices/search?q=" + url.QueryEscape(tc.q)
|
||||
if tc.limit != "" {
|
||||
value := tc.limit
|
||||
if value == "empty" {
|
||||
value = ""
|
||||
}
|
||||
path += "&limit=" + value
|
||||
}
|
||||
request := httptest.NewRequest("GET", path, nil)
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, request)
|
||||
if w.Code != tc.want || fake.calls != tc.calls {
|
||||
t.Fatalf("status=%d calls=%d body=%s", w.Code, fake.calls, w.Body.String())
|
||||
}
|
||||
if fake.calls > 0 && (fake.query != strings.TrimSpace(tc.q) || fake.limit < 1 || fake.limit > 20) {
|
||||
t.Fatal("invalid port arguments")
|
||||
}
|
||||
if strings.Contains(w.Body.String(), "private-key") {
|
||||
t.Fatal("private error leaked")
|
||||
}
|
||||
if tc.name == "success" && (fake.limit != 10 || !strings.Contains(w.Body.String(), `"data":[{"name":"NASDAQ","symbol":"IXIC","figi":"figi"}]`)) {
|
||||
t.Fatalf("unexpected envelope: %s", w.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user