feat(orgs): add organisation block helpers
All checks were successful
Security Scan / security (push) Successful in 12s
Test / test (push) Successful in 1m14s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 23:39:46 +00:00
parent ed4a2c7b0c
commit 9f140e3f4f
2 changed files with 125 additions and 0 deletions

38
orgs.go
View file

@ -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))

View file

@ -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")
}
}