Files
TradarrV2/backend/internal/api/index_search_ticket3_test.go
Blomios 5cb45138df 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>
2026-11-20 22:31:35 +01:00

81 lines
3.0 KiB
Go

package api_test
import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
"github.com/gin-gonic/gin"
"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"
)
const ticket3JWTSecret = "ticket-3-test-jwt-secret"
func TestIndexSearchHandlerExposesFakeSearcherInjectionSeam(t *testing.T) {
handler := handlers.New(nil, &config.Config{JWTSecret: ticket3JWTSecret}, nil, nil, nil, nil)
if method := reflect.ValueOf(handler).MethodByName("SetIndexSearcher"); !method.IsValid() {
t.Fatal("Handler must expose SetIndexSearcher so HTTP tests can inject a fake and never call OpenFIGI")
}
}
func TestIndexSearchRouteRequiresAuthenticationAndValidatesQuery(t *testing.T) {
previousMode := gin.Mode()
gin.SetMode(gin.TestMode)
t.Cleanup(func() { gin.SetMode(previousMode) })
router := api.SetupRouter(handlers.New(nil, &config.Config{JWTSecret: ticket3JWTSecret}, nil, nil, nil, nil), ticket3JWTSecret)
userToken, err := auth.GenerateToken("ticket3-user", "index-user@example.test", "user", ticket3JWTSecret)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
query string
omitQuery bool
limit string
token string
wantStatus int
}{
{name: "anonymous denied", query: "NASDAQ", wantStatus: http.StatusUnauthorized},
{name: "missing query rejected", omitQuery: true, token: userToken, wantStatus: http.StatusBadRequest},
{name: "blank query rejected after trim", query: " ", token: userToken, wantStatus: http.StatusBadRequest},
{name: "101 Unicode characters rejected", query: strings.Repeat("界", 101), token: userToken, wantStatus: http.StatusBadRequest},
{name: "limit 0 rejected", query: "NASDAQ", limit: "0", token: userToken, wantStatus: http.StatusBadRequest},
{name: "limit 21 rejected", query: "NASDAQ", limit: "21", token: userToken, wantStatus: http.StatusBadRequest},
{name: "non-integer limit rejected", query: "NASDAQ", limit: "many", token: userToken, wantStatus: http.StatusBadRequest},
}
// Only malformed authenticated requests are used here. A valid search would
// require the handler's injected fake IndexSearcher seam before this test may
// exercise it, so this suite can never send a live provider request.
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params := url.Values{}
if !tt.omitQuery {
params.Set("q", tt.query)
}
if tt.limit != "" {
params.Set("limit", tt.limit)
}
path := "/api/indices/search"
if encoded := params.Encode(); encoded != "" {
path += "?" + encoded
}
request := httptest.NewRequest(http.MethodGet, path, nil)
if tt.token != "" {
request.Header.Set("Authorization", "Bearer "+tt.token)
}
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
if response.Code != tt.wantStatus {
t.Errorf("GET /api/indices/search: got %d, want %d; body=%s", response.Code, tt.wantStatus, response.Body.String())
}
})
}
}