Debian Codegraph @

usergroups_test.go

raw symbols SCIP
1 package slack
2
3 import (
4 "net/http"
5 "reflect"
6 "testing"
7 )
8
9 type userGroupsHandler struct {
10 gotParams map[string]string
11 response string
12 }
13
14 func newUserGroupsHandler() *userGroupsHandler {
15 return &userGroupsHandler{
16 gotParams: make(map[string]string),
17 response: `{
18 "ok": true,
19 "usergroup": {
20 "id": "S0615G0KT",
21 "team_id": "T060RNRCH",
22 "is_usergroup": true,
23 "name": "Marketing Team",
24 "description": "Marketing gurus, PR experts and product advocates.",
25 "handle": "marketing-team",
26 "is_external": false,
27 "date_create": 1446746793,
28 "date_update": 1446746793,
29 "date_delete": 0,
30 "auto_type": null,
31 "created_by": "U060RNRCZ",
32 "updated_by": "U060RNRCZ",
33 "deleted_by": null,
34 "prefs": {
35 "channels": [
36
37 ],
38 "groups": [
39
40 ]
41 },
42 "user_count": 0
43 }
44 }`,
45 }
46 }
47
48 func (ugh *userGroupsHandler) handler(w http.ResponseWriter, r *http.Request) {
49 r.ParseForm()
50 for k, v := range r.Form {
51 ugh.gotParams[k] = v[0]
52 }
53 w.Header().Set("Content-Type", "application/json")
54 w.Write([]byte(ugh.response))
55 }
56
57 func TestCreateUserGroup(t *testing.T) {
58 once.Do(startServer)
59 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
60
61 tests := []struct {
62 userGroup UserGroup
63 wantParams map[string]string
64 }{
65 {
66 UserGroup{
67 Name: "Marketing Team",
68 Description: "Marketing gurus, PR experts and product advocates.",
69 Handle: "marketing-team"},
70 map[string]string{
71 "token": "testing-token",
72 "name": "Marketing Team",
73 "description": "Marketing gurus, PR experts and product advocates.",
74 "handle": "marketing-team",
75 },
76 },
77 }
78
79 var rh *userGroupsHandler
80 http.HandleFunc("/usergroups.create", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
81
82 for i, test := range tests {
83 rh = newUserGroupsHandler()
84 _, err := api.CreateUserGroup(test.userGroup)
85 if err != nil {
86 t.Fatalf("%d: Unexpected error: %s", i, err)
87 }
88 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
89 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
90 }
91 }
92 }
93
94 func getUserGroups(rw http.ResponseWriter, r *http.Request) {
95 rw.Header().Set("Content-Type", "application/json")
96 response := []byte(`{
97 "ok": true,
98 "usergroups": [
99 {
100 "id": "S0614TZR7",
101 "team_id": "T060RNRCH",
102 "is_usergroup": true,
103 "name": "Team Admins",
104 "description": "A group of all Administrators on your team.",
105 "handle": "admins",
106 "is_external": false,
107 "date_create": 1446598059,
108 "date_update": 1446670362,
109 "date_delete": 0,
110 "auto_type": "admin",
111 "created_by": "USLACKBOT",
112 "updated_by": "U060RNRCZ",
113 "deleted_by": null,
114 "prefs": {
115 "channels": [
116 "channel1",
117 "channel2"
118 ],
119 "groups": [
120 "group1",
121 "group2",
122 "group3"
123 ]
124 },
125 "users": [
126 "user1",
127 "user2"
128 ],
129 "user_count": 2
130 }
131 ]
132 }`)
133 rw.Write(response)
134 }
135
136 func TestGetUserGroups(t *testing.T) {
137 http.HandleFunc("/usergroups.list", getUserGroups)
138
139 once.Do(startServer)
140 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
141
142 userGroups, err := api.GetUserGroups(GetUserGroupsOptionIncludeUsers(true))
143 if err != nil {
144 t.Errorf("Unexpected error: %s", err)
145 return
146 }
147
148 // t.Fatal refers to -> t.Errorf & return
149 if len(userGroups) != 1 {
150 t.Fatal(ErrIncorrectResponse)
151 }
152
153 S0614TZR7 := UserGroup{
154 ID: "S0614TZR7",
155 TeamID: "T060RNRCH",
156 IsUserGroup: true,
157 Name: "Team Admins",
158 Description: "A group of all Administrators on your team.",
159 Handle: "admins",
160 IsExternal: false,
161 DateCreate: 1446598059,
162 DateUpdate: 1446670362,
163 DateDelete: 0,
164 AutoType: "admin",
165 CreatedBy: "USLACKBOT",
166 UpdatedBy: "U060RNRCZ",
167 DeletedBy: "",
168 Prefs: UserGroupPrefs{
169 Channels: []string{"channel1", "channel2"},
170 Groups: []string{"group1", "group2", "group3"},
171 },
172 Users: []string{
173 "user1",
174 "user2",
175 },
176 UserCount: 2,
177 }
178
179 if !reflect.DeepEqual(userGroups[0], S0614TZR7) {
180 t.Errorf("Got %#v, want %#v", userGroups[0], S0614TZR7)
181 }
182 }
183
184 func updateUserGroupsHandler() *userGroupsHandler {
185 return &userGroupsHandler{
186 gotParams: make(map[string]string),
187 response: `{
188 "ok": true,
189 "usergroup": {
190 "id": "S0615G0KT",
191 "team_id": "T060RNRCH",
192 "is_usergroup": true,
193 "name": "Marketing Team",
194 "description": "Marketing gurus, PR experts and product advocates.",
195 "handle": "marketing-team",
196 "is_external": false,
197 "date_create": 1446746793,
198 "date_update": 1446746793,
199 "date_delete": 0,
200 "auto_type": null,
201 "created_by": "U060RNRCZ",
202 "updated_by": "U060RNRCZ",
203 "deleted_by": null,
204 "prefs": {
205 "channels": [
206 "channel1",
207 "channel2"
208 ],
209 "groups": [
210
211 ]
212 },
213 "user_count": 0
214 }
215 }`,
216 }
217 }
218 func TestUpdateUserGroup(t *testing.T) {
219 once.Do(startServer)
220 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
221
222 emptyDescription := ""
223 presenceDescription := "Marketing gurus, PR experts and product advocates."
224
225 tests := []struct {
226 options []UpdateUserGroupsOption
227 wantParams map[string]string
228 }{
229 {
230 []UpdateUserGroupsOption{
231 UpdateUserGroupsOptionName("Marketing Team"),
232 UpdateUserGroupsOptionHandle("marketing-team"),
233 },
234 map[string]string{
235 "token": "testing-token",
236 "usergroup": "S0615G0KT",
237 "name": "Marketing Team",
238 "handle": "marketing-team",
239 },
240 },
241 {
242 []UpdateUserGroupsOption{
243 UpdateUserGroupsOptionDescription(&presenceDescription),
244 UpdateUserGroupsOptionChannels([]string{"channel1", "channel2"}),
245 },
246 map[string]string{
247 "token": "testing-token",
248 "usergroup": "S0615G0KT",
249 "description": "Marketing gurus, PR experts and product advocates.",
250 "channels": "channel1,channel2",
251 },
252 },
253 {
254 []UpdateUserGroupsOption{
255 UpdateUserGroupsOptionDescription(&emptyDescription),
256 UpdateUserGroupsOptionChannels([]string{}),
257 },
258 map[string]string{
259 "token": "testing-token",
260 "usergroup": "S0615G0KT",
261 "description": "",
262 "channels": "",
263 },
264 },
265 }
266
267 var rh *userGroupsHandler
268 http.HandleFunc("/usergroups.update", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
269
270 for i, test := range tests {
271 rh = updateUserGroupsHandler()
272 _, err := api.UpdateUserGroup("S0615G0KT", test.options...)
273 if err != nil {
274 t.Fatalf("%d: Unexpected error: %s", i, err)
275 }
276 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
277 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
278 }
279 }
280 }