2020-04-03 13:07:40 +00:00
|
|
|
use matrix_sdk::identifiers::{RoomId, UserId};
|
2020-02-24 11:51:42 +00:00
|
|
|
use matrix_sdk::{AsyncClient, Session, SyncSettings};
|
2019-11-17 20:23:01 +00:00
|
|
|
|
|
|
|
use mockito::{mock, Matcher};
|
2019-11-17 18:59:08 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2019-11-17 20:23:01 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::str::FromStr;
|
2020-03-24 15:18:56 +00:00
|
|
|
use std::time::Duration;
|
2019-11-17 20:23:01 +00:00
|
|
|
|
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();
|
|
|
|
|
2020-04-21 13:47:36 +00:00
|
|
|
let client = AsyncClient::new(homeserver, None).unwrap();
|
2019-11-17 18:59:08 +00:00
|
|
|
|
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;
|
2020-03-11 10:42:59 +00:00
|
|
|
assert!(logged_in, "Clint should be logged in");
|
2019-11-17 18:59:08 +00:00
|
|
|
}
|
2019-11-17 20:23:01 +00:00
|
|
|
|
2020-03-28 12:27:16 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn sync() {
|
2019-11-17 20:23:01 +00:00
|
|
|
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
|
|
|
|
|
|
|
|
let session = Session {
|
|
|
|
access_token: "1234".to_owned(),
|
2020-03-31 19:18:27 +00:00
|
|
|
user_id: UserId::try_from("@example:localhost").unwrap(),
|
2019-11-17 20:23:01 +00:00
|
|
|
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();
|
|
|
|
|
2020-04-21 13:47:36 +00:00
|
|
|
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
|
2019-11-17 20:23:01 +00:00
|
|
|
|
2020-03-24 15:18:56 +00:00
|
|
|
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
|
2019-11-17 20:23:01 +00:00
|
|
|
|
2020-03-28 12:27:16 +00:00
|
|
|
let response = client.sync(sync_settings).await.unwrap();
|
2020-03-11 10:42:59 +00:00
|
|
|
|
|
|
|
assert_ne!(response.next_batch, "");
|
|
|
|
|
2020-03-28 12:27:16 +00:00
|
|
|
assert!(client.sync_token().await.is_some());
|
2019-11-17 20:23:01 +00:00
|
|
|
}
|
2020-03-27 21:22:11 +00:00
|
|
|
|
2020-03-28 12:27:16 +00:00
|
|
|
#[tokio::test]
|
2020-03-31 13:01:48 +00:00
|
|
|
async fn room_names() {
|
2020-03-27 21:22:11 +00:00
|
|
|
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
|
|
|
|
|
|
|
|
let session = Session {
|
|
|
|
access_token: "1234".to_owned(),
|
2020-03-31 19:18:27 +00:00
|
|
|
user_id: UserId::try_from("@example:localhost").unwrap(),
|
2020-03-27 21:22:11 +00:00
|
|
|
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")
|
2020-03-27 21:22:11 +00:00
|
|
|
.create();
|
|
|
|
|
2020-04-21 13:47:36 +00:00
|
|
|
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
|
2020-03-27 21:22:11 +00:00
|
|
|
|
2020-03-28 12:27:16 +00:00
|
|
|
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
|
2020-03-27 21:22:11 +00:00
|
|
|
|
2020-03-28 12:27:16 +00:00
|
|
|
let _response = client.sync(sync_settings).await.unwrap();
|
2020-03-27 21:22:11 +00:00
|
|
|
|
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-04-03 13:14:46 +00:00
|
|
|
client
|
|
|
|
.get_room_name(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap())
|
|
|
|
.await
|
2020-03-27 21:23:49 +00:00
|
|
|
);
|
2020-03-27 21:22:11 +00:00
|
|
|
}
|