Files
gochat/internal/handler/api/v1/linear_integration_handler_test.go
T

212 lines
6.5 KiB
Go

package v1
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
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()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/integrations/linear/create_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_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)
// account_id passes, ShouldBindJSON fails
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()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/integrations/linear/link_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_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)
// account_id passes, ShouldBindJSON fails
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()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/integrations/linear/unlink_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_UnlinkIssue_InvalidJSON(t *testing.T) {
r := setupLinearIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/integrations/linear/unlink_issue", bytes.NewReader([]byte("invalid json")))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
// account_id passes, ShouldBindJSON fails
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_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_SearchIssue_BlankQuery(t *testing.T) {
r := setupLinearIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/integrations/linear/search_issue", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
assert.Equal(t, "Specify search string with parameter q", resp["error"])
}
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")
}