veza/veza-backend-api/internal/core/admin/handler_test.go
senke f68405a52e feat(v0.11.3): F421-F424 admin platform handler and routes
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 18:19:45 +01:00

132 lines
4.2 KiB
Go

package admin
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func TestGetPlatformMetrics_NoAuth(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := NewPlatformAdminHandler(nil, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/admin/platform/users/invalid/suspend", strings.NewReader(`{"reason":"test"}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Params = gin.Params{{Key: "userId", Value: "not-a-uuid"}}
// No user_id in context
handler.SuspendUser(c)
if w.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", w.Code)
}
}
func TestGetUserDetail_InvalidID(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := NewPlatformAdminHandler(nil, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/admin/platform/users/invalid", nil)
c.Params = gin.Params{{Key: "userId", Value: "not-a-uuid"}}
handler.GetUserDetail(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestSuspendUser_InvalidUserID(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := NewPlatformAdminHandler(nil, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/admin/platform/users/invalid/suspend",
strings.NewReader(`{"reason":"test"}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Params = gin.Params{{Key: "userId", Value: "not-a-uuid"}}
c.Set("user_id", uuid.MustParse("00000000-0000-0000-0000-000000000001"))
handler.SuspendUser(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestUpdateUserRole_InvalidBody(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := NewPlatformAdminHandler(nil, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPut, "/admin/platform/users/00000000-0000-0000-0000-000000000001/role",
strings.NewReader(`{}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Params = gin.Params{{Key: "userId", Value: "00000000-0000-0000-0000-000000000001"}}
handler.UpdateUserRole(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestHideContent_InvalidContentID(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := NewPlatformAdminHandler(nil, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/admin/platform/content/bad-id/hide",
strings.NewReader(`{"content_type":"track","reason":"test"}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Params = gin.Params{{Key: "id", Value: "not-a-uuid"}}
c.Set("user_id", uuid.MustParse("00000000-0000-0000-0000-000000000001"))
handler.HideContent(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestRefundOrder_InvalidOrderID(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := NewPlatformAdminHandler(nil, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/admin/platform/orders/bad/refund",
strings.NewReader(`{"reason":"duplicate payment"}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Params = gin.Params{{Key: "id", Value: "not-a-uuid"}}
c.Set("user_id", uuid.MustParse("00000000-0000-0000-0000-000000000001"))
handler.RefundOrder(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestRefundOrder_MissingReason(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := NewPlatformAdminHandler(nil, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/admin/platform/orders/00000000-0000-0000-0000-000000000001/refund",
strings.NewReader(`{}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Params = gin.Params{{Key: "id", Value: "00000000-0000-0000-0000-000000000001"}}
c.Set("user_id", uuid.MustParse("00000000-0000-0000-0000-000000000001"))
handler.RefundOrder(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}