feat(help-center): expose public article lists

This commit is contained in:
2026-06-06 07:09:52 +08:00
parent 0aa0470ac9
commit 53434ab397
11 changed files with 298 additions and 10 deletions
@@ -277,6 +277,57 @@ func (s *ArticleHandlerTestSuite) TestList_Success() {
assert.Contains(s.T(), meta, "all_articles_count")
}
func (s *ArticleHandlerTestSuite) TestPublicList_WidgetPopularArticles() {
r := gin.New()
r.GET("/hc/:slug/:locale/articles.json", s.handler.PublicList)
author := &model.User{AccountID: s.account.ID, Name: "Article Author", DisplayName: "Writer", Email: "public-article-author@example.com", Password: "secret"}
s.Require().NoError(s.db.Create(author).Error)
category := &model.Category{AccountID: s.account.ID, PortalID: s.portal.ID, Name: "Guides", Slug: "guides", Locale: "en"}
s.Require().NoError(s.db.Create(category).Error)
low := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, CategoryID: &category.ID, AuthorID: &author.ID, Title: "Low Views", Slug: "public-low-views", Status: "published", Locale: "en", Views: 3, Content: "low"}
high := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, CategoryID: &category.ID, AuthorID: &author.ID, Title: "High Views", Slug: "public-high-views", Status: "published", Locale: "en", Views: 9, Content: "high"}
draft := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "Draft", Slug: "public-draft", Status: "draft", Locale: "en", Views: 99, Content: "draft"}
french := &model.Article{AccountID: s.account.ID, PortalID: s.portal.ID, Title: "French", Slug: "public-french", Status: "published", Locale: "fr", Views: 99, Content: "fr"}
s.Require().NoError(s.db.Create(low).Error)
s.Require().NoError(s.db.Create(high).Error)
s.Require().NoError(s.db.Create(draft).Error)
s.Require().NoError(s.db.Create(french).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/hc/test-portal/en/articles.json?page=1&sort=views&status=1&per_page=2", nil)
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"].([]interface{})
assert.Len(s.T(), payload, 2)
first := payload[0].(map[string]interface{})
second := payload[1].(map[string]interface{})
assert.Equal(s.T(), "High Views", first["title"])
assert.Equal(s.T(), "Low Views", second["title"])
assert.Equal(s.T(), "hc/test-portal/articles/public-high-views", first["link"])
assert.Equal(s.T(), "guides", first["category"].(map[string]interface{})["slug"])
assert.Equal(s.T(), "Writer", first["author"].(map[string]interface{})["available_name"])
meta := resp["meta"].(map[string]interface{})
assert.EqualValues(s.T(), 2, meta["articles_count"])
}
func (s *ArticleHandlerTestSuite) TestPublicList_ArchivedPortalNotFound() {
r := gin.New()
r.GET("/hc/:slug/:locale/articles.json", s.handler.PublicList)
portal := &model.Portal{AccountID: s.account.ID, Name: "Archived", Slug: "archived-public", Archived: true}
s.Require().NoError(s.db.Create(portal).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/hc/archived-public/en/articles.json", nil)
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusNotFound, w.Code)
}
func (s *ArticleHandlerTestSuite) TestCreate_RawFrontendPayloadAndSlugPortal() {
r := gin.New()
r.POST("/api/v1/accounts/:account_id/portals/:portal_id/articles", s.handler.Create)