refac: reamaining auth surface rewrite, T0
This commit is contained in:
1 parent
6d510d2805
commit
aa865af2e9
50 files changed
+3723
-149
No files matched your search
@@ -0,0 +1,81 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/user"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAccountVerificationUseCase(t *testing.T) {
|
||||
t.Run("The use case itself enforces the rule, independently of any handler", func(t *testing.T) {
|
||||
repo := newMockUserRepo()
|
||||
repo.users[3] = user.User{
|
||||
ID: 3,
|
||||
VerificationCode: "hashed:right-code",
|
||||
VerificationExpiration: time.Now().Add(time.Hour),
|
||||
}
|
||||
uc := useCases.NewVerifyUserUseCase(repo, &stubHasher{})
|
||||
|
||||
_, err := uc.Verify(3, "wrong-code")
|
||||
|
||||
if !errors.Is(err, user.ErrInvalidVerificationCode) {
|
||||
t.Fatalf("expected ErrInvalidVerificationCode, got %v", err)
|
||||
}
|
||||
if repo.clearVerificationCodeCalls != 0 {
|
||||
t.Errorf("expected ClearVerificationCode to never be called, got %d calls", repo.clearVerificationCodeCalls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("The use case rejects an expired code independently of any handler", func(t *testing.T) {
|
||||
repo := newMockUserRepo()
|
||||
repo.users[3] = user.User{
|
||||
ID: 3,
|
||||
VerificationCode: "hashed:right-code",
|
||||
VerificationExpiration: time.Now().Add(-time.Hour),
|
||||
}
|
||||
uc := useCases.NewVerifyUserUseCase(repo, &stubHasher{})
|
||||
|
||||
_, err := uc.Verify(3, "right-code")
|
||||
|
||||
if !errors.Is(err, user.ErrVerificationCodeExpired) {
|
||||
t.Fatalf("expected ErrVerificationCodeExpired, got %v", err)
|
||||
}
|
||||
if repo.clearVerificationCodeCalls != 0 {
|
||||
t.Errorf("expected ClearVerificationCode to never be called, got %d calls", repo.clearVerificationCodeCalls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("The use case succeeds and clears the code independently of any handler", func(t *testing.T) {
|
||||
repo := newMockUserRepo()
|
||||
repo.users[3] = user.User{
|
||||
ID: 3,
|
||||
VerificationCode: "hashed:right-code",
|
||||
VerificationExpiration: time.Now().Add(time.Hour),
|
||||
}
|
||||
uc := useCases.NewVerifyUserUseCase(repo, &stubHasher{})
|
||||
|
||||
result, err := uc.Verify(3, "right-code")
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if !result.IsVerified {
|
||||
t.Errorf("expected IsVerified true")
|
||||
}
|
||||
if result.VerificationCode != "" {
|
||||
t.Errorf("expected VerificationCode empty, got %q", result.VerificationCode)
|
||||
}
|
||||
if repo.clearVerificationCodeCalls != 1 {
|
||||
t.Errorf("expected ClearVerificationCode to be called exactly once, got %d calls", repo.clearVerificationCodeCalls)
|
||||
}
|
||||
if len(repo.clearVerificationCodeUserIds) != 1 || repo.clearVerificationCodeUserIds[0] != 3 {
|
||||
t.Errorf("expected ClearVerificationCode to be called with userId 3, got %v", repo.clearVerificationCodeUserIds)
|
||||
}
|
||||
stored := repo.users[3]
|
||||
if !stored.IsVerified || stored.VerificationCode != "" {
|
||||
t.Errorf("expected stored record updated, got %+v", stored)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user