Files
apocapoc-api/internal/application/commands/archive_habit_test.go
T
david 74cd2ec84d Complete CRUD operations for habits
Implement all missing endpoints for full habit management:
- GET /api/v1/habits - List all user habits
- GET /api/v1/habits/{id} - Get specific habit
- PUT /api/v1/habits/{id} - Update habit
- DELETE /api/v1/habits/{id} - Archive habit (soft delete)
- GET /api/v1/habits/{id}/entries - Get habit entry history
- DELETE /api/v1/habits/{id}/entries/{date} - Unmark habit (soft delete entry)

All endpoints include:
- TDD approach with comprehensive test coverage
- JWT authentication and ownership validation
- Proper error handling (404, 403, 400, 500)
- Clean architecture with separated commands/queries
2025-11-26 14:50:11 +01:00

106 lines
2.4 KiB
Go

package commands
import (
"context"
"testing"
"apocapoc-api/internal/domain/entities"
"apocapoc-api/internal/domain/value_objects"
"apocapoc-api/internal/shared/errors"
)
func TestArchiveHabitHandler_ArchivesSuccessfully(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
handler := NewArchiveHabitHandler(habitRepo)
cmd := ArchiveHabitCommand{
HabitID: "habit-1",
UserID: "user-123",
}
err := handler.Handle(context.Background(), cmd)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if habitRepo.updatedHabit.ArchivedAt == nil {
t.Error("Expected habit to be archived")
}
if habitRepo.updatedHabit.IsActive() {
t.Error("Expected habit to not be active")
}
}
func TestArchiveHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
habitRepo := &mockHabitRepoForUpdate{
errorOnFind: errors.ErrNotFound,
}
handler := NewArchiveHabitHandler(habitRepo)
cmd := ArchiveHabitCommand{
HabitID: "non-existent",
UserID: "user-123",
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrNotFound {
t.Errorf("Expected ErrNotFound, got %v", err)
}
}
func TestArchiveHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
handler := NewArchiveHabitHandler(habitRepo)
cmd := ArchiveHabitCommand{
HabitID: "habit-1",
UserID: "user-456", // Different user
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrUnauthorized {
t.Errorf("Expected ErrUnauthorized, got %v", err)
}
}
func TestArchiveHabitHandler_CanArchiveAlreadyArchivedHabit(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
habit.ID = "habit-1"
habit.Archive()
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
handler := NewArchiveHabitHandler(habitRepo)
cmd := ArchiveHabitCommand{
HabitID: "habit-1",
UserID: "user-123",
}
err := handler.Handle(context.Background(), cmd)
// Should be idempotent - no error
if err != nil {
t.Fatalf("Expected no error for already archived habit, got %v", err)
}
}