103 lines
4.1 KiB
Go
103 lines
4.1 KiB
Go
package accounts
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.118im.com/im_team/olons_server/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) AccountByMobile(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 required", input: RegisterInput{Username: "alice", Phone: "13800138000", Password: "secret1", PrivacyAccepted: true}, want: ErrReferralInvalid},
|
|
{name: "referral", input: RegisterInput{Username: "alice", Phone: "13800138000", Password: "secret1", ReferralCode: "bad!", PrivacyAccepted: true}, want: ErrReferralInvalid},
|
|
{name: "referral length", input: RegisterInput{Username: "alice", Phone: "13800138000", Password: "secret1", ReferralCode: "12345", PrivacyAccepted: true}, want: ErrReferralInvalid},
|
|
{name: "privacy", input: RegisterInput{Username: "alice", Phone: "13800138000", Password: "secret1", ReferralCode: "123456"}, 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 TestNewInviteCodeIsSixDigits(t *testing.T) {
|
|
for i := 0; i < 100; i++ {
|
|
code, err := newInviteCode()
|
|
if err != nil || !referralPattern.MatchString(code) {
|
|
t.Fatalf("newInviteCode() = %q, %v", code, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidMemberNameSupportsChinese(t *testing.T) {
|
|
for _, name := range []string{"杨情", "欧阳娜娜", "Alicia", "阿依古丽·买买提"} {
|
|
if !validMemberName(name) {
|
|
t.Errorf("validMemberName(%q) = false, want true", name)
|
|
}
|
|
}
|
|
for _, name := range []string{"", "张 三", "user01", "这是一个超过十个字符长度的名字"} {
|
|
if validMemberName(name) {
|
|
t.Errorf("validMemberName(%q) = true, want false", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoginRateKeyDoesNotExposePhone(t *testing.T) {
|
|
s := &Service{}
|
|
key := s.loginRateKey("13800138000")
|
|
if strings.Contains(key, "13800138000") {
|
|
t.Fatal("login rate key must not expose the phone number")
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|