1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use crate::team::log::{Log, Paging};
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct IntegrationLogsRequest {
10 pub app_id: Option<String>,
11 pub change_type: Option<String>,
12 pub count: Option<String>,
13 pub page: Option<String>,
14 pub service_id: Option<String>,
15 pub team_id: Option<String>,
16 pub user: Option<String>,
17}
18
19#[skip_serializing_none]
20#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
21pub struct IntegrationLogsResponse {
22 pub ok: bool,
23 pub error: Option<String>,
24 pub response_metadata: Option<ResponseMetadata>,
25 pub logs: Option<Vec<Log>>,
26 pub paging: Option<Paging>,
27}
28
29pub async fn integration_logs<T>(
30 client: &T,
31 param: &IntegrationLogsRequest,
32 bot_token: &str,
33) -> Result<IntegrationLogsResponse, Error>
34where
35 T: SlackWebAPIClient,
36{
37 let url = get_slack_url("team.integrationLogs");
38 let json = serde_json::to_string(¶m)?;
39
40 client
41 .post_json(&url, &json, bot_token)
42 .await
43 .and_then(|result| {
44 serde_json::from_str::<IntegrationLogsResponse>(&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 = IntegrationLogsRequest {
56 app_id: Some("xxxxxxxxxxx".to_string()),
57 change_type: Some("added".to_string()),
58 count: Some("20".to_string()),
59 page: Some("2".to_string()),
60 service_id: Some("xxxxxxxxxxx".to_string()),
61 team_id: Some("T1234567890".to_string()),
62 user: Some("W1234567890".to_string()),
63 };
64 let json = r##"{
65 "app_id": "xxxxxxxxxxx",
66 "change_type": "added",
67 "count": "20",
68 "page": "2",
69 "service_id": "xxxxxxxxxxx",
70 "team_id": "T1234567890",
71 "user": "W1234567890"
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::<IntegrationLogsRequest>(json).unwrap();
78 assert_eq!(request, s);
79 }
80
81 #[test]
82 fn convert_response() {
83 let response = IntegrationLogsResponse {
84 ok: true,
85 logs: Some(vec![
86 Log {
87 service_id: Some(1234567890),
88 service_type: Some("Google Calendar".to_string()),
89 user_id: Some("U1234ABCD".to_string()),
90 user_name: Some("Johnny".to_string()),
91 channel: Some("C1234567890".to_string()),
92 date: Some("1392163200".to_string()),
93 change_type: Some("enabled".to_string()),
94 scope: Some("incoming-webhook".to_string()),
95 ..Default::default()
96 },
97 Log {
98 app_id: Some("2345678901".to_string()),
99 app_type: Some("Johnny App".to_string()),
100 user_id: Some("U1234ABCD".to_string()),
101 user_name: Some("Johnny".to_string()),
102 channel: Some("C1234567890".to_string()),
103 date: Some("1392163200".to_string()),
104 change_type: Some("enabled".to_string()),
105 scope: Some("incoming-webhook".to_string()),
106 ..Default::default()
107 },
108 ]),
109 paging: Some(Paging {
110 count: Some(3),
111 total: Some(3),
112 page: Some(1),
113 pages: Some(1),
114 }),
115 ..Default::default()
116 };
117 let json = r##"{
118 "ok": true,
119 "logs": [
120 {
121 "service_id": 1234567890,
122 "service_type": "Google Calendar",
123 "user_id": "U1234ABCD",
124 "user_name": "Johnny",
125 "channel": "C1234567890",
126 "date": "1392163200",
127 "change_type": "enabled",
128 "scope": "incoming-webhook"
129 },
130 {
131 "app_id": "2345678901",
132 "app_type": "Johnny App",
133 "user_id": "U1234ABCD",
134 "user_name": "Johnny",
135 "channel": "C1234567890",
136 "date": "1392163200",
137 "change_type": "enabled",
138 "scope": "incoming-webhook"
139 }
140 ],
141 "paging": {
142 "count": 3,
143 "total": 3,
144 "page": 1,
145 "pages": 1
146 }
147}"##;
148
149 let j = serde_json::to_string_pretty(&response).unwrap();
150 assert_eq!(json, j);
151
152 let s = serde_json::from_str::<IntegrationLogsResponse>(json).unwrap();
153 assert_eq!(response, s);
154 }
155
156 #[async_std::test]
157 async fn test_integration_logs() {
158 let param = IntegrationLogsRequest {
159 app_id: Some("xxxxxxxxxxx".to_string()),
160 change_type: Some("added".to_string()),
161 count: Some("20".to_string()),
162 page: Some("2".to_string()),
163 service_id: Some("xxxxxxxxxxx".to_string()),
164 team_id: Some("T1234567890".to_string()),
165 user: Some("W1234567890".to_string()),
166 };
167 let mut mock = MockSlackWebAPIClient::new();
168 mock.expect_post_json().returning(|_, _, _| {
169 Ok(r##"{
170 "ok": true,
171 "logs": [
172 {
173 "service_id": 1234567890,
174 "service_type": "Google Calendar",
175 "user_id": "U1234ABCD",
176 "user_name": "Johnny",
177 "channel": "C1234567890",
178 "date": "1392163200",
179 "change_type": "enabled",
180 "scope": "incoming-webhook"
181 },
182 {
183 "app_id": "2345678901",
184 "app_type": "Johnny App",
185 "user_id": "U1234ABCD",
186 "user_name": "Johnny",
187 "channel": "C1234567890",
188 "date": "1392163200",
189 "change_type": "enabled",
190 "scope": "incoming-webhook"
191 }
192 ],
193 "paging": {
194 "count": 3,
195 "total": 3,
196 "page": 1,
197 "pages": 1
198 }
199}"##
200 .to_string())
201 });
202
203 let response = integration_logs(&mock, ¶m, &"test_token".to_string())
204 .await
205 .unwrap();
206
207 let expect = IntegrationLogsResponse {
208 ok: true,
209 logs: Some(vec![
210 Log {
211 service_id: Some(1234567890),
212 service_type: Some("Google Calendar".to_string()),
213 user_id: Some("U1234ABCD".to_string()),
214 user_name: Some("Johnny".to_string()),
215 channel: Some("C1234567890".to_string()),
216 date: Some("1392163200".to_string()),
217 change_type: Some("enabled".to_string()),
218 scope: Some("incoming-webhook".to_string()),
219 ..Default::default()
220 },
221 Log {
222 app_id: Some("2345678901".to_string()),
223 app_type: Some("Johnny App".to_string()),
224 user_id: Some("U1234ABCD".to_string()),
225 user_name: Some("Johnny".to_string()),
226 channel: Some("C1234567890".to_string()),
227 date: Some("1392163200".to_string()),
228 change_type: Some("enabled".to_string()),
229 scope: Some("incoming-webhook".to_string()),
230 ..Default::default()
231 },
232 ]),
233 paging: Some(Paging {
234 count: Some(3),
235 total: Some(3),
236 page: Some(1),
237 pages: Some(1),
238 }),
239 ..Default::default()
240 };
241
242 assert_eq!(expect, response);
243 }
244}