2026-06-09 16:26:03 +08:00

140 lines
4.4 KiB
Go

package gameprovider
import (
"context"
"testing"
"chatapp3-golang/internal/integration"
)
type stubVisibilityGateway struct {
region integration.UserRegion
level integration.UserLevel
targetRegionCode string
}
func (s stubVisibilityGateway) GetUserRegion(context.Context, int64, string) (integration.UserRegion, error) {
return s.region, nil
}
func (s stubVisibilityGateway) GetUserLevel(context.Context, string, int64) (integration.UserLevel, error) {
return s.level, nil
}
func (s stubVisibilityGateway) ResolveRegionCodeByCountryCode(context.Context, string, string) (string, error) {
return s.targetRegionCode, nil
}
type stubIPCountryResolver struct {
countryCode string
}
func (s stubIPCountryResolver) CountryCode(context.Context, string) (string, error) {
return s.countryCode, nil
}
const account1007500UserID int64 = 2054163533368717314
func TestVisibilityPolicyAppliesTurkeyRegionOrIPWealthGate(t *testing.T) {
tests := []struct {
name string
region integration.UserRegion
ipCountry string
wealth int
wantAllow bool
wantRegion integration.UserRegion
}{
{
name: "turkey region level 2 is blocked",
region: integration.UserRegion{RegionID: "2046066409959649281", RegionCode: "土耳其"},
ipCountry: "ZA",
wealth: 2,
wantAllow: false,
wantRegion: integration.UserRegion{RegionID: "2046066409959649281", RegionCode: "土耳其"},
},
{
name: "turkey region level 3 is visible even when ip is not turkey",
region: integration.UserRegion{RegionID: "2046066409959649281", RegionCode: "土耳其"},
ipCountry: "ZA",
wealth: 3,
wantAllow: true,
wantRegion: integration.UserRegion{RegionID: "2046066409959649281", RegionCode: "土耳其"},
},
{
name: "turkey ip level 2 is blocked",
region: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
ipCountry: "TR",
wealth: 2,
wantAllow: false,
wantRegion: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
},
{
name: "turkey ip level 3 is visible",
region: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
ipCountry: "TR",
wealth: 3,
wantAllow: true,
wantRegion: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
},
{
name: "non turkey region and non turkey ip are visible even at level 1",
region: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
ipCountry: "ZA",
wealth: 1,
wantAllow: true,
wantRegion: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
},
{
name: "account 1007500 current region and ip are visible",
region: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
ipCountry: "ZA",
wealth: 35,
wantAllow: true,
wantRegion: integration.UserRegion{RegionID: "2045389832842178561", RegionCode: "南亚"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
policy := NewVisibilityPolicy(
stubVisibilityGateway{
region: tt.region,
level: integration.UserLevel{WealthLevel: tt.wealth},
targetRegionCode: "土耳其",
},
stubIPCountryResolver{countryCode: tt.ipCountry},
)
result, err := policy.Evaluate(context.Background(), AuthUser{UserID: account1007500UserID, SysOrigin: "LIKEI"}, "1.1.1.1")
if err != nil {
t.Fatalf("Evaluate() error = %v", err)
}
if result.Allow != tt.wantAllow {
t.Fatalf("Evaluate() allow = %v, want %v", result.Allow, tt.wantAllow)
}
if result.RegionID != tt.wantRegion.RegionID || result.RegionCode != tt.wantRegion.RegionCode {
t.Fatalf("Evaluate() region = %#v, want %#v", result, tt.wantRegion)
}
})
}
}
func TestVisibilityPolicySkipsOtherSysOrigin(t *testing.T) {
policy := NewVisibilityPolicy(
stubVisibilityGateway{
region: integration.UserRegion{RegionID: "2046067036517363714", RegionCode: "AR"},
level: integration.UserLevel{WealthLevel: 0},
targetRegionCode: "TR",
},
stubIPCountryResolver{countryCode: "SA"},
)
result, err := policy.Evaluate(context.Background(), AuthUser{UserID: 1001, SysOrigin: "ASWAT"}, "1.1.1.1")
if err != nil {
t.Fatalf("Evaluate() error = %v", err)
}
if !result.Allow {
t.Fatalf("Evaluate() allow = false, want true")
}
}