租户库共库分表实现

This commit is contained in:
richjohnrambo
2026-09-23 09:36:17 +08:00
parent 137d6fb6da
commit 7c1fab5065
4 changed files with 131 additions and 27 deletions
+50 -1
View File
@@ -55,6 +55,7 @@ func TestVerificationCodeCachePreservesInternalFields(t *testing.T) {
Action: "signup",
PhoneHash: "phone-hash",
PhoneMask: "+861****000",
TargetUser: types.ParseUserId("usrR25C_ZgHzyg"),
CodeHash: "code-hash",
VerificationTokenHash: &tokenHash,
State: types.VerificationStateVerified,
@@ -81,6 +82,7 @@ func TestVerificationCodeCachePreservesInternalFields(t *testing.T) {
}
if !strings.Contains(string(cachePayload), `"code_hash":"code-hash"`) ||
!strings.Contains(string(cachePayload), `"phone_hash":"phone-hash"`) ||
!strings.Contains(string(cachePayload), `"target_user":"usrR25C_ZgHzyg"`) ||
!strings.Contains(string(cachePayload), `"max_attempts":5`) {
t.Fatalf("cache verification JSON missing internal fields: %s", cachePayload)
}
@@ -91,8 +93,55 @@ func TestVerificationCodeCachePreservesInternalFields(t *testing.T) {
}
got := cached.toType()
if got.CodeHash != vc.CodeHash || got.PhoneHash != vc.PhoneHash ||
got.State != vc.State || got.Attempts != vc.Attempts || got.MaxAttempts != vc.MaxAttempts ||
got.TargetUser != vc.TargetUser || got.State != vc.State || got.Attempts != vc.Attempts || got.MaxAttempts != vc.MaxAttempts ||
got.VerificationTokenHash == nil || *got.VerificationTokenHash != tokenHash {
t.Fatalf("cache round trip lost fields: got %#v", got)
}
}
func TestActionTokenCachePreservesInternalFields(t *testing.T) {
now := time.Now().UTC().Round(time.Millisecond)
consumedAt := now.Add(time.Minute)
token := &types.ActionToken{
ID: 9,
TenantID: 7,
UserID: types.ParseUserId("usrR25C_ZgHzyg"),
TokenID: "token-id",
Action: "close_youth_mode",
Target: "youth_mode",
TokenHash: "token-hash",
State: 1,
ExpiresAt: now.Add(5 * time.Minute),
ConsumedAt: &consumedAt,
CreatedAt: now,
UpdatedAt: consumedAt,
}
publicPayload, err := json.Marshal(token)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(publicPayload), "token-hash") || strings.Contains(string(publicPayload), "token-id") {
t.Fatalf("public action token JSON leaked internal fields: %s", publicPayload)
}
cachePayload, err := json.Marshal(actionTokenCacheFromType(token))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(cachePayload), `"token_hash":"token-hash"`) ||
!strings.Contains(string(cachePayload), `"token_id":"token-id"`) ||
!strings.Contains(string(cachePayload), `"state":1`) {
t.Fatalf("cache action token JSON missing internal fields: %s", cachePayload)
}
var cached actionTokenCache
if err := json.Unmarshal(cachePayload, &cached); err != nil {
t.Fatal(err)
}
got := cached.toType()
if got.TokenHash != token.TokenHash || got.TokenID != token.TokenID ||
got.UserID != token.UserID || got.State != token.State || got.Action != token.Action {
t.Fatalf("cache round trip lost fields: got %#v", got)
}
}