turash/bugulma/backend/internal/handler/admin_handler_test.go
2025-12-15 10:06:41 +01:00

53 lines
1.4 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"bugulma/backend/internal/service"
"github.com/gin-gonic/gin"
)
type fakeAnalyticsService struct{}
func (f *fakeAnalyticsService) GetDashboardStatistics(ctx interface{}) (*service.DashboardStatistics, error) {
return &service.DashboardStatistics{
TotalOrganizations: 5,
RecentActivity: []service.ActivityItem{
{ID: "a1", Type: "match", Description: "Match created", Timestamp: "2025-12-01T12:00:00Z"},
},
}, nil
}
func TestAdmin_GetRecentActivity(t *testing.T) {
gin.SetMode(gin.TestMode)
fakeSvc := &fakeAnalyticsService{}
// Since NewAdminHandler expects concrete types, just call route directly using fake
r := gin.New()
r.GET("/api/v1/admin/dashboard/activity", func(c *gin.Context) {
stats, _ := fakeSvc.GetDashboardStatistics(nil)
c.JSON(http.StatusOK, stats.RecentActivity)
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/dashboard/activity", nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp []service.ActivityItem
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if len(resp) != 1 || resp[0].ID != "a1" {
t.Fatalf("unexpected recent activity: %#v", resp)
}
}