feat(api): enhance community retrieval with flexible ID handling
CI / changes (push) Successful in 6s
CI / commitlint (push) Skipped
CI / openapi (push) Skipped
CI / web (push) Skipped
CI / go (push) Successful in 1m16s
CI / bird2 (push) Successful in 14s
CI / release (push) Successful in 4m5s

Updated the GetCommunity function to accept both UUIDs and community titles for improved flexibility in community retrieval. Added error handling for invalid ID formats and adjusted related functions to ensure consistent behavior across memory and PostgreSQL storage. This change enhances the API's usability by allowing more intuitive community lookups.
This commit is contained in:
Denozordec
2026-07-23 11:30:46 +07:00
parent ff6efec4c5
commit fd3a217cbe
4 changed files with 162 additions and 18 deletions
@@ -0,0 +1,106 @@
package httpapi
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestListCommunityPrefixesByIDAndLabel(t *testing.T) {
srv, err := New(Options{
InsecureDev: true,
SeedDemo: true,
})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
tenant, _, _, _, _ := srv.Store().DemoIDs()
mustSetTestAPIKeys(t, srv, "vwkey|"+tenant+"|viewer")
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
client := ts.Client()
base := ts.URL
reqList, _ := http.NewRequest(http.MethodGet, base+"/v1/communities?limit=10", nil)
reqList.Header.Set("Authorization", "Bearer vwkey")
respList, err := client.Do(reqList)
if err != nil {
t.Fatal(err)
}
defer func() { _ = respList.Body.Close() }()
if respList.StatusCode != http.StatusOK {
b, _ := io.ReadAll(respList.Body)
t.Fatalf("communities status %d: %s", respList.StatusCode, b)
}
var listBody struct {
Items []struct {
ID string `json:"id"`
Community string `json:"community"`
Title string `json:"title"`
} `json:"items"`
}
if err := json.NewDecoder(respList.Body).Decode(&listBody); err != nil {
t.Fatal(err)
}
if len(listBody.Items) == 0 {
t.Fatal("expected seeded community")
}
comm := listBody.Items[0]
assertPrefixesOK := func(t *testing.T, path string) {
t.Helper()
req, _ := http.NewRequest(http.MethodGet, base+path, nil)
req.Header.Set("Authorization", "Bearer vwkey")
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
t.Fatalf("%s status %d: %s", path, resp.StatusCode, b)
}
var body struct {
Items []map[string]any `json:"items"`
Prefixes []string `json:"prefixes"`
HasMore bool `json:"has_more"`
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
t.Fatal(err)
}
if body.Items == nil {
t.Fatalf("%s: expected items array (got nil)", path)
}
if body.Prefixes == nil {
t.Fatalf("%s: expected prefixes array (got nil)", path)
}
}
// UUID id
assertPrefixesOK(t, "/v1/communities/"+comm.ID+"/prefixes?limit=100")
// Community string (legacy / autocomplete label without title)
assertPrefixesOK(t, "/v1/communities/"+url.PathEscape(comm.Community)+"/prefixes?limit=100")
if comm.Title != "" {
// Full Base UI {value,label} display string
label := comm.Community + " · " + comm.Title
assertPrefixesOK(t, "/v1/communities/"+url.PathEscape(label)+"/prefixes?limit=100")
}
req404, _ := http.NewRequest(http.MethodGet, base+"/v1/communities/missing-community/prefixes", nil)
req404.Header.Set("Authorization", "Bearer vwkey")
resp404, err := client.Do(req404)
if err != nil {
t.Fatal(err)
}
defer func() { _ = resp404.Body.Close() }()
if resp404.StatusCode != http.StatusNotFound {
b, _ := io.ReadAll(resp404.Body)
t.Fatalf("expected 404, got %d: %s", resp404.StatusCode, b)
}
}
+4 -1
View File
@@ -57,8 +57,8 @@ func (s *Server) registerCRUDRoutes(m *http.ServeMux) {
m.HandleFunc("GET /communities", s.handleListComm)
m.HandleFunc("POST /communities", s.handlePostComm)
m.HandleFunc("GET /communities/{id}", s.handleGetComm)
m.HandleFunc("GET /communities/{id}/prefixes", s.handleListCommPrefixes)
m.HandleFunc("GET /communities/{id}", s.handleGetComm)
m.HandleFunc("PATCH /communities/{id}", s.handlePatchComm)
m.HandleFunc("DELETE /communities/{id}", s.handleDeleteComm)
@@ -232,6 +232,9 @@ func writePostgresStoreErr(w http.ResponseWriter, err error) bool {
case "23505":
writeProblem(w, http.StatusConflict, "Conflict", "resource already exists")
return true
case "22P02":
writeProblem(w, http.StatusBadRequest, "Bad Request", "invalid id format")
return true
}
return false
}