feat(orgs): add organisation membership check
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
9f263a77ec
commit
204696915f
2 changed files with 35 additions and 0 deletions
13
orgs.go
13
orgs.go
|
|
@ -65,6 +65,19 @@ func (s *OrgService) RemoveMember(ctx context.Context, org, username string) err
|
|||
return s.client.Delete(ctx, path)
|
||||
}
|
||||
|
||||
// IsMember reports whether a user is a member of an organisation.
|
||||
func (s *OrgService) IsMember(ctx context.Context, org, username string) (bool, error) {
|
||||
path := ResolvePath("/api/v1/orgs/{org}/members/{username}", pathParams("org", org, "username", username))
|
||||
resp, err := s.client.doJSON(ctx, http.MethodGet, path, nil, nil)
|
||||
if err != nil {
|
||||
if IsNotFound(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return resp.StatusCode == http.StatusNoContent, nil
|
||||
}
|
||||
|
||||
// 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))
|
||||
|
|
|
|||
22
orgs_test.go
22
orgs_test.go
|
|
@ -88,6 +88,28 @@ func TestOrgService_ListMembers_Good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestOrgService_IsMember_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/members/alice" {
|
||||
t.Errorf("wrong path: %s", r.URL.Path)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
f := NewForge(srv.URL, "tok")
|
||||
member, err := f.Orgs.IsMember(context.Background(), "core", "alice")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !member {
|
||||
t.Fatal("got member=false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrgService_ListPublicMembers_Good(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue