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 Oct 26, 2022
1 parent 82f4261 commit 2cf2d3d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
24 changes: 24 additions & 0 deletions auth.go
Expand Up @@ -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()
}
49 changes: 49 additions & 0 deletions 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)
}

0 comments on commit 2cf2d3d

Please sign in to comment.