fix: allow legacy enterprise registration payloads

This commit is contained in:
richjohnrambo
2026-09-22 00:23:53 +08:00
parent dc36ce292e
commit 3276c6d1cd
2 changed files with 127 additions and 1 deletions
+65
View File
@@ -2,6 +2,7 @@ package main
import (
"regexp"
"strings"
"testing"
"time"
@@ -83,6 +84,70 @@ func TestRegistrationSecretForAccount(t *testing.T) {
}
}
func TestValidateAccountRegistrationTenantDefaultsEnterpriseFromSession(t *testing.T) {
msg := &ClientComMessage{Acc: &MsgClientAcc{}}
s := &Session{tenantCode: "system"}
if err := validateAccountRegistrationTenant(s, msg); err != nil {
t.Fatalf("validateAccountRegistrationTenant returned %v", err)
}
if msg.Acc.RegistrationType == nil || *msg.Acc.RegistrationType != registrationTypeEnterprise {
t.Fatalf("registration type was not defaulted to enterprise: %#v", msg.Acc.RegistrationType)
}
if msg.Acc.TenantCode != "system" {
t.Fatalf("tenant code default = %q, want system", msg.Acc.TenantCode)
}
}
func TestValidateAccountRegistrationTenantStillRequiresPersonalType(t *testing.T) {
err := validateAccountRegistrationTenant(&Session{tenantCode: imDefaultTenantCode}, &ClientComMessage{Acc: &MsgClientAcc{}})
if err != errRegistrationTypeRequired {
t.Fatalf("got %v, want registration type required", err)
}
}
func TestAttachLegacyRegistrationSMSChallenge(t *testing.T) {
oldVerifications := store.Verifications
defer func() {
store.Verifications = oldVerifications
}()
tenantID := types.TenantID(7)
phone := "+8618612345678"
code := "123456"
phoneHash := hashIMSecret(tenantID, "register", phone, "phone")
challengeID := "challenge-legacy"
store.Verifications = &testVerificationStore{active: &types.VerificationCode{
TenantID: tenantID,
ChallengeID: challengeID,
Scene: "register",
PhoneHash: phoneHash,
CodeHash: hashIMSecret(tenantID, "register", challengeID, phoneHash, code),
State: types.VerificationStatePending,
MaxAttempts: 5,
ExpiresAt: time.Now().UTC().Add(time.Minute),
}}
cred := &MsgCredClient{Method: "tel", Value: phone, Response: code}
if err := attachLegacyRegistrationSMSChallenge(tenantID, cred, phone, time.Now().UTC()); err != nil {
t.Fatalf("attachLegacyRegistrationSMSChallenge returned %v", err)
}
if cred.Params["challenge_id"] != challengeID {
t.Fatalf("challenge id = %#v, want %q", cred.Params["challenge_id"], challengeID)
}
if cred.Response == code || strings.TrimSpace(cred.Response) == "" {
t.Fatalf("legacy code was not replaced by verification token: %q", cred.Response)
}
verifications := store.Verifications.(*testVerificationStore)
if !verifications.verified {
t.Fatal("verification challenge was not marked verified")
}
wantHash := hashIMSecret(tenantID, "register", challengeID, phoneHash, cred.Response)
if verifications.tokenHash != wantHash {
t.Fatalf("token hash mismatch")
}
}
func TestRegistrationPublicWithUsername(t *testing.T) {
if got := registrationPublicWithUsername(nil, "alice").(map[string]any)["fn"]; got != "alice" {
t.Fatalf("got fn=%v", got)