109 lines
4.0 KiB
Go
109 lines
4.0 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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 LiveReportHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *LiveReportHandler
|
|
router *gin.Engine
|
|
}
|
|
|
|
func (s *LiveReportHandlerTestSuite) SetupSuite() {
|
|
s.db, _ = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
s.db.AutoMigrate(&model.Account{}, &model.Conversation{}, &model.ReportingEventsRollup{})
|
|
|
|
anSvc := service.NewAnalyticsService(
|
|
repository.NewReportingEventRepo(s.db),
|
|
repository.NewReportingEventsRollupRepo(s.db),
|
|
)
|
|
s.handler = NewLiveReportHandler(anSvc)
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/live_reports/conversation_metrics", s.handler.ConversationMetrics)
|
|
r.GET("/api/v1/accounts/:account_id/live_reports/grouped_conversation_metrics", s.handler.GroupedConversationMetrics)
|
|
s.router = r
|
|
}
|
|
|
|
func (s *LiveReportHandlerTestSuite) SetupTest() {
|
|
s.db.Exec("DELETE FROM conversations")
|
|
}
|
|
|
|
func TestLiveReportHandlerTestSuite(t *testing.T) {
|
|
suite.Run(t, new(LiveReportHandlerTestSuite))
|
|
}
|
|
|
|
func (s *LiveReportHandlerTestSuite) TestConversationMetrics_InvalidAccountID() {
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/live_reports/conversation_metrics", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *LiveReportHandlerTestSuite) TestConversationMetrics_Success() {
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/live_reports/conversation_metrics", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusOK, w.Code)
|
|
var body map[string]interface{}
|
|
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
|
|
s.NotContains(body, "success")
|
|
s.Equal(float64(0), body["open"])
|
|
s.Equal(float64(0), body["unattended"])
|
|
s.Equal(float64(0), body["unassigned"])
|
|
s.Equal(float64(0), body["pending"])
|
|
}
|
|
|
|
func (s *LiveReportHandlerTestSuite) TestGroupedConversationMetrics_InvalidGroupByReturnsChatwootError() {
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/live_reports/grouped_conversation_metrics?group_by=invalid_param", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusUnprocessableEntity, w.Code)
|
|
var body map[string]interface{}
|
|
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
|
|
s.Equal("invalid group_by", body["error"])
|
|
s.NotContains(body, "success")
|
|
}
|
|
|
|
func (s *LiveReportHandlerTestSuite) TestGroupedConversationMetrics_ByAssignee() {
|
|
agentID := uint(42)
|
|
firstReply := int64(1760000000)
|
|
assigned := model.Conversation{AccountID: 1, AssigneeID: &agentID, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget"}
|
|
replied := model.Conversation{AccountID: 1, AssigneeID: &agentID, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget", FirstReplyCreatedAt: &firstReply}
|
|
unassigned := model.Conversation{AccountID: 1, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget"}
|
|
s.Require().NoError(s.db.Create(&assigned).Error)
|
|
s.Require().NoError(s.db.Create(&replied).Error)
|
|
s.Require().NoError(s.db.Create(&unassigned).Error)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/live_reports/grouped_conversation_metrics?group_by=assignee_id", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusOK, w.Code)
|
|
var body []map[string]interface{}
|
|
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
|
|
s.Len(body, 2)
|
|
s.Nil(body[0]["assignee_id"])
|
|
s.Equal(float64(1), body[0]["open"])
|
|
s.Equal(float64(1), body[0]["unattended"])
|
|
s.Equal(float64(1), body[0]["unassigned"])
|
|
s.Equal(float64(agentID), body[1]["assignee_id"])
|
|
s.Equal(float64(2), body[1]["open"])
|
|
s.Equal(float64(1), body[1]["unattended"])
|
|
s.Equal(float64(0), body[1]["unassigned"])
|
|
}
|