Skip to content

Commit

Permalink
Merge pull request #692 from isdzulqor/add-get-users-info
Browse files Browse the repository at this point in the history
add support for multi-users in users.info
  • Loading branch information
ollieparsley authored Apr 19, 2020
2 parents a01e031 + 055f592 commit 9d8728c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/url"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -184,6 +185,7 @@ type TeamIdentity struct {
type userResponseFull struct {
Members []User `json:"members,omitempty"`
User `json:"user,omitempty"`
Users []User `json:"users,omitempty"`
UserPresence
SlackResponse
Metadata ResponseMetadata `json:"response_metadata"`
Expand Down Expand Up @@ -252,6 +254,26 @@ func (api *Client) GetUserInfoContext(ctx context.Context, user string) (*User,
return &response.User, nil
}

// GetUsersInfo will retrieve the complete multi-users information
func (api *Client) GetUsersInfo(users ...string) (*[]User, error) {
return api.GetUsersInfoContext(context.Background(), users...)
}

// GetUsersInfoContext will retrieve the complete multi-users information with a custom context
func (api *Client) GetUsersInfoContext(ctx context.Context, users ...string) (*[]User, error) {
values := url.Values{
"token": {api.token},
"users": {strings.Join(users, ",")},
"include_locale": {strconv.FormatBool(true)},
}

response, err := api.userRequest(ctx, "users.info", values)
if err != nil {
return nil, err
}
return &response.Users, nil
}

// GetUsersOption options for the GetUsers method call.
type GetUsersOption func(*UserPagination)

Expand Down
37 changes: 37 additions & 0 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func getTestUser() User {
return getTestUserWithId("UXXXXXXXX")
}

func getTestUsers() []User {
return []User{
getTestUserWithId("UYYYYYYYY"),
getTestUserWithId("UZZZZZZZZ"),
}
}

func getUserIdentity(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response := []byte(`{
Expand Down Expand Up @@ -120,6 +127,18 @@ func getUserInfo(rw http.ResponseWriter, r *http.Request) {
rw.Write(response)
}

func getUsersInfo(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(struct {
Ok bool `json:"ok"`
Users []User `json:"users"`
}{
Ok: true,
Users: getTestUsers(),
})
rw.Write(response)
}

func getUserByEmail(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(struct {
Expand Down Expand Up @@ -248,6 +267,24 @@ func TestGetUserInfo(t *testing.T) {
}
}

func TestGetUsersInfo(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/users.info", getUsersInfo)
expectedUsers := getTestUsers()

once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))

user, err := api.GetUsersInfo("UYYYYYYYY", "UZZZZZZZZ")
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if !reflect.DeepEqual(expectedUsers, *user) {
t.Fatal(ErrIncorrectResponse)
}
}

func TestGetUserByEmail(t *testing.T) {
http.HandleFunc("/users.lookupByEmail", getUserByEmail)
expectedUser := getTestUser()
Expand Down

0 comments on commit 9d8728c

Please sign in to comment.