Skip to content

Commit

Permalink
api/auth: add ListTeams for auth.teams.list
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwan-opal committed Dec 7, 2022
1 parent 7a7c3e2 commit 26ae199
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
34 changes: 34 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,37 @@ 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.
// More info: https://api.slack.com/methods/admin.teams.list
func (api *Client) ListTeams(params ListTeamsParameters) ([]Team, string, 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, string, 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, "", err
}

return response.Teams, response.ResponseMetadata.Cursor, response.Err()
}
51 changes: 51 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -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, cursor, 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", cursor)
}

0 comments on commit 26ae199

Please sign in to comment.