Files
gochat/internal/handler/api/v1/contact_handler_test.go_BAK
T
2026-06-04 15:44:48 +08:00

117 lines
3.7 KiB
Plaintext

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 setupContactRouter(handler *ContactHandler) *gin.Engine {
gin.SetMode(gin.TestMode)
router := gin.New()
router.GET("/api/v1/accounts/:id/contacts", handler.List)
router.GET("/api/v1/accounts/:id/contacts/:contact_id", handler.Get)
router.POST("/api/v1/accounts/:id/contacts", handler.Create)
router.PUT("/api/v1/accounts/:id/contacts/:contact_id", handler.Update)
router.DELETE("/api/v1/accounts/:id/contacts/:contact_id", handler.Delete)
router.GET("/api/v1/accounts/:id/contacts/search", handler.Search)
return router
}
func TestContactListBadAccountID(t *testing.T) {
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
router := setupContactRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts", 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 TestContactGetBadAccountID(t *testing.T) {
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
router := setupContactRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts/1", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactGetBadContactID(t *testing.T) {
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
router := setupContactRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/contacts/abc", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactCreateBadAccountID(t *testing.T) {
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
router := setupContactRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/contacts", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestContactUpdateBadIDs(t *testing.T) {
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
router := setupContactRouter(handler)
// Bad account ID
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/api/v1/accounts/abc/contacts/1", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
// Bad contact ID
w2 := httptest.NewRecorder()
req2, _ := http.NewRequest("PUT", "/api/v1/accounts/1/contacts/abc", nil)
router.ServeHTTP(w2, req2)
assert.Equal(t, http.StatusBadRequest, w2.Code)
}
func TestContactDeleteBadIDs(t *testing.T) {
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
router := setupContactRouter(handler)
// Bad account ID
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/abc/contacts/1", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
// Bad contact ID
w2 := httptest.NewRecorder()
req2, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/contacts/abc", nil)
router.ServeHTTP(w2, req2)
assert.Equal(t, http.StatusBadRequest, w2.Code)
}
func TestContactSearchBadAccountID(t *testing.T) {
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
router := setupContactRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts/search?q=test", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}