From ba0cc3d45f9c03590c34bfb7bbb0dd5bc56c0346 Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Fri, 11 Jun 2021 09:37:30 +0200 Subject: [PATCH] matrix-sdk: Add Client::whoami() --- matrix_sdk/src/client.rs | 23 ++++++++++++++++++++++- matrix_sdk_test/src/test_json/mod.rs | 6 ++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 47227b47..d104f7ab 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -100,7 +100,7 @@ use ruma::{ api::{ client::{ r0::{ - account::register, + account::{register, whoami}, device::{delete_devices, get_devices}, directory::{get_public_rooms, get_public_rooms_filtered}, filter::{create_filter::Request as FilterUploadRequest, FilterDefinition}, @@ -2686,6 +2686,12 @@ impl Client { Ok(()) } + + /// Gets information about the owner of a given access token. + pub async fn whoami(&self) -> Result { + let request = whoami::Request::new(); + self.send(request, None).await + } } #[cfg(test)] @@ -3862,4 +3868,19 @@ mod test { .is_ok()); m.assert(); } + + #[tokio::test] + async fn whoami() { + let client = logged_in_client().await; + + let _m = mock("GET", "/_matrix/client/r0/account/whoami") + .with_status(200) + .with_body(test_json::WHOAMI.to_string()) + .match_header("authorization", "Bearer 1234") + .create(); + + let user_id = user_id!("@joe:example.org"); + + assert_eq!(client.whoami().await.unwrap().user_id, user_id); + } } diff --git a/matrix_sdk_test/src/test_json/mod.rs b/matrix_sdk_test/src/test_json/mod.rs index 64b628f6..c0ab33e3 100644 --- a/matrix_sdk_test/src/test_json/mod.rs +++ b/matrix_sdk_test/src/test_json/mod.rs @@ -68,3 +68,9 @@ lazy_static! { } }); } + +lazy_static! { + pub static ref WHOAMI: JsonValue = json!({ + "user_id": "@joe:example.org" + }); +}