From 2cf2d3de45f7b509a697bdef0ce586db61f3b0ca Mon Sep 17 00:00:00 2001 From: Stephen Wan Date: Wed, 26 Oct 2022 09:43:42 -0500 Subject: [PATCH] api/auth: add ListTeams for auth.teams.list --- auth.go | 24 ++++++++++++++++++++++++ auth_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 auth_test.go diff --git a/auth.go b/auth.go index f4f7f003a..f2ce89ead 100644 --- a/auth.go +++ b/auth.go @@ -38,3 +38,27 @@ func (api *Client) SendAuthRevokeContext(ctx context.Context, token string) (*Au return api.authRequest(ctx, "auth.revoke", values) } + +type listTeamsResponse struct { + Teams []Team `json:"teams"` + SlackResponse +} + +// ListTeams returns all workspaces a token can access. +func (api *Client) ListTeams() ([]Team, error) { + return api.ListTeamsContext(context.Background()) +} + +// ListTeams returns all workspaces a token can access with a custom context. +func (api *Client) ListTeamsContext(ctx context.Context) ([]Team, error) { + values := url.Values{ + "token": {api.token}, + } + + response := &listTeamsResponse{} + err := api.postMethod(ctx, "auth.teams.list", values, response) + if err != nil { + return nil, err + } + return response.Teams, response.Err() +} diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 000000000..911ad5526 --- /dev/null +++ b/auth_test.go @@ -0,0 +1,49 @@ +package slack + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func getTeamList(rw http.ResponseWriter, r *http.Request) { + rw.Header().Set("Content-Type", "application/json") + response := []byte(`{ + "ok": true, + "teams": [ + { + "name": "Shinichi's workspace", + "id": "T12345678" + }, + { + "name": "Migi's workspace", + "id": "T12345679" + } + ], + "response_metadata": { + "next_cursor": "dXNlcl9pZDo5MTQyOTI5Mzkz" + } +}`) + rw.Write(response) +} + +func TestListTeams(t *testing.T) { + http.HandleFunc("/auth.teams.list", getTeamList) + + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + + teams, err := api.ListTeams() + if err != nil { + t.Errorf("Unexpected error: %s", err) + return + } + + assert.Len(t, teams, 2) + assert.Equal(t, "T12345678", teams[0].ID) + assert.Equal(t, "Shinichi's workspace", teams[0].Name) + + assert.Equal(t, "T12345679", teams[1].ID) + assert.Equal(t, "Migi's workspace", teams[1].Name) +}