base: Move some test that use the async client out of the base.

master
Damir Jelić 2020-05-07 12:51:53 +02:00
parent 8a4cca3a85
commit a4f5a93880
4 changed files with 127 additions and 83 deletions

View File

@ -1164,8 +1164,9 @@ mod test {
ban_user, create_receipt, create_typing_event, forget_room, invite_user, kick_user, ban_user, create_receipt, create_typing_event, forget_room, invite_user, kick_user,
leave_room, leave_room,
}; };
use super::{AsyncClient, Session, Url}; use super::{AsyncClient, Session, SyncSettings, Url};
use crate::events::collections::all::RoomEvent; use crate::events::collections::all::RoomEvent;
use crate::events::room::member::MembershipState;
use crate::identifiers::{EventId, RoomId, UserId}; use crate::identifiers::{EventId, RoomId, UserId};
use crate::test_builder::EventBuilder; use crate::test_builder::EventBuilder;
@ -1173,6 +1174,36 @@ mod test {
use mockito::{mock, Matcher}; use mockito::{mock, Matcher};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::str::FromStr; use std::str::FromStr;
use std::time::Duration;
#[tokio::test]
async fn account_data() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let session = Session {
access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:example.com").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("../test_data/sync.json")
.create();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let _response = client.sync(sync_settings).await.unwrap();
let bc = &client.base_client;
let ignored_users = bc.ignored_users.read().await;
assert_eq!(1, ignored_users.len())
}
#[tokio::test] #[tokio::test]
async fn client_runner() { async fn client_runner() {
@ -1540,4 +1571,69 @@ mod test {
) )
} }
} }
#[tokio::test]
async fn user_presence() {
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("../test_data/sync.json")
.create();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let _response = client.sync(sync_settings).await.unwrap();
let rooms_lock = &client.base_client.joined_rooms();
let rooms = rooms_lock.read().await;
let room = &rooms
.get(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap())
.unwrap()
.read()
.await;
assert_eq!(2, room.members.len());
for member in room.members.values() {
assert_eq!(MembershipState::Join, member.membership);
}
assert!(room.power_levels.is_some())
}
#[tokio::test]
async fn calculate_room_names_from_summary() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let mut bld = EventBuilder::default().build_with_response(
// this sync has no room.name or room.alias events so only relies on summary
"../test_data/sync_with_summary.json",
"GET",
Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
);
let session = Session {
access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:localhost").unwrap(),
device_id: "DEVICEID".to_owned(),
};
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let client = bld.set_client(client).to_client().await.unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let _response = client.sync(sync_settings).await.unwrap();
assert_eq!(vec!["example, example2"], client.get_room_names().await);
}
} }

View File

@ -918,46 +918,3 @@ impl Client {
} }
} }
} }
#[cfg(test)]
mod test {
use crate::identifiers::UserId;
use crate::{AsyncClient, Session, SyncSettings};
use mockito::{mock, Matcher};
use url::Url;
use std::convert::TryFrom;
use std::str::FromStr;
use std::time::Duration;
#[tokio::test]
async fn account_data() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let session = Session {
access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:example.com").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("../test_data/sync.json")
.create();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let _response = client.sync(sync_settings).await.unwrap();
let bc = &client.base_client;
let ignored_users = bc.ignored_users.read().await;
assert_eq!(1, ignored_users.len())
}
}

View File

@ -7,6 +7,3 @@ mod room_member;
pub use room::{Room, RoomName}; pub use room::{Room, RoomName};
pub use room_member::RoomMember; pub use room_member::RoomMember;
#[allow(dead_code)]
pub type Token = String;

View File

@ -237,7 +237,7 @@ impl Room {
} }
/// Return the display name of the room. /// Return the display name of the room.
pub fn calculate_name(&self) -> String { pub fn display_name(&self) -> String {
self.room_name.calculate_name(&self.members) self.room_name.calculate_name(&self.members)
} }
@ -490,44 +490,42 @@ impl Room {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::api::r0::sync::sync_events::Response as SyncResponse;
use crate::events::room::member::MembershipState; use crate::events::room::member::MembershipState;
use crate::identifiers::UserId; use crate::identifiers::UserId;
use crate::test_builder::EventBuilder; use crate::test_builder::EventBuilder;
use crate::{AsyncClient, Session, SyncSettings}; use crate::{Client, Session};
use mockito::{mock, Matcher}; use http::Response;
use url::Url;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fs::File;
use std::io::Read;
use std::ops::Deref; use std::ops::Deref;
use std::str::FromStr;
use std::time::Duration; fn sync_response(file: &str) -> SyncResponse {
let mut file = File::open(file).unwrap();
let mut data = vec![];
file.read_to_end(&mut data).unwrap();
let response = Response::builder().body(data).unwrap();
SyncResponse::try_from(response).unwrap()
}
#[tokio::test] #[tokio::test]
async fn user_presence() { async fn user_presence() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let session = Session { let session = Session {
access_token: "1234".to_owned(), access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:localhost").unwrap(), user_id: UserId::try_from("@example:localhost").unwrap(),
device_id: "DEVICEID".to_owned(), device_id: "DEVICEID".to_owned(),
}; };
let _m = mock( let mut response = sync_response("../test_data/sync.json");
"GET",
Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
)
.with_status(200)
.with_body_from_file("../test_data/sync.json")
.create();
let client = AsyncClient::new(homeserver, Some(session)).unwrap(); let client = Client::new(Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); client.receive_sync_response(&mut response).await.unwrap();
let _response = client.sync(sync_settings).await.unwrap(); let rooms_lock = &client.joined_rooms();
let rooms_lock = &client.base_client.joined_rooms();
let rooms = rooms_lock.read().await; let rooms = rooms_lock.read().await;
let room = &rooms let room = &rooms
.get(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()) .get(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap())
@ -585,7 +583,7 @@ mod test {
let room = bld.to_room(); let room = bld.to_room();
assert_eq!("tutorial", room.calculate_name()); assert_eq!("tutorial", room.display_name());
} }
#[test] #[test]
@ -602,7 +600,7 @@ mod test {
let room = bld.to_room(); let room = bld.to_room();
assert_eq!("tutorial", room.calculate_name()); assert_eq!("tutorial", room.display_name());
} }
#[test] #[test]
@ -616,31 +614,27 @@ mod test {
let room = bld.to_room(); let room = bld.to_room();
assert_eq!("room name", room.calculate_name()); assert_eq!("room name", room.display_name());
} }
#[tokio::test] #[tokio::test]
async fn calculate_room_names_from_summary() { async fn calculate_room_names_from_summary() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap(); let mut response = sync_response("../test_data/sync_with_summary.json");
let mut bld = EventBuilder::default().build_with_response(
// this sync has no room.name or room.alias events so only relies on summary
"../test_data/sync_with_summary.json",
"GET",
Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
);
let session = Session { let session = Session {
access_token: "1234".to_owned(), access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:localhost").unwrap(), user_id: UserId::try_from("@example:localhost").unwrap(),
device_id: "DEVICEID".to_owned(), device_id: "DEVICEID".to_owned(),
}; };
let client = AsyncClient::new(homeserver, Some(session)).unwrap(); let client = Client::new(Some(session)).unwrap();
let client = bld.set_client(client).to_client().await.unwrap(); client.receive_sync_response(&mut response).await.unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); let mut room_names = vec![];
let _response = client.sync(sync_settings).await.unwrap();
assert_eq!(vec!["example, example2"], client.get_room_names().await); for room in client.joined_rooms().read().await.values() {
room_names.push(room.read().await.display_name())
}
assert_eq!(vec!["example, example2"], room_names);
} }
} }