Files
RenewCMS/test/account_verification_test.go
T
2026-10-06 22:25:23 +02:00

82 lines
2.8 KiB
Go

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{}, discardRecorder)
_, err := uc.Verify(testCtx, 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{}, discardRecorder)
_, err := uc.Verify(testCtx, 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{}, discardRecorder)
result, err := uc.Verify(testCtx, 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)
}
})
}