Skip to content

Commit

Permalink
test: make sure empty values are sent
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatsu committed Jun 28, 2022
1 parent 7fc0e57 commit 38367d1
Showing 1 changed file with 106 additions and 9 deletions.
115 changes: 106 additions & 9 deletions usergroups_test.go
Original file line number Diff line number Diff line change
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,104 @@ 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."

emptyChannels := []string{}
presenceChannels := []string{"channel1", "channel2"}

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(&presenceChannels),
},
map[string]string{
"token": "testing-token",
"usergroup": "S0615G0KT",
"description": "Marketing gurus, PR experts and product advocates.",
"channels": "channel1,channel2",
},
},
{
[]UpdateUserGroupsOption{
UpdateUserGroupsOptionDescription(&emptyDescription),
UpdateUserGroupsOptionChannels(&emptyChannels),
},
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)
}
}
}

0 comments on commit 38367d1

Please sign in to comment.