package test import ( "RenewCMS/internal/domain/user" "RenewCMS/internal/infrastructure/useCases" "bytes" "log" "os" "strings" "testing" "time" ) func captureRequestPasswordResetLog(t *testing.T) *bytes.Buffer { t.Helper() var buf bytes.Buffer log.SetOutput(&buf) t.Cleanup(func() { log.SetOutput(os.Stderr) }) return &buf } func newRequestPasswordResetTestUserRepo() *mockUserRepo { repo := newMockUserRepo() repo.users[3] = user.User{ID: 3, Email: "alice@example.com"} repo.nextID = 3 return repo } func TestRequestPasswordResetUseCase(t *testing.T) { t.Run("The use case itself hashes the code, stores it with a one-hour expiration, and sends the email", func(t *testing.T) { repo := newRequestPasswordResetTestUserRepo() mailRepo := newMockMailRepo() uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{}, mailRepo, "https://cms.example.com", discardRecorder) err := uc.RequestPasswordReset(testCtx, "alice@example.com") if err != nil { t.Fatalf("expected nil error, got %v", err) } if repo.updatePasswordResetCodeCalls != 1 { t.Fatalf("expected UpdatePasswordResetCode to be called once, got %d", repo.updatePasswordResetCodeCalls) } if repo.lastUpdatePasswordResetCodeUserId != 3 { t.Errorf("expected userId 3, got %d", repo.lastUpdatePasswordResetCodeUserId) } if got, want := repo.lastUpdatePasswordResetCodeExpiration, time.Now().Add(time.Hour); got.Sub(want).Abs() > 5*time.Second { t.Errorf("expected expiration near %v, got %v", want, got) } if len(mailRepo.SentEmails) != 1 { t.Fatalf("expected exactly one email, got %d", len(mailRepo.SentEmails)) } sent := mailRepo.SentEmails[0] if sent.Address != "alice@example.com" { t.Errorf("expected email to alice@example.com, got %s", sent.Address) } if sent.Template != "password-reset" { t.Errorf("expected template password-reset, got %s", sent.Template) } data, ok := sent.Data.(useCases.MailPasswordResetData) if !ok { t.Fatalf("expected data of type MailPasswordResetData, got %T", sent.Data) } if data.Host != "https://cms.example.com" { t.Errorf("expected Host https://cms.example.com, got %s", data.Host) } if (&stubHasher{}).Compare(repo.lastUpdatePasswordResetCodeCode, data.VerificationCode) != nil { t.Errorf("expected stored hash to match the raw code sent in the email") } }) t.Run("The use case swallows an unknown email and returns nil without touching the repository or mailer", func(t *testing.T) { repo := newMockUserRepo() mailRepo := newMockMailRepo() uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{}, mailRepo, "https://cms.example.com", discardRecorder) err := uc.RequestPasswordReset(testCtx, "nobody@example.com") if err != nil { t.Fatalf("expected nil error, got %v", err) } if repo.updatePasswordResetCodeCalls != 0 { t.Errorf("expected UpdatePasswordResetCode to never be called, got %d calls", repo.updatePasswordResetCodeCalls) } if len(mailRepo.SentEmails) != 0 { t.Errorf("expected no email to be sent, got %d", len(mailRepo.SentEmails)) } }) t.Run("The use case swallows a mail repository failure and still returns nil", func(t *testing.T) { repo := newRequestPasswordResetTestUserRepo() mailRepo := newMockMailRepo() mailRepo.fail.failNext("Send") uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{}, mailRepo, "https://cms.example.com", discardRecorder) logBuf := captureRequestPasswordResetLog(t) err := uc.RequestPasswordReset(testCtx, "alice@example.com") if err != nil { t.Fatalf("expected nil error, got %v", err) } if repo.updatePasswordResetCodeCalls != 1 { t.Errorf("expected UpdatePasswordResetCode to be called once, got %d", repo.updatePasswordResetCodeCalls) } if logBuf.Len() == 0 { t.Fatalf("expected a failure to be logged") } if !strings.Contains(strings.ToLower(logBuf.String()), "send") && !strings.Contains(strings.ToLower(logBuf.String()), "mail") { t.Errorf("expected the log to identify a mail-send failure, got %q", logBuf.String()) } }) t.Run("The use case logs and swallows a hashing failure", func(t *testing.T) { repo := newRequestPasswordResetTestUserRepo() mailRepo := newMockMailRepo() uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{hashErr: errStubHashFailure}, mailRepo, "https://cms.example.com", discardRecorder) logBuf := captureRequestPasswordResetLog(t) err := uc.RequestPasswordReset(testCtx, "alice@example.com") if err != nil { t.Fatalf("expected nil error, got %v", err) } if repo.updatePasswordResetCodeCalls != 0 { t.Errorf("expected UpdatePasswordResetCode to never be called, got %d calls", repo.updatePasswordResetCodeCalls) } if len(mailRepo.SentEmails) != 0 { t.Errorf("expected no email to be sent, got %d", len(mailRepo.SentEmails)) } if logBuf.Len() == 0 { t.Fatalf("expected a failure to be logged") } if !strings.Contains(strings.ToLower(logBuf.String()), "hash") { t.Errorf("expected the log to identify a hashing failure, got %q", logBuf.String()) } }) t.Run("The use case logs and swallows a repository failure while storing the reset code", func(t *testing.T) { repo := newRequestPasswordResetTestUserRepo() repo.fail.failNext("UpdatePasswordResetCode") mailRepo := newMockMailRepo() uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{}, mailRepo, "https://cms.example.com", discardRecorder) logBuf := captureRequestPasswordResetLog(t) err := uc.RequestPasswordReset(testCtx, "alice@example.com") if err != nil { t.Fatalf("expected nil error, got %v", err) } if len(mailRepo.SentEmails) != 0 { t.Errorf("expected no email to be sent, got %d", len(mailRepo.SentEmails)) } if logBuf.Len() == 0 { t.Fatalf("expected a failure to be logged") } if !strings.Contains(strings.ToLower(logBuf.String()), "updatepasswordresetcode") && !strings.Contains(strings.ToLower(logBuf.String()), "store") { t.Errorf("expected the log to identify a storage failure, got %q", logBuf.String()) } }) t.Run("An unverified user can request a password reset exactly like a verified one", func(t *testing.T) { repo := newMockUserRepo() repo.users[1] = user.User{ID: 1, Email: "alice@example.com", IsVerified: false} repo.users[2] = user.User{ID: 2, Email: "bob@example.com", IsVerified: false} repo.nextID = 2 mailRepo := newMockMailRepo() uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{}, mailRepo, "https://cms.example.com", discardRecorder) err := uc.RequestPasswordReset(testCtx, "alice@example.com") if err != nil { t.Fatalf("expected nil error, got %v", err) } if len(mailRepo.SentEmails) != 1 { t.Fatalf("expected exactly one email, got %d", len(mailRepo.SentEmails)) } }) }