142 lines
5.5 KiB
Go
142 lines
5.5 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"
|
|
)
|
|
|
|
func setupContactG3Router(handler *ContactHandler) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
router.Use(gin.Recovery()) // Recovery middleware so nil-service panics return 500 instead of crashing the test
|
|
router.GET("/api/v1/accounts/:id/contacts/active", handler.Active)
|
|
router.GET("/api/v1/accounts/:id/contacts/export", handler.Export)
|
|
router.POST("/api/v1/accounts/:id/contacts/import", handler.Import)
|
|
router.GET("/api/v1/accounts/:id/contacts/:contact_id/contactable_inboxes", handler.ContactableInboxes)
|
|
router.DELETE("/api/v1/accounts/:id/contacts/:contact_id/custom_attributes", handler.DeleteCustomAttributes)
|
|
return router
|
|
}
|
|
|
|
func TestContactActiveBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts/active?sort=name&page=1&page_size=25", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
var resp map[string]interface{}
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
assert.Contains(t, resp, "error")
|
|
}
|
|
|
|
func TestContactActiveSuccess(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/contacts/active?sort=name&page=1&page_size=25", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
// With a nil/zero service, the handler will panic on the nil repo pointer.
|
|
// Gin recovers panics and returns 500 by default — routing and param parsing
|
|
// succeeded, which is what we're testing.
|
|
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
|
|
}
|
|
|
|
func TestContactExportBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts/export", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
var resp map[string]interface{}
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
assert.Contains(t, resp, "error")
|
|
}
|
|
|
|
func TestContactImportBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/contacts/import", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
var resp map[string]interface{}
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
assert.Contains(t, resp, "error")
|
|
}
|
|
|
|
func TestContactImportMissingFile(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
// Send a POST with no multipart form — account_id is valid but file is missing
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/contacts/import", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
// Chatwoot returns 422 when import_file is missing.
|
|
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
|
|
var resp map[string]interface{}
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
assert.Contains(t, resp, "error")
|
|
}
|
|
|
|
func TestContactableInboxesBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts/1/contactable_inboxes", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactableInboxesBadContactID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/contacts/abc/contactable_inboxes", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestDeleteCustomAttributesBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/abc/contacts/1/custom_attributes", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestDeleteCustomAttributesBadContactID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
router := setupContactG3Router(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/contacts/abc/custom_attributes", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|