153 lines
4.9 KiB
Go
153 lines
4.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
)
|
|
|
|
// setupInboxMemberActionRouter creates a test router with inbox member-action routes.
|
|
// We use nil InboxService since we only test param validation (bad IDs).
|
|
// Body/service-level logic is covered in service tests with real DB.
|
|
func setupInboxMemberActionRouter(handler *InboxHandler) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/set_agent_bot", handler.SetAgentBot)
|
|
r.GET("/api/v1/accounts/:account_id/inboxes/:inbox_id/health", handler.Health)
|
|
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/sync_templates", handler.SyncTemplates)
|
|
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/register_webhook", handler.RegisterWebhook)
|
|
return r
|
|
}
|
|
|
|
func newNilInboxHandler() *InboxHandler {
|
|
return NewInboxHandler(&service.InboxService{})
|
|
}
|
|
|
|
// parseJSONResponse extracts the "error" key from a JSON response body.
|
|
func parseJSONError(body []byte) string {
|
|
var resp map[string]interface{}
|
|
json.Unmarshal(body, &resp)
|
|
if v, ok := resp["error"]; ok {
|
|
return v.(string)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ========================================
|
|
// SetAgentBot — param validation tests
|
|
// ========================================
|
|
|
|
func TestInboxSetAgentBot_BadAccountID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/inboxes/5/set_agent_bot", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid account id")
|
|
}
|
|
|
|
func TestInboxSetAgentBot_BadInboxID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/inboxes/xyz/set_agent_bot", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid inbox id")
|
|
}
|
|
|
|
// ========================================
|
|
// Health — param validation tests
|
|
// ========================================
|
|
|
|
func TestInboxHealth_BadAccountID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/inboxes/5/health", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid account id")
|
|
}
|
|
|
|
func TestInboxHealth_BadInboxID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/inboxes/xyz/health", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid inbox id")
|
|
}
|
|
|
|
// ========================================
|
|
// SyncTemplates — param validation tests
|
|
// ========================================
|
|
|
|
func TestInboxSyncTemplates_BadAccountID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/inboxes/5/sync_templates", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid account id")
|
|
}
|
|
|
|
func TestInboxSyncTemplates_BadInboxID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/inboxes/xyz/sync_templates", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid inbox id")
|
|
}
|
|
|
|
// ========================================
|
|
// RegisterWebhook — param validation tests
|
|
// ========================================
|
|
|
|
func TestInboxRegisterWebhook_BadAccountID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/inboxes/5/register_webhook", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid account id")
|
|
}
|
|
|
|
func TestInboxRegisterWebhook_BadInboxID(t *testing.T) {
|
|
handler := newNilInboxHandler()
|
|
router := setupInboxMemberActionRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/inboxes/xyz/register_webhook", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Contains(t, parseJSONError(w.Body.Bytes()), "invalid inbox id")
|
|
}
|