veza/veza-backend-api/internal/handlers/distribution_handler_test.go
senke 6063bfdeea feat(v0.12.2): F501-F510 distribution service, handler, and routes
- Distribution module: submit tracks to Spotify, Apple Music, Deezer
- Subscription eligibility check (Creator/Premium only)
- Distribution status tracking with platform-specific statuses
- Status history audit trail
- External streaming royalties import and aggregation
- Distributor provider interface for DistroKid/TuneCore integration
- Handler and service unit tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 19:54:26 +01:00

143 lines
3.9 KiB
Go

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)
}
}