Files
olons_server/internal/accounts/service_test.go
T
2026-10-12 20:29:27 +08:00

79 lines
3.1 KiB
Go

package accounts
import (
"context"
"errors"
"strings"
"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) 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", 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 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)
}
}
}