feat(watchlist): add company identity catalog
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>
This commit is contained in:
164
backend/internal/api/handlers/company_asset_ticket19_test.go
Normal file
164
backend/internal/api/handlers/company_asset_ticket19_test.go
Normal file
@ -0,0 +1,164 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tradarr/backend/internal/auth"
|
||||
"github.com/tradarr/backend/internal/companycatalog"
|
||||
"github.com/tradarr/backend/internal/config"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
)
|
||||
|
||||
const (
|
||||
ticket19OwnerA = "00000000-0000-0000-0000-000000000191"
|
||||
ticket19OwnerB = "00000000-0000-0000-0000-000000000192"
|
||||
ticket19BaiduCompany = "isin:KYG070341048"
|
||||
ticket19NVDACompany = "figi:BBG000BBJQV0"
|
||||
ticket19SpoofFIGI = "BBG-CLIENT-SPOOF"
|
||||
)
|
||||
|
||||
// ticket19CompanyCatalogFake models the injected, local catalog. It supplies
|
||||
// canonical domain records; the HTTP DTO remains outside this test contract.
|
||||
type ticket19CompanyCatalogFake struct {
|
||||
companies map[string]companycatalog.Company
|
||||
}
|
||||
|
||||
func (f ticket19CompanyCatalogFake) SearchCompanies(query string, limit int) ([]companycatalog.Company, error) {
|
||||
result := make([]companycatalog.Company, 0, limit)
|
||||
for _, company := range f.companies {
|
||||
if query != "" && containsFold(company.Name, query) {
|
||||
result = append(result, company)
|
||||
}
|
||||
}
|
||||
if len(result) > limit {
|
||||
result = result[:limit]
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (f ticket19CompanyCatalogFake) ResolveCompany(companyID string) (companycatalog.Company, error) {
|
||||
company, ok := f.companies[companyID]
|
||||
if !ok {
|
||||
return companycatalog.Company{}, fmt.Errorf("unknown company id")
|
||||
}
|
||||
return company, nil
|
||||
}
|
||||
|
||||
func TestTicket19FollowCompanyResolvesOpaqueIdentityIdempotentlyPerOwnerPostgres(t *testing.T) {
|
||||
previousMode := gin.Mode()
|
||||
gin.SetMode(gin.TestMode)
|
||||
t.Cleanup(func() { gin.SetMode(previousMode) })
|
||||
db := ticket4AssetDB(t)
|
||||
migration, err := os.ReadFile("../../database/migrations/000017_company_catalog.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(string(migration)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
catalog := ticket19CompanyCatalogFake{companies: map[string]companycatalog.Company{
|
||||
ticket19BaiduCompany: {
|
||||
CompanyID: ticket19BaiduCompany, ISIN: "KYG070341048", Name: "Baidu, Inc.",
|
||||
YahooSymbol: "9888.HK", FIGI: "BBG00ZMFX1S5", Exchange: "Hong Kong Stock Exchange", Country: "China",
|
||||
},
|
||||
ticket19NVDACompany: {
|
||||
CompanyID: ticket19NVDACompany, Name: "NVIDIA Corporation",
|
||||
YahooSymbol: "NVDA", FIGI: "BBG000BBJQV0", Exchange: "Nasdaq", Country: "United States",
|
||||
},
|
||||
}}
|
||||
const secret = "ticket-19-company-follow-test-secret"
|
||||
tokenA, err := auth.GenerateToken(ticket19OwnerA, "owner-a@example.test", "user", secret)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tokenB, err := auth.GenerateToken(ticket19OwnerB, "owner-b@example.test", "user", secret)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h := New(models.NewRepository(db), &config.Config{JWTSecret: secret}, nil, nil, nil, nil)
|
||||
h.SetCompanyCatalog(catalog)
|
||||
router := gin.New()
|
||||
authed := router.Group("/api")
|
||||
authed.Use(auth.Middleware(secret))
|
||||
authed.POST("/me/assets", h.AddMyAsset)
|
||||
post := func(token, companyID, symbol, name, figi string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
requestBody := map[string]string{"company_id": companyID}
|
||||
// The client may send old or hostile values; the injected catalog wins.
|
||||
if symbol != "" {
|
||||
requestBody["symbol"] = symbol
|
||||
}
|
||||
if name != "" {
|
||||
requestBody["name"] = name
|
||||
}
|
||||
if figi != "" {
|
||||
requestBody["figi"] = figi
|
||||
}
|
||||
payload, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/me/assets", bytes.NewReader(payload))
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, req)
|
||||
return response
|
||||
}
|
||||
|
||||
first := post(tokenA, ticket19BaiduCompany, "CLIENT-SPOOF-A", "Client supplied name A", ticket19SpoofFIGI)
|
||||
if first.Code != http.StatusCreated {
|
||||
t.Fatalf("follow company status=%d body=%s, want 201", first.Code, first.Body.String())
|
||||
}
|
||||
duplicate := post(tokenA, ticket19BaiduCompany, "", "", "")
|
||||
if duplicate.Code != http.StatusCreated {
|
||||
t.Fatalf("repeat follow by company identity status=%d body=%s, want idempotent success", duplicate.Code, duplicate.Body.String())
|
||||
}
|
||||
otherOwner := post(tokenB, ticket19BaiduCompany, "CLIENT-SPOOF-C", "Client supplied name C", "BBG-CLIENT-SPOOF-3")
|
||||
if otherOwner.Code != http.StatusCreated {
|
||||
t.Fatalf("other owner's same-company follow status=%d body=%s", otherOwner.Code, otherOwner.Body.String())
|
||||
}
|
||||
fallback := post(tokenB, ticket19NVDACompany, "", "", "")
|
||||
if fallback.Code != http.StatusCreated {
|
||||
t.Fatalf("FIGI-fallback company follow status=%d body=%s", fallback.Code, fallback.Body.String())
|
||||
}
|
||||
|
||||
for _, tc := range []struct{ owner, companyID string }{
|
||||
{ticket19OwnerA, ticket19BaiduCompany},
|
||||
{ticket19OwnerB, ticket19BaiduCompany},
|
||||
{ticket19OwnerB, ticket19NVDACompany},
|
||||
} {
|
||||
var count int
|
||||
if err := db.QueryRow(`SELECT COUNT(*) FROM user_assets WHERE user_id=$1 AND company_id=$2`, tc.owner, tc.companyID).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("owner %s company %s rows=%d; want one", tc.owner, tc.companyID, count)
|
||||
}
|
||||
}
|
||||
var userID, companyID, symbol, name string
|
||||
var figi sql.NullString
|
||||
err = db.QueryRow(`SELECT user_id::text,company_id,symbol,name,figi FROM user_assets WHERE company_id=$1 AND user_id=$2`, ticket19BaiduCompany, ticket19OwnerA).Scan(&userID, &companyID, &symbol, &name, &figi)
|
||||
if err != nil {
|
||||
t.Fatalf("read resolved Baidu asset: %v", err)
|
||||
}
|
||||
if userID != ticket19OwnerA || companyID != ticket19BaiduCompany || symbol != "9888.HK" || name != "Baidu, Inc." || !figi.Valid || figi.String != "BBG00ZMFX1S5" {
|
||||
t.Errorf("persisted identity did not come from CompanyCatalog: owner=%s id=%q symbol=%q name=%q figi=%v", userID, companyID, symbol, name, figi)
|
||||
}
|
||||
if bytes.Contains(first.Body.Bytes(), []byte("Client supplied")) || bytes.Contains(first.Body.Bytes(), []byte("CLIENT-SPOOF")) || bytes.Contains(first.Body.Bytes(), []byte(ticket19SpoofFIGI)) {
|
||||
t.Errorf("POST response leaked client-controlled identity: %s", first.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func containsFold(value, query string) bool {
|
||||
return len(query) == 0 || strings.Contains(strings.ToLower(value), strings.ToLower(query))
|
||||
}
|
||||
Reference in New Issue
Block a user