Something went wrong. Try again.
Cooperative email for PDS operators
Something went wrong. Try again.
2.3 kB · 81 lines
Go
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182// SPDX-License-Identifier: AGPL-3.0-or-later
package main
import ( "encoding/json" "net/http" "net/http/httptest" "testing")
func TestHandleGetLabels_MethodNotAllowed(t *testing.T) { handler := handleGetLabels(nil) req := httptest.NewRequest(http.MethodPost, "/entities/labels", nil) rr := httptest.NewRecorder() handler(rr, req) if rr.Code != http.StatusMethodNotAllowed { t.Fatalf("expected 405, got %d", rr.Code) }}
func TestHandleGetLabels_MissingParams(t *testing.T) { handler := handleGetLabels(nil)
tests := []struct { name string query string }{ {"no params", ""}, {"only entity_type", "?entity_type=SenderDID"}, {"only entity_id", "?entity_id=did:plc:abc"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/entities/labels"+tt.query, nil) rr := httptest.NewRecorder() handler(rr, req) if rr.Code != http.StatusBadRequest { t.Fatalf("expected 400, got %d", rr.Code) } }) }}
func TestLabelsResponseContract(t *testing.T) { // Verify our response struct matches what ospreyenforce.go expects. raw := `{"labels":{"auto_suspended":{"status":1},"highly_trusted":{"status":0}}}` var resp labelsResponse if err := json.Unmarshal([]byte(raw), &resp); err != nil { t.Fatalf("unmarshal: %v", err) } if resp.Labels["auto_suspended"].Status != 1 { t.Fatal("auto_suspended status should be 1") } if resp.Labels["highly_trusted"].Status != 0 { t.Fatal("highly_trusted status should be 0") }}
func TestLabelsResponseWithReasons(t *testing.T) { // The PG JSONB includes reasons — verify our struct handles the superset. raw := `{"labels":{"warming_period":{"status":1,"reasons":{"WarmingMember":{"pending":false}},"previous_states":[]}}}` var resp labelsResponse if err := json.Unmarshal([]byte(raw), &resp); err != nil { t.Fatalf("unmarshal: %v", err) } if resp.Labels["warming_period"].Status != 1 { t.Fatal("warming_period status should be 1") }}
func TestEmptyLabelsResponse(t *testing.T) { raw := `{"labels":{}}` var resp labelsResponse if err := json.Unmarshal([]byte(raw), &resp); err != nil { t.Fatalf("unmarshal: %v", err) } if len(resp.Labels) != 0 { t.Fatal("expected empty labels map") }}