feat(help-center): expose public portal search
This commit is contained in:
@@ -180,6 +180,71 @@ func (s *ArticleHandlerTestSuite) TestSearch_BadRequest_InvalidPortalID() {
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestPublicSearch_ReturnsPublishedLocaleSearchArticlePayload() {
|
||||
portal := &model.Portal{AccountID: s.account.ID, Name: "Search Portal", Slug: "search-portal"}
|
||||
s.Require().NoError(s.db.Create(portal).Error)
|
||||
category := &model.Category{AccountID: s.account.ID, PortalID: portal.ID, Name: "Billing", Slug: "billing", Locale: "en"}
|
||||
s.Require().NoError(s.db.Create(category).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Article{AccountID: s.account.ID, PortalID: portal.ID, CategoryID: &category.ID, Title: "Billing setup", Slug: "billing-setup", Content: "# Billing\nUse the billing portal to update invoices and cards.", Status: "published", Locale: "en"}).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Article{AccountID: s.account.ID, PortalID: portal.ID, CategoryID: &category.ID, Title: "Billing draft", Slug: "billing-draft", Content: "billing hidden", Status: "draft", Locale: "en"}).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Article{AccountID: s.account.ID, PortalID: portal.ID, CategoryID: &category.ID, Title: "Facturation", Slug: "facturation", Content: "billing french", Status: "published", Locale: "fr"}).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/hc/:slug/:locale/search", s.handler.PublicSearch)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/hc/search-portal/en/search?query=%20billing%20", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var resp map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
payload := resp["payload"].([]any)
|
||||
s.Require().Len(payload, 1)
|
||||
item := payload[0].(map[string]any)
|
||||
assert.Equal(s.T(), "Billing setup", item["title"])
|
||||
assert.Equal(s.T(), "/hc/search-portal/articles/billing-setup", item["link"])
|
||||
assert.EqualValues(s.T(), category.ID, item["category_id"])
|
||||
assert.Contains(s.T(), item["content"], "billing portal")
|
||||
assert.NotContains(s.T(), item, "status")
|
||||
meta := resp["meta"].(map[string]any)
|
||||
assert.EqualValues(s.T(), 1, meta["articles_count"])
|
||||
assert.EqualValues(s.T(), 1, meta["current_page"])
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestPublicSearch_EmptyQueryReturnsEmptyPayload() {
|
||||
portal := &model.Portal{AccountID: s.account.ID, Name: "Empty Search Portal", Slug: "empty-search-portal"}
|
||||
s.Require().NoError(s.db.Create(portal).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Article{AccountID: s.account.ID, PortalID: portal.ID, Title: "Published Searchable", Slug: "published-searchable", Content: "searchable", Status: "published", Locale: "en"}).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/hc/:slug/:locale/search", s.handler.PublicSearch)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/hc/empty-search-portal/en/search?query=%20", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var resp map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
assert.Empty(s.T(), resp["payload"].([]any))
|
||||
assert.EqualValues(s.T(), 0, resp["meta"].(map[string]any)["articles_count"])
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestPublicSearch_NotFoundForArchivedPortal() {
|
||||
portal := &model.Portal{AccountID: s.account.ID, Name: "Archived Search Portal", Slug: "archived-search-portal", Archived: true}
|
||||
s.Require().NoError(s.db.Create(portal).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/hc/:slug/:locale/search", s.handler.PublicSearch)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/hc/archived-search-portal/en/search?query=billing", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestStatusCounts_BadRequest_InvalidPortalID() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/portals/:portal_id/articles/status_counts", s.handler.StatusCounts)
|
||||
|
||||
Reference in New Issue
Block a user