Files
gochat/internal/router/router_test.go
T

117 lines
3.4 KiB
Go

package router
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/config"
"github.com/gochat/gochat/internal/middleware"
)
func TestRegisterRoutesBootsWithChatwootParityConflictGroups(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
RegisterRoutes(
engine,
nil,
nil,
nil,
&Handlers{},
nil,
nil,
&config.JWTConfig{},
middleware.CORSConfig{},
nil,
)
routes := map[string]bool{}
for _, route := range engine.Routes() {
routes[route.Method+" "+route.Path] = true
}
expected := []string{
"GET /app",
"GET /app/*params",
"GET /api/v1/accounts/:account_id/captain/assistants/tools",
"GET /api/v1/accounts/:account_id/captain/assistants/:assistant_id",
"GET /api/v1/widget/conversations",
"GET /api/v1/widget/conversations/toggle_status",
"PUT /public/api/v1/inboxes/:inbox_id/contacts/:contact_id/conversations/:conversation_id/messages/:message_id",
"GET /hc/:slug",
"GET /hc/:slug/sitemap.xml",
"GET /hc/:slug/:locale",
"GET /hc/:slug/:locale/search",
"GET /hc/:slug/:locale/articles.json",
"GET /hc/:slug/:locale/categories.json",
"GET /hc/:slug/:locale/categories/:category_slug",
"GET /hc/:slug/articles/:article_slug",
"GET /api/v2/accounts/:account_id/reports/summary",
"GET /api/v2/accounts/:account_id/live_reports/grouped_conversation_metrics",
"GET /webhooks/twitter",
"POST /webhooks/twitter",
"POST /webhooks/telegram/:bot_token",
"POST /webhooks/line/:line_channel_id",
"POST /webhooks/sms/:phone_number",
"GET /webhooks/whatsapp/:phone_number",
"POST /webhooks/whatsapp/:phone_number",
"POST /webhooks/tiktok",
"POST /webhooks/shopify",
}
for _, key := range expected {
if !routes[key] {
t.Fatalf("expected route %s to be registered", key)
}
}
}
func TestDashboardIndexServesChatwootShell(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Setenv("INSTALLATION_NAME", "GoChat Test")
t.Setenv("FRONTEND_URL", "https://app.example.test/")
t.Setenv("HELPCENTER_URL", "https://help.example.test/")
engine := gin.New()
engine.GET("/app", dashboardIndex)
engine.GET("/app/*params", dashboardIndex)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/app/accounts/1/conversations/42", nil)
req.Header.Set("Accept", "text/html")
engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", recorder.Code)
}
body := recorder.Body.String()
if !strings.Contains(body, `<div id="app"></div>`) {
t.Fatalf("expected dashboard app mount in response: %s", body)
}
if !strings.Contains(body, `"hostURL":"https://app.example.test"`) {
t.Fatalf("expected frontend URL in chatwoot config: %s", body)
}
}
func TestDashboardIndexRejectsJSONLikeChatwoot(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
engine.GET("/app", dashboardIndex)
engine.GET("/app/*params", dashboardIndex)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/app/accounts/1/conversations/42", nil)
req.Header.Set("Accept", "application/json")
engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusNotAcceptable {
t.Fatalf("expected 406, got %d", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "Please use API routes instead of dashboard routes for JSON requests") {
t.Fatalf("expected Chatwoot dashboard JSON error, got %s", recorder.Body.String())
}
}