fix: accept fixed legacy registration code

This commit is contained in:
richjohnrambo
2026-09-22 00:39:14 +08:00
parent 3276c6d1cd
commit ac10b31aa1
2 changed files with 81 additions and 0 deletions
+39
View File
@@ -6,7 +6,9 @@ import (
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/tinode/chat/server/store"
"github.com/tinode/chat/server/store/mock_store"
"github.com/tinode/chat/server/store/types"
)
@@ -148,6 +150,43 @@ func TestAttachLegacyRegistrationSMSChallenge(t *testing.T) {
}
}
func TestLegacyFixedRegistrationCredWithoutChallenge(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
oldUsers := store.Users
oldVerifications := store.Verifications
defer func() {
store.Users = oldUsers
store.Verifications = oldVerifications
}()
tenantID := types.TenantID(7)
phone := "+8618612345678"
store.Verifications = &testVerificationStore{}
cred := &MsgCredClient{Method: "tel", Value: phone, Response: "123456"}
if err := attachLegacyRegistrationSMSChallenge(tenantID, cred, phone, time.Now().UTC()); err != nil {
t.Fatalf("attach legacy fixed code returned %v", err)
}
users := mock_store.NewMockUsersPersistenceInterface(ctrl)
users.EXPECT().UpsertCred(tenantID, gomock.AssignableToTypeOf(&types.Credential{})).
DoAndReturn(func(_ types.TenantID, got *types.Credential) (bool, error) {
if got.User != types.Uid(42).String() || got.Method != "tel" || got.Value != phone || !got.Done {
t.Fatalf("unexpected credential: %#v", got)
}
return true, nil
})
store.Users = users
ok, err := consumeLegacyFixedRegistrationCred(tenantID, 42, cred, func(value string, _ map[string]any) (string, error) {
return "tel:" + value, nil
})
if err != nil || !ok {
t.Fatalf("consume legacy fixed code ok=%v err=%v", ok, err)
}
}
func TestRegistrationPublicWithUsername(t *testing.T) {
if got := registrationPublicWithUsername(nil, "alice").(map[string]any)["fn"]; got != "alice" {
t.Fatalf("got fn=%v", got)