Embed a pinned FinanceDatabase equity snapshot, add authenticated company search and identity-based follows, preserve legacy assets, and map selected Yahoo symbols for collection. Refs #19 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
83 lines
3.1 KiB
Go
83 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/api/handlers"
|
|
"github.com/tradarr/backend/internal/auth"
|
|
"github.com/tradarr/backend/internal/config"
|
|
)
|
|
|
|
func TestTicket19CompanySearchIsAuthenticatedBoundedAndReturnsSafeCompanyDTO(t *testing.T) {
|
|
previousMode := gin.Mode()
|
|
gin.SetMode(gin.TestMode)
|
|
t.Cleanup(func() { gin.SetMode(previousMode) })
|
|
const secret = "ticket-19-company-search-test-secret"
|
|
const userID = "00000000-0000-0000-0000-000000000019"
|
|
h := handlers.New(nil, &config.Config{JWTSecret: secret}, nil, nil, nil, nil)
|
|
router := SetupRouter(h, secret)
|
|
|
|
call := func(token, path string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, req)
|
|
return response
|
|
}
|
|
unauthenticated := call("", "/api/companies/search?q=BYD&limit=2")
|
|
if unauthenticated.Code != http.StatusUnauthorized {
|
|
t.Errorf("anonymous company search status=%d, want 401 (body=%s)", unauthenticated.Code, unauthenticated.Body.String())
|
|
}
|
|
token, err := auth.GenerateToken(userID, "ticket19@example.test", "user", secret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
valid := call(token, "/api/companies/search?q=BYD&limit=2")
|
|
if valid.Code != http.StatusOK {
|
|
t.Errorf("authenticated company search status=%d, want 200 (body=%s)", valid.Code, valid.Body.String())
|
|
} else {
|
|
var envelope struct {
|
|
Data []map[string]json.RawMessage `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(valid.Body.Bytes(), &envelope); err != nil {
|
|
t.Fatalf("decode company results: %v", err)
|
|
}
|
|
if len(envelope.Data) == 0 {
|
|
t.Fatal("company search returned no local results for BYD")
|
|
}
|
|
for i, result := range envelope.Data {
|
|
var name, companyID string
|
|
if err := json.Unmarshal(result["name"], &name); err != nil || strings.TrimSpace(name) == "" {
|
|
t.Errorf("result %d lacks display name: %s", i, valid.Body.String())
|
|
}
|
|
if err := json.Unmarshal(result["company_id"], &companyID); err != nil || strings.TrimSpace(companyID) == "" {
|
|
t.Errorf("result %d lacks resolved company_id: %s", i, valid.Body.String())
|
|
}
|
|
if len(result["exchange"]) == 0 && len(result["country"]) == 0 {
|
|
t.Errorf("result %d lacks a company disambiguator: %s", i, valid.Body.String())
|
|
}
|
|
}
|
|
for _, privateKey := range []string{`"symbol":`, `"ticker":`, `"figi":`, `"composite_figi":`} {
|
|
if strings.Contains(valid.Body.String(), privateKey) {
|
|
t.Errorf("search DTO exposes private identity field %s: %s", privateKey, valid.Body.String())
|
|
}
|
|
}
|
|
}
|
|
maxValidLimit := call(token, "/api/companies/search?q=BYD&limit=20")
|
|
if maxValidLimit.Code != http.StatusOK {
|
|
t.Errorf("company search limit=20 status=%d, want 200 (body=%s)", maxValidLimit.Code, maxValidLimit.Body.String())
|
|
}
|
|
invalidLimit := call(token, "/api/companies/search?q=BYD&limit=21")
|
|
if invalidLimit.Code != http.StatusBadRequest {
|
|
t.Errorf("company search limit=21 status=%d, want 400 (body=%s)", invalidLimit.Code, invalidLimit.Body.String())
|
|
}
|
|
}
|