package handlers import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "github.com/gin-gonic/gin" "github.com/google/uuid" "go.uber.org/zap" "veza-backend-api/internal/core/distribution" ) func TestNewDistributionHandler(t *testing.T) { logger := zap.NewNop() svc := distribution.NewService(nil, logger, nil) handler := NewDistributionHandler(svc, logger) if handler == nil { t.Fatal("expected non-nil handler") } if handler.service == nil { t.Error("expected non-nil service") } } func TestDistributionHandler_Submit_NoAuth(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() svc := distribution.NewService(nil, logger, nil) handler := NewDistributionHandler(svc, logger) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodPost, "/distributions/submit", strings.NewReader(`{}`)) c.Request.Header.Set("Content-Type", "application/json") handler.Submit(c) if w.Code != http.StatusUnauthorized { t.Errorf("expected 401, got %d", w.Code) } } func TestDistributionHandler_Submit_InvalidBody(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() svc := distribution.NewService(nil, logger, nil) handler := NewDistributionHandler(svc, logger) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Set("user_id", uuid.New()) c.Request = httptest.NewRequest(http.MethodPost, "/distributions/submit", strings.NewReader(`{}`)) c.Request.Header.Set("Content-Type", "application/json") handler.Submit(c) var resp map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { t.Fatalf("failed to decode response: %v", err) } if resp["success"] != false { t.Error("expected success=false for invalid request") } } func TestDistributionHandler_GetDistribution_InvalidID(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() svc := distribution.NewService(nil, logger, nil) handler := NewDistributionHandler(svc, logger) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Set("user_id", uuid.New()) c.Params = gin.Params{{Key: "id", Value: "not-a-uuid"}} c.Request = httptest.NewRequest(http.MethodGet, "/distributions/not-a-uuid", nil) handler.GetDistribution(c) var resp map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { t.Fatalf("failed to decode response: %v", err) } if resp["success"] != false { t.Error("expected success=false for invalid ID") } } func TestDistributionHandler_ListDistributions_NoAuth(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() svc := distribution.NewService(nil, logger, nil) handler := NewDistributionHandler(svc, logger) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/distributions", nil) handler.ListDistributions(c) if w.Code != http.StatusUnauthorized { t.Errorf("expected 401, got %d", w.Code) } } func TestDistributionHandler_RemoveDistribution_NoAuth(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() svc := distribution.NewService(nil, logger, nil) handler := NewDistributionHandler(svc, logger) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodPost, "/distributions/123/remove", nil) handler.RemoveDistribution(c) if w.Code != http.StatusUnauthorized { t.Errorf("expected 401, got %d", w.Code) } } func TestDistributionHandler_GetExternalRoyalties_NoAuth(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() svc := distribution.NewService(nil, logger, nil) handler := NewDistributionHandler(svc, logger) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/creators/me/external-royalties", nil) handler.GetExternalRoyalties(c) if w.Code != http.StatusUnauthorized { t.Errorf("expected 401, got %d", w.Code) } }