Add robust input validation system and fix test suite

- Add comprehensive validation package with email (RFC 5322), password strength, and IANA timezone validation
- Implement strict password requirements: min 8 chars, uppercase, lowercase, digit, special character
- Integrate validation into RegisterUserHandler with complete test coverage (59 validation tests + 25 handler tests)
- Fix pre-existing test failures:
  - Remove tests for non-existent HabitEntry.DeletedAt and Delete() methods
  - Replace deprecated HabitTypeQuantity with HabitTypeValue
  - Add missing FindByHabitIDAndDateRange mock implementation
- Remove hardcoded localhost:8080 from Swagger config for self-hosted flexibility
This commit is contained in:
2025-11-27 00:17:18 +01:00
parent 95088a8162
commit 7768037724
9 changed files with 673 additions and 39 deletions
+3 -11
View File
@@ -7,6 +7,7 @@ import (
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/domain/services"
"apocapoc-api/internal/shared/errors"
"apocapoc-api/internal/shared/validation"
)
type RegisterUserCommand struct {
@@ -28,11 +29,7 @@ func NewRegisterUserHandler(userRepo repositories.UserRepository, passwordHasher
}
func (h *RegisterUserHandler) Handle(ctx context.Context, cmd RegisterUserCommand) (string, error) {
if cmd.Email == "" || cmd.Password == "" {
return "", errors.ErrInvalidInput
}
if len(cmd.Password) < 8 {
if err := validation.ValidateRegistration(cmd.Email, cmd.Password, cmd.Timezone); err != nil {
return "", errors.ErrInvalidInput
}
@@ -46,12 +43,7 @@ func (h *RegisterUserHandler) Handle(ctx context.Context, cmd RegisterUserComman
return "", err
}
timezone := cmd.Timezone
if timezone == "" {
timezone = "UTC"
}
user := entities.NewUser(cmd.Email, hashedPassword, timezone)
user := entities.NewUser(cmd.Email, hashedPassword, cmd.Timezone)
if err := h.userRepo.Create(ctx, user); err != nil {
return "", err