124 lines
4.1 KiB
Go
124 lines
4.1 KiB
Go
package hostcenter
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/integration"
|
|
)
|
|
|
|
type stubHostCenterJavaGateway struct {
|
|
credential integration.UserCredential
|
|
profile integration.UserProfile
|
|
region integration.UserRegion
|
|
releases []integration.TeamPolicyRelease
|
|
queries []integration.TeamPolicyReleaseQuery
|
|
authErr error
|
|
}
|
|
|
|
func (s *stubHostCenterJavaGateway) AuthenticateToken(context.Context, string) (integration.UserCredential, error) {
|
|
if s.authErr != nil {
|
|
return integration.UserCredential{}, s.authErr
|
|
}
|
|
return s.credential, nil
|
|
}
|
|
|
|
func (s *stubHostCenterJavaGateway) GetUserProfile(context.Context, int64) (integration.UserProfile, error) {
|
|
return s.profile, nil
|
|
}
|
|
|
|
func (s *stubHostCenterJavaGateway) GetUserRegion(context.Context, int64, string) (integration.UserRegion, error) {
|
|
return s.region, nil
|
|
}
|
|
|
|
func (s *stubHostCenterJavaGateway) GetTeamPolicyRelease(_ context.Context, query integration.TeamPolicyReleaseQuery) (integration.TeamPolicyRelease, error) {
|
|
s.queries = append(s.queries, query)
|
|
if len(s.releases) == 0 {
|
|
return integration.TeamPolicyRelease{}, nil
|
|
}
|
|
release := s.releases[0]
|
|
s.releases = s.releases[1:]
|
|
return release, nil
|
|
}
|
|
|
|
func TestGetAnchorPolicyUsesTokenRegionAndCountryPolicy(t *testing.T) {
|
|
java := &stubHostCenterJavaGateway{
|
|
credential: integration.UserCredential{UserID: integration.Int64Value(1001), SysOrigin: "LIKEI"},
|
|
profile: integration.UserProfile{CountryCode: "AE"},
|
|
region: integration.UserRegion{RegionID: "MENA", RegionCode: "MENA"},
|
|
releases: []integration.TeamPolicyRelease{{
|
|
ID: integration.Int64Value(99),
|
|
Release: true,
|
|
Title: "MENA Policy",
|
|
Policy: []integration.TeamPolicyItem{{
|
|
Level: 1,
|
|
OnlineTime: integration.Int64Value(2),
|
|
EffectiveDay: 5,
|
|
Target: integration.Int64Value(10000),
|
|
}},
|
|
}},
|
|
}
|
|
service := NewService(config.Config{}, java)
|
|
|
|
resp, err := service.GetAnchorPolicy(context.Background(), AnchorPolicyRequest{Token: "token-value"})
|
|
if err != nil {
|
|
t.Fatalf("GetAnchorPolicy() error = %v", err)
|
|
}
|
|
if resp.UserID != 1001 || resp.Region != "MENA" || resp.CountryCode != "AE" || resp.PolicyType != defaultPolicyType {
|
|
t.Fatalf("unexpected response metadata: %+v", resp)
|
|
}
|
|
if len(resp.Policy) != 1 || resp.Policy[0].Level != 1 {
|
|
t.Fatalf("unexpected policy rows: %+v", resp.Policy)
|
|
}
|
|
if len(java.queries) != 1 {
|
|
t.Fatalf("query count = %d, want 1", len(java.queries))
|
|
}
|
|
if got := java.queries[0]; got.SysOrigin != "LIKEI" || got.Region != "MENA" || got.CountryCode != "AE" || got.PolicyType != defaultPolicyType {
|
|
t.Fatalf("unexpected query: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestGetAnchorPolicyFallsBackWhenCountryPolicyIsEmpty(t *testing.T) {
|
|
java := &stubHostCenterJavaGateway{
|
|
credential: integration.UserCredential{UserID: integration.Int64Value(1001), SysOrigin: "LIKEI"},
|
|
profile: integration.UserProfile{CountryCode: "AE"},
|
|
region: integration.UserRegion{RegionID: "MENA"},
|
|
releases: []integration.TeamPolicyRelease{
|
|
{},
|
|
{ID: integration.Int64Value(100), Title: "Default Policy"},
|
|
},
|
|
}
|
|
service := NewService(config.Config{}, java)
|
|
|
|
resp, err := service.GetAnchorPolicy(context.Background(), AnchorPolicyRequest{Token: "token-value"})
|
|
if err != nil {
|
|
t.Fatalf("GetAnchorPolicy() error = %v", err)
|
|
}
|
|
if resp.Title != "Default Policy" {
|
|
t.Fatalf("fallback title = %q", resp.Title)
|
|
}
|
|
if len(java.queries) != 2 {
|
|
t.Fatalf("query count = %d, want 2", len(java.queries))
|
|
}
|
|
if java.queries[0].CountryCode != "AE" || java.queries[1].CountryCode != "" {
|
|
t.Fatalf("fallback queries = %+v", java.queries)
|
|
}
|
|
}
|
|
|
|
func TestGetAnchorPolicyRejectsInvalidToken(t *testing.T) {
|
|
java := &stubHostCenterJavaGateway{authErr: errors.New("bad token")}
|
|
service := NewService(config.Config{}, java)
|
|
|
|
_, err := service.GetAnchorPolicy(context.Background(), AnchorPolicyRequest{Token: "bad"})
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
appErr, ok := err.(*AppError)
|
|
if !ok || appErr.Status != http.StatusUnauthorized || appErr.Code != "invalid_token" {
|
|
t.Fatalf("unexpected error = %#v", err)
|
|
}
|
|
}
|