From 8d4bc404fd9d0884121408a6acb99dff8bfc4a9b 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 | 35 +++++++++++++++++++++++++++++++++++ auth_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 auth_test.go diff --git a/auth.go b/auth.go index f4f7f003a..ec463551c 100644 --- a/auth.go +++ b/auth.go @@ -38,3 +38,38 @@ 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 +} + +type ListTeamsParameters struct { + Limit int + Cursor string +} + +// ListTeams returns all workspaces a token can access. +func (api *Client) ListTeams(params ListTeamsParameters) ([]Team, *ListTeamsParameters, error) { + return api.ListTeamsContext(context.Background(), params) +} + +// ListTeams returns all workspaces a token can access with a custom context. +func (api *Client) ListTeamsContext(ctx context.Context, params ListTeamsParameters) ([]Team, *ListTeamsParameters, error) { + values := url.Values{ + "token": {api.token}, + } + if params.Cursor != "" { + values.Add("cursor", params.Cursor) + } + + response := &listTeamsResponse{} + err := api.postMethod(ctx, "auth.teams.list", values, response) + if err != nil { + return nil, nil, err + } + + params.Cursor = response.ResponseMetadata.Cursor + + return response.Teams, ¶ms, response.Err() +} diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 000000000..295f110b6 --- /dev/null +++ b/auth_test.go @@ -0,0 +1,51 @@ +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, params, err := api.ListTeams(ListTeamsParameters{}) + 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) + + assert.Equal(t, "dXNlcl9pZDo5MTQyOTI5Mzkz", params.Cursor) +}