131 lines
4.2 KiB
Go
131 lines
4.2 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
)
|
|
|
|
func setupInboxAgentBotRouter(handler *InboxHandler) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
r.GET("/api/v1/accounts/:account_id/inboxes/:inbox_id/agent_bot", handler.GetAgentBot)
|
|
r.DELETE("/api/v1/accounts/:account_id/inboxes/:inbox_id/avatar", handler.DeleteAvatar)
|
|
r.GET("/api/v1/accounts/:account_id/inboxes/:inbox_id/campaigns", handler.ListCampaigns)
|
|
return r
|
|
}
|
|
|
|
// --- GetAgentBot tests ---
|
|
|
|
func TestInboxHandler_GetAgentBot_InvalidAccountID(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/inboxes/1/agent_bot", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestInboxHandler_GetAgentBot_InvalidInboxID(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/inboxes/abc/agent_bot", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestInboxHandler_GetAgentBot_ValidIDs_ZeroService(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/inboxes/1/agent_bot", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
// Zero service will panic on method call; Recovery middleware catches it → 500
|
|
assert.True(t, w.Code == http.StatusUnprocessableEntity || w.Code == http.StatusOK)
|
|
}
|
|
|
|
// --- DeleteAvatar tests ---
|
|
|
|
func TestInboxHandler_DeleteAvatar_InvalidAccountID(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/abc/inboxes/1/avatar", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestInboxHandler_DeleteAvatar_InvalidInboxID(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/inboxes/abc/avatar", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestInboxHandler_DeleteAvatar_ValidIDs_ZeroService(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/inboxes/1/avatar", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
// Zero service will panic on method call; Recovery middleware catches it → 500
|
|
assert.True(t, w.Code == http.StatusUnprocessableEntity || w.Code == http.StatusOK)
|
|
}
|
|
|
|
// --- ListCampaigns tests ---
|
|
|
|
func TestInboxHandler_ListCampaigns_InvalidAccountID(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/inboxes/1/campaigns", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestInboxHandler_ListCampaigns_InvalidInboxID(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/inboxes/abc/campaigns", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestInboxHandler_ListCampaigns_ValidIDs_ZeroService(t *testing.T) {
|
|
handler := NewInboxHandler(&service.InboxService{})
|
|
router := setupInboxAgentBotRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/inboxes/1/campaigns", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
// Zero service will panic on method call; Recovery middleware catches it → 500
|
|
assert.True(t, w.Code == http.StatusUnprocessableEntity || w.Code == http.StatusOK)
|
|
}
|