1use crate::error::Error;
5use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
6use serde::{Deserialize, Serialize};
7use serde_with::skip_serializing_none;
8
9#[skip_serializing_none]
10#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
11pub struct TestResponse {
12 pub ok: bool,
13 pub error: Option<String>,
14 pub response_metadata: Option<ResponseMetadata>,
15 pub url: Option<String>,
16 pub team: Option<String>,
17 pub user: Option<String>,
18 pub team_id: Option<String>,
19 pub user_id: Option<String>,
20}
21
22pub async fn test<T>(client: &T, bot_token: &str) -> Result<TestResponse, Error>
25where
26 T: SlackWebAPIClient,
27{
28 let url = get_slack_url("auth.test");
29
30 client.post(&url, bot_token).await.and_then(|result| {
31 serde_json::from_str::<TestResponse>(&result).map_err(Error::SerdeJsonError)
32 })
33}
34
35#[cfg(test)]
36mod test {
37 use super::*;
38 use crate::http_client::MockSlackWebAPIClient;
39
40 #[test]
41 fn convert_response() {
42 let response = TestResponse {
43 ok: true,
44 url: Some("https://subarachnoid.slack.com/".to_string()),
45 team: Some("Subarachnoid Workspace".to_string()),
46 user: Some("grace".to_string()),
47 team_id: Some("T12345678".to_string()),
48 user_id: Some("W12345678".to_string()),
49 ..Default::default()
50 };
51 let json = r##"{
52 "ok": true,
53 "url": "https://subarachnoid.slack.com/",
54 "team": "Subarachnoid Workspace",
55 "user": "grace",
56 "team_id": "T12345678",
57 "user_id": "W12345678"
58}"##;
59
60 let j = serde_json::to_string_pretty(&response).unwrap();
61 assert_eq!(json, j);
62
63 let s = serde_json::from_str::<TestResponse>(json).unwrap();
64 assert_eq!(response, s);
65 }
66
67 #[async_std::test]
68 async fn test_auth_test() {
69 let mut mock = MockSlackWebAPIClient::new();
70 mock.expect_post().returning(|_, _| {
71 Ok(r##"{
72 "ok": true,
73 "url": "https://subarachnoid.slack.com/",
74 "team": "Subarachnoid Workspace",
75 "user": "grace",
76 "team_id": "T12345678",
77 "user_id": "W12345678"
78 }"##
79 .to_string())
80 });
81
82 let response = test(&mock, &"test_token".to_string()).await.unwrap();
83 let expect = TestResponse {
84 ok: true,
85 url: Some("https://subarachnoid.slack.com/".to_string()),
86 team: Some("Subarachnoid Workspace".to_string()),
87 user: Some("grace".to_string()),
88 team_id: Some("T12345678".to_string()),
89 user_id: Some("W12345678".to_string()),
90 ..Default::default()
91 };
92
93 assert_eq!(expect, response);
94 }
95}