95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
package appconfig
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
)
|
|
|
|
func TestListH5LinksReadsOnlyRequestedApp(t *testing.T) {
|
|
db, mock, err := sqlmock.New()
|
|
if err != nil {
|
|
t.Fatalf("new sql mock failed: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
mock.ExpectQuery(regexp.QuoteMeta(listH5LinksSQL)).
|
|
WithArgs("yumi", h5LinkGroup).
|
|
WillReturnRows(sqlmock.NewRows([]string{"key", "description", "value", "updated_at_ms"}).
|
|
AddRow("admin", "Yumi Admin", "https://yumi.example.com/admin", int64(200)).
|
|
AddRow("public", "", "https://yumi.example.com/public", int64(300)))
|
|
|
|
reader := &MySQLReader{db: db}
|
|
items, err := reader.ListH5Links(context.Background(), H5LinkQuery{AppCode: " YUMI "})
|
|
if err != nil {
|
|
t.Fatalf("list scoped h5 links failed: %v", err)
|
|
}
|
|
if len(items) != 2 {
|
|
t.Fatalf("unexpected merged h5 links: %+v", items)
|
|
}
|
|
if items[0].Key != "admin" || items[0].Label != "Yumi Admin" || items[0].URL != "https://yumi.example.com/admin" {
|
|
t.Fatalf("scoped override did not replace baseline: %+v", items[0])
|
|
}
|
|
if items[1].Key != "public" || items[1].Label != "public" || items[1].URL != "https://yumi.example.com/public" {
|
|
t.Fatalf("requested app config mismatch: %+v", items[1])
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatalf("sql expectations mismatch: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeBannerDisplayScopeAcceptsChineseLabels(t *testing.T) {
|
|
cases := map[string]string{
|
|
"": "home",
|
|
"首页": "home",
|
|
"房间内": "room",
|
|
"充值页": "recharge",
|
|
"我的页": "me",
|
|
"ME": "me",
|
|
"ROOM": "room",
|
|
"bad": "",
|
|
}
|
|
for input, want := range cases {
|
|
if got := NormalizeBannerDisplayScope(input); got != want {
|
|
t.Fatalf("scope %q mismatch: got %q want %q", input, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBannerDisplayScopeListParsesMultiScopeValue(t *testing.T) {
|
|
got := bannerDisplayScopeList("room,home,recharge,me")
|
|
want := []string{"home", "room", "recharge", "me"}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("scope count mismatch: got %+v want %+v", got, want)
|
|
}
|
|
for index := range want {
|
|
if got[index] != want[index] {
|
|
t.Fatalf("scope %d mismatch: got %+v want %+v", index, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseRoomRegionWhitelistSupportsObjectAndArrayPayloads(t *testing.T) {
|
|
cases := []string{
|
|
`{"user_ids":["325379237278126080","1001","1001","0","bad"]}`,
|
|
`["325379237278126080","1001","1001","0","bad"]`,
|
|
}
|
|
for _, input := range cases {
|
|
got, err := parseRoomRegionWhitelist(input)
|
|
if err != nil {
|
|
t.Fatalf("parse whitelist %s failed: %v", input, err)
|
|
}
|
|
if _, ok := got[325379237278126080]; !ok {
|
|
t.Fatalf("long user id missing: %+v", got)
|
|
}
|
|
if _, ok := got[1001]; !ok {
|
|
t.Fatalf("small user id missing: %+v", got)
|
|
}
|
|
if _, ok := got[0]; ok {
|
|
t.Fatalf("zero user id must be ignored: %+v", got)
|
|
}
|
|
}
|
|
}
|