#![allow(unused_imports)]
use crate::sdk::{self, types};
use crate::support;
#[tokio::test]
async fn reports_api_errors() {
let body = support::json(r#"{"code":"not_found","message":"No such resource.","details":{"field":"id"}}"#);
let reply = support::Reply::json(404, body).header("x-request-id", "req_test");
let api = support::MockApi::start(vec![reply]).await;
let error = api.client().privacy().get("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr").await.unwrap_err();
assert!(matches!(error, sdk::Error::Api { .. }), "{error:?}");
assert_eq!(error.status(), Some(404));
assert_eq!(error.code(), "not_found");
assert_eq!(error.request_id(), Some("req_test"));
assert_eq!(error.details().and_then(|d| d.get("field")), Some(&"id".into()));
assert_eq!(error.to_string(), "No such resource. (404 not_found, request req_test)");
assert_eq!(api.calls().await.len(), 1);
}
#[tokio::test]
async fn get() {
let response = support::json(r#"{
"object": "privacy_settings",
"groupAdd": "groupAdd 1",
"lastSeen": "lastSeen 1",
"stories": "stories 1",
"profile": "profile 1",
"readReceipts": "readReceipts 1",
"online": "online 1",
"callAdd": "callAdd 1",
"messages": "messages 1",
"defense": "defense 1",
"stickers": "stickers 1"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().privacy().get("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr").await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/privacy");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn update() {
let response = support::json(r#"{
"object": "privacy_settings",
"groupAdd": "groupAdd 1",
"lastSeen": "lastSeen 1",
"stories": "stories 1",
"profile": "profile 1",
"readReceipts": "readReceipts 1",
"online": "online 1",
"callAdd": "callAdd 1",
"messages": "messages 1",
"defense": "defense 1",
"stickers": "stickers 1"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let params: types::PrivacyUpdateParams = support::from_json(r#"{
"groupAdd": "groupAdd 1",
"lastSeen": "lastSeen 1",
"stories": "stories 1",
"profile": "profile 1",
"readReceipts": "readReceipts 1",
"online": "online 1",
"callAdd": "callAdd 1",
"messages": "messages 1"
}"#);
let result = api.client().privacy().update("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "PATCH");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/privacy");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{
"groupAdd": "groupAdd 1",
"lastSeen": "lastSeen 1",
"stories": "stories 1",
"profile": "profile 1",
"readReceipts": "readReceipts 1",
"online": "online 1",
"callAdd": "callAdd 1",
"messages": "messages 1"
}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn update_without_params() {
let response = support::json(r#"{
"object": "privacy_settings",
"groupAdd": "groupAdd 1",
"lastSeen": "lastSeen 1",
"stories": "stories 1",
"profile": "profile 1",
"readReceipts": "readReceipts 1",
"online": "online 1",
"callAdd": "callAdd 1",
"messages": "messages 1",
"defense": "defense 1",
"stickers": "stickers 1"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().privacy().update("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", Default::default()).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "PATCH");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/privacy");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn update_is_not_repeated() {
let api = support::MockApi::start(vec![
support::Reply::status(503),
support::Reply::status(503),
])
.await;
let params: types::PrivacyUpdateParams = support::from_json(r#"{
"groupAdd": "groupAdd 1",
"lastSeen": "lastSeen 1",
"stories": "stories 1",
"profile": "profile 1",
"readReceipts": "readReceipts 1",
"online": "online 1",
"callAdd": "callAdd 1",
"messages": "messages 1"
}"#);
let error = api.client().privacy().update("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).await.unwrap_err();
assert_eq!(error.status(), Some(503));
assert_eq!(api.calls().await.len(), 1);
}
#[tokio::test]
async fn get_story_privacy() {
let response = support::json(r#"{
"object": "story_privacy",
"lists": [
{
"type": "contacts",
"contactIds": [
"contactIds 1"
],
"default": true
}
]
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().privacy().get_story_privacy("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr").await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/privacy/stories");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}