slack_rust/apps/
uninstall.rs

1//! Uninstalls your app from a workspace.  
2//! See: <https://api.slack.com/methods?filter=apps>
3
4use crate::error::Error;
5use crate::http_client::{get_slack_url, DefaultResponse, SlackWebAPIClient};
6use serde::{Deserialize, Serialize};
7
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct UninstallRequest {
10    pub client_id: String,
11    pub client_secret: String,
12}
13
14impl UninstallRequest {
15    pub fn new(client_id: String, client_secret: String) -> Self {
16        UninstallRequest {
17            client_id,
18            client_secret,
19        }
20    }
21}
22
23/// Uninstalls your app from a workspace.  
24/// See: <https://api.slack.com/methods?filter=apps>
25pub async fn uninstall<T>(
26    client: &T,
27    param: &UninstallRequest,
28    bot_token: &str,
29) -> Result<DefaultResponse, Error>
30where
31    T: SlackWebAPIClient,
32{
33    let url = get_slack_url("apps.uninstall");
34    let json = serde_json::to_string(&param)?;
35
36    client
37        .post_json(&url, &json, bot_token)
38        .await
39        .and_then(|result| {
40            serde_json::from_str::<DefaultResponse>(&result).map_err(Error::SerdeJsonError)
41        })
42}
43
44#[cfg(test)]
45mod test {
46    use super::*;
47    use crate::http_client::MockSlackWebAPIClient;
48
49    #[test]
50    fn convert_request() {
51        let request = UninstallRequest {
52            client_id: "56579136444.26251006572".to_string(),
53            client_secret: "f25b5ceaf8a3c2a2c4f52bb4f0b0499e".to_string(),
54        };
55        let json = r##"{
56  "client_id": "56579136444.26251006572",
57  "client_secret": "f25b5ceaf8a3c2a2c4f52bb4f0b0499e"
58}"##;
59
60        let j = serde_json::to_string_pretty(&request).unwrap();
61        assert_eq!(json, j);
62
63        let s = serde_json::from_str::<UninstallRequest>(json).unwrap();
64        assert_eq!(request, s);
65    }
66
67    #[async_std::test]
68    async fn test_uninstall() {
69        let param = UninstallRequest {
70            client_id: "56579136444.26251006572".to_string(),
71            client_secret: "f25b5ceaf8a3c2a2c4f52bb4f0b0499e".to_string(),
72        };
73
74        let mut mock = MockSlackWebAPIClient::new();
75        mock.expect_post_json().returning(|_, _, _| {
76            Ok(r##"{
77          "ok": true
78        }"##
79            .to_string())
80        });
81
82        let response = uninstall(&mock, &param, &"test_token".to_string())
83            .await
84            .unwrap();
85        let expect = DefaultResponse {
86            ok: true,
87            ..Default::default()
88        };
89
90        assert_eq!(expect, response);
91    }
92}