113 lines
3.7 KiB
Go
113 lines
3.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/user-management-system/internal/domain"
|
|
)
|
|
|
|
type totpTestRepo struct {
|
|
user *domain.User
|
|
getErr error
|
|
updateTOTPErr error
|
|
}
|
|
|
|
func (r *totpTestRepo) Create(ctx context.Context, user *domain.User) error { return nil }
|
|
func (r *totpTestRepo) Update(ctx context.Context, user *domain.User) error { return nil }
|
|
func (r *totpTestRepo) UpdateTOTP(ctx context.Context, user *domain.User) error {
|
|
if r.updateTOTPErr != nil {
|
|
return r.updateTOTPErr
|
|
}
|
|
copyUser := *user
|
|
r.user = ©User
|
|
return nil
|
|
}
|
|
func (r *totpTestRepo) Delete(ctx context.Context, id int64) error { return nil }
|
|
func (r *totpTestRepo) GetByID(ctx context.Context, id int64) (*domain.User, error) {
|
|
if r.getErr != nil {
|
|
return nil, r.getErr
|
|
}
|
|
if r.user == nil || r.user.ID != id {
|
|
return nil, errors.New("not found")
|
|
}
|
|
copyUser := *r.user
|
|
return ©User, nil
|
|
}
|
|
func (r *totpTestRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) {
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) GetByEmail(ctx context.Context, email string) (*domain.User, error) {
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) GetByPhone(ctx context.Context, phone string) (*domain.User, error) {
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) List(ctx context.Context, offset, limit int) ([]*domain.User, int64, error) {
|
|
return nil, 0, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) ListByStatus(ctx context.Context, status domain.UserStatus, offset, limit int) ([]*domain.User, int64, error) {
|
|
return nil, 0, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) UpdateStatus(ctx context.Context, id int64, status domain.UserStatus) error {
|
|
return errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) UpdateLastLogin(ctx context.Context, id int64, ip string) error {
|
|
return errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) ExistsByUsername(ctx context.Context, username string) (bool, error) {
|
|
return false, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) ExistsByEmail(ctx context.Context, email string) (bool, error) {
|
|
return false, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) ExistsByPhone(ctx context.Context, phone string) (bool, error) {
|
|
return false, errors.New("not implemented")
|
|
}
|
|
func (r *totpTestRepo) Search(ctx context.Context, keyword string, offset, limit int) ([]*domain.User, int64, error) {
|
|
return nil, 0, errors.New("not implemented")
|
|
}
|
|
|
|
func TestTOTPService_ReturnsDecodeErrorForCorruptedRecoveryCodes(t *testing.T) {
|
|
repo := &totpTestRepo{user: &domain.User{
|
|
ID: 42,
|
|
Username: "totp-user",
|
|
TOTPEnabled: true,
|
|
TOTPSecret: "invalid-secret",
|
|
TOTPRecoveryCodes: "not-json",
|
|
}}
|
|
svc := NewTOTPService(repo)
|
|
|
|
err := svc.VerifyTOTP(context.Background(), 42, "recovery-code")
|
|
if err == nil {
|
|
t.Fatal("expected corrupted recovery-code payload to fail")
|
|
}
|
|
if !strings.Contains(err.Error(), "解析恢复码失败") {
|
|
t.Fatalf("expected decode error, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTOTPService_ReturnsUpdateErrorAfterRecoveryCodeConsumption(t *testing.T) {
|
|
repo := &totpTestRepo{
|
|
user: &domain.User{
|
|
ID: 7,
|
|
Username: "totp-user",
|
|
TOTPEnabled: true,
|
|
TOTPSecret: "invalid-secret",
|
|
TOTPRecoveryCodes: `["RECOVERY-1"]`,
|
|
},
|
|
updateTOTPErr: errors.New("write failed"),
|
|
}
|
|
svc := NewTOTPService(repo)
|
|
|
|
err := svc.VerifyTOTP(context.Background(), 7, "RECOVERY-1")
|
|
if err == nil {
|
|
t.Fatal("expected update failure to be returned")
|
|
}
|
|
if !strings.Contains(err.Error(), "更新恢复码失败") {
|
|
t.Fatalf("expected update error, got: %v", err)
|
|
}
|
|
}
|