matrix-rust-sdk/tests/async_client_tests.rs

90 lines
2.4 KiB
Rust
Raw Normal View History

2020-02-24 11:51:42 +00:00
use matrix_sdk::identifiers::UserId;
use matrix_sdk::{AsyncClient, Session, SyncSettings};
use mockito::{mock, Matcher};
2019-11-17 18:59:08 +00:00
use url::Url;
use std::convert::TryFrom;
use std::str::FromStr;
use std::time::Duration;
2020-03-28 12:27:16 +00:00
#[tokio::test]
async fn login() {
2019-11-17 18:59:08 +00:00
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let _m = mock("POST", "/_matrix/client/r0/login")
.with_status(200)
.with_body_from_file("tests/data/login_response.json")
.create();
let mut client = AsyncClient::new(homeserver, None).unwrap();
2020-03-28 12:27:16 +00:00
client
.login("example", "wordpass", None, None)
.await
2019-11-17 18:59:08 +00:00
.unwrap();
2020-03-28 12:27:16 +00:00
let logged_in = client.logged_in().await;
assert!(logged_in, "Clint should be logged in");
2019-11-17 18:59:08 +00:00
}
2020-03-28 12:27:16 +00:00
#[tokio::test]
async fn sync() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let session = Session {
access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:localhost").unwrap(),
device_id: "DEVICEID".to_owned(),
};
let _m = mock(
"GET",
Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
)
.with_status(200)
.with_body_from_file("tests/data/sync.json")
.create();
let mut client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
2020-03-28 12:27:16 +00:00
let response = client.sync(sync_settings).await.unwrap();
assert_ne!(response.next_batch, "");
2020-03-28 12:27:16 +00:00
assert!(client.sync_token().await.is_some());
}
2020-03-28 12:27:16 +00:00
#[tokio::test]
async fn room_names() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let session = Session {
access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:localhost").unwrap(),
device_id: "DEVICEID".to_owned(),
};
let _m = mock(
"GET",
Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
)
.with_status(200)
2020-03-27 21:26:10 +00:00
.with_body_from_file("tests/data/sync.json")
.create();
let mut client = AsyncClient::new(homeserver, Some(session)).unwrap();
2020-03-28 12:27:16 +00:00
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
2020-03-28 12:27:16 +00:00
let _response = client.sync(sync_settings).await.unwrap();
2020-03-28 12:27:16 +00:00
assert_eq!(vec!["tutorial"], client.get_room_names().await);
2020-03-27 21:23:49 +00:00
assert_eq!(
Some("tutorial".into()),
2020-03-28 12:27:16 +00:00
client.get_room_name("!SVkFJHzfwvuaIEawgC:localhost").await
2020-03-27 21:23:49 +00:00
);
}