From deccddbb3b2be01165661c8a5d1a19beb51c5778 Mon Sep 17 00:00:00 2001 From: Daniel Felizi Date: Sun, 5 Jul 2020 19:45:14 -0300 Subject: [PATCH] Add SetUserRealName for change user's realName --- users.go | 35 +++++++++++++++++++++++++++++++++++ users_test.go | 18 +++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/users.go b/users.go index 364958e85..cdef42421 100644 --- a/users.go +++ b/users.go @@ -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 "" diff --git a/users_test.go b/users_test.go index ab1697e4f..34502a376 100644 --- a/users_test.go +++ b/users_test.go @@ -302,7 +302,7 @@ func TestGetUserByEmail(t *testing.T) { } } -func TestUserCustomStatus(t *testing.T) { +func TestUserProfileSet(t *testing.T) { up := &UserProfile{} setUserProfile := newProfileHandler(up) @@ -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 `, 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) {