package v1 import ( "bytes" "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 setupLinearIntegrationRouter() *gin.Engine { gin.SetMode(gin.TestMode) r := gin.New() r.RedirectTrailingSlash = false handler := NewLinearIntegrationHandler(nil) integrations := r.Group("/api/v1/accounts/:account_id/integrations") RegisterLinearIntegrationRoutes(integrations, handler) return r } // ======================================== // LinearIntegration — param validation tests // ======================================== func TestLinearIntegration_Delete_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("DELETE", "/api/v1/accounts/abc/integrations/linear/", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestLinearIntegration_GetTeams_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/integrations/linear/teams", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestLinearIntegration_GetTeamEntities_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/integrations/linear/team_entities", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestLinearIntegration_CreateIssue_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() body, _ := json.Marshal(service.CreateIssueRequest{TeamID: "team1", Title: "Bug"}) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/integrations/linear/create_issue", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestLinearIntegration_CreateIssue_InvalidJSON(t *testing.T) { r := setupLinearIntegrationRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/accounts/1/integrations/linear/create_issue", bytes.NewReader([]byte("invalid json"))) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) assert.False(t, resp["success"].(bool)) } func TestLinearIntegration_LinkIssue_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() body, _ := json.Marshal(service.LinkIssueRequest{IssueID: "issue1", ConversationID: 1}) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/integrations/linear/link_issue", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestLinearIntegration_LinkIssue_InvalidJSON(t *testing.T) { r := setupLinearIntegrationRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/accounts/1/integrations/linear/link_issue", bytes.NewReader([]byte("invalid json"))) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) assert.False(t, resp["success"].(bool)) } func TestLinearIntegration_UnlinkIssue_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() body, _ := json.Marshal(service.UnlinkIssueRequest{IssueID: "issue1", ConversationID: 1}) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/integrations/linear/unlink_issue", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestLinearIntegration_SearchIssue_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/integrations/linear/search_issue", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestLinearIntegration_GetLinkedIssues_BadAccountID(t *testing.T) { r := setupLinearIntegrationRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/integrations/linear/linked_issues", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") }