package v1 import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" "gorm.io/driver/sqlite" "gorm.io/gorm" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" "github.com/gochat/gochat/internal/service" ) type SummaryReportHandlerTestSuite struct { suite.Suite db *gorm.DB handler *SummaryReportHandler router *gin.Engine account *model.Account } func (s *SummaryReportHandlerTestSuite) SetupSuite() { s.db, _ = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) s.db.AutoMigrate(&model.Account{}, &model.ReportingEventsRollup{}) repo := repository.NewReportingEventsRollupRepo(s.db) svc := service.NewSummaryReportService(repo) s.handler = NewSummaryReportHandler(svc) gin.SetMode(gin.TestMode) r := gin.New() accountGroup := r.Group("/api/v1/accounts/:account_id") accountGroup.GET("/summary_reports/agent", s.handler.Agent) accountGroup.GET("/summary_reports/team", s.handler.Team) accountGroup.GET("/summary_reports/inbox", s.handler.Inbox) accountGroup.GET("/summary_reports/label", s.handler.Label) accountGroup.GET("/summary_reports/channel", s.handler.Channel) s.router = r s.account = &model.Account{Name: "TestAccount"} s.db.Create(s.account) } func TestSummaryReportHandlerTestSuite(t *testing.T) { suite.Run(t, new(SummaryReportHandlerTestSuite)) } func (s *SummaryReportHandlerTestSuite) TestAgent_InvalidAccountID() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/agent?since=1704067200&until=1735689599", nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) } func (s *SummaryReportHandlerTestSuite) TestAgent_MissingDateRange() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/summary_reports/agent", s.account.ID), nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) } func (s *SummaryReportHandlerTestSuite) TestAgent_Success() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/summary_reports/agent?since=1704067200&until=1735689599", s.account.ID), nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusOK, w.Code) var body []interface{} s.NoError(json.Unmarshal(w.Body.Bytes(), &body)) } func (s *SummaryReportHandlerTestSuite) TestTeam_InvalidAccountID() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/team?since=1704067200&until=1735689599", nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) } func (s *SummaryReportHandlerTestSuite) TestTeam_MissingDateRange() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/summary_reports/team", s.account.ID), nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) } func (s *SummaryReportHandlerTestSuite) TestInbox_InvalidAccountID() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/inbox?since=1704067200&until=1735689599", nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) } func (s *SummaryReportHandlerTestSuite) TestLabel_InvalidAccountID() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/label?since=1704067200&until=1735689599", nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) } func (s *SummaryReportHandlerTestSuite) TestChannel_DateRangeTooLong() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/summary_reports/channel?since=1704067200&until=1735689599", s.account.ID), nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) }