Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetUserRealName for change user's realName #755

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,41 @@ func (api *Client) DeleteUserPhotoContext(ctx context.Context) (err error) {
return response.Err()
}

// SetUserRealName changes the currently authenticated user's realName
//
// For more information see SetUserRealNameContextWithUser
func (api *Client) SetUserRealName(realName string) error {
return api.SetUserRealNameContextWithUser(context.Background(), realName, realName)
}

// SetUserRealNameContextWithUser will set a real name for the provided user with a custom context
func (api *Client) SetUserRealNameContextWithUser(ctx context.Context, user, realName string) error {
profile, err := json.Marshal(
&struct {
RealName string `json:"real_name"`
}{
RealName: realName,
},
)

if err != nil {
return err
}

values := url.Values{
"user": {user},
"token": {api.token},
"profile": {string(profile)},
}

response := &userResponseFull{}
if err = api.postMethod(ctx, "users.profile.set", values, response); err != nil {
return err
}

return response.Err()
}

// SetUserCustomStatus will set a custom status and emoji for the currently
// authenticated user. If statusEmoji is "" and statusText is not, the Slack API
// will automatically set it to ":speech_balloon:". Otherwise, if both are ""
Expand Down
18 changes: 17 additions & 1 deletion users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestGetUserByEmail(t *testing.T) {
}
}

func TestUserCustomStatus(t *testing.T) {
func TestUserProfileSet(t *testing.T) {
up := &UserProfile{}

setUserProfile := newProfileHandler(up)
Expand All @@ -317,6 +317,22 @@ func TestUserCustomStatus(t *testing.T) {

up.RealName = "Test User"
testSetUserCustomStatusWithUser(api, "Test User", up, t)

up.RealName = "Real Name Test"
testSetUserRealName(api, up, t)
}

func testSetUserRealName(api *Client, up *UserProfile, t *testing.T) {
const (
realName = "Real Name Test"
)
if err := api.SetUserRealName(realName); err != nil {
t.Fatalf(`SetUserRealName(%q) = %#v, want <nil>`, realName, err)
}

if up.RealName != realName {
t.Fatalf(`UserProfile.RealName = %q, want %q`, up.RealName, realName)
}
}

func testSetUserCustomStatus(api *Client, up *UserProfile, t *testing.T) {
Expand Down