From 38367d1ba7a2afb4321629f6b52fe1653927f900 Mon Sep 17 00:00:00 2001 From: Jumpei Matsuda Date: Wed, 29 Jun 2022 01:14:30 +0900 Subject: [PATCH] test: make sure empty values are sent --- usergroups_test.go | 115 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 9 deletions(-) diff --git a/usergroups_test.go b/usergroups_test.go index 5b92dcdf1..d98ca257e 100644 --- a/usergroups_test.go +++ b/usergroups_test.go @@ -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)) } @@ -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", @@ -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) + } + } +}