Files
gochat/backend/internal/handler/api/v1/coverage18_test.go
T
Rogeeandrogee 2b182f9956 H-300: wire Captain Skills into Web runtime (#48)
* H-300: wire Captain Skills into Web runtime

* H-300: enforce effective model and conservative skill budget

* H-300: fix CI gosec step

* ci: extend golangci-lint timeout

* fix lint findings across backend

* fix(push): resolve delivery protocol blockers

* test(repository): close SQLite test databases

* test(repository): reuse SQLite schema per package

* H-307: restore backend Go cache in CI

* H-307: prefetch modules before cold lint

* H-307: resolve govulncheck security gate

* H-307: build lint with patched Go toolchain

* H-307: clear remaining security scan findings

---------

Co-authored-by: Rogee <rogee@ipao.vip>
2026-08-19 07:08:14 +08:00

187 lines
6.6 KiB
Go

package v1
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
)
func newTestDB_Cov18(t *testing.T) *gorm.DB {
t.Helper()
models := []interface{}{
&model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{},
&model.Contact{}, &model.ContactInbox{}, &model.Conversation{}, &model.Message{},
&model.Attachment{}, &model.Notification{}, &model.NotificationPreference{},
&model.NotificationSetting{}, &model.NotificationSubscription{}, &model.CustomRole{},
&model.PlatformApp{}, &model.Permissible{}, &model.AgentBot{}, &model.AgentBotInbox{},
&model.AgentCapacityPolicy{}, &model.AssignmentPolicy{}, &model.Article{}, &model.Banner{},
&model.Category{}, &model.Company{}, &model.ConversationLabel{}, &model.ConversationParticipant{},
&model.CsatTemplate{}, &model.CustomAttributeDefinition{}, &model.CustomFilter{},
&model.DashboardApp{}, &model.DeliveryStatus{}, &model.DraftMessage{}, &model.Folder{},
&model.InboxLimit{}, &model.InboxMember{}, &model.InstallationConfig{}, &model.IntegrationHook{},
&model.Note{}, &model.Portal{}, &model.PortalMember{}, &model.PushToken{},
&model.ReportingEvent{}, &model.SlaPolicy{}, &model.Tag{}, &model.Team{}, &model.TeamMember{},
&model.WorkingHour{}, &model.CaptainAssistant{}, &model.CaptainDocument{},
&model.CaptainPreference{},
}
return sharedCoverageTestDB(t, "coverage18", models...)
}
func seedAccount_Cov18(t *testing.T, db *gorm.DB) uint {
t.Helper()
acc := model.Account{Name: "test-account"}
require.NoError(t, db.Create(&acc).Error)
return acc.ID
}
func seedInbox_Cov18(t *testing.T, db *gorm.DB, accountID uint) uint {
t.Helper()
inbox := model.Inbox{Name: "test-inbox", AccountID: accountID, ChannelType: "web_widget"}
require.NoError(t, db.Create(&inbox).Error)
return inbox.ID
}
func newCtx_Cov18(t *testing.T, method, path, body string) (*gin.Context, *httptest.ResponseRecorder) {
t.Helper()
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
var req *http.Request
if body != "" {
req = httptest.NewRequest(method, path, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
} else {
req = httptest.NewRequest(method, path, nil)
}
c.Request = req
return c, w
}
func TestConversationHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
convRepo := repository.NewConversationRepo(db)
msgRepo := repository.NewMessageRepo(db)
convSvc := service.NewConversationService(convRepo, msgRepo, nil, nil, nil, nil, nil)
msgSvc := service.NewMessageService(msgRepo, nil, nil)
h := NewConversationHandler(convSvc, msgSvc)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/conversations", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}}
h.List(c)
}
func TestContactHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
contactRepo := repository.NewContactRepo(db)
contactSvc := service.NewContactService(contactRepo, nil, nil)
h := NewContactHandler(contactSvc, nil, nil, nil)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/contacts", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}}
h.List(c)
}
func TestInboxHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
seedInbox_Cov18(t, db, accID)
inboxRepo := repository.NewInboxRepo(db)
inboxSvc := service.NewInboxService(inboxRepo, nil, nil, nil, nil, nil, nil)
h := NewInboxHandler(inboxSvc)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/inboxes", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}}
h.List(c)
}
func TestAccountHandler_Show_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
accRepo := repository.NewAccountRepo(db)
accSvc := service.NewAccountService(accRepo)
h := NewAccountHandler(accSvc)
c, _ := newCtx_Cov18(t, "GET", "/platform/api/v1/accounts/1", "")
c.Params = gin.Params{{Key: "id", Value: "1"}}
c.Set("account_id", accID)
h.Get(c)
}
func TestBannerHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
bannerRepo := repository.NewBannerRepo(db)
bannerSvc := service.NewBannerService(bannerRepo)
h := NewBannerHandler(bannerSvc)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/banners", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}}
h.List(c)
}
func TestNoteHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
noteRepo := repository.NewNoteRepo(db)
noteSvc := service.NewNoteService(noteRepo)
h := NewNoteHandler(noteSvc)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/notes", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}}
h.List(c)
}
func TestAgentBotHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
botRepo := repository.NewAgentBotRepo(db)
botSvc := service.NewAgentBotService(botRepo)
h := NewAgentBotHandler(botSvc)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/agent_bots", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}}
h.List(c)
}
func TestDashboardAppHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
dashRepo := repository.NewDashboardAppRepo(db)
dashSvc := service.NewDashboardAppService(dashRepo)
h := NewDashboardAppHandler(dashSvc)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/dashboard_apps", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}}
h.List(c)
}
func TestFolderHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
accID := seedAccount_Cov18(t, db)
folderRepo := repository.NewFolderRepo(db)
folderSvc := service.NewFolderService(folderRepo)
h := NewFolderHandler(folderSvc)
c, _ := newCtx_Cov18(t, "GET", "/api/v1/accounts/1/portals/1/folders", "")
c.Set("account_id", accID)
c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}}
h.List(c)
}
func TestInstallationConfigHandler_List_DB_Cov18(t *testing.T) {
db := newTestDB_Cov18(t)
icRepo := repository.NewInstallationConfigRepo(db)
icSvc := service.NewInstallationConfigService(icRepo)
h := NewInstallationConfigHandler(icSvc)
c, _ := newCtx_Cov18(t, "GET", "/platform/api/v1/installation_configs", "")
h.List(c)
}