slack_rust/auth/
teams_list.rs

1//! List the workspaces a token can access.
2//! See: <https://api.slack.com/methods/auth.teams.list>
3
4use crate::error::Error;
5use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
6use crate::team::teams::Team;
7use serde::{Deserialize, Serialize};
8use serde_with::skip_serializing_none;
9
10#[skip_serializing_none]
11#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
12pub struct TeamsListRequest {
13    pub cursor: Option<String>,
14    pub include_icon: Option<bool>,
15    pub limit: Option<i32>,
16}
17
18#[skip_serializing_none]
19#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
20pub struct TeamsListResponse {
21    pub ok: bool,
22    pub error: Option<String>,
23    pub response_metadata: Option<ResponseMetadata>,
24    pub teams: Option<Vec<Team>>,
25}
26
27/// List the workspaces a token can access.
28/// See: <https://api.slack.com/methods/auth.teams.list>
29pub async fn teams_list<T>(
30    client: &T,
31    param: &TeamsListRequest,
32    bot_token: &str,
33) -> Result<TeamsListResponse, Error>
34where
35    T: SlackWebAPIClient,
36{
37    let url = get_slack_url("auth.teams.list");
38    let json = serde_json::to_string(&param)?;
39
40    client
41        .post_json(&url, &json, bot_token)
42        .await
43        .and_then(|result| {
44            serde_json::from_str::<TeamsListResponse>(&result).map_err(Error::SerdeJsonError)
45        })
46}
47
48#[cfg(test)]
49mod test {
50    use super::*;
51    use crate::http_client::MockSlackWebAPIClient;
52
53    #[test]
54    fn convert_request() {
55        let request = TeamsListRequest {
56            cursor: Some("5c3e53d5".to_string()),
57            include_icon: Some(true),
58            limit: Some(1),
59        };
60        let json = r##"{
61  "cursor": "5c3e53d5",
62  "include_icon": true,
63  "limit": 1
64}"##;
65
66        let j = serde_json::to_string_pretty(&request).unwrap();
67        assert_eq!(json, j);
68
69        let s = serde_json::from_str::<TeamsListRequest>(json).unwrap();
70        assert_eq!(request, s);
71    }
72
73    #[test]
74    fn convert_response() {
75        let response = TeamsListResponse {
76            ok: true,
77            teams: Some(vec![
78                Team {
79                    id: Some("T12345678".to_string()),
80                    name: Some("Shinichi's workspace".to_string()),
81                    ..Default::default()
82                },
83                Team {
84                    id: Some("T12345679".to_string()),
85                    name: Some("Migi's workspace".to_string()),
86                    ..Default::default()
87                },
88            ]),
89            ..Default::default()
90        };
91        let json = r##"{
92  "ok": true,
93  "teams": [
94    {
95      "id": "T12345678",
96      "name": "Shinichi's workspace"
97    },
98    {
99      "id": "T12345679",
100      "name": "Migi's workspace"
101    }
102  ]
103}"##;
104
105        let j = serde_json::to_string_pretty(&response).unwrap();
106        assert_eq!(json, j);
107
108        let s = serde_json::from_str::<TeamsListResponse>(json).unwrap();
109        assert_eq!(response, s);
110    }
111
112    #[async_std::test]
113    async fn test_teams_list() {
114        let param = TeamsListRequest {
115            cursor: Some("5c3e53d5".to_string()),
116            include_icon: Some(true),
117            limit: Some(1),
118        };
119
120        let mut mock = MockSlackWebAPIClient::new();
121        mock.expect_post_json().returning(|_, _, _| {
122            Ok(r##"{
123  "ok": true,
124  "teams": [
125    {
126      "id": "T12345678",
127      "name": "Shinichi's workspace"
128    },
129    {
130      "id": "T12345679",
131      "name": "Migi's workspace"
132    }
133  ]
134}"##
135            .to_string())
136        });
137
138        let response = teams_list(&mock, &param, &"test_token".to_string())
139            .await
140            .unwrap();
141        let expect = TeamsListResponse {
142            ok: true,
143            teams: Some(vec![
144                Team {
145                    id: Some("T12345678".to_string()),
146                    name: Some("Shinichi's workspace".to_string()),
147                    ..Default::default()
148                },
149                Team {
150                    id: Some("T12345679".to_string()),
151                    name: Some("Migi's workspace".to_string()),
152                    ..Default::default()
153                },
154            ]),
155            ..Default::default()
156        };
157
158        assert_eq!(expect, response);
159    }
160}