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.
107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
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)
|
|
}
|
|
}
|