package test import ( "RenewCMS/internal/domain/user" "RenewCMS/internal/infrastructure/useCases" "testing" "time" ) 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") err := uc.RequestPasswordReset("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 != "passwordReset" { t.Errorf("expected template passwordReset, 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") err := uc.RequestPasswordReset("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") err := uc.RequestPasswordReset("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) } }) t.Run("The use case swallows a hash failure and returns nil without storing or sending", func(t *testing.T) { repo := newRequestPasswordResetTestUserRepo() mailRepo := newMockMailRepo() uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{hashErr: errStubHashFailure}, mailRepo, "https://cms.example.com") err := uc.RequestPasswordReset("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)) } }) t.Run("The use case swallows a store failure and returns nil without sending", func(t *testing.T) { repo := newRequestPasswordResetTestUserRepo() repo.fail.failNext("UpdatePasswordResetCode") mailRepo := newMockMailRepo() uc := useCases.NewRequestPasswordResetUseCase(repo, &stubHasher{}, mailRepo, "https://cms.example.com") err := uc.RequestPasswordReset("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)) } }) 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") err := uc.RequestPasswordReset("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)) } }) }