package handlers import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "time" "veza-backend-api/internal/models" "veza-backend-api/internal/services" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" ) // mockDashboardAuditService returns empty stats and activity for tests type mockDashboardAuditService struct { statsErr error activityErr error } func (m *mockDashboardAuditService) GetStats(ctx context.Context, startDate, endDate time.Time) ([]*services.AuditStats, error) { if m.statsErr != nil { return nil, m.statsErr } return []*services.AuditStats{}, nil } func (m *mockDashboardAuditService) GetUserActivity(ctx context.Context, userID uuid.UUID, limit int) ([]*services.AuditLog, error) { if m.activityErr != nil { return nil, m.activityErr } return []*services.AuditLog{}, nil } // mockDashboardTrackService returns empty tracks for tests type mockDashboardTrackService struct { tracksErr error } func (m *mockDashboardTrackService) ListTracks(ctx context.Context, params TrackListParamsForDashboard) ([]*models.Track, int64, error) { if m.tracksErr != nil { return nil, 0, m.tracksErr } return []*models.Track{}, 0, nil } func TestDashboardHandler_GetDashboard_HappyPath(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() handler := NewDashboardHandlerWithInterface( &mockDashboardAuditService{}, &mockDashboardTrackService{}, logger, ) router := gin.New() userID := uuid.New() router.Use(func(c *gin.Context) { c.Set("user_id", userID) c.Next() }) router.GET("/dashboard", handler.GetDashboard()) req := httptest.NewRequest("GET", "/dashboard", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) var resp APIResponse err := json.Unmarshal(w.Body.Bytes(), &resp) require.NoError(t, err) assert.True(t, resp.Success) assert.NotNil(t, resp.Data) } func TestDashboardHandler_GetDashboard_Unauthorized(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() handler := NewDashboardHandlerWithInterface( &mockDashboardAuditService{}, &mockDashboardTrackService{}, logger, ) router := gin.New() // No auth middleware - no user_id in context router.GET("/dashboard", handler.GetDashboard()) req := httptest.NewRequest("GET", "/dashboard", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusUnauthorized, w.Code) } func TestDashboardHandler_GetDashboard_WithQueryParams(t *testing.T) { gin.SetMode(gin.TestMode) logger := zap.NewNop() handler := NewDashboardHandlerWithInterface( &mockDashboardAuditService{}, &mockDashboardTrackService{}, logger, ) router := gin.New() userID := uuid.New() router.Use(func(c *gin.Context) { c.Set("user_id", userID) c.Next() }) router.GET("/dashboard", handler.GetDashboard()) req := httptest.NewRequest("GET", "/dashboard?activity_limit=5&library_limit=3&stats_period=7d", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) }