对接
This commit is contained in:
@@ -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