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

Fixes updateUserGroup cannot clear the description and the default channels #1082

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 57 additions & 12 deletions usergroups.go
Expand Up @@ -183,32 +183,77 @@ func (api *Client) GetUserGroupsContext(ctx context.Context, options ...GetUserG
return response.UserGroups, nil
}

// UpdateUserGroupsOption options for the UpdateUserGroup method call.
type UpdateUserGroupsOption func(*UpdateUserGroupsParams)

// UpdateUserGroupsOptionName change the name of the User Group (default: empty, so it's no-op)
func UpdateUserGroupsOptionName(name string) UpdateUserGroupsOption {
return func(params *UpdateUserGroupsParams) {
params.Name = name
}
}

// UpdateUserGroupsOptionHandle change the handle of the User Group (default: empty, so it's no-op)
func UpdateUserGroupsOptionHandle(handle string) UpdateUserGroupsOption {
return func(params *UpdateUserGroupsParams) {
params.Handle = handle
}
}

// UpdateUserGroupsOptionDescription change the description of the User Group. (default: nil, so it's no-op)
func UpdateUserGroupsOptionDescription(description *string) UpdateUserGroupsOption {
return func(params *UpdateUserGroupsParams) {
params.Description = description
}
}

// UpdateUserGroupsOptionChannels change the default channels of the User Group. (default: unspecified, so it's no-op)
func UpdateUserGroupsOptionChannels(channels []string) UpdateUserGroupsOption {
return func(params *UpdateUserGroupsParams) {
params.Channels = &channels
}
}

// UpdateUserGroupsParams contains arguments for UpdateUserGroup method call
type UpdateUserGroupsParams struct {
Name string
Handle string
Description *string
Channels *[]string
}

// UpdateUserGroup will update an existing user group
func (api *Client) UpdateUserGroup(userGroup UserGroup) (UserGroup, error) {
return api.UpdateUserGroupContext(context.Background(), userGroup)
func (api *Client) UpdateUserGroup(userGroupID string, options ...UpdateUserGroupsOption) (UserGroup, error) {
return api.UpdateUserGroupContext(context.Background(), userGroupID, options...)
}

// UpdateUserGroupContext will update an existing user group with a custom context
func (api *Client) UpdateUserGroupContext(ctx context.Context, userGroup UserGroup) (UserGroup, error) {
func (api *Client) UpdateUserGroupContext(ctx context.Context, userGroupID string, options ...UpdateUserGroupsOption) (UserGroup, error) {
params := UpdateUserGroupsParams{}

for _, opt := range options {
opt(&params)
}

values := url.Values{
"token": {api.token},
"usergroup": {userGroup.ID},
"usergroup": {userGroupID},
}

if userGroup.Name != "" {
values["name"] = []string{userGroup.Name}
if params.Name != "" {
values["name"] = []string{params.Name}
}

if userGroup.Handle != "" {
values["handle"] = []string{userGroup.Handle}
if params.Handle != "" {
values["handle"] = []string{params.Handle}
}

if userGroup.Description != "" {
values["description"] = []string{userGroup.Description}
if params.Description != nil {
values["description"] = []string{*params.Description}
}

if len(userGroup.Prefs.Channels) > 0 {
values["channels"] = []string{strings.Join(userGroup.Prefs.Channels, ",")}
if params.Channels != nil {
values["channels"] = []string{strings.Join(*params.Channels, ",")}
}

response, err := api.userGroupRequest(ctx, "usergroups.update", values)
Expand Down
112 changes: 103 additions & 9 deletions usergroups_test.go
Expand Up @@ -45,16 +45,11 @@ func newUserGroupsHandler() *userGroupsHandler {
}
}

func (ugh *userGroupsHandler) accumulateFormValue(k string, r *http.Request) {
if v := r.FormValue(k); v != "" {
ugh.gotParams[k] = v
}
}

func (ugh *userGroupsHandler) handler(w http.ResponseWriter, r *http.Request) {
ugh.accumulateFormValue("name", r)
ugh.accumulateFormValue("description", r)
ugh.accumulateFormValue("handle", r)
r.ParseForm()
for k, v := range r.Form {
ugh.gotParams[k] = v[0]
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(ugh.response))
}
Expand All @@ -73,6 +68,7 @@ func TestCreateUserGroup(t *testing.T) {
Description: "Marketing gurus, PR experts and product advocates.",
Handle: "marketing-team"},
map[string]string{
"token": "testing-token",
"name": "Marketing Team",
"description": "Marketing gurus, PR experts and product advocates.",
"handle": "marketing-team",
Expand Down Expand Up @@ -184,3 +180,101 @@ func TestGetUserGroups(t *testing.T) {
t.Errorf("Got %#v, want %#v", userGroups[0], S0614TZR7)
}
}

func updateUserGroupsHandler() *userGroupsHandler {
return &userGroupsHandler{
gotParams: make(map[string]string),
response: `{
"ok": true,
"usergroup": {
"id": "S0615G0KT",
"team_id": "T060RNRCH",
"is_usergroup": true,
"name": "Marketing Team",
"description": "Marketing gurus, PR experts and product advocates.",
"handle": "marketing-team",
"is_external": false,
"date_create": 1446746793,
"date_update": 1446746793,
"date_delete": 0,
"auto_type": null,
"created_by": "U060RNRCZ",
"updated_by": "U060RNRCZ",
"deleted_by": null,
"prefs": {
"channels": [
"channel1",
"channel2"
],
"groups": [

]
},
"user_count": 0
}
}`,
}
}
func TestUpdateUserGroup(t *testing.T) {
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))

emptyDescription := ""
presenceDescription := "Marketing gurus, PR experts and product advocates."

tests := []struct {
options []UpdateUserGroupsOption
wantParams map[string]string
}{
{
[]UpdateUserGroupsOption{
UpdateUserGroupsOptionName("Marketing Team"),
UpdateUserGroupsOptionHandle("marketing-team"),
},
map[string]string{
"token": "testing-token",
"usergroup": "S0615G0KT",
"name": "Marketing Team",
"handle": "marketing-team",
},
},
{
[]UpdateUserGroupsOption{
UpdateUserGroupsOptionDescription(&presenceDescription),
UpdateUserGroupsOptionChannels([]string{"channel1", "channel2"}),
},
map[string]string{
"token": "testing-token",
"usergroup": "S0615G0KT",
"description": "Marketing gurus, PR experts and product advocates.",
"channels": "channel1,channel2",
},
},
{
[]UpdateUserGroupsOption{
UpdateUserGroupsOptionDescription(&emptyDescription),
UpdateUserGroupsOptionChannels([]string{}),
},
map[string]string{
"token": "testing-token",
"usergroup": "S0615G0KT",
"description": "",
"channels": "",
},
},
}

var rh *userGroupsHandler
http.HandleFunc("/usergroups.update", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })

for i, test := range tests {
rh = updateUserGroupsHandler()
_, err := api.UpdateUserGroup("S0615G0KT", test.options...)
if err != nil {
t.Fatalf("%d: Unexpected error: %s", i, err)
}
if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
}
}
}