1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use crate::team::teams::Team;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct InfoRequest {
10 pub team: Option<String>,
11}
12
13#[skip_serializing_none]
14#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
15pub struct InfoResponse {
16 pub ok: bool,
17 pub error: Option<String>,
18 pub response_metadata: Option<ResponseMetadata>,
19 pub team: Option<Team>,
20}
21
22pub async fn info<T>(
23 client: &T,
24 param: &InfoRequest,
25 bot_token: &str,
26) -> Result<InfoResponse, Error>
27where
28 T: SlackWebAPIClient,
29{
30 let url = get_slack_url("team.info");
31 let json = serde_json::to_string(¶m)?;
32
33 client
34 .post_json(&url, &json, bot_token)
35 .await
36 .and_then(|result| {
37 serde_json::from_str::<InfoResponse>(&result).map_err(Error::SerdeJsonError)
38 })
39}
40
41#[cfg(test)]
42mod test {
43 use super::*;
44 use crate::http_client::MockSlackWebAPIClient;
45 use crate::team::teams::Icon;
46
47 #[test]
48 fn convert_request() {
49 let request = InfoRequest {
50 team: Some("T1234567890".to_string()),
51 };
52 let json = r##"{
53 "team": "T1234567890"
54}"##;
55
56 let j = serde_json::to_string_pretty(&request).unwrap();
57 assert_eq!(json, j);
58
59 let s = serde_json::from_str::<InfoRequest>(json).unwrap();
60 assert_eq!(request, s);
61 }
62
63 #[test]
64 fn convert_response() {
65 let response = InfoResponse {
66 ok: true,
67 team: Some(Team {
68 id: Some("T12345".to_string()),
69 name: Some("My Team".to_string()),
70 domain: Some("example".to_string()),
71 email_domain: Some("example.com".to_string()),
72 icon: Some(Icon {
73 image_34: Some("https://...".to_string()),
74 image_44: Some("https://...".to_string()),
75 image_68: Some("https://...".to_string()),
76 image_88: Some("https://...".to_string()),
77 image_102: Some("https://...".to_string()),
78 image_132: Some("https://...".to_string()),
79 image_default: Some(true),
80 }),
81 enterprise_id: Some("E1234A12AB".to_string()),
82 enterprise_name: Some("Umbrella Corporation".to_string()),
83 }),
84 ..Default::default()
85 };
86 let json = r##"{
87 "ok": true,
88 "team": {
89 "id": "T12345",
90 "name": "My Team",
91 "domain": "example",
92 "email_domain": "example.com",
93 "icon": {
94 "image_34": "https://...",
95 "image_44": "https://...",
96 "image_68": "https://...",
97 "image_88": "https://...",
98 "image_102": "https://...",
99 "image_132": "https://...",
100 "image_default": true
101 },
102 "enterprise_id": "E1234A12AB",
103 "enterprise_name": "Umbrella Corporation"
104 }
105}"##;
106
107 let j = serde_json::to_string_pretty(&response).unwrap();
108 assert_eq!(json, j);
109
110 let s = serde_json::from_str::<InfoResponse>(json).unwrap();
111 assert_eq!(response, s);
112 }
113
114 #[async_std::test]
115 async fn test_info() {
116 let param = InfoRequest {
117 team: Some("T12345".to_string()),
118 };
119 let mut mock = MockSlackWebAPIClient::new();
120 mock.expect_post_json().returning(|_, _, _| {
121 Ok(r##"{
122 "ok": true,
123 "team": {
124 "id": "T12345",
125 "name": "My Team",
126 "domain": "example",
127 "email_domain": "example.com",
128 "icon": {
129 "image_34": "https://...",
130 "image_44": "https://...",
131 "image_68": "https://...",
132 "image_88": "https://...",
133 "image_102": "https://...",
134 "image_132": "https://...",
135 "image_default": true
136 },
137 "enterprise_id": "E1234A12AB",
138 "enterprise_name": "Umbrella Corporation"
139 }
140}"##
141 .to_string())
142 });
143
144 let response = info(&mock, ¶m, &"test_token".to_string())
145 .await
146 .unwrap();
147
148 let expect = InfoResponse {
149 ok: true,
150 team: Some(Team {
151 id: Some("T12345".to_string()),
152 name: Some("My Team".to_string()),
153 domain: Some("example".to_string()),
154 email_domain: Some("example.com".to_string()),
155 icon: Some(Icon {
156 image_34: Some("https://...".to_string()),
157 image_44: Some("https://...".to_string()),
158 image_68: Some("https://...".to_string()),
159 image_88: Some("https://...".to_string()),
160 image_102: Some("https://...".to_string()),
161 image_132: Some("https://...".to_string()),
162 image_default: Some(true),
163 }),
164 enterprise_id: Some("E1234A12AB".to_string()),
165 enterprise_name: Some("Umbrella Corporation".to_string()),
166 }),
167 ..Default::default()
168 };
169
170 assert_eq!(expect, response);
171 }
172}