#![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 params: types::ProfileUpdateParams = support::from_json(r#"{"about":"about 1","name":"name 1"}"#);
let error = api.client().profile().update("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).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 update() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
let params: types::ProfileUpdateParams = support::from_json(r#"{"about":"about 1","name":"name 1"}"#);
api.client().profile().update("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "PATCH");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/profile");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"about":"about 1","name":"name 1"}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn update_without_params() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().profile().update("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", Default::default()).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "PATCH");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/profile");
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::ProfileUpdateParams = support::from_json(r#"{"about":"about 1","name":"name 1"}"#);
let error = api.client().profile().update("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).await.unwrap_err();
assert_eq!(error.status(), Some(503));
assert_eq!(api.calls().await.len(), 1);
}
#[tokio::test]
async fn set_picture() {
let response = support::json(r#"{"object":"picture","id":"id 1","url":"url 1","preview":true}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let params: types::ProfileSetPictureParams = support::from_json(r#"{"url":"https://example.com/url"}"#);
let result = api.client().profile().set_picture("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "PUT");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/profile/picture");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"url":"https://example.com/url"}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn delete_picture() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().profile().delete_picture("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "DELETE");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/profile/picture");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}