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

122 lines
4.0 KiB
Plaintext

package v1
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/canned"
)
// mockDBProvider implements canned.DBProvider for validation-only tests.
// DB() returns nil — service methods are never reached since validation fails first.
type mockDBProvider struct{}
func (m *mockDBProvider) DB() *gorm.DB { return nil }
func setupCannedResponseRouter(handler *CannedResponseHandler) *gin.Engine {
gin.SetMode(gin.TestMode)
r := gin.New()
r.POST("/api/v1/accounts/:account_id/canned_responses", handler.Create)
r.GET("/api/v1/accounts/:account_id/canned_responses/:id", handler.Get)
r.PUT("/api/v1/accounts/:account_id/canned_responses/:id", handler.Update)
r.DELETE("/api/v1/accounts/:account_id/canned_responses/:id", handler.Delete)
r.GET("/api/v1/accounts/:account_id/canned_responses", handler.List)
r.GET("/api/v1/accounts/:account_id/canned_responses/search", handler.Search)
return r
}
func newTestCannedResponseHandler() *CannedResponseHandler {
svc := canned.NewCannedResponseService(&mockDBProvider{})
return NewCannedResponseHandler(svc)
}
func TestCannedResponseGet_InvalidID(t *testing.T) {
handler := newTestCannedResponseHandler()
router := setupCannedResponseRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/canned_responses/xyz", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCannedResponseCreate_InvalidJSON(t *testing.T) {
handler := newTestCannedResponseHandler()
router := setupCannedResponseRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/canned_responses", strings.NewReader("{invalid}"))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCannedResponseCreate_MissingShortCode(t *testing.T) {
handler := newTestCannedResponseHandler()
router := setupCannedResponseRouter(handler)
body := `{"content":"hello world"}`
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/canned_responses", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCannedResponseCreate_MissingContent(t *testing.T) {
handler := newTestCannedResponseHandler()
router := setupCannedResponseRouter(handler)
body := `{"short_code":"greeting"}`
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/canned_responses", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCannedResponseUpdate_InvalidID(t *testing.T) {
handler := newTestCannedResponseHandler()
router := setupCannedResponseRouter(handler)
body := `{"short_code":"greeting","content":"hello"}`
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/api/v1/accounts/1/canned_responses/xyz", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCannedResponseUpdate_InvalidJSON(t *testing.T) {
handler := newTestCannedResponseHandler()
router := setupCannedResponseRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/api/v1/accounts/1/canned_responses/1", strings.NewReader("{invalid}"))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCannedResponseDelete_InvalidID(t *testing.T) {
handler := newTestCannedResponseHandler()
router := setupCannedResponseRouter(handler)
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/canned_responses/xyz", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}