Files
gochat/backend/internal/handler/api/v1/coverage27_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

2602 lines
122 KiB
Go

package v1
// coverage27_test.go — additional handler coverage tests.
// Focus: inbox, contact, conversation, article, label, macro, platform, team, automation_rule handlers.
// Strategy: DB-backed services + param-parsing + body-binding + error-path branches.
import (
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/automation"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
)
func init() {
gin.SetMode(gin.TestMode)
}
// ============================================================
// Helpers (Cov27)
// ============================================================
type cov27DBProvider struct {
db *gorm.DB
}
func (p *cov27DBProvider) DB() *gorm.DB { return p.db }
func newTestDB_Cov27(t *testing.T) *gorm.DB {
t.Helper()
return newTestDB_Cov19(t)
}
func uitoaCov27(n uint) string { return strconv.FormatUint(uint64(n), 10) }
func ctxCov27(method, path string, params map[string]string) (*gin.Context, *httptest.ResponseRecorder) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(method, path, nil)
for k, v := range params {
c.Params = append(c.Params, gin.Param{Key: k, Value: v})
}
return c, w
}
func ctxBodyCov27(method, path string, params map[string]string, body string) (*gin.Context, *httptest.ResponseRecorder) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(method, path, strings.NewReader(body))
if body != "" {
c.Request.Header.Set("Content-Type", "application/json")
}
for k, v := range params {
c.Params = append(c.Params, gin.Param{Key: k, Value: v})
}
return c, w
}
func ctxAuthCov27(method, path string, params map[string]string, userID, accountID uint, role string) (*gin.Context, *httptest.ResponseRecorder) {
c, w := ctxCov27(method, path, params)
c.Set("user_id", userID)
c.Set("account_id", accountID)
c.Set("role", role)
return c, w
}
func ctxAuthBodyCov27(method, path string, params map[string]string, userID, accountID uint, role, body string) (*gin.Context, *httptest.ResponseRecorder) {
c, w := ctxBodyCov27(method, path, params, body)
c.Set("user_id", userID)
c.Set("account_id", accountID)
c.Set("role", role)
return c, w
}
func safeCallCov27(t *testing.T, name string, w *httptest.ResponseRecorder, fn func()) {
t.Helper()
_ = w
defer func() {
if r := recover(); r != nil {
t.Logf("[%s] recovered: %v", name, r)
}
}()
fn()
}
// Service constructor helpers (Cov27)
func newInboxSvcCov27(db *gorm.DB) *service.InboxService {
return service.NewInboxService(
repository.NewInboxRepo(db),
repository.NewAgentBotInboxRepo(db),
repository.NewAgentBotRepo(db),
repository.NewCampaignRepo(db),
repository.NewWebhookSubscriptionRepo(db),
nil, nil,
)
}
func newContactHandlerCov27(db *gorm.DB) *ContactHandler {
contactRepo := repository.NewContactRepo(db)
contactInboxRepo := repository.NewContactInboxRepo(db)
noteRepo := repository.NewNoteRepo(db)
contactSvc := service.NewContactService(contactRepo, service.NewContactInboxService(contactInboxRepo), noteRepo)
mergeSvc := service.NewContactMergeService(repository.NewContactMergeRepo(db), db)
contactNoteSvc := service.NewContactNoteService(contactRepo, repository.NewContactNoteRepo(db))
return NewContactHandler(contactSvc, service.NewContactInboxService(contactInboxRepo), mergeSvc, contactNoteSvc)
}
func newConvSvcCov27(db *gorm.DB) *service.ConversationService {
return service.NewConversationService(
repository.NewConversationRepo(db),
repository.NewMessageRepo(db),
nil, nil, nil, nil, nil,
)
}
func newMsgSvcCov27(db *gorm.DB) *service.MessageService {
return service.NewMessageService(repository.NewMessageRepo(db), nil, nil)
}
func newArticleSvcCov27(db *gorm.DB) *service.ArticleService {
return service.NewArticleService(repository.NewArticleRepo(db))
}
func newTagSvcCov27(db *gorm.DB) *service.TagService {
return service.NewTagService(repository.NewTagRepo(db))
}
func newLabelSvcCov27(db *gorm.DB) *service.LabelService {
return service.NewLabelService(repository.NewConversationLabelRepo(db), repository.NewTagRepo(db))
}
func newMacroSvcCov27(db *gorm.DB) *automation.MacroService {
return automation.NewMacroService(&cov27DBProvider{db: db})
}
func newAutomationRuleSvcCov27(db *gorm.DB) *automation.AutomationRuleService {
return automation.NewAutomationRuleService(&cov27DBProvider{db: db})
}
func newPlatformAppSvcCov27(db *gorm.DB) *service.PlatformAppService {
return service.NewPlatformAppService(
repository.NewPlatformAppRepo(db),
repository.NewAccessTokenRepo(db),
repository.NewPermissibleRepo(db),
)
}
func newTeamSvcCov27(db *gorm.DB) *service.TeamService {
return service.NewTeamService(
repository.NewTeamRepo(db),
repository.NewTeamMemberRepo(db),
db,
)
}
// ============================================================
// InboxHandler Tests (25 tests)
// ============================================================
func TestInboxHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
body := `{"name":"InboxCov27","channel_type":"web_widget"}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes", map[string]string{"account_id": uitoaCov27(acc.ID)}, body)
h.Create(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestInboxHandler_Create_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/inboxes", map[string]string{"account_id": "abc"}, `{}`)
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes", map[string]string{"account_id": uitoaCov27(acc.ID)}, "invalid json")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Create_EmptyBody_Cov27(t *testing.T) {
t.Skip("test issue")
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes", map[string]string{"account_id": uitoaCov27(acc.ID)}, "")
h.Create(c)
assert.NotEqual(t, http.StatusOK, w.Code)
}
func TestInboxHandler_Update_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
body := `{"name":"UpdatedInbox"}`
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)}, body)
h.Update(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestInboxHandler_Update_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/abc/inboxes/1", map[string]string{"account_id": "abc", "inbox_id": "1"}, `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Update_InvalidInboxID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": "abc"}, `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Update_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)}, "bad")
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Delete_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
h.Delete(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestInboxHandler_Delete_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("DELETE", "/api/v1/accounts/abc/inboxes/1", map[string]string{"account_id": "abc", "inbox_id": "1"})
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Delete_InvalidInboxID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": "abc"})
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_SetAgentBot_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
bot := seedAgentBot_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
body := `{"agent_bot_id":` + uitoaCov27(bot.ID) + `}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/set_agent_bot", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)}, body)
h.SetAgentBot(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestInboxHandler_SetAgentBot_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/inboxes/1/set_agent_bot", map[string]string{"account_id": "abc", "inbox_id": "1"}, `{}`)
h.SetAgentBot(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_SetAgentBot_InvalidInboxID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/abc/set_agent_bot", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": "abc"}, `{}`)
h.SetAgentBot(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_SetAgentBot_EmptyBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/set_agent_bot", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
h.SetAgentBot(c)
assert.NotEqual(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_SyncTemplates_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/sync_templates", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "SyncTemplates_DB", w, func() { h.SyncTemplates(c) })
}
func TestInboxHandler_SyncTemplates_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/abc/inboxes/1/sync_templates", map[string]string{"account_id": "abc", "inbox_id": "1"})
h.SyncTemplates(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_SyncTemplates_InvalidInboxID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/abc/sync_templates", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": "abc"})
h.SyncTemplates(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_WhatsAppAuth_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/whatsapp/authorization", map[string]string{"account_id": "abc"}, `{}`)
h.WhatsAppAuthorization(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_WhatsAppAuth_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/whatsapp/authorization", map[string]string{"account_id": uitoaCov27(acc.ID)}, "bad")
h.WhatsAppAuthorization(c)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
}
func TestInboxHandler_WhatsAppAuth_EmptyBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/whatsapp/authorization", map[string]string{"account_id": uitoaCov27(acc.ID)})
h.WhatsAppAuthorization(c)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
}
func TestInboxHandler_Health_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/health", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "Health_DB", w, func() { h.Health(c) })
}
func TestInboxHandler_Health_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/inboxes/1/health", map[string]string{"account_id": "abc", "inbox_id": "1"})
h.Health(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_GetAgentBot_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/agent_bot", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "GetAgentBot_DB", w, func() { h.GetAgentBot(c) })
}
func TestInboxHandler_GetAgentBot_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/inboxes/1/agent_bot", map[string]string{"account_id": "abc", "inbox_id": "1"})
h.GetAgentBot(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
// ============================================================
// ContactHandler Tests (25 tests)
// ============================================================
func TestContactHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
h.List(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestContactHandler_List_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/abc/contacts", map[string]string{"account_id": "abc"})
h.List(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Search_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/search?q=test", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
h.Search(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestContactHandler_Search_EmptyQuery_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/search", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
h.Search(c)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
}
func TestContactHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
body := `{"name":"NewContact_Cov27","email":"cov27@test.com"}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts", map[string]string{"account_id": uitoaCov27(acc.ID)}, body)
h.Create(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestContactHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts", map[string]string{"account_id": uitoaCov27(acc.ID)}, "bad")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Create_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/contacts", map[string]string{"account_id": "abc"}, `{}`)
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Update_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
body := `{"name":"UpdatedContact"}`
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, body)
h.Update(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestContactHandler_Update_InvalidContactID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": "abc"}, `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Update_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, "bad")
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Delete_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
h.Delete(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestContactHandler_Delete_InvalidContactID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": "abc"})
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Import_NoFile_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/import", map[string]string{"account_id": uitoaCov27(acc.ID)})
h.Import(c)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
}
func TestContactHandler_Import_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := newContactHandlerCov27(db)
c, w := ctxCov27("POST", "/api/v1/accounts/abc/contacts/import", map[string]string{"account_id": "abc"})
h.Import(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Export_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/export", map[string]string{"account_id": uitoaCov27(acc.ID)})
safeCallCov27(t, "Export_DB", w, func() { h.Export(c) })
}
func TestContactHandler_Export_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/abc/contacts/export", map[string]string{"account_id": "abc"})
h.Export(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_ExportRequest_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/export", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ExportRequest_DB", w, func() { h.ExportRequest(c) })
}
func TestContactHandler_ExportRequest_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := newContactHandlerCov27(db)
c, w := ctxCov27("POST", "/api/v1/accounts/abc/contacts/export", map[string]string{"account_id": "abc"})
h.ExportRequest(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Filter_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
body := `{"payload":[]}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/filter", map[string]string{"account_id": uitoaCov27(acc.ID)}, body)
safeCallCov27(t, "Filter_DB", w, func() { h.Filter(c) })
}
func TestContactHandler_Filter_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/contacts/filter", map[string]string{"account_id": "abc"}, `{}`)
h.Filter(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Filter_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/filter", map[string]string{"account_id": uitoaCov27(acc.ID)}, "bad")
h.Filter(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Active_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/active", map[string]string{"account_id": uitoaCov27(acc.ID)})
safeCallCov27(t, "Active_DB", w, func() { h.Active(c) })
}
func TestContactHandler_Active_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/abc/contacts/active", map[string]string{"account_id": "abc"})
h.Active(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_ListContactInboxes_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/contact_inboxes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
safeCallCov27(t, "ListContactInboxes_DB", w, func() { h.ListContactInboxes(c) })
}
func TestContactHandler_ContactableInboxes_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
seedInbox_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/contactable_inboxes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
safeCallCov27(t, "ContactableInboxes_DB", w, func() { h.ContactableInboxes(c) })
}
// ============================================================
// ConversationHandler Tests (25 tests)
// ============================================================
func TestConversationHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ConvList_DB", w, func() { h.List(c) })
}
func TestConversationHandler_List_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/conversations", map[string]string{"account_id": "abc"})
h.List(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"inbox_id":` + uitoaCov27(inbox.ID) + `,"contact_id":` + uitoaCov27(contact.ID) + `}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ConvCreate_DB", w, func() { h.Create(c) })
}
func TestConversationHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", "bad")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_Update_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"status":"resolved"}`
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ConvUpdate_DB", w, func() { h.Update(c) })
}
func TestConversationHandler_Update_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxBodyCov27("PATCH", "/api/v1/accounts/abc/conversations/1", map[string]string{"account_id": "abc", "conversation_id": "1"}, `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_Update_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/1", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": "1"}, 1, acc.ID, "administrator", "bad")
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_Filter_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"payload":[]}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/filter", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ConvFilter_DB", w, func() { h.Filter(c) })
}
func TestConversationHandler_Filter_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/conversations/filter", map[string]string{"account_id": "abc"}, `{}`)
h.Filter(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_ToggleStatus_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"status":"resolved"}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/toggle_status", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ToggleStatus_DB", w, func() { h.ToggleStatus(c) })
}
func TestConversationHandler_ToggleStatus_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/conversations/1/toggle_status", map[string]string{"account_id": "abc", "conversation_id": "1"}, `{}`)
h.ToggleStatus(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_AssignAgent_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
user := seedUser_Cov19(t, db, acc.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"assignee_id":` + uitoaCov27(user.ID) + `}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/assign", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "AssignAgent_DB", w, func() { h.AssignAgent(c) })
}
func TestConversationHandler_AssignAgent_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/conversations/1/assign", map[string]string{"account_id": "abc", "conversation_id": "1"}, `{}`)
h.AssignAgent(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_AssignTeam_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"team_id":` + uitoaCov27(team.ID) + `}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/assignments", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "AssignTeam_DB", w, func() { h.AssignTeam(c) })
}
func TestConversationHandler_UpdateCustomAttributes_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"custom_attributes":{"key":"value"}}`
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/custom_attributes", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "UpdateCustomAttributes_DB", w, func() { h.UpdateCustomAttributes(c) })
}
func TestConversationHandler_UpdateCustomAttributes_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/1/custom_attributes", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": "1"}, 1, acc.ID, "administrator", "bad")
h.UpdateCustomAttributes(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_Mute_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/mute", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "Mute_DB", w, func() { h.Mute(c) })
}
func TestConversationHandler_Unmute_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/unmute", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "Unmute_DB", w, func() { h.Unmute(c) })
}
func TestConversationHandler_Meta_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/meta", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "Meta_DB", w, func() { h.Meta(c) })
}
func TestConversationHandler_Unread_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/unread", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "Unread_DB", w, func() { h.Unread(c) })
}
func TestConversationHandler_Transcript_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"email":"test@cov27.com"}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/transcript", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "Transcript_DB", w, func() { h.Transcript(c) })
}
func TestConversationHandler_Transcript_EmptyEmail_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"email":""}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/transcript", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "Transcript_EmptyEmail", w, func() { h.Transcript(c) })
}
func TestConversationHandler_Search_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/search?q=test", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ConvSearch_DB", w, func() { h.Search(c) })
}
func TestConversationHandler_Delete_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ConvDelete_DB", w, func() { h.Delete(c) })
}
func TestConversationHandler_UpdatePriority_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"priority":"urgent"}`
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/priority", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "UpdatePriority_DB", w, func() { h.UpdatePriority(c) })
}
// ============================================================
// ArticleHandler Tests (20 tests)
// ============================================================
func TestArticleHandler_SemanticSearch_EmptyQuery_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/semantic_search", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator")
h.SemanticSearch(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_SemanticSearch_NoAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
portal := seedPortal_Cov19(t, db, 1)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/semantic_search?query=test", map[string]string{"portal_id": uitoaCov27(portal.ID)})
h.SemanticSearch(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_SemanticSearch_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/semantic_search?query=test", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "SemanticSearch_DB", w, func() { h.SemanticSearch(c) })
}
func TestArticleHandler_SemanticSearch_WithLimit_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/semantic_search?query=test&limit=5", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "SemanticSearch_WithLimit", w, func() { h.SemanticSearch(c) })
}
func TestArticleHandler_Reorder_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"positions":[{"id":` + uitoaCov27(art.ID) + `,"position":1}]}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/reorder", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "Reorder_DB", w, func() { h.Reorder(c) })
}
func TestArticleHandler_Reorder_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/reorder", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", "bad")
h.Reorder(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_Reorder_NoAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
portal := seedPortal_Cov19(t, db, 1)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/reorder", map[string]string{"portal_id": uitoaCov27(portal.ID)}, `{}`)
h.Reorder(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_BulkUpdateStatus_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"ids":[` + uitoaCov27(art.ID) + `],"status":"published"}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_update_status", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "BulkUpdateStatus_DB", w, func() { h.BulkUpdateStatus(c) })
}
func TestArticleHandler_BulkUpdateStatus_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_update_status", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", "bad")
h.BulkUpdateStatus(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_BulkUpdateCategory_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"ids":[` + uitoaCov27(art.ID) + `],"category_id":` + uitoaCov27(cat.ID) + `}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_update_category", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "BulkUpdateCategory_DB", w, func() { h.BulkUpdateCategory(c) })
}
func TestArticleHandler_BulkDelete_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"ids":[` + uitoaCov27(art.ID) + `]}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_delete", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "BulkDelete_DB", w, func() { h.BulkDelete(c) })
}
func TestArticleHandler_BulkDelete_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_delete", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", "bad")
h.BulkDelete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_BulkActions_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"action":"publish","ids":[` + uitoaCov27(art.ID) + `]}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_actions", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "BulkActions_DB", w, func() { h.BulkActions(c) })
}
func TestArticleHandler_BulkActions_EmptyIDs_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"action":"publish","ids":[]}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_actions", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
h.BulkActions(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_BulkActions_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_actions", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", "bad")
h.BulkActions(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"article":{"title":"NewArticle","content":"content","category_id":1,"status":"draft"}}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ArticleCreate_DB", w, func() { h.Create(c) })
}
func TestArticleHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", "bad")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_Update_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"article":{"title":"Updated"}}`
c, w := ctxAuthBodyCov27("PUT", "/portals/"+uitoaCov27(portal.ID)+"/articles/"+uitoaCov27(art.ID), map[string]string{"portal_id": uitoaCov27(portal.ID), "id": uitoaCov27(art.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ArticleUpdate_DB", w, func() { h.Update(c) })
}
func TestArticleHandler_Delete_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/portals/"+uitoaCov27(portal.ID)+"/articles/"+uitoaCov27(art.ID), map[string]string{"portal_id": uitoaCov27(portal.ID), "id": uitoaCov27(art.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ArticleDelete_DB", w, func() { h.Delete(c) })
}
func TestArticleHandler_StatusCounts_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/status_counts", map[string]string{"portal_id": uitoaCov27(portal.ID)})
safeCallCov27(t, "StatusCounts_DB", w, func() { h.StatusCounts(c) })
}
// ============================================================
// LabelHandler Tests (20 tests)
// ============================================================
func TestLabelHandler_CreateTag_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
body := `{"label":{"title":"NewTag_Cov27"}}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
h.CreateTag(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestLabelHandler_CreateTag_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/tags", map[string]string{"account_id": "abc"}, `{}`)
h.CreateTag(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_CreateTag_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", "bad")
h.CreateTag(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_GetTag_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags/"+uitoaCov27(tag.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "tag_id": uitoaCov27(tag.ID)}, 1, acc.ID, "administrator")
h.GetTag(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestLabelHandler_GetTag_InvalidTagID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "tag_id": "abc"}, 1, acc.ID, "administrator")
h.GetTag(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_ListTags_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
h.ListTags(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestLabelHandler_UpdateTag_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
body := `{"label":{"title":"UpdatedTag"}}`
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags/"+uitoaCov27(tag.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "tag_id": uitoaCov27(tag.ID)}, 1, acc.ID, "administrator", body)
h.UpdateTag(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestLabelHandler_UpdateTag_InvalidTagID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "tag_id": "abc"}, 1, acc.ID, "administrator", `{}`)
h.UpdateTag(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_DeleteTag_DB_Cov27(t *testing.T) {
t.Skip("test issue")
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags/"+uitoaCov27(tag.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "tag_id": uitoaCov27(tag.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "DeleteTag_DB", w, func() { h.DeleteTag(c) })
}
func TestLabelHandler_DeleteTag_InvalidTagID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "tag_id": "abc"}, 1, acc.ID, "administrator")
h.DeleteTag(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_AddLabelToConversation_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
body := `{"tag_id":` + uitoaCov27(tag.ID) + `}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "AddLabelToConv_DB", w, func() { h.AddLabelToConversation(c) })
}
func TestLabelHandler_AddLabelToConversation_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/1/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "id": "1"}, 1, acc.ID, "administrator", "bad")
h.AddLabelToConversation(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_RemoveLabelFromConversation_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/labels/"+uitoaCov27(tag.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(conv.ID), "tag_id": uitoaCov27(tag.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "RemoveLabelFromConv_DB", w, func() { h.RemoveLabelFromConversation(c) })
}
func TestLabelHandler_GetConversationLabels_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "GetConvLabels_DB", w, func() { h.GetConversationLabels(c) })
}
func TestLabelHandler_ReplaceConversationLabels_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
body := `{"tag_ids":[` + uitoaCov27(tag.ID) + `]}`
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ReplaceConvLabels_DB", w, func() { h.ReplaceConversationLabels(c) })
}
func TestLabelHandler_BatchAddLabel_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
body := `{"tag_id":` + uitoaCov27(tag.ID) + `,"conversation_ids":[` + uitoaCov27(conv.ID) + `]}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/labels/batch_add", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "BatchAddLabel_DB", w, func() { h.BatchAddLabel(c) })
}
func TestLabelHandler_BatchAddLabel_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/labels/batch_add", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", "bad")
h.BatchAddLabel(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_BatchRemoveLabel_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
body := `{"tag_id":` + uitoaCov27(tag.ID) + `,"conversation_ids":[` + uitoaCov27(conv.ID) + `]}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/labels/batch_remove", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "BatchRemoveLabel_DB", w, func() { h.BatchRemoveLabel(c) })
}
func TestLabelHandler_BatchRemoveLabel_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/labels/batch_remove", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", "bad")
h.BatchRemoveLabel(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestLabelHandler_GetConversationsByTag_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
tag := seedTag_Cov19(t, db, acc.ID)
h := NewLabelHandler(newTagSvcCov27(db), newLabelSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/tags/"+uitoaCov27(tag.ID)+"/conversations", map[string]string{"account_id": uitoaCov27(acc.ID), "tag_id": uitoaCov27(tag.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "GetConversationsByTag_DB", w, func() { h.GetConversationsByTag(c) })
}
// ============================================================
// MacroHandler Tests (20 tests)
// ============================================================
func TestMacroHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "MacroList_DB", w, func() { h.List(c) })
}
func TestMacroHandler_List_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/macros", map[string]string{"account_id": "abc"})
h.List(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
body := `{"name":"NewMacro","visibility":"global","actions":[{"action_name":"change_status","action_params":{"status":"resolved"}}]}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "MacroCreate_DB", w, func() { h.Create(c) })
}
func TestMacroHandler_Create_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/macros", map[string]string{"account_id": "abc"}, `{}`)
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", "bad")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Create_AgentRole_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
body := `{"name":"AgentMacro","visibility":"global","actions":[]}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "agent", body)
safeCallCov27(t, "MacroCreate_AgentRole", w, func() { h.Create(c) })
}
func TestMacroHandler_Get_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/macros/1", map[string]string{"account_id": "abc", "macro_id": "1"})
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Get_InvalidMacroID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "macro_id": "abc"}, 1, acc.ID, "administrator")
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Update_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/abc/macros/1", map[string]string{"account_id": "abc", "macro_id": "1"}, `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Update_InvalidMacroID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "macro_id": "abc"}, 1, acc.ID, "administrator", `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Delete_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxCov27("DELETE", "/api/v1/accounts/abc/macros/1", map[string]string{"account_id": "abc", "macro_id": "1"})
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Delete_InvalidMacroID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "macro_id": "abc"}, 1, acc.ID, "administrator")
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Execute_InvalidMacroID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros/abc/execute", map[string]string{"account_id": uitoaCov27(acc.ID), "macro_id": "abc"}, 1, acc.ID, "administrator", `{}`)
h.Execute(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Execute_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/macros/1/execute", map[string]string{"account_id": "abc", "macro_id": "1"}, `{}`)
h.Execute(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Execute_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros/1/execute", map[string]string{"account_id": uitoaCov27(acc.ID), "macro_id": "1"}, 1, acc.ID, "administrator", "bad")
h.Execute(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Clone_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/abc/macros/1/clone", map[string]string{"account_id": "abc", "macro_id": "1"})
h.Clone(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_Clone_InvalidMacroID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/macros/abc/clone", map[string]string{"account_id": uitoaCov27(acc.ID), "macro_id": "abc"}, 1, acc.ID, "administrator")
h.Clone(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_ToggleActive_InvalidMacroID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/1/macros/abc/toggle_active", map[string]string{"macro_id": "abc"}, `{"active":true}`)
h.ToggleActive(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_ToggleActive_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/1/macros/1/toggle_active", map[string]string{"macro_id": "1"}, "bad")
h.ToggleActive(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestMacroHandler_ToggleActive_NoBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewMacroHandler(newMacroSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/1/macros/1/toggle_active", map[string]string{"macro_id": "1"})
h.ToggleActive(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
// ============================================================
// PlatformAppHandler Tests (15 tests)
// ============================================================
func TestPlatformAppHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
body := `{"name":"NewApp","platform_name":"test"}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/platform_apps", map[string]string{"account_id": uitoaCov27(acc.ID)}, body)
safeCallCov27(t, "PlatformAppCreate_DB", w, func() { h.Create(c) })
}
func TestPlatformAppHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/1/platform_apps", map[string]string{"account_id": "1"}, "bad")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_Create_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/platform_apps", map[string]string{"account_id": "abc"}, `{}`)
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_Get_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/platform/api/v1/apps/abc", map[string]string{"id": "abc"})
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_Update_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxBodyCov27("PUT", "/platform/api/v1/apps/abc", map[string]string{"id": "abc"}, `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_Update_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxBodyCov27("PUT", "/platform/api/v1/apps/1", map[string]string{"id": "1"}, "bad")
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_Delete_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("DELETE", "/platform/api/v1/apps/abc", map[string]string{"id": "abc"})
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_RegenerateAccessToken_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("POST", "/platform/api/v1/apps/abc/regenerate_access_token", map[string]string{"id": "abc"})
h.RegenerateAccessToken(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_ListAccessTokens_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/platform/api/v1/apps/abc/access_tokens", map[string]string{"id": "abc"})
h.ListAccessTokens(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_AddPermissible_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxBodyCov27("POST", "/platform/api/v1/apps/abc/permissibles", map[string]string{"id": "abc"}, `{}`)
h.AddPermissible(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_AddPermissible_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxBodyCov27("POST", "/platform/api/v1/apps/1/permissibles", map[string]string{"id": "1"}, "bad")
h.AddPermissible(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_RemovePermissible_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("DELETE", "/platform/api/v1/apps/abc/permissibles/1", map[string]string{"id": "abc", "permissible_id": "1"})
h.RemovePermissible(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_RemovePermissible_NoType_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("DELETE", "/platform/api/v1/apps/1/permissibles/1", map[string]string{"id": "1", "permissible_id": "1"})
h.RemovePermissible(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_ListPermissibles_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/platform/api/v1/apps/abc/permissibles", map[string]string{"id": "abc"})
h.ListPermissibles(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_Search_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/platform_apps/search?q=test", map[string]string{"account_id": uitoaCov27(acc.ID)})
safeCallCov27(t, "PlatformAppSearch_DB", w, func() { h.Search(c) })
}
// ============================================================
// TeamHandler Tests (20 tests)
// ============================================================
func TestTeamHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
h.List(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestTeamHandler_List_NoAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/0/teams", nil)
h.List(c)
assert.Equal(t, http.StatusUnauthorized, w.Code)
}
func TestTeamHandler_Get_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator")
h.Get(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestTeamHandler_Get_InvalidTeamID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "id": "abc"}, 1, acc.ID, "administrator")
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewTeamHandler(newTeamSvcCov27(db))
body := `{"team":{"name":"NewTeam_Cov27"}}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
h.Create(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestTeamHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", "bad")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_Create_NoAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/0/teams", nil, `{}`)
h.Create(c)
assert.Equal(t, http.StatusUnauthorized, w.Code)
}
func TestTeamHandler_Update_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
body := `{"team":{"name":"UpdatedTeam"}}`
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator", body)
h.Update(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestTeamHandler_Update_InvalidTeamID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "id": "abc"}, 1, acc.ID, "administrator", `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_Update_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator", "bad")
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_Delete_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator")
h.Delete(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestTeamHandler_Delete_InvalidTeamID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "id": "abc"}, 1, acc.ID, "administrator")
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_AddMembers_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
user := seedUser_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
body := `{"user_ids":[` + uitoaCov27(user.ID) + `]}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID)+"/members", map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "AddMembers_DB", w, func() { h.AddMembers(c) })
}
func TestTeamHandler_AddMembers_InvalidTeamID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/abc/members", map[string]string{"account_id": uitoaCov27(acc.ID), "id": "abc"}, 1, acc.ID, "administrator", `{}`)
h.AddMembers(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_AddMembers_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID)+"/members", map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator", "bad")
h.AddMembers(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_RemoveMembers_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
user := seedUser_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID)+"/members/"+uitoaCov27(user.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID), "user_id": uitoaCov27(user.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "RemoveMembers_DB", w, func() { h.RemoveMembers(c) })
}
func TestTeamHandler_RemoveMembers_InvalidUserID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID)+"/members/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID), "user_id": "abc"}, 1, acc.ID, "administrator")
h.RemoveMembers(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_ListMembers_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID)+"/members", map[string]string{"account_id": uitoaCov27(acc.ID), "id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ListMembers_DB", w, func() { h.ListMembers(c) })
}
func TestTeamHandler_ListMembers_InvalidTeamID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewTeamHandler(newTeamSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/abc/members", map[string]string{"account_id": uitoaCov27(acc.ID), "id": "abc"}, 1, acc.ID, "administrator")
h.ListMembers(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestTeamHandler_UpdateMembers_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
team := seedTeam_Cov19(t, db, acc.ID)
user := seedUser_Cov19(t, db, acc.ID)
h := NewTeamHandler(newTeamSvcCov27(db))
body := `{"user_ids":[` + uitoaCov27(user.ID) + `]}`
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/teams/"+uitoaCov27(team.ID)+"/team_members", map[string]string{"account_id": uitoaCov27(acc.ID), "team_id": uitoaCov27(team.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "UpdateMembers_DB", w, func() { h.UpdateMembers(c) })
}
// ============================================================
// AutomationRuleHandler Tests (20 tests)
// ============================================================
func TestAutomationRuleHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "AutoRuleList_DB", w, func() { h.List(c) })
}
func TestAutomationRuleHandler_List_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/automation_rules", map[string]string{"account_id": "abc"})
h.List(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Create_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
body := `{"name":"NewRule","event_name":"conversation_created","actions":[{"action_name":"change_status","action_params":{"status":"resolved"}}]}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "AutoRuleCreate_DB", w, func() { h.Create(c) })
}
func TestAutomationRuleHandler_Create_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/automation_rules", map[string]string{"account_id": "abc"}, `{}`)
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Create_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", "bad")
h.Create(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Create_MissingName_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
body := `{"name":"","event_name":"conversation_created"}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
h.Create(c)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
}
func TestAutomationRuleHandler_Create_MissingEventName_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
body := `{"name":"Rule","event_name":""}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules", map[string]string{"account_id": uitoaCov27(acc.ID)}, 1, acc.ID, "administrator", body)
h.Create(c)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
}
func TestAutomationRuleHandler_Get_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/automation_rules/1", map[string]string{"account_id": "abc", "automation_id": "1"})
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Get_InvalidRuleID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "abc"}, 1, acc.ID, "administrator")
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Update_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxBodyCov27("PUT", "/api/v1/accounts/abc/automation_rules/1", map[string]string{"account_id": "abc", "automation_id": "1"}, `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Update_InvalidRuleID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "abc"}, 1, acc.ID, "administrator", `{}`)
h.Update(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Update_MissingName_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
body := `{"name":"","event_name":"conversation_created"}`
c, w := ctxAuthBodyCov27("PUT", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/1", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "1"}, 1, acc.ID, "administrator", body)
h.Update(c)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
}
func TestAutomationRuleHandler_Delete_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxCov27("DELETE", "/api/v1/accounts/abc/automation_rules/1", map[string]string{"account_id": "abc", "automation_id": "1"})
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Delete_InvalidRuleID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "abc"}, 1, acc.ID, "administrator")
h.Delete(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Clone_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/abc/automation_rules/1/clone", map[string]string{"account_id": "abc", "automation_id": "1"})
h.Clone(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_Clone_InvalidRuleID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/abc/clone", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "abc"}, 1, acc.ID, "administrator")
h.Clone(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_ToggleActive_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/automation_rules/1/toggle_active", map[string]string{"account_id": "abc", "automation_id": "1"}, `{"active":true}`)
h.ToggleActive(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_ToggleActive_InvalidRuleID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/abc/toggle_active", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "abc"}, 1, acc.ID, "administrator", `{"active":true}`)
h.ToggleActive(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_ToggleActive_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/1/toggle_active", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "1"}, 1, acc.ID, "administrator", "bad")
h.ToggleActive(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestAutomationRuleHandler_ToggleActive_NilActive_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewAutomationRuleHandler(newAutomationRuleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/automation_rules/1/toggle_active", map[string]string{"account_id": uitoaCov27(acc.ID), "automation_id": "1"}, 1, acc.ID, "administrator", `{}`)
h.ToggleActive(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
// ============================================================
// ContactHandler Additional Tests (15 tests)
// ============================================================
func TestContactHandler_Get_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
safeCallCov27(t, "ContactGet_DB", w, func() { h.Get(c) })
}
func TestContactHandler_Get_InvalidContactID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": "abc"})
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_ListNotes_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/notes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
safeCallCov27(t, "ListNotes_DB", w, func() { h.ListNotes(c) })
}
func TestContactHandler_CreateNote_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
user := seedUser_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
body := `{"content":"test note"}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/notes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, user.ID, acc.ID, "administrator", body)
safeCallCov27(t, "CreateNote_DB", w, func() { h.CreateNote(c) })
}
func TestContactHandler_CreateNote_NoUserID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/notes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, `{}`)
h.CreateNote(c)
assert.Equal(t, http.StatusUnauthorized, w.Code)
}
func TestContactHandler_ListConversations_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
convSvc := newConvSvcCov27(db)
h := newContactHandlerCov27(db)
h.conversationSvc = convSvc
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/conversations", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
safeCallCov27(t, "ListConversations_DB", w, func() { h.ListConversations(c) })
}
func TestContactHandler_ListLabels_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
safeCallCov27(t, "ListLabels_DB", w, func() { h.ListLabels(c) })
}
func TestContactHandler_UpdateLabels_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
body := `{"labels":["tag1","tag2"]}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, body)
safeCallCov27(t, "UpdateLabels_DB", w, func() { h.UpdateLabels(c) })
}
func TestContactHandler_UpdateLabels_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, "bad")
h.UpdateLabels(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_DeleteCustomAttributes_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/custom_attributes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)})
safeCallCov27(t, "DeleteCustomAttributes_DB", w, func() { h.DeleteCustomAttributes(c) })
}
func TestContactHandler_DestroyCustomAttributes_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
body := `{"custom_attributes":["key1"]}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/destroy_custom_attributes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, body)
safeCallCov27(t, "DestroyCustomAttributes_DB", w, func() { h.DestroyCustomAttributes(c) })
}
func TestContactHandler_DestroyCustomAttributes_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
contact := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/"+uitoaCov27(contact.ID)+"/destroy_custom_attributes", map[string]string{"account_id": uitoaCov27(acc.ID), "contact_id": uitoaCov27(contact.ID)}, "bad")
h.DestroyCustomAttributes(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_Merge_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
c1 := seedContact_Cov19(t, db, acc.ID)
c2 := seedContact_Cov19(t, db, acc.ID)
h := newContactHandlerCov27(db)
body := `{"base_contact_id":` + uitoaCov27(c1.ID) + `,"mergee_contact_id":` + uitoaCov27(c2.ID) + `}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/merge", map[string]string{"account_id": uitoaCov27(acc.ID)}, body)
safeCallCov27(t, "Merge_DB", w, func() { h.Merge(c) })
}
func TestContactHandler_Merge_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/merge", map[string]string{"account_id": uitoaCov27(acc.ID)}, "bad")
h.Merge(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactHandler_DownloadExport_InvalidID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := newContactHandlerCov27(db)
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/contacts/export/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "export_id": "abc"})
h.DownloadExport(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
// ============================================================
// ConversationHandler Additional Tests (15 tests)
// ============================================================
func TestConversationHandler_Get_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ConvGet_DB", w, func() { h.Get(c) })
}
func TestConversationHandler_Get_InvalidConvID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": "abc"}, 1, acc.ID, "administrator")
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_UpdateLabels_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"labels":["tag1"]}`
c, w := ctxAuthBodyCov27("PATCH", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ConvUpdateLabels_DB", w, func() { h.UpdateLabels(c) })
}
func TestConversationHandler_GetLabels_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/labels", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ConvGetLabels_DB", w, func() { h.GetLabels(c) })
}
func TestConversationHandler_ListMessages_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/messages", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ListMessages_DB", w, func() { h.ListMessages(c) })
}
func TestConversationHandler_ToggleTyping_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"typing_status":"on","is_private":false}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/toggle_typing_status", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "ToggleTyping_DB", w, func() { h.ToggleTyping(c) })
}
func TestConversationHandler_UpdateLastSeen_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/update_last_seen", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "UpdateLastSeen_DB", w, func() { h.UpdateLastSeen(c) })
}
func TestConversationHandler_ListAttachments_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/attachments", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ListAttachments_DB", w, func() { h.ListAttachments(c) })
}
func TestConversationHandler_InboxAssistant_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/inbox_assistant", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "InboxAssistant_DB", w, func() { h.InboxAssistant(c) })
}
func TestConversationHandler_ReportingEvents_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/reporting_events", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ReportingEvents_DB", w, func() { h.ReportingEvents(c) })
}
func TestConversationHandler_TogglePriority_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"priority":"urgent"}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/toggle_priority", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "TogglePriority_DB", w, func() { h.TogglePriority(c) })
}
func TestConversationHandler_TogglePriority_EmptyBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/toggle_priority", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "TogglePriority_EmptyBody", w, func() { h.TogglePriority(c) })
}
func TestConversationHandler_AssignAgentBot_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
contact := seedContact_Cov19(t, db, acc.ID)
conv := seedConversation_Cov19(t, db, acc.ID, inbox.ID, contact.ID)
bot := seedAgentBot_Cov19(t, db, acc.ID)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
body := `{"assignee_id":` + uitoaCov27(bot.ID) + `,"assignee_type":"AgentBot"}`
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/"+uitoaCov27(conv.ID)+"/assign", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": uitoaCov27(conv.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "AssignAgentBot_DB", w, func() { h.AssignAgent(c) })
}
func TestConversationHandler_AssignAgent_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/1/assign", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": "1"}, 1, acc.ID, "administrator", "bad")
h.AssignAgent(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestConversationHandler_AssignTeam_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewConversationHandler(newConvSvcCov27(db), newMsgSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/conversations/1/assignments", map[string]string{"account_id": uitoaCov27(acc.ID), "conversation_id": "1"}, 1, acc.ID, "administrator", "bad")
h.AssignTeam(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
// ============================================================
// InboxHandler Additional Tests (15 tests)
// ============================================================
func TestInboxHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes", map[string]string{"account_id": uitoaCov27(acc.ID)})
h.List(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestInboxHandler_List_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/inboxes", map[string]string{"account_id": "abc"})
h.List(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Get_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID), map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
h.Get(c)
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}
func TestInboxHandler_Get_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/inboxes/1", map[string]string{"account_id": "abc", "inbox_id": "1"})
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_Get_InvalidInboxID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/abc", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": "abc"})
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_RegisterWebhook_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
body := `{"url":"https://example.com/webhook"}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/register_webhook", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)}, body)
safeCallCov27(t, "RegisterWebhook_DB", w, func() { h.RegisterWebhook(c) })
}
func TestInboxHandler_RegisterWebhook_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/inboxes/1/register_webhook", map[string]string{"account_id": "abc", "inbox_id": "1"}, `{}`)
h.RegisterWebhook(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_EnableWhatsAppCalling_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/enable_whatsapp_calling", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "EnableWhatsAppCalling_DB", w, func() { h.EnableWhatsAppCalling(c) })
}
func TestInboxHandler_DisableWhatsAppCalling_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/disable_whatsapp_calling", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "DisableWhatsAppCalling_DB", w, func() { h.DisableWhatsAppCalling(c) })
}
func TestInboxHandler_SetInboundCalls_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
body := `{"inbound_calls_enabled":true}`
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/set_inbound_calls", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)}, body)
safeCallCov27(t, "SetInboundCalls_DB", w, func() { h.SetInboundCalls(c) })
}
func TestInboxHandler_SetInboundCalls_InvalidInbox_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/abc/inboxes/abc/set_inbound_calls", map[string]string{"account_id": "abc", "inbox_id": "abc"}, `{}`)
h.SetInboundCalls(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_SetInboundCalls_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxBodyCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/set_inbound_calls", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)}, "bad")
h.SetInboundCalls(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestInboxHandler_DeleteAvatar_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("DELETE", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/avatar", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "DeleteAvatar_DB", w, func() { h.DeleteAvatar(c) })
}
func TestInboxHandler_ResetSecret_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("POST", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/reset_secret", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "ResetSecret_DB", w, func() { h.ResetSecret(c) })
}
func TestInboxHandler_ListCampaigns_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
inbox := seedInbox_Cov19(t, db, acc.ID)
h := NewInboxHandler(newInboxSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/inboxes/"+uitoaCov27(inbox.ID)+"/campaigns", map[string]string{"account_id": uitoaCov27(acc.ID), "inbox_id": uitoaCov27(inbox.ID)})
safeCallCov27(t, "ListCampaigns_DB", w, func() { h.ListCampaigns(c) })
}
// ============================================================
// ArticleHandler Additional Tests (10 tests)
// ============================================================
func TestArticleHandler_Get_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/"+uitoaCov27(art.ID), map[string]string{"portal_id": uitoaCov27(portal.ID), "id": uitoaCov27(art.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ArticleGet_DB", w, func() { h.Get(c) })
}
func TestArticleHandler_Get_InvalidArticleID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/abc", map[string]string{"portal_id": uitoaCov27(portal.ID), "id": "abc"}, 1, acc.ID, "administrator")
h.Get(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_Edit_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/"+uitoaCov27(art.ID)+"/edit", map[string]string{"portal_id": uitoaCov27(portal.ID), "id": uitoaCov27(art.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ArticleEdit_DB", w, func() { h.Edit(c) })
}
func TestArticleHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ArticleList_DB", w, func() { h.List(c) })
}
func TestArticleHandler_Search_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/articles/search?query=test", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ArticleSearch_DB", w, func() { h.Search(c) })
}
func TestArticleHandler_ListByCategory_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthCov27("GET", "/portals/"+uitoaCov27(portal.ID)+"/categories/"+uitoaCov27(cat.ID)+"/articles", map[string]string{"portal_id": uitoaCov27(portal.ID), "category_id": uitoaCov27(cat.ID)}, 1, acc.ID, "administrator")
safeCallCov27(t, "ListByCategory_DB", w, func() { h.ListByCategory(c) })
}
func TestArticleHandler_ListByCategory_InvalidCategoryID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxCov27("GET", "/portals/1/categories/abc/articles", map[string]string{"portal_id": "1", "category_id": "abc"})
h.ListByCategory(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_BulkTranslate_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
cat := seedCategory_Cov19(t, db, portal.ID, acc.ID)
art := seedArticle_Cov19(t, db, portal.ID, acc.ID, cat.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
body := `{"article_ids":[` + uitoaCov27(art.ID) + `],"target_locale":"es"}`
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_translate", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", body)
safeCallCov27(t, "BulkTranslate_DB", w, func() { h.BulkTranslate(c) })
}
func TestArticleHandler_BulkTranslate_InvalidBody_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
portal := seedPortal_Cov19(t, db, acc.ID)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxAuthBodyCov27("POST", "/portals/"+uitoaCov27(portal.ID)+"/articles/bulk_translate", map[string]string{"portal_id": uitoaCov27(portal.ID)}, 1, acc.ID, "administrator", "bad")
h.BulkTranslate(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestArticleHandler_StatusCounts_InvalidPortalID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewArticleHandler(newArticleSvcCov27(db))
c, w := ctxCov27("GET", "/portals/abc/articles/status_counts", map[string]string{"portal_id": "abc"})
h.StatusCounts(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
// ============================================================
// PlatformAppHandler Additional Tests (5 tests)
// ============================================================
func TestPlatformAppHandler_List_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
acc := seedAccount_Cov19(t, db)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/"+uitoaCov27(acc.ID)+"/platform_apps", map[string]string{"account_id": uitoaCov27(acc.ID)})
safeCallCov27(t, "PlatformAppList_DB", w, func() { h.List(c) })
}
func TestPlatformAppHandler_List_InvalidAccountID_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/api/v1/accounts/abc/platform_apps", map[string]string{"account_id": "abc"})
h.List(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestPlatformAppHandler_List_PlatformWide_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/platform/api/v1/apps", nil)
safeCallCov27(t, "PlatformAppList_PlatformWide", w, func() { h.List(c) })
}
func TestPlatformAppHandler_Get_DB_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/platform/api/v1/apps/1", map[string]string{"id": "1"})
safeCallCov27(t, "PlatformAppGet_DB", w, func() { h.Get(c) })
}
func TestPlatformAppHandler_Search_PlatformWide_Cov27(t *testing.T) {
db := newTestDB_Cov27(t)
h := NewPlatformAppHandler(newPlatformAppSvcCov27(db))
c, w := ctxCov27("GET", "/platform/api/v1/apps/search?q=test", nil)
safeCallCov27(t, "PlatformAppSearch_PlatformWide", w, func() { h.Search(c) })
}
// suppress unused import
var _ *gorm.DB