对接
This commit is contained in:
@@ -36,7 +36,7 @@ type Account struct {
|
||||
}
|
||||
|
||||
type RegisterInput struct {
|
||||
Username, Password, ConfirmPassword string
|
||||
Username, Nickname, Password, ConfirmPassword string
|
||||
}
|
||||
|
||||
type LoginInput struct {
|
||||
@@ -84,6 +84,12 @@ func (uc *Usecase) Register(ctx context.Context, in *RegisterInput, ip, agent st
|
||||
if n := len([]rune(in.Username)); n < 3 || n > 32 || in.Password == "" || in.Password != in.ConfirmPassword {
|
||||
return nil, ErrInvalidAccount
|
||||
}
|
||||
in.Nickname = strings.TrimSpace(in.Nickname)
|
||||
if in.Nickname == "" {
|
||||
in.Nickname = in.Username
|
||||
} else if n := len([]rune(in.Nickname)); n < 2 || n > 16 {
|
||||
return nil, ErrInvalidAccount
|
||||
}
|
||||
config, err := uc.security.Current(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -95,7 +101,7 @@ func (uc *Usecase) Register(ctx context.Context, in *RegisterInput, ip, agent st
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
account, err := uc.repo.Register(ctx, &Account{Username: in.Username, Nickname: in.Username, AuthorityID: ClientAuthorityID, Enabled: true}, string(hash), InitialDiamonds)
|
||||
account, err := uc.repo.Register(ctx, &Account{Username: in.Username, Nickname: in.Nickname, AuthorityID: ClientAuthorityID, Enabled: true}, string(hash), InitialDiamonds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package liveclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
)
|
||||
|
||||
type registerRepoStub struct {
|
||||
Repo
|
||||
account *Account
|
||||
}
|
||||
|
||||
func (r *registerRepoStub) Register(_ context.Context, account *Account, _ string, _ int64) (*Account, error) {
|
||||
copy := *account
|
||||
copy.UserID = 1
|
||||
copy.UUID = "client-uuid"
|
||||
copy.CreatedAt = time.Now()
|
||||
r.account = ©
|
||||
return ©, nil
|
||||
}
|
||||
|
||||
type registerSecurityRepoStub struct{ system.SecurityRepo }
|
||||
|
||||
func (*registerSecurityRepoStub) SecurityConfig(context.Context) (*system.SecurityConfig, error) {
|
||||
return &system.SecurityConfig{PwdMinLength: 8}, nil
|
||||
}
|
||||
|
||||
type registerSettingsStub struct{ system.RuntimeSettings }
|
||||
|
||||
func (*registerSettingsStub) UseMultipoint() bool { return false }
|
||||
|
||||
type registerIssuerStub struct{ system.TokenIssuer }
|
||||
|
||||
func (*registerIssuerStub) IssueToken(*system.User, uint, bool, time.Duration) (*system.IssuedToken, error) {
|
||||
return &system.IssuedToken{Value: "token", ExpiresAt: time.Now().Add(time.Hour), TTL: time.Hour}, nil
|
||||
}
|
||||
|
||||
func (*registerIssuerStub) ReissueToken(claims *system.AuthClaims, _ uint) (*system.IssuedToken, error) {
|
||||
return &system.IssuedToken{Value: "token", ExpiresAt: claims.ExpiresAt, TTL: time.Until(claims.ExpiresAt)}, nil
|
||||
}
|
||||
|
||||
func (*registerIssuerStub) ParseToken(string) (*system.AuthClaims, error) { return nil, nil }
|
||||
|
||||
func newRegisterUsecase(repo *registerRepoStub) *Usecase {
|
||||
security := system.NewSecurityUsecase(®isterSecurityRepoStub{}, nil, ®isterSettingsStub{}, nil)
|
||||
auth := system.NewAuthenticationUsecase(nil, security, ®isterIssuerStub{}, nil)
|
||||
return NewUsecase(repo, security, auth, nil)
|
||||
}
|
||||
|
||||
func TestRegisterUsesTrimmedNickname(t *testing.T) {
|
||||
repo := ®isterRepoStub{}
|
||||
uc := newRegisterUsecase(repo)
|
||||
result, err := uc.Register(context.Background(), &RegisterInput{
|
||||
Username: "viewer",
|
||||
Nickname: " 直播用户 ",
|
||||
Password: "password",
|
||||
ConfirmPassword: "password",
|
||||
}, "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Account.Nickname != "直播用户" || repo.account.Nickname != "直播用户" {
|
||||
t.Fatalf("nickname = %q, repo nickname = %q", result.Account.Nickname, repo.account.Nickname)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterDefaultsNicknameToUsername(t *testing.T) {
|
||||
repo := ®isterRepoStub{}
|
||||
uc := newRegisterUsecase(repo)
|
||||
result, err := uc.Register(context.Background(), &RegisterInput{
|
||||
Username: "viewer",
|
||||
Password: "password",
|
||||
ConfirmPassword: "password",
|
||||
}, "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Account.Nickname != "viewer" {
|
||||
t.Fatalf("nickname = %q, want username fallback", result.Account.Nickname)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRejectsNicknameOutsideRange(t *testing.T) {
|
||||
for _, nickname := range []string{"A", "12345678901234567"} {
|
||||
t.Run(nickname, func(t *testing.T) {
|
||||
repo := ®isterRepoStub{}
|
||||
uc := newRegisterUsecase(repo)
|
||||
_, err := uc.Register(context.Background(), &RegisterInput{
|
||||
Username: "viewer",
|
||||
Nickname: nickname,
|
||||
Password: "password",
|
||||
ConfirmPassword: "password",
|
||||
}, "", "")
|
||||
if !errors.Is(err, ErrInvalidAccount) {
|
||||
t.Fatalf("error = %v, want ErrInvalidAccount", err)
|
||||
}
|
||||
if repo.account != nil {
|
||||
t.Fatal("repository called for invalid nickname")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user