diff --git a/orgs.go b/orgs.go index 8001e86..6649795 100644 --- a/orgs.go +++ b/orgs.go @@ -3,6 +3,7 @@ package forge import ( "context" "iter" + "net/http" "dappco.re/go/core/forge/types" ) @@ -49,6 +50,43 @@ func (s *OrgService) RemoveMember(ctx context.Context, org, username string) err return s.client.Delete(ctx, path) } +// ListBlockedUsers returns all users blocked by an organisation. +func (s *OrgService) ListBlockedUsers(ctx context.Context, org string) ([]types.User, error) { + path := ResolvePath("/api/v1/orgs/{org}/blocks", pathParams("org", org)) + return ListAll[types.User](ctx, s.client, path, nil) +} + +// IterBlockedUsers returns an iterator over all users blocked by an organisation. +func (s *OrgService) IterBlockedUsers(ctx context.Context, org string) iter.Seq2[types.User, error] { + path := ResolvePath("/api/v1/orgs/{org}/blocks", pathParams("org", org)) + return ListIter[types.User](ctx, s.client, path, nil) +} + +// IsBlocked reports whether a user is blocked by an organisation. +func (s *OrgService) IsBlocked(ctx context.Context, org, username string) (bool, error) { + path := ResolvePath("/api/v1/orgs/{org}/blocks/{username}", pathParams("org", org, "username", username)) + resp, err := s.client.doJSON(ctx, "GET", path, nil, nil) + if err != nil { + if IsNotFound(err) { + return false, nil + } + return false, err + } + return resp.StatusCode == http.StatusNoContent, nil +} + +// Block blocks a user within an organisation. +func (s *OrgService) Block(ctx context.Context, org, username string) error { + path := ResolvePath("/api/v1/orgs/{org}/blocks/{username}", pathParams("org", org, "username", username)) + return s.client.Put(ctx, path, nil, nil) +} + +// Unblock unblocks a user within an organisation. +func (s *OrgService) Unblock(ctx context.Context, org, username string) error { + path := ResolvePath("/api/v1/orgs/{org}/blocks/{username}", pathParams("org", org, "username", username)) + return s.client.Delete(ctx, path) +} + // ListUserOrgs returns all organisations for a user. func (s *OrgService) ListUserOrgs(ctx context.Context, username string) ([]types.Organization, error) { path := ResolvePath("/api/v1/users/{username}/orgs", pathParams("username", username)) diff --git a/orgs_test.go b/orgs_test.go index 04dbdb3..eac1653 100644 --- a/orgs_test.go +++ b/orgs_test.go @@ -86,3 +86,90 @@ func TestOrgService_ListMembers_Good(t *testing.T) { t.Errorf("got username=%q, want %q", members[0].UserName, "alice") } } + +func TestOrgService_ListBlockedUsers_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/orgs/core/blocks" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("X-Total-Count", "2") + json.NewEncoder(w).Encode([]types.User{ + {ID: 1, UserName: "alice"}, + {ID: 2, UserName: "bob"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + blocked, err := f.Orgs.ListBlockedUsers(context.Background(), "core") + if err != nil { + t.Fatal(err) + } + if len(blocked) != 2 { + t.Fatalf("got %d blocked users, want 2", len(blocked)) + } + if blocked[0].UserName != "alice" { + t.Errorf("got username=%q, want %q", blocked[0].UserName, "alice") + } +} + +func TestOrgService_Block_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPut { + t.Errorf("expected PUT, got %s", r.Method) + } + if r.URL.Path != "/api/v1/orgs/core/blocks/alice" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Orgs.Block(context.Background(), "core", "alice"); err != nil { + t.Fatal(err) + } +} + +func TestOrgService_Unblock_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodDelete { + t.Errorf("expected DELETE, got %s", r.Method) + } + if r.URL.Path != "/api/v1/orgs/core/blocks/alice" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Orgs.Unblock(context.Background(), "core", "alice"); err != nil { + t.Fatal(err) + } +} + +func TestOrgService_IsBlocked_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/orgs/core/blocks/alice" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + blocked, err := f.Orgs.IsBlocked(context.Background(), "core", "alice") + if err != nil { + t.Fatal(err) + } + if !blocked { + t.Fatal("got blocked=false, want true") + } +}