Files

78 lines
2.9 KiB
Go
Raw Permalink Normal View History

package httpapi
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/descrybe/descrybe-v2/apps/api/internal/shopify"
"github.com/descrybe/descrybe-v2/apps/api/internal/woocommerce"
"github.com/google/uuid"
)
func TestHandleUpdateShopifyScheduleRejectsInvalidJSON(t *testing.T) {
t.Parallel()
s := &Server{Shopify: &shopify.Service{}}
ctx := context.WithValue(context.Background(), ctxCompanyID, uuid.MustParse("11111111-1111-1111-1111-111111111111"))
ctx = context.WithValue(ctx, ctxRole, "admin")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/api/shopify/schedule", strings.NewReader(`{bad`))
req = req.WithContext(ctx)
s.handleUpdateShopifySchedule(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
}
func TestHandleUpdateShopifyScheduleRejectsInvalidInterval(t *testing.T) {
t.Parallel()
s := &Server{Shopify: &shopify.Service{}} // Pool nil — interval check runs before DB
ctx := context.WithValue(context.Background(), ctxCompanyID, uuid.MustParse("11111111-1111-1111-1111-111111111111"))
ctx = context.WithValue(ctx, ctxRole, "admin")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/api/shopify/schedule", strings.NewReader(`{"schedule_interval_hours":999,"schedule_paused":false}`))
req = req.WithContext(ctx)
s.handleUpdateShopifySchedule(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
}
func TestHandleSyncShopifyRejectsInvalidJSON(t *testing.T) {
t.Parallel()
s := &Server{} // Shopify unused — DecodeJSONOptional fails first
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/shopify/sync", strings.NewReader(`{"status":`))
s.handleSyncShopify(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
}
func TestHandleUpdateWooScheduleRejectsInvalidInterval(t *testing.T) {
t.Parallel()
s := &Server{Woo: &woocommerce.Service{}}
ctx := context.WithValue(context.Background(), ctxCompanyID, uuid.MustParse("11111111-1111-1111-1111-111111111111"))
ctx = context.WithValue(ctx, ctxRole, "admin")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/api/woocommerce/schedule", strings.NewReader(`{"schedule_interval_hours":-2,"schedule_paused":true}`))
req = req.WithContext(ctx)
s.handleUpdateWooSchedule(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
}
func TestHandleSyncWooRejectsInvalidJSON(t *testing.T) {
t.Parallel()
s := &Server{}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/woocommerce/sync", strings.NewReader(`[`))
s.handleSyncWoo(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
}
}