feat(help-center): align article payloads
This commit is contained in:
@@ -37,12 +37,14 @@ func (s *ArticleHandlerTestSuite) SetupSuite() {
|
||||
s.Require().NoError(db.AutoMigrate(
|
||||
&model.Account{}, &model.User{}, &model.Portal{},
|
||||
&model.Category{}, &model.Folder{}, &model.Article{},
|
||||
&model.PortalMember{},
|
||||
))
|
||||
s.db = db
|
||||
|
||||
repo := repository.NewArticleRepo(db)
|
||||
svc := service.NewArticleService(repo)
|
||||
s.handler = NewArticleHandler(svc)
|
||||
portalRepo := repository.NewPortalRepo(db)
|
||||
s.handler = NewArticleHandler(svc, service.NewPortalService(portalRepo))
|
||||
|
||||
s.account = &model.Account{Name: "test-article-account"}
|
||||
s.Require().NoError(db.Create(s.account).Error)
|
||||
@@ -82,7 +84,7 @@ func (s *ArticleHandlerTestSuite) TestCreate_BadRequest_InvalidPortalID() {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestCreate_BadRequest_EmptyBody() {
|
||||
@@ -142,8 +144,6 @@ func (s *ArticleHandlerTestSuite) TestDelete_BadRequest_InvalidID() {
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestList_BadRequest_InvalidPortalID() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/portals/:portal_id/articles", s.handler.List)
|
||||
@@ -152,9 +152,7 @@ func (s *ArticleHandlerTestSuite) TestList_BadRequest_InvalidPortalID() {
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/portals/abc/articles", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
// List doesn't validate account_id, so invalid portal_id may pass or return empty
|
||||
// We just check it doesn't crash
|
||||
assert.True(s.T(), w.Code == http.StatusOK || w.Code == http.StatusBadRequest)
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestListByCategory_BadRequest_InvalidCategoryID() {
|
||||
@@ -176,7 +174,7 @@ func (s *ArticleHandlerTestSuite) TestSearch_BadRequest_InvalidPortalID() {
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/portals/abc/articles/search?q=test", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestStatusCounts_BadRequest_InvalidPortalID() {
|
||||
@@ -242,7 +240,12 @@ func (s *ArticleHandlerTestSuite) TestCreate_Success() {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusCreated, w.Code)
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var resp map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
payload := resp["payload"].(map[string]interface{})
|
||||
assert.Equal(s.T(), "test-article", payload["title"])
|
||||
assert.Equal(s.T(), "draft", payload["status"])
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestList_Success() {
|
||||
@@ -264,4 +267,141 @@ func (s *ArticleHandlerTestSuite) TestList_Success() {
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
}
|
||||
var resp map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
assert.Contains(s.T(), resp, "payload")
|
||||
meta := resp["meta"].(map[string]interface{})
|
||||
assert.Contains(s.T(), meta, "all_articles_count")
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestCreate_RawFrontendPayloadAndSlugPortal() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/portals/:portal_id/articles", s.handler.Create)
|
||||
|
||||
body := map[string]interface{}{
|
||||
"title": "Raw Article",
|
||||
"content": "raw content",
|
||||
"author_id": uint(7),
|
||||
"category_id": nil,
|
||||
"locale": "en",
|
||||
}
|
||||
b, _ := json.Marshal(body)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles", s.account.ID), bytes.NewBuffer(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var resp map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
payload := resp["payload"].(map[string]interface{})
|
||||
assert.Equal(s.T(), "Raw Article", payload["title"])
|
||||
assert.NotEmpty(s.T(), payload["slug"])
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestPatch_RawPayloadClearsDescription() {
|
||||
article := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "patch-article", Slug: "patch-article", Description: "old", Status: "draft"}
|
||||
s.Require().NoError(s.db.Create(article).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.PATCH("/api/v1/accounts/:account_id/portals/:portal_id/articles/:article_id", s.handler.Update)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PATCH", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles/%d", s.account.ID, article.ID), bytes.NewBufferString(`{"description":""}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var resp map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
payload := resp["payload"].(map[string]interface{})
|
||||
assert.Equal(s.T(), "", payload["description"])
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestDelete_ReturnsEmptyOK() {
|
||||
article := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "delete-article", Slug: "delete-article", Status: "draft"}
|
||||
s.Require().NoError(s.db.Create(article).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.DELETE("/api/v1/accounts/:account_id/portals/:portal_id/articles/:article_id", s.handler.Delete)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles/%d", s.account.ID, article.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestReorder_PositionsHashScoped() {
|
||||
article := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "reorder-article", Slug: "reorder-article", Status: "draft", Position: 1}
|
||||
s.Require().NoError(s.db.Create(article).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/portals/:portal_id/articles/reorder", s.handler.Reorder)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
body := fmt.Sprintf(`{"positions_hash":{"%d":30}}`, article.ID)
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles/reorder", s.account.ID), bytes.NewBufferString(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var updated model.Article
|
||||
s.Require().NoError(s.db.First(&updated, article.ID).Error)
|
||||
assert.Equal(s.T(), 30, updated.Position)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestBulkActions_FrontendRoutes() {
|
||||
category := &model.Category{AccountID: s.account.ID, PortalID: s.portal.ID, Name: "BulkCat", Slug: "bulk-cat", Locale: "en"}
|
||||
s.Require().NoError(s.db.Create(category).Error)
|
||||
a1 := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "bulk-one", Slug: "bulk-one", Status: "draft"}
|
||||
a2 := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "bulk-two", Slug: "bulk-two", Status: "draft"}
|
||||
s.Require().NoError(s.db.Create(a1).Error)
|
||||
s.Require().NoError(s.db.Create(a2).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.PATCH("/api/v1/accounts/:account_id/portals/:portal_id/articles/bulk_actions/update_status", s.handler.BulkUpdateStatus)
|
||||
r.PATCH("/api/v1/accounts/:account_id/portals/:portal_id/articles/bulk_actions/update_category", s.handler.BulkUpdateCategory)
|
||||
r.DELETE("/api/v1/accounts/:account_id/portals/:portal_id/articles/bulk_actions/delete_articles", s.handler.BulkDelete)
|
||||
|
||||
statusBody := fmt.Sprintf(`{"ids":[%d,%d],"status":"published"}`, a1.ID, a2.ID)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PATCH", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles/bulk_actions/update_status", s.account.ID), bytes.NewBufferString(statusBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
|
||||
categoryBody := fmt.Sprintf(`{"ids":[%d,%d],"category_id":%d}`, a1.ID, a2.ID, category.ID)
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("PATCH", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles/bulk_actions/update_category", s.account.ID), bytes.NewBufferString(categoryBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
|
||||
deleteBody := fmt.Sprintf(`{"ids":[%d,%d]}`, a1.ID, a2.ID)
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles/bulk_actions/delete_articles", s.account.ID), bytes.NewBufferString(deleteBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestBulkUpdateStatus_InvalidStatusReturnsChatwootError() {
|
||||
article := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "bulk-invalid", Slug: "bulk-invalid", Status: "draft"}
|
||||
s.Require().NoError(s.db.Create(article).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.PATCH("/api/v1/accounts/:account_id/portals/:portal_id/articles/bulk_actions/update_status", s.handler.BulkUpdateStatus)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
body := fmt.Sprintf(`{"ids":[%d],"status":"missing"}`, article.ID)
|
||||
req, _ := http.NewRequest("PATCH", fmt.Sprintf("/api/v1/accounts/%d/portals/test-portal/articles/bulk_actions/update_status", s.account.ID), bytes.NewBufferString(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusUnprocessableEntity, w.Code)
|
||||
var resp map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
assert.Contains(s.T(), resp, "error")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user