use crate::error::Error;
use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
pub struct ListRequest {
pub usergroup: String,
pub include_disabled: Option<bool>,
pub team_id: Option<String>,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
pub struct ListResponse {
pub ok: bool,
pub error: Option<String>,
pub response_metadata: Option<ResponseMetadata>,
pub users: Option<Vec<String>>,
}
pub async fn list<T>(
client: &T,
param: &ListRequest,
bot_token: &str,
) -> Result<ListResponse, Error>
where
T: SlackWebAPIClient,
{
let url = get_slack_url("usergroups.users.list");
let json = serde_json::to_string(¶m)?;
client
.post_json(&url, &json, bot_token)
.await
.and_then(|result| {
serde_json::from_str::<ListResponse>(&result).map_err(Error::SerdeJsonError)
})
}
#[cfg(test)]
mod test {
use super::*;
use crate::http_client::MockSlackWebAPIClient;
#[test]
fn convert_request() {
let request = ListRequest {
usergroup: "S0604QSJC".to_string(),
team_id: Some("T1234567890".to_string()),
include_disabled: Some(true),
};
let json = r##"{
"usergroup": "S0604QSJC",
"include_disabled": true,
"team_id": "T1234567890"
}"##;
let j = serde_json::to_string_pretty(&request).unwrap();
assert_eq!(json, j);
let s = serde_json::from_str::<ListRequest>(json).unwrap();
assert_eq!(request, s);
}
#[test]
fn convert_response() {
let response = ListResponse {
ok: true,
users: Some(vec!["xxxxxxxx".to_string(), "xxxxxxxx".to_string()]),
..Default::default()
};
let json = r##"{
"ok": true,
"users": [
"xxxxxxxx",
"xxxxxxxx"
]
}"##;
let j = serde_json::to_string_pretty(&response).unwrap();
assert_eq!(json, j);
let s = serde_json::from_str::<ListResponse>(json).unwrap();
assert_eq!(response, s);
}
#[async_std::test]
async fn test_list() {
let param = ListRequest {
usergroup: "S0604QSJC".to_string(),
team_id: Some("T1234567890".to_string()),
include_disabled: Some(true),
};
let mut mock = MockSlackWebAPIClient::new();
mock.expect_post_json().returning(|_, _, _| {
Ok(r##"{
"ok": true,
"users": [
"xxxxxxxx",
"xxxxxxxx"
]
}"##
.to_string())
});
let response = list(&mock, ¶m, &"test_token".to_string())
.await
.unwrap();
let expect = ListResponse {
ok: true,
users: Some(vec!["xxxxxxxx".to_string(), "xxxxxxxx".to_string()]),
..Default::default()
};
assert_eq!(expect, response);
}
}