1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use crate::usergroups::usergroup::Usergroup;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct ListRequest {
10 pub include_count: Option<bool>,
11 pub include_disabled: Option<bool>,
12 pub include_users: Option<bool>,
13 pub team_id: Option<String>,
14}
15
16#[skip_serializing_none]
17#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
18pub struct ListResponse {
19 pub ok: bool,
20 pub error: Option<String>,
21 pub response_metadata: Option<ResponseMetadata>,
22 pub usergroups: Option<Vec<Usergroup>>,
23}
24
25pub async fn list<T>(
26 client: &T,
27 param: &ListRequest,
28 bot_token: &str,
29) -> Result<ListResponse, Error>
30where
31 T: SlackWebAPIClient,
32{
33 let url = get_slack_url("usergroups.list");
34 let json = serde_json::to_string(¶m)?;
35
36 client
37 .post_json(&url, &json, bot_token)
38 .await
39 .and_then(|result| {
40 serde_json::from_str::<ListResponse>(&result).map_err(Error::SerdeJsonError)
41 })
42}
43
44#[cfg(test)]
45mod test {
46 use super::*;
47 use crate::http_client::MockSlackWebAPIClient;
48 use crate::usergroups::usergroup::Pref;
49
50 #[test]
51 fn convert_request() {
52 let request = ListRequest {
53 include_count: Some(true),
54 include_disabled: Some(true),
55 include_users: Some(true),
56 team_id: Some("T1234567890".to_string()),
57 };
58 let json = r##"{
59 "include_count": true,
60 "include_disabled": true,
61 "include_users": true,
62 "team_id": "T1234567890"
63}"##;
64
65 let j = serde_json::to_string_pretty(&request).unwrap();
66 assert_eq!(json, j);
67
68 let s = serde_json::from_str::<ListRequest>(json).unwrap();
69 assert_eq!(request, s);
70 }
71
72 #[test]
73 fn convert_response() {
74 let response = ListResponse {
75 ok: true,
76 usergroups: Some(vec![
77 Usergroup {
78 id: Some("S0615G0KT".to_string()),
79 team_id: Some("T060RNRCH".to_string()),
80 is_usergroup: Some(true),
81 name: Some("Marketing Team".to_string()),
82 description: Some(
83 "Marketing gurus, PR experts and product advocates.".to_string(),
84 ),
85 handle: Some("marketing-team".to_string()),
86 is_external: Some(false),
87 date_create: Some(1446746793),
88 date_update: Some(1446746793),
89 date_delete: Some(0),
90 auto_type: Some("".to_string()),
91 created_by: Some("U060RNRCZ".to_string()),
92 updated_by: Some("U060RNRCZ".to_string()),
93 deleted_by: Some("".to_string()),
94 prefs: Some(Pref {
95 channels: Some(vec![]),
96 groups: Some(vec![]),
97 }),
98 user_count: Some("0".to_string()),
99 },
100 Usergroup {
101 id: Some("S0615G0KT".to_string()),
102 team_id: Some("T060RNRCH".to_string()),
103 is_usergroup: Some(true),
104 name: Some("Marketing Team".to_string()),
105 description: Some(
106 "Marketing gurus, PR experts and product advocates.".to_string(),
107 ),
108 handle: Some("marketing-team".to_string()),
109 is_external: Some(false),
110 date_create: Some(1446746793),
111 date_update: Some(1446746793),
112 date_delete: Some(0),
113 auto_type: Some("".to_string()),
114 created_by: Some("U060RNRCZ".to_string()),
115 updated_by: Some("U060RNRCZ".to_string()),
116 deleted_by: Some("".to_string()),
117 prefs: Some(Pref {
118 channels: Some(vec![]),
119 groups: Some(vec![]),
120 }),
121 user_count: Some("0".to_string()),
122 },
123 ]),
124 ..Default::default()
125 };
126 let json = r##"{
127 "ok": true,
128 "usergroups": [
129 {
130 "id": "S0615G0KT",
131 "team_id": "T060RNRCH",
132 "is_usergroup": true,
133 "name": "Marketing Team",
134 "description": "Marketing gurus, PR experts and product advocates.",
135 "handle": "marketing-team",
136 "is_external": false,
137 "date_create": 1446746793,
138 "date_update": 1446746793,
139 "date_delete": 0,
140 "auto_type": "",
141 "created_by": "U060RNRCZ",
142 "updated_by": "U060RNRCZ",
143 "deleted_by": "",
144 "prefs": {
145 "channels": [],
146 "groups": []
147 },
148 "user_count": "0"
149 },
150 {
151 "id": "S0615G0KT",
152 "team_id": "T060RNRCH",
153 "is_usergroup": true,
154 "name": "Marketing Team",
155 "description": "Marketing gurus, PR experts and product advocates.",
156 "handle": "marketing-team",
157 "is_external": false,
158 "date_create": 1446746793,
159 "date_update": 1446746793,
160 "date_delete": 0,
161 "auto_type": "",
162 "created_by": "U060RNRCZ",
163 "updated_by": "U060RNRCZ",
164 "deleted_by": "",
165 "prefs": {
166 "channels": [],
167 "groups": []
168 },
169 "user_count": "0"
170 }
171 ]
172}"##;
173
174 let j = serde_json::to_string_pretty(&response).unwrap();
175 assert_eq!(json, j);
176
177 let s = serde_json::from_str::<ListResponse>(json).unwrap();
178 assert_eq!(response, s);
179 }
180
181 #[async_std::test]
182 async fn test_list() {
183 let param = ListRequest {
184 include_count: Some(true),
185 include_disabled: Some(true),
186 include_users: Some(true),
187 team_id: Some("T1234567890".to_string()),
188 };
189 let mut mock = MockSlackWebAPIClient::new();
190 mock.expect_post_json().returning(|_, _, _| {
191 Ok(r##"{
192 "ok": true,
193 "usergroups": [
194 {
195 "id": "S0615G0KT",
196 "team_id": "T060RNRCH",
197 "is_usergroup": true,
198 "name": "Marketing Team",
199 "description": "Marketing gurus, PR experts and product advocates.",
200 "handle": "marketing-team",
201 "is_external": false,
202 "date_create": 1446746793,
203 "date_update": 1446746793,
204 "date_delete": 0,
205 "auto_type": "",
206 "created_by": "U060RNRCZ",
207 "updated_by": "U060RNRCZ",
208 "deleted_by": "",
209 "prefs": {
210 "channels": [],
211 "groups": []
212 },
213 "user_count": "0"
214 },
215 {
216 "id": "S0615G0KT",
217 "team_id": "T060RNRCH",
218 "is_usergroup": true,
219 "name": "Marketing Team",
220 "description": "Marketing gurus, PR experts and product advocates.",
221 "handle": "marketing-team",
222 "is_external": false,
223 "date_create": 1446746793,
224 "date_update": 1446746793,
225 "date_delete": 0,
226 "auto_type": "",
227 "created_by": "U060RNRCZ",
228 "updated_by": "U060RNRCZ",
229 "deleted_by": "",
230 "prefs": {
231 "channels": [],
232 "groups": []
233 },
234 "user_count": "0"
235 }
236 ]
237}"##
238 .to_string())
239 });
240
241 let response = list(&mock, ¶m, &"test_token".to_string())
242 .await
243 .unwrap();
244 let expect = ListResponse {
245 ok: true,
246 usergroups: Some(vec![
247 Usergroup {
248 id: Some("S0615G0KT".to_string()),
249 team_id: Some("T060RNRCH".to_string()),
250 is_usergroup: Some(true),
251 name: Some("Marketing Team".to_string()),
252 description: Some(
253 "Marketing gurus, PR experts and product advocates.".to_string(),
254 ),
255 handle: Some("marketing-team".to_string()),
256 is_external: Some(false),
257 date_create: Some(1446746793),
258 date_update: Some(1446746793),
259 date_delete: Some(0),
260 auto_type: Some("".to_string()),
261 created_by: Some("U060RNRCZ".to_string()),
262 updated_by: Some("U060RNRCZ".to_string()),
263 deleted_by: Some("".to_string()),
264 prefs: Some(Pref {
265 channels: Some(vec![]),
266 groups: Some(vec![]),
267 }),
268 user_count: Some("0".to_string()),
269 },
270 Usergroup {
271 id: Some("S0615G0KT".to_string()),
272 team_id: Some("T060RNRCH".to_string()),
273 is_usergroup: Some(true),
274 name: Some("Marketing Team".to_string()),
275 description: Some(
276 "Marketing gurus, PR experts and product advocates.".to_string(),
277 ),
278 handle: Some("marketing-team".to_string()),
279 is_external: Some(false),
280 date_create: Some(1446746793),
281 date_update: Some(1446746793),
282 date_delete: Some(0),
283 auto_type: Some("".to_string()),
284 created_by: Some("U060RNRCZ".to_string()),
285 updated_by: Some("U060RNRCZ".to_string()),
286 deleted_by: Some("".to_string()),
287 prefs: Some(Pref {
288 channels: Some(vec![]),
289 groups: Some(vec![]),
290 }),
291 user_count: Some("0".to_string()),
292 },
293 ]),
294 ..Default::default()
295 };
296
297 assert_eq!(expect, response);
298 }
299}