Add NoOp email service, /docs shortcut and fix streak timezone bug
Replace nil email service pattern with NoOpEmailService (Null Object) to eliminate nil pointer panics across all handlers. Add /docs route as a shortcut to Swagger UI. Fix streak calculation returning 0 when server timezone differs from UTC — CreatedAt was not converted to UTC before date extraction.
This commit is contained in:
+1
-1
@@ -95,7 +95,7 @@ func main() {
|
|||||||
jwtService := auth.NewJWTService(cfg.JWTSecret, jwtExpiryHours)
|
jwtService := auth.NewJWTService(cfg.JWTSecret, jwtExpiryHours)
|
||||||
passwordHasher := crypto.NewBcryptHasher()
|
passwordHasher := crypto.NewBcryptHasher()
|
||||||
|
|
||||||
var emailService services.EmailService
|
var emailService services.EmailService = &services.NoOpEmailService{}
|
||||||
if cfg.SMTPHost != "" {
|
if cfg.SMTPHost != "" {
|
||||||
smtpPort, err := strconv.Atoi(cfg.SMTPPort)
|
smtpPort, err := strconv.Atoi(cfg.SMTPPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func (h *RegisterUserHandler) Handle(ctx context.Context, cmd RegisterUserComman
|
|||||||
user := entities.NewUser(cmd.Email, hashedPassword)
|
user := entities.NewUser(cmd.Email, hashedPassword)
|
||||||
|
|
||||||
emailVerificationRequired := false
|
emailVerificationRequired := false
|
||||||
if h.emailService != nil {
|
if h.emailService.IsEnabled() {
|
||||||
token, err := h.generateVerificationToken()
|
token, err := h.generateVerificationToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate verification token: %w", err)
|
return nil, fmt.Errorf("failed to generate verification token: %w", err)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"apocapoc-api/internal/domain/repositories"
|
"apocapoc-api/internal/domain/repositories"
|
||||||
|
"apocapoc-api/internal/domain/services"
|
||||||
"apocapoc-api/internal/shared/pagination"
|
"apocapoc-api/internal/shared/pagination"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -71,7 +72,7 @@ func TestRegisterUserHandler_Success(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
hasher := &mockPasswordHasher{}
|
hasher := &mockPasswordHasher{}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "open", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "open", false)
|
||||||
|
|
||||||
cmd := RegisterUserCommand{
|
cmd := RegisterUserCommand{
|
||||||
Email: "test@example.com",
|
Email: "test@example.com",
|
||||||
@@ -88,7 +89,7 @@ func TestRegisterUserHandler_Success(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if result.EmailVerificationRequired {
|
if result.EmailVerificationRequired {
|
||||||
t.Error("expected email verification to not be required when emailService is nil")
|
t.Error("expected email verification to not be required when email is disabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
if createdUser == nil {
|
if createdUser == nil {
|
||||||
@@ -103,7 +104,7 @@ func TestRegisterUserHandler_Success(t *testing.T) {
|
|||||||
func TestRegisterUserHandler_InvalidEmail(t *testing.T) {
|
func TestRegisterUserHandler_InvalidEmail(t *testing.T) {
|
||||||
repo := &mockUserRepo{}
|
repo := &mockUserRepo{}
|
||||||
hasher := &mockPasswordHasher{}
|
hasher := &mockPasswordHasher{}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "open", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "open", false)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -135,7 +136,7 @@ func TestRegisterUserHandler_InvalidEmail(t *testing.T) {
|
|||||||
func TestRegisterUserHandler_InvalidPassword(t *testing.T) {
|
func TestRegisterUserHandler_InvalidPassword(t *testing.T) {
|
||||||
repo := &mockUserRepo{}
|
repo := &mockUserRepo{}
|
||||||
hasher := &mockPasswordHasher{}
|
hasher := &mockPasswordHasher{}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "open", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "open", false)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -174,7 +175,7 @@ func TestRegisterUserHandler_EmailAlreadyExists(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
hasher := &mockPasswordHasher{}
|
hasher := &mockPasswordHasher{}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "open", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "open", false)
|
||||||
|
|
||||||
cmd := RegisterUserCommand{
|
cmd := RegisterUserCommand{
|
||||||
Email: "test@example.com",
|
Email: "test@example.com",
|
||||||
@@ -195,7 +196,7 @@ func TestRegisterUserHandler_PasswordHashingError(t *testing.T) {
|
|||||||
return "", expectedErr
|
return "", expectedErr
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "open", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "open", false)
|
||||||
|
|
||||||
cmd := RegisterUserCommand{
|
cmd := RegisterUserCommand{
|
||||||
Email: "test@example.com",
|
Email: "test@example.com",
|
||||||
@@ -216,7 +217,7 @@ func TestRegisterUserHandler_RepositoryError(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
hasher := &mockPasswordHasher{}
|
hasher := &mockPasswordHasher{}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "open", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "open", false)
|
||||||
|
|
||||||
cmd := RegisterUserCommand{
|
cmd := RegisterUserCommand{
|
||||||
Email: "test@example.com",
|
Email: "test@example.com",
|
||||||
@@ -232,7 +233,7 @@ func TestRegisterUserHandler_RepositoryError(t *testing.T) {
|
|||||||
func TestRegisterUserHandler_EdgeCases(t *testing.T) {
|
func TestRegisterUserHandler_EdgeCases(t *testing.T) {
|
||||||
repo := &mockUserRepo{}
|
repo := &mockUserRepo{}
|
||||||
hasher := &mockPasswordHasher{}
|
hasher := &mockPasswordHasher{}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "open", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "open", false)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -285,7 +286,7 @@ func TestRegisterUserHandler_EdgeCases(t *testing.T) {
|
|||||||
func TestRegisterUserHandler_ClosedRegistration(t *testing.T) {
|
func TestRegisterUserHandler_ClosedRegistration(t *testing.T) {
|
||||||
repo := &mockUserRepo{}
|
repo := &mockUserRepo{}
|
||||||
hasher := &mockPasswordHasher{}
|
hasher := &mockPasswordHasher{}
|
||||||
handler := NewRegisterUserHandler(repo, hasher, nil, "", "closed", false)
|
handler := NewRegisterUserHandler(repo, hasher, &services.NoOpEmailService{}, "", "closed", false)
|
||||||
|
|
||||||
cmd := RegisterUserCommand{
|
cmd := RegisterUserCommand{
|
||||||
Email: "test@example.com",
|
Email: "test@example.com",
|
||||||
|
|||||||
@@ -89,6 +89,10 @@ func (m *mockRequestResetEmailService) HealthCheck() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockRequestResetEmailService) IsEnabled() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func TestRequestPasswordResetHandler_Success(t *testing.T) {
|
func TestRequestPasswordResetHandler_Success(t *testing.T) {
|
||||||
user := entities.NewUser("test@example.com", "hash")
|
user := entities.NewUser("test@example.com", "hash")
|
||||||
user.ID = "user-123"
|
user.ID = "user-123"
|
||||||
|
|||||||
@@ -69,6 +69,10 @@ func (m *mockEmailService) HealthCheck() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockEmailService) IsEnabled() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func TestVerifyEmailHandler_Success(t *testing.T) {
|
func TestVerifyEmailHandler_Success(t *testing.T) {
|
||||||
token := "valid-token"
|
token := "valid-token"
|
||||||
expiry := time.Now().Add(24 * time.Hour)
|
expiry := time.Now().Add(24 * time.Hour)
|
||||||
|
|||||||
@@ -10,4 +10,11 @@ type EmailMessage struct {
|
|||||||
type EmailService interface {
|
type EmailService interface {
|
||||||
Send(message EmailMessage) error
|
Send(message EmailMessage) error
|
||||||
HealthCheck() error
|
HealthCheck() error
|
||||||
|
IsEnabled() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NoOpEmailService struct{}
|
||||||
|
|
||||||
|
func (n *NoOpEmailService) Send(_ EmailMessage) error { return nil }
|
||||||
|
func (n *NoOpEmailService) HealthCheck() error { return nil }
|
||||||
|
func (n *NoOpEmailService) IsEnabled() bool { return false }
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ func buildEntryMap(entries []*entities.HabitEntry) map[string]*entities.HabitEnt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func allScheduledDates(habit *entities.Habit, now time.Time) []time.Time {
|
func allScheduledDates(habit *entities.Habit, now time.Time) []time.Time {
|
||||||
start := time.Date(habit.CreatedAt.Year(), habit.CreatedAt.Month(), habit.CreatedAt.Day(), 0, 0, 0, 0, time.UTC)
|
createdUTC := habit.CreatedAt.UTC()
|
||||||
|
start := time.Date(createdUTC.Year(), createdUTC.Month(), createdUTC.Day(), 0, 0, 0, 0, time.UTC)
|
||||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
|
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
|
||||||
freq := string(habit.Frequency)
|
freq := string(habit.Frequency)
|
||||||
|
|
||||||
|
|||||||
@@ -107,6 +107,10 @@ func (s *SMTPService) GetConfig() SMTPConfig {
|
|||||||
return s.config
|
return s.config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SMTPService) IsEnabled() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SMTPService) HealthCheck() error {
|
func (s *SMTPService) HealthCheck() error {
|
||||||
dialer := mail.NewDialer(s.config.Host, s.config.Port, s.config.Username, s.config.Password)
|
dialer := mail.NewDialer(s.config.Host, s.config.Port, s.config.Username, s.config.Password)
|
||||||
dialer.TLSConfig = &tls.Config{
|
dialer.TLSConfig = &tls.Config{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"apocapoc-api/internal/application/commands"
|
"apocapoc-api/internal/application/commands"
|
||||||
"apocapoc-api/internal/application/queries"
|
"apocapoc-api/internal/application/queries"
|
||||||
|
"apocapoc-api/internal/domain/services"
|
||||||
"apocapoc-api/internal/i18n"
|
"apocapoc-api/internal/i18n"
|
||||||
"apocapoc-api/internal/infrastructure/auth"
|
"apocapoc-api/internal/infrastructure/auth"
|
||||||
"apocapoc-api/internal/infrastructure/crypto"
|
"apocapoc-api/internal/infrastructure/crypto"
|
||||||
@@ -43,14 +44,15 @@ func setupTestServer(t *testing.T) *TestServer {
|
|||||||
refreshTokenRepo := sqlite.NewRefreshTokenRepository(db)
|
refreshTokenRepo := sqlite.NewRefreshTokenRepository(db)
|
||||||
passwordResetTokenRepo := sqlite.NewPasswordResetTokenRepository(db)
|
passwordResetTokenRepo := sqlite.NewPasswordResetTokenRepository(db)
|
||||||
|
|
||||||
registerHandler := commands.NewRegisterUserHandler(userRepo, passwordHasher, nil, "", "open", false)
|
noOpEmail := &services.NoOpEmailService{}
|
||||||
|
registerHandler := commands.NewRegisterUserHandler(userRepo, passwordHasher, noOpEmail, "", "open", false)
|
||||||
loginHandler := queries.NewLoginUserHandler(userRepo, passwordHasher)
|
loginHandler := queries.NewLoginUserHandler(userRepo, passwordHasher)
|
||||||
refreshTokenHandler := queries.NewRefreshTokenHandler(refreshTokenRepo, userRepo)
|
refreshTokenHandler := queries.NewRefreshTokenHandler(refreshTokenRepo, userRepo)
|
||||||
revokeTokenHandler := commands.NewRevokeTokenHandler(refreshTokenRepo)
|
revokeTokenHandler := commands.NewRevokeTokenHandler(refreshTokenRepo)
|
||||||
revokeAllTokensHandler := commands.NewRevokeAllTokensHandler(refreshTokenRepo)
|
revokeAllTokensHandler := commands.NewRevokeAllTokensHandler(refreshTokenRepo)
|
||||||
verifyEmailHandler := commands.NewVerifyEmailHandler(userRepo, nil, false)
|
verifyEmailHandler := commands.NewVerifyEmailHandler(userRepo, noOpEmail, false)
|
||||||
resendVerificationEmailHandler := commands.NewResendVerificationEmailHandler(userRepo, nil, "")
|
resendVerificationEmailHandler := commands.NewResendVerificationEmailHandler(userRepo, noOpEmail, "")
|
||||||
requestPasswordResetHandler := commands.NewRequestPasswordResetHandler(userRepo, passwordResetTokenRepo, nil, "")
|
requestPasswordResetHandler := commands.NewRequestPasswordResetHandler(userRepo, passwordResetTokenRepo, noOpEmail, "")
|
||||||
resetPasswordHandler := commands.NewResetPasswordHandler(userRepo, passwordResetTokenRepo, passwordHasher)
|
resetPasswordHandler := commands.NewResetPasswordHandler(userRepo, passwordResetTokenRepo, passwordHasher)
|
||||||
createHandler := commands.NewCreateHabitHandler(habitRepo)
|
createHandler := commands.NewCreateHabitHandler(habitRepo)
|
||||||
getTodaysHandler := queries.NewGetTodaysHabitsHandler(habitRepo, entryRepo)
|
getTodaysHandler := queries.NewGetTodaysHabitsHandler(habitRepo, entryRepo)
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ func NewRouter(appURL string, habitHandlers *HabitHandlers, authHandlers *AuthHa
|
|||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
r.Get("/docs", httpSwagger.Handler(
|
||||||
|
httpSwagger.URL("/api/v1/docs/doc.json"),
|
||||||
|
))
|
||||||
r.Get("/api/v1/docs", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/api/v1/docs", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, "/api/v1/docs/index.html", http.StatusMovedPermanently)
|
http.Redirect(w, r, "/api/v1/docs/index.html", http.StatusMovedPermanently)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user