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>
99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/companycatalog"
|
|
"github.com/tradarr/backend/internal/config"
|
|
"github.com/tradarr/backend/internal/models"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCompanyRepositoryPreservesLegacyAndReturnsHintsPostgres(t *testing.T) {
|
|
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)
|
|
}
|
|
repo := models.NewRepository(db)
|
|
figi := "BBG00ZMFX1S5"
|
|
legacy, err := repo.UpsertUserAsset(ticket4OwnerA, "LEGACY INDEX", "legacy retained", &figi)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
company := companycatalog.Company{CompanyID: "isin:KYG070341048", Name: "Baidu, Inc.", YahooSymbol: "9888.HK", FIGI: figi, Country: "China", Exchange: "HKG"}
|
|
first, err := repo.UpsertCompanyAsset(ticket4OwnerA, company)
|
|
if err != nil {
|
|
t.Fatalf("company upsert: %v", err)
|
|
}
|
|
again, err := repo.UpsertCompanyAsset(ticket4OwnerA, company)
|
|
if err != nil || again.ID != first.ID {
|
|
t.Fatalf("non-idempotent: %v", err)
|
|
}
|
|
other, err := repo.UpsertCompanyAsset(ticket4OwnerB, company)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assets, err := repo.GetUserAssets(ticket4OwnerA)
|
|
if err != nil || len(assets) != 2 {
|
|
t.Fatalf("assets=%+v err=%v", assets, err)
|
|
}
|
|
for _, a := range assets {
|
|
if a.ID == legacy.ID && (a.Name != "legacy retained" || a.Symbol != "LEGACY INDEX" || a.CompanyID != nil || a.YahooSymbol != nil) {
|
|
t.Fatal("legacy row changed")
|
|
}
|
|
if a.ID == first.ID && (a.YahooSymbol == nil || *a.YahooSymbol != "9888.HK" || a.CompanyID == nil || *a.CompanyID != company.CompanyID) {
|
|
t.Fatal("lost company hints")
|
|
}
|
|
}
|
|
if err := repo.RemoveUserAssetByID(ticket4OwnerA, other.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
theirs, err := repo.GetUserAssets(ticket4OwnerB)
|
|
if err != nil || len(theirs) != 1 {
|
|
t.Fatal("foreign delete changed data")
|
|
}
|
|
}
|
|
|
|
func TestCompanyFollowIgnoresEvenMalformedSpoofFieldsPostgres(t *testing.T) {
|
|
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)
|
|
}
|
|
repo := models.NewRepository(db)
|
|
h := New(repo, &config.Config{}, nil, nil, nil, nil)
|
|
company := companycatalog.Company{CompanyID: "isin:KYG070341048", Name: "Baidu, Inc.", YahooSymbol: "9888.HK"}
|
|
h.SetCompanyCatalog(ticket19CompanyCatalogFake{companies: map[string]companycatalog.Company{company.CompanyID: company}})
|
|
router := gin.New()
|
|
router.Use(func(c *gin.Context) { c.Set("userID", ticket4OwnerA) })
|
|
router.POST("/assets", h.AddMyAsset)
|
|
for _, tc := range []struct {
|
|
body string
|
|
status int
|
|
}{
|
|
{`{"company_id":"isin:KYG070341048","name":{"private":"spoof"},"symbol":["SSAAPL INDEX"],"figi":123,"user_id":"foreign"}`, 201},
|
|
{`{"company_id":"isin:unknown","symbol":"CLIENT-SPOOF"}`, 400},
|
|
} {
|
|
req := httptest.NewRequest("POST", "/assets", strings.NewReader(tc.body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, req)
|
|
if response.Code != tc.status {
|
|
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
}
|
|
assets, err := repo.GetUserAssets(ticket4OwnerA)
|
|
if err != nil || len(assets) != 1 || assets[0].Name != company.Name {
|
|
t.Fatalf("assets=%+v err=%v", assets, err)
|
|
}
|
|
}
|