Update usergroups and add tests
Matt Whiteley authored 7 years ago
Matt Whiteley committed 7 years ago
2 | 2 | import ( |
3 | 3 | "errors" |
4 | 4 | "net/url" |
5 | "strings" | |
5 | 6 | ) |
6 | 7 | |
7 | // User contains all the information of a user group | |
8 | // UserGroup contains all the information of a user group | |
8 | 9 | type UserGroup struct { |
9 | 10 | ID string `json:"id"` |
11 | TeamID string `json:"team_id"` | |
12 | IsUserGroup bool `json:"is_usergroup"` | |
10 | 13 | Name string `json:"name"` |
11 | TeamId string `json:"team_id"` | |
12 | IsUserGroup bool `json:"is_usergroup"` | |
13 | 14 | Description string `json:"description"` |
14 | 15 | Handle string `json:"handle"` |
15 | 16 | IsExternal bool `json:"is_external"` |
17 | DateCreate int `json:"date_create"` | |
18 | DateUpdate int `json:"date_update"` | |
19 | DateDelete int `json:"date_delete"` | |
16 | 20 | AutoType string `json:"auto_type"` |
17 | 21 | CreatedBy string `json:"created_by"` |
18 | 22 | UpdatedBy string `json:"updated_by"` |
22 | 26 | Groups []string `json:"groups"` |
23 | 27 | } `json:"prefs"` |
24 | 28 | Users []string |
25 | UserCount int `json:"user_count"` | |
29 | UserCount int `json:"user_count,string"` | |
26 | 30 | } |
27 | 31 | |
28 | 32 | type userGroupResponseFull struct { |
44 | 48 | return response, nil |
45 | 49 | } |
46 | 50 | |
51 | // CreateUserGroup creates a new user group | |
52 | func (api *Client) CreateUserGroup(userGroup UserGroup) (UserGroup, error) { | |
53 | values := url.Values{ | |
54 | "token": {api.config.token}, | |
55 | "name": {userGroup.Name}, | |
56 | } | |
57 | ||
58 | if userGroup.Handle != "" { | |
59 | values["handle"] = []string{userGroup.Handle} | |
60 | } | |
61 | ||
62 | if userGroup.Description != "" { | |
63 | values["description"] = []string{userGroup.Description} | |
64 | } | |
65 | ||
66 | if len(userGroup.Prefs.Channels) > 0 { | |
67 | values["channels"] = []string{strings.Join(userGroup.Prefs.Channels, ",")} | |
68 | } | |
69 | ||
70 | response, err := userGroupRequest("usergroups.create", values, api.debug) | |
71 | if err != nil { | |
72 | return UserGroup{}, err | |
73 | } | |
74 | return response.UserGroup, nil | |
75 | } | |
76 | ||
77 | // DisableUserGroup disables an existing user group | |
78 | func (api *Client) DisableUserGroup(userGroup string) (UserGroup, error) { | |
79 | values := url.Values{ | |
80 | "token": {api.config.token}, | |
81 | "usergroup": {userGroup}, | |
82 | } | |
83 | ||
84 | response, err := userGroupRequest("usergroups.disable", values, api.debug) | |
85 | if err != nil { | |
86 | return UserGroup{}, err | |
87 | } | |
88 | return response.UserGroup, nil | |
89 | } | |
90 | ||
91 | // EnableUserGroup enables an existing user group | |
92 | func (api *Client) EnableUserGroup(userGroup string) (UserGroup, error) { | |
93 | values := url.Values{ | |
94 | "token": {api.config.token}, | |
95 | "usergroup": {userGroup}, | |
96 | } | |
97 | ||
98 | response, err := userGroupRequest("usergroups.enable", values, api.debug) | |
99 | if err != nil { | |
100 | return UserGroup{}, err | |
101 | } | |
102 | return response.UserGroup, nil | |
103 | } | |
104 | ||
47 | 105 | // GetUserGroups returns a list of user groups for the team |
48 | 106 | func (api *Client) GetUserGroups() ([]UserGroup, error) { |
49 | 107 | values := url.Values{ |
50 | 108 | "token": {api.config.token}, |
51 | 109 | } |
110 | ||
52 | 111 | response, err := userGroupRequest("usergroups.list", values, api.debug) |
53 | 112 | if err != nil { |
54 | 113 | return nil, err |
56 | 115 | return response.UserGroups, nil |
57 | 116 | } |
58 | 117 | |
118 | // UpdateUserGroup will update an existing user group | |
59 | 119 | func (api *Client) UpdateUserGroup(userGroup UserGroup) (UserGroup, error) { |
60 | 120 | values := url.Values{ |
61 | 121 | "token": {api.config.token}, |
81 | 141 | return response.UserGroup, nil |
82 | 142 | } |
83 | 143 | |
144 | // GetUserGroupMembers will retrieve the current list of users in a group | |
84 | 145 | func (api *Client) GetUserGroupMembers(userGroup string) ([]string, error) { |
85 | 146 | values := url.Values{ |
86 | 147 | "token": {api.config.token}, |
87 | 148 | "usergroup": {userGroup}, |
88 | 149 | } |
150 | ||
89 | 151 | response, err := userGroupRequest("usergroups.users.list", values, api.debug) |
90 | 152 | if err != nil { |
91 | 153 | return []string{}, err |
93 | 155 | return response.Users, nil |
94 | 156 | } |
95 | 157 | |
158 | // UpdateUserGroupMembers will update the members of an existing user group | |
96 | 159 | func (api *Client) UpdateUserGroupMembers(userGroup string, members string) (UserGroup, error) { |
97 | 160 | values := url.Values{ |
98 | 161 | "token": {api.config.token}, |
0 | package slack | |
1 | ||
2 | import ( | |
3 | "net/http" | |
4 | "reflect" | |
5 | "testing" | |
6 | ) | |
7 | ||
8 | type userGroupsHandler struct { | |
9 | gotParams map[string]string | |
10 | response string | |
11 | } | |
12 | ||
13 | func newUserGroupsHandler() *userGroupsHandler { | |
14 | return &userGroupsHandler{ | |
15 | gotParams: make(map[string]string), | |
16 | response: `{ | |
17 | "ok": true, | |
18 | "usergroup": { | |
19 | "id": "S0615G0KT", | |
20 | "team_id": "T060RNRCH", | |
21 | "is_usergroup": true, | |
22 | "name": "Marketing Team", | |
23 | "description": "Marketing gurus, PR experts and product advocates.", | |
24 | "handle": "marketing-team", | |
25 | "is_external": false, | |
26 | "date_create": 1446746793, | |
27 | "date_update": 1446746793, | |
28 | "date_delete": 0, | |
29 | "auto_type": null, | |
30 | "created_by": "U060RNRCZ", | |
31 | "updated_by": "U060RNRCZ", | |
32 | "deleted_by": null, | |
33 | "prefs": { | |
34 | "channels": [ | |
35 | ||
36 | ], | |
37 | "groups": [ | |
38 | ||
39 | ] | |
40 | }, | |
41 | "user_count": "0" | |
42 | } | |
43 | }`, | |
44 | } | |
45 | } | |
46 | ||
47 | func (ugh *userGroupsHandler) accumulateFormValue(k string, r *http.Request) { | |
48 | if v := r.FormValue(k); v != "" { | |
49 | ugh.gotParams[k] = v | |
50 | } | |
51 | } | |
52 | ||
53 | func (ugh *userGroupsHandler) handler(w http.ResponseWriter, r *http.Request) { | |
54 | ugh.accumulateFormValue("name", r) | |
55 | ugh.accumulateFormValue("description", r) | |
56 | ugh.accumulateFormValue("handle", r) | |
57 | w.Header().Set("Content-Type", "application/json") | |
58 | w.Write([]byte(ugh.response)) | |
59 | } | |
60 | ||
61 | func TestCreateUserGroup(t *testing.T) { | |
62 | once.Do(startServer) | |
63 | SLACK_API = "http://" + serverAddr + "/" | |
64 | api := New("testing-token") | |
65 | ||
66 | tests := []struct { | |
67 | userGroup UserGroup | |
68 | wantParams map[string]string | |
69 | }{ | |
70 | { | |
71 | UserGroup{ | |
72 | Name: "Marketing Team", | |
73 | Description: "Marketing gurus, PR experts and product advocates.", | |
74 | Handle: "marketing-team"}, | |
75 | map[string]string{ | |
76 | "name": "Marketing Team", | |
77 | "description": "Marketing gurus, PR experts and product advocates.", | |
78 | "handle": "marketing-team", | |
79 | }, | |
80 | }, | |
81 | } | |
82 | ||
83 | var rh *userGroupsHandler | |
84 | http.HandleFunc("/usergroups.create", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) | |
85 | ||
86 | for i, test := range tests { | |
87 | rh = newUserGroupsHandler() | |
88 | _, err := api.CreateUserGroup(test.userGroup) | |
89 | if err != nil { | |
90 | t.Fatalf("%d: Unexpected error: %s", i, err) | |
91 | } | |
92 | if !reflect.DeepEqual(rh.gotParams, test.wantParams) { | |
93 | t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams) | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | func getUserGroups(rw http.ResponseWriter, r *http.Request) { | |
99 | rw.Header().Set("Content-Type", "application/json") | |
100 | response := []byte(`{ | |
101 | "ok": true, | |
102 | "usergroups": [ | |
103 | { | |
104 | "id": "S0614TZR7", | |
105 | "team_id": "T060RNRCH", | |
106 | "is_usergroup": true, | |
107 | "name": "Team Admins", | |
108 | "description": "A group of all Administrators on your team.", | |
109 | "handle": "admins", | |
110 | "is_external": false, | |
111 | "date_create": 1446598059, | |
112 | "date_update": 1446670362, | |
113 | "date_delete": 0, | |
114 | "auto_type": "admin", | |
115 | "created_by": "USLACKBOT", | |
116 | "updated_by": "U060RNRCZ", | |
117 | "deleted_by": null, | |
118 | "prefs": { | |
119 | "channels": [ | |
120 | ||
121 | ], | |
122 | "groups": [ | |
123 | ||
124 | ] | |
125 | }, | |
126 | "user_count": "2" | |
127 | } | |
128 | ] | |
129 | }`) | |
130 | rw.Write(response) | |
131 | } | |
132 | ||
133 | func TestGetUserGroups(t *testing.T) { | |
134 | http.HandleFunc("/usergroups.list", getUserGroups) | |
135 | ||
136 | once.Do(startServer) | |
137 | SLACK_API = "http://" + serverAddr + "/" | |
138 | api := New("testing-token") | |
139 | ||
140 | userGroups, err := api.GetUserGroups() | |
141 | if err != nil { | |
142 | t.Errorf("Unexpected error: %s", err) | |
143 | return | |
144 | } | |
145 | ||
146 | // t.Fatal refers to -> t.Errorf & return | |
147 | if len(userGroups) != 1 { | |
148 | t.Fatal(ErrIncorrectResponse) | |
149 | } | |
150 | ||
151 | S0614TZR7 := UserGroup{ | |
152 | ID: "S0614TZR7", | |
153 | TeamID: "T060RNRCH", | |
154 | IsUserGroup: true, | |
155 | Name: "Team Admins", | |
156 | Description: "A group of all Administrators on your team.", | |
157 | Handle: "admins", | |
158 | IsExternal: false, | |
159 | DateCreate: 1446598059, | |
160 | DateUpdate: 1446670362, | |
161 | DateDelete: 0, | |
162 | AutoType: "admin", | |
163 | CreatedBy: "USLACKBOT", | |
164 | UpdatedBy: "U060RNRCZ", | |
165 | DeletedBy: "", | |
166 | Prefs: struct { | |
167 | Channels []string "json:\"channels\"" | |
168 | Groups []string "json:\"groups\"" | |
169 | }{Channels: []string{}, Groups: []string{}}, | |
170 | UserCount: 2, | |
171 | } | |
172 | ||
173 | if !reflect.DeepEqual(userGroups[0], S0614TZR7) { | |
174 | t.Errorf("Got %#v, want %#v", userGroups[0], S0614TZR7) | |
175 | } | |
176 | } |