slack_rust/usergroups/
create.rs

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 CreateRequest {
10    pub name: String,
11    pub channels: Option<Vec<String>>,
12    pub description: Option<String>,
13    pub handle: Option<String>,
14    pub include_count: Option<bool>,
15    pub team_id: Option<String>,
16}
17
18#[skip_serializing_none]
19#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
20pub struct CreateResponse {
21    pub ok: bool,
22    pub error: Option<String>,
23    pub response_metadata: Option<ResponseMetadata>,
24    pub usergroup: Option<Usergroup>,
25}
26
27pub async fn create<T>(
28    client: &T,
29    param: &CreateRequest,
30    bot_token: &str,
31) -> Result<CreateResponse, Error>
32where
33    T: SlackWebAPIClient,
34{
35    let url = get_slack_url("usergroups.create");
36    let json = serde_json::to_string(&param)?;
37
38    client
39        .post_json(&url, &json, bot_token)
40        .await
41        .and_then(|result| {
42            serde_json::from_str::<CreateResponse>(&result).map_err(Error::SerdeJsonError)
43        })
44}
45
46#[cfg(test)]
47mod test {
48    use super::*;
49    use crate::http_client::MockSlackWebAPIClient;
50    use crate::usergroups::usergroup::Pref;
51
52    #[test]
53    fn convert_request() {
54        let request = CreateRequest {
55            name: "My Test Team".to_string(),
56            channels: Some(vec!["xxxxxxxxxx".to_string(), "xxxxxxxxxx".to_string()]),
57            description: Some("xxxxxxxxxx".to_string()),
58            handle: Some("marketing".to_string()),
59            team_id: Some("T1234567890".to_string()),
60            include_count: Some(true),
61        };
62        let json = r##"{
63  "name": "My Test Team",
64  "channels": [
65    "xxxxxxxxxx",
66    "xxxxxxxxxx"
67  ],
68  "description": "xxxxxxxxxx",
69  "handle": "marketing",
70  "include_count": true,
71  "team_id": "T1234567890"
72}"##;
73
74        let j = serde_json::to_string_pretty(&request).unwrap();
75        assert_eq!(json, j);
76
77        let s = serde_json::from_str::<CreateRequest>(json).unwrap();
78        assert_eq!(request, s);
79    }
80
81    #[test]
82    fn convert_response() {
83        let response = CreateResponse {
84            ok: true,
85            usergroup: Some(Usergroup {
86                id: Some("S0615G0KT".to_string()),
87                team_id: Some("T060RNRCH".to_string()),
88                is_usergroup: Some(true),
89                name: Some("Marketing Team".to_string()),
90                description: Some("Marketing gurus, PR experts and product advocates.".to_string()),
91                handle: Some("marketing-team".to_string()),
92                is_external: Some(false),
93                date_create: Some(1446746793),
94                date_update: Some(1446746793),
95                date_delete: Some(0),
96                auto_type: Some("".to_string()),
97                created_by: Some("U060RNRCZ".to_string()),
98                updated_by: Some("U060RNRCZ".to_string()),
99                deleted_by: Some("".to_string()),
100                prefs: Some(Pref {
101                    channels: Some(vec![]),
102                    groups: Some(vec![]),
103                }),
104                user_count: Some("0".to_string()),
105            }),
106            ..Default::default()
107        };
108        let json = r##"{
109  "ok": true,
110  "usergroup": {
111    "id": "S0615G0KT",
112    "team_id": "T060RNRCH",
113    "is_usergroup": true,
114    "name": "Marketing Team",
115    "description": "Marketing gurus, PR experts and product advocates.",
116    "handle": "marketing-team",
117    "is_external": false,
118    "date_create": 1446746793,
119    "date_update": 1446746793,
120    "date_delete": 0,
121    "auto_type": "",
122    "created_by": "U060RNRCZ",
123    "updated_by": "U060RNRCZ",
124    "deleted_by": "",
125    "prefs": {
126      "channels": [],
127      "groups": []
128    },
129    "user_count": "0"
130  }
131}"##;
132
133        let j = serde_json::to_string_pretty(&response).unwrap();
134        assert_eq!(json, j);
135
136        let s = serde_json::from_str::<CreateResponse>(json).unwrap();
137        assert_eq!(response, s);
138    }
139
140    #[async_std::test]
141    async fn test_create() {
142        let param = CreateRequest {
143            name: "My Test Team".to_string(),
144            channels: Some(vec!["xxxxxxxxxx".to_string(), "xxxxxxxxxx".to_string()]),
145            description: Some("xxxxxxxxxx".to_string()),
146            handle: Some("marketing".to_string()),
147            team_id: Some("T1234567890".to_string()),
148            include_count: Some(true),
149        };
150        let mut mock = MockSlackWebAPIClient::new();
151        mock.expect_post_json().returning(|_, _, _| {
152            Ok(r##"{
153  "ok": true,
154  "usergroup": {
155    "id": "S0615G0KT",
156    "team_id": "T1234567890",
157    "is_usergroup": true,
158    "name": "My Test Team",
159    "description": "Marketing gurus, PR experts and product advocates.",
160    "handle": "marketing-team",
161    "is_external": false,
162    "date_create": 1446746793,
163    "date_update": 1446746793,
164    "date_delete": 0,
165    "auto_type": "",
166    "created_by": "U060RNRCZ",
167    "updated_by": "U060RNRCZ",
168    "deleted_by": "",
169    "prefs": {
170      "channels": [],
171      "groups": []
172    },
173    "user_count": "0"
174  }
175}"##
176            .to_string())
177        });
178
179        let response = create(&mock, &param, &"test_token".to_string())
180            .await
181            .unwrap();
182        let expect = CreateResponse {
183            ok: true,
184            usergroup: Some(Usergroup {
185                id: Some("S0615G0KT".to_string()),
186                team_id: Some("T1234567890".to_string()),
187                is_usergroup: Some(true),
188                name: Some("My Test Team".to_string()),
189                description: Some("Marketing gurus, PR experts and product advocates.".to_string()),
190                handle: Some("marketing-team".to_string()),
191                is_external: Some(false),
192                date_create: Some(1446746793),
193                date_update: Some(1446746793),
194                date_delete: Some(0),
195                auto_type: Some("".to_string()),
196                created_by: Some("U060RNRCZ".to_string()),
197                updated_by: Some("U060RNRCZ".to_string()),
198                deleted_by: Some("".to_string()),
199                prefs: Some(Pref {
200                    channels: Some(vec![]),
201                    groups: Some(vec![]),
202                }),
203                user_count: Some("0".to_string()),
204            }),
205            ..Default::default()
206        };
207
208        assert_eq!(expect, response);
209    }
210}