Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ed50d2427e | |||
| c20720f120 |
@@ -139,7 +139,9 @@ API runs on `http://localhost:8080`
|
|||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
|
||||||
Access the interactive Swagger UI at `http://localhost:8080/api/v1/docs`
|
**Live:** [apocapoc.app/api/v1/docs](https://apocapoc.app/api/v1/docs)
|
||||||
|
|
||||||
|
**Local:** `http://localhost:8080/api/v1/docs`
|
||||||
|
|
||||||
Includes endpoint reference, schemas, authentication examples, and live testing.
|
Includes endpoint reference, schemas, authentication examples, and live testing.
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"apocapoc-api/internal/domain/entities"
|
"apocapoc-api/internal/domain/entities"
|
||||||
"apocapoc-api/internal/domain/repositories"
|
"apocapoc-api/internal/domain/repositories"
|
||||||
@@ -32,19 +33,19 @@ func NewCreateHabitHandler(habitRepo repositories.HabitRepository) *CreateHabitH
|
|||||||
|
|
||||||
func (h *CreateHabitHandler) Handle(ctx context.Context, cmd CreateHabitCommand) (string, error) {
|
func (h *CreateHabitHandler) Handle(ctx context.Context, cmd CreateHabitCommand) (string, error) {
|
||||||
if !cmd.Type.IsValid() {
|
if !cmd.Type.IsValid() {
|
||||||
return "", errors.ErrInvalidInput
|
return "", fmt.Errorf("%w: type: type_invalid", errors.ErrInvalidInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cmd.Frequency.IsValid() {
|
if !cmd.Frequency.IsValid() {
|
||||||
return "", errors.ErrInvalidInput
|
return "", fmt.Errorf("%w: frequency: frequency_invalid", errors.ErrInvalidInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Frequency == value_objects.FrequencyWeekly && len(cmd.SpecificDays) == 0 {
|
if cmd.Frequency == value_objects.FrequencyWeekly && len(cmd.SpecificDays) == 0 {
|
||||||
return "", errors.ErrInvalidInput
|
return "", fmt.Errorf("%w: specific_days: specific_days_required", errors.ErrInvalidInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Frequency == value_objects.FrequencyMonthly && len(cmd.SpecificDates) == 0 {
|
if cmd.Frequency == value_objects.FrequencyMonthly && len(cmd.SpecificDates) == 0 {
|
||||||
return "", errors.ErrInvalidInput
|
return "", fmt.Errorf("%w: specific_dates: specific_dates_required", errors.ErrInvalidInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
habit := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver, cmd.IsNegative)
|
habit := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver, cmd.IsNegative)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"apocapoc-api/internal/domain/repositories"
|
"apocapoc-api/internal/domain/repositories"
|
||||||
"apocapoc-api/internal/shared/pagination"
|
"apocapoc-api/internal/shared/pagination"
|
||||||
"context"
|
"context"
|
||||||
|
stderrors "errors"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -110,8 +112,31 @@ func TestCreateHabitHandler_InvalidType(t *testing.T) {
|
|||||||
|
|
||||||
_, err := handler.Handle(context.Background(), cmd)
|
_, err := handler.Handle(context.Background(), cmd)
|
||||||
|
|
||||||
if err != errors.ErrInvalidInput {
|
if !stderrors.Is(err, errors.ErrInvalidInput) {
|
||||||
t.Errorf("Expected ErrInvalidInput, got %v", err)
|
t.Fatalf("Expected ErrInvalidInput wrapper, got %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "type: type_invalid") {
|
||||||
|
t.Errorf("Expected 'type: type_invalid' in error, got %q", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateHabitHandler_EmptyType(t *testing.T) {
|
||||||
|
mock := &mockHabitRepo{}
|
||||||
|
handler := NewCreateHabitHandler(mock)
|
||||||
|
|
||||||
|
cmd := CreateHabitCommand{
|
||||||
|
UserID: "user-123",
|
||||||
|
Name: "Exercise",
|
||||||
|
Frequency: "DAILY",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := handler.Handle(context.Background(), cmd)
|
||||||
|
|
||||||
|
if !stderrors.Is(err, errors.ErrInvalidInput) {
|
||||||
|
t.Fatalf("Expected ErrInvalidInput wrapper, got %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "type: type_invalid") {
|
||||||
|
t.Errorf("Expected 'type: type_invalid' in error, got %q", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,8 +153,11 @@ func TestCreateHabitHandler_InvalidFrequency(t *testing.T) {
|
|||||||
|
|
||||||
_, err := handler.Handle(context.Background(), cmd)
|
_, err := handler.Handle(context.Background(), cmd)
|
||||||
|
|
||||||
if err != errors.ErrInvalidInput {
|
if !stderrors.Is(err, errors.ErrInvalidInput) {
|
||||||
t.Errorf("Expected ErrInvalidInput, got %v", err)
|
t.Fatalf("Expected ErrInvalidInput wrapper, got %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "frequency: frequency_invalid") {
|
||||||
|
t.Errorf("Expected 'frequency: frequency_invalid' in error, got %q", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,8 +175,11 @@ func TestCreateHabitHandler_WeeklyWithoutSpecificDays(t *testing.T) {
|
|||||||
|
|
||||||
_, err := handler.Handle(context.Background(), cmd)
|
_, err := handler.Handle(context.Background(), cmd)
|
||||||
|
|
||||||
if err != errors.ErrInvalidInput {
|
if !stderrors.Is(err, errors.ErrInvalidInput) {
|
||||||
t.Errorf("Expected ErrInvalidInput, got %v", err)
|
t.Fatalf("Expected ErrInvalidInput wrapper, got %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "specific_days: specific_days_required") {
|
||||||
|
t.Errorf("Expected 'specific_days: specific_days_required' in error, got %q", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,8 +197,11 @@ func TestCreateHabitHandler_MonthlyWithoutSpecificDates(t *testing.T) {
|
|||||||
|
|
||||||
_, err := handler.Handle(context.Background(), cmd)
|
_, err := handler.Handle(context.Background(), cmd)
|
||||||
|
|
||||||
if err != errors.ErrInvalidInput {
|
if !stderrors.Is(err, errors.ErrInvalidInput) {
|
||||||
t.Errorf("Expected ErrInvalidInput, got %v", err)
|
t.Fatalf("Expected ErrInvalidInput wrapper, got %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "specific_dates: specific_dates_required") {
|
||||||
|
t.Errorf("Expected 'specific_dates: specific_dates_required' in error, got %q", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
stderrors "errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -97,8 +98,8 @@ func (h *HabitHandlers) CreateHabit(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
habitID, err := h.createHandler.Handle(r.Context(), cmd)
|
habitID, err := h.createHandler.Handle(r.Context(), cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errors.ErrInvalidInput {
|
if stderrors.Is(err, errors.ErrInvalidInput) {
|
||||||
respondError(w, http.StatusBadRequest, err.Error())
|
respondValidationErrorI18n(w, r, h.translator, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
respondErrorI18n(w, r, h.translator, http.StatusInternalServerError, "failed_create_habit")
|
respondErrorI18n(w, r, h.translator, http.StatusInternalServerError, "failed_create_habit")
|
||||||
|
|||||||
@@ -123,6 +123,47 @@ func TestHabitCRUDFlow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("Create habit with missing type returns field-level error", func(t *testing.T) {
|
||||||
|
token := registerAndLogin(t, *ts.Router, "validationuser@example.com", "Password123!")
|
||||||
|
|
||||||
|
reqBody := map[string]any{"name": "No Type"}
|
||||||
|
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/habits", reqBody, token)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusBadRequest {
|
||||||
|
t.Fatalf("Expected 400, got %d. Body: %s", rr.Code, rr.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp ValidationErrorResponse
|
||||||
|
decodeResponse(t, rr, &resp)
|
||||||
|
if resp.Field != "type" {
|
||||||
|
t.Errorf("Expected field 'type', got %q. Body: %s", resp.Field, rr.Body.String())
|
||||||
|
}
|
||||||
|
if resp.Error == "" {
|
||||||
|
t.Errorf("Expected non-empty translated error message. Body: %s", rr.Body.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Create weekly habit without specific_days returns field-level error", func(t *testing.T) {
|
||||||
|
token := registerAndLogin(t, *ts.Router, "weeklyuser@example.com", "Password123!")
|
||||||
|
|
||||||
|
reqBody := CreateHabitRequest{
|
||||||
|
Name: "Cut nails",
|
||||||
|
Type: "BOOLEAN",
|
||||||
|
Frequency: "WEEKLY",
|
||||||
|
}
|
||||||
|
rr := makeRequest(t, *ts.Router, "POST", "/api/v1/habits", reqBody, token)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusBadRequest {
|
||||||
|
t.Fatalf("Expected 400, got %d. Body: %s", rr.Code, rr.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp ValidationErrorResponse
|
||||||
|
decodeResponse(t, rr, &resp)
|
||||||
|
if resp.Field != "specific_days" {
|
||||||
|
t.Errorf("Expected field 'specific_days', got %q. Body: %s", resp.Field, rr.Body.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("Access other user's habit", func(t *testing.T) {
|
t.Run("Access other user's habit", func(t *testing.T) {
|
||||||
otherToken := registerAndLogin(t, *ts.Router, "otheruser@example.com", "Password123!")
|
otherToken := registerAndLogin(t, *ts.Router, "otheruser@example.com", "Password123!")
|
||||||
|
|
||||||
|
|||||||
@@ -30,9 +30,6 @@ 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