package accounts import ( "context" "crypto/aes" "crypto/cipher" "encoding/base64" "errors" "testing" "git.118im.com/im_team/im_workbench/internal/store" ) type fakeRepository struct{} func (fakeRepository) RegisterAccount(context.Context, store.AccountRegistration) (*store.Account, error) { return nil, errors.New("should not be called") } func (fakeRepository) AccountByMobileHash(context.Context, int64, string) (*store.Account, error) { return nil, errors.New("should not be called") } func (fakeRepository) RecordLogin(context.Context, int64, int64) error { return errors.New("should not be called") } func (fakeRepository) MemberOverview(context.Context, int64, int64) (*store.MemberOverview, error) { return nil, errors.New("should not be called") } func (fakeRepository) UpdateAvatar(context.Context, int64, int64, int64) (*store.Avatar, error) { return nil, errors.New("should not be called") } func (fakeRepository) AccountSecurity(context.Context, int64, int64) (*store.AccountSecurity, error) { return nil, errors.New("should not be called") } func (fakeRepository) UpdateLoginPassword(context.Context, int64, int64, string, string) error { return errors.New("should not be called") } func (fakeRepository) UpdateTradePassword(context.Context, int64, int64, string, string) error { return errors.New("should not be called") } func TestRegisterValidation(t *testing.T) { s := &Service{repository: fakeRepository{}, ready: true} tests := []struct { name string input RegisterInput want error }{ {name: "username", input: RegisterInput{Username: "中文", Phone: "13800138000", Password: "secret1", PrivacyAccepted: true}, want: ErrUsernameInvalid}, {name: "phone", input: RegisterInput{Username: "alice", Phone: "12800138000", Password: "secret1", PrivacyAccepted: true}, want: ErrPhoneInvalid}, {name: "password", input: RegisterInput{Username: "alice", Phone: "13800138000", Password: "123", PrivacyAccepted: true}, want: ErrPasswordInvalid}, {name: "referral", input: RegisterInput{Username: "alice", Phone: "13800138000", Password: "secret1", ReferralCode: "bad!", PrivacyAccepted: true}, want: ErrReferralInvalid}, {name: "privacy", input: RegisterInput{Username: "alice", Phone: "13800138000", Password: "secret1"}, want: ErrPrivacyNotAccepted}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { _, err := s.Register(context.Background(), test.input) if !errors.Is(err, test.want) { t.Fatalf("Register() error = %v, want %v", err, test.want) } }) } } func TestMobileEncryptionRoundTrip(t *testing.T) { key := make([]byte, 32) block, err := aes.NewCipher(key) if err != nil { t.Fatal(err) } aead, err := cipher.NewGCM(block) if err != nil { t.Fatal(err) } s := &Service{aead: aead, lookupKey: []byte("01234567890123456789012345678901")} encrypted, err := s.encrypt("+8613800138000") if err != nil { t.Fatal(err) } raw, err := base64.RawStdEncoding.DecodeString(encrypted) if err != nil { t.Fatal(err) } plain, err := aead.Open(nil, raw[:aead.NonceSize()], raw[aead.NonceSize():], nil) if err != nil || string(plain) != "+8613800138000" { t.Fatalf("decrypt = %q, err=%v", plain, err) } if len(s.lookupHash("+8613800138000")) != 64 { t.Fatal("mobile lookup hash must be hex sha256") } } func TestLoginRequiresMainlandPhone(t *testing.T) { s := &Service{repository: fakeRepository{}, ready: true} for _, phone := range []string{"", "alice01", "12800138000", "+8613800138000"} { _, err := s.Login(context.Background(), LoginInput{Phone: phone, Password: "secret1"}) if !errors.Is(err, ErrPhoneInvalid) { t.Errorf("Login(phone=%q) error = %v, want %v", phone, err, ErrPhoneInvalid) } } }