slack_rust/reactions/
add.rs

1use crate::error::Error;
2use crate::http_client::{get_slack_url, DefaultResponse, SlackWebAPIClient};
3use serde::{Deserialize, Serialize};
4
5#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
6pub struct AddRequest {
7    pub channel: String,
8    pub name: String,
9    pub timestamp: String,
10}
11
12pub async fn add<T>(
13    client: &T,
14    param: &AddRequest,
15    bot_token: &str,
16) -> Result<DefaultResponse, Error>
17where
18    T: SlackWebAPIClient,
19{
20    let url = get_slack_url("reactions.add");
21    let json = serde_json::to_string(&param)?;
22
23    client
24        .post_json(&url, &json, bot_token)
25        .await
26        .and_then(|result| {
27            serde_json::from_str::<DefaultResponse>(&result).map_err(Error::SerdeJsonError)
28        })
29}
30
31#[cfg(test)]
32mod test {
33    use super::*;
34    use crate::http_client::MockSlackWebAPIClient;
35
36    #[test]
37    fn convert_request() {
38        let request = AddRequest {
39            channel: "C1234567890".to_string(),
40            name: "thumbsup".to_string(),
41            timestamp: "1234567890.123456".to_string(),
42        };
43        let json = r##"{
44  "channel": "C1234567890",
45  "name": "thumbsup",
46  "timestamp": "1234567890.123456"
47}"##;
48
49        let j = serde_json::to_string_pretty(&request).unwrap();
50        assert_eq!(json, j);
51
52        let s = serde_json::from_str::<AddRequest>(json).unwrap();
53        assert_eq!(request, s);
54    }
55
56    #[async_std::test]
57    async fn test_add() {
58        let param = AddRequest {
59            channel: "C1234567890".to_string(),
60            name: "thumbsup".to_string(),
61            timestamp: "1234567890.123456".to_string(),
62        };
63
64        let mut mock = MockSlackWebAPIClient::new();
65        mock.expect_post_json().returning(|_, _, _| {
66            Ok(r##"{
67  "ok": true
68}"##
69            .to_string())
70        });
71
72        let response = add(&mock, &param, &"test_token".to_string()).await.unwrap();
73        let expect = DefaultResponse {
74            ok: true,
75            ..Default::default()
76        };
77
78        assert_eq!(expect, response);
79    }
80}