709 lines
22 KiB
Rust
709 lines
22 KiB
Rust
//! The handlers against the SDK's own mock homeserver
|
|
//! (`matrix_sdk::test_utils::mocks`): what each one asks the server and what it
|
|
//! answers the agent, with the server's replies scripted per test. The pure
|
|
//! helpers have their own tests next to them in `handlers.rs`.
|
|
|
|
use matrix_sdk::Client;
|
|
use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType;
|
|
use matrix_sdk::ruma::events::room::member::MembershipState;
|
|
use matrix_sdk::ruma::events::{AnyGlobalAccountDataEvent, AnyTimelineEvent};
|
|
use matrix_sdk::ruma::serde::Raw;
|
|
use matrix_sdk::ruma::{EventId, event_id, room_id, user_id};
|
|
use matrix_sdk::test_utils::mocks::{
|
|
MatrixMockServer, RoomContextResponseTemplate, RoomMessagesResponseTemplate,
|
|
};
|
|
use matrix_sdk_test::event_factory::EventFactory;
|
|
use matrix_sdk_test::{InvitedRoomBuilder, JoinedRoomBuilder};
|
|
use serde_json::Value;
|
|
|
|
use super::*;
|
|
|
|
/// The payload of an `Ok` response, or a panic naming the error.
|
|
fn ok(response: DaemonResponse) -> Value {
|
|
match response {
|
|
DaemonResponse::Ok { payload } => payload,
|
|
DaemonResponse::Error { message } => panic!("expected ok, got error: {message}"),
|
|
}
|
|
}
|
|
|
|
/// The message of an `Error` response, or a panic naming the payload.
|
|
fn err(response: DaemonResponse) -> String {
|
|
match response {
|
|
DaemonResponse::Error { message } => message,
|
|
DaemonResponse::Ok { payload } => panic!("expected an error, got ok: {payload}"),
|
|
}
|
|
}
|
|
|
|
/// A client on a mock server that has joined `!room:localhost`, whose history
|
|
/// is `history` (newest first) and which is not encrypted.
|
|
async fn room_with(history: Vec<Raw<AnyTimelineEvent>>) -> (MatrixMockServer, Client) {
|
|
let (server, client) = joined_room().await;
|
|
server
|
|
.mock_room_messages()
|
|
.ok(RoomMessagesResponseTemplate::default().events(history))
|
|
.mount()
|
|
.await;
|
|
(server, client)
|
|
}
|
|
|
|
/// A client on a mock server that has joined `!room:localhost`, which is not
|
|
/// encrypted, with no history mocked yet.
|
|
async fn joined_room() -> (MatrixMockServer, Client) {
|
|
let server = MatrixMockServer::new().await;
|
|
let client = server.client_builder().build().await;
|
|
server
|
|
.sync_joined_room(&client, room_id!("!room:localhost"))
|
|
.await;
|
|
server.mock_room_state_encryption().plain().mount().await;
|
|
(server, client)
|
|
}
|
|
|
|
/// [`room_with`] an empty history: the agent is caught up.
|
|
async fn caught_up_room() -> (MatrixMockServer, Client) {
|
|
room_with(Vec::new()).await
|
|
}
|
|
|
|
/// A text message from `@bob:localhost` in `!room:localhost`.
|
|
fn bobs_message(id: &str, body: &str) -> Raw<AnyTimelineEvent> {
|
|
EventFactory::new()
|
|
.room(room_id!("!room:localhost"))
|
|
.sender(user_id!("@bob:localhost"))
|
|
.text_msg(body)
|
|
.event_id(<&EventId>::try_from(id).expect("event id"))
|
|
.into_raw_timeline()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_message_to_a_caught_up_room_is_sent() {
|
|
let (server, client) = caught_up_room().await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$sent:localhost"))
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(send_message(&client, "!room:localhost", "hello").await);
|
|
|
|
assert_eq!(got["event_id"], "$sent:localhost");
|
|
assert_eq!(got["room_id"], "!room:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_message_from_someone_else_blocks_the_send() {
|
|
let (server, client) = room_with(vec![bobs_message("$new:localhost", "are you there?")]).await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$x:localhost"))
|
|
.never()
|
|
.mount()
|
|
.await;
|
|
|
|
let message = err(send_message(&client, "!room:localhost", "hello").await);
|
|
|
|
assert!(message.contains("unread message"), "{message}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn marking_an_event_read_sends_a_read_receipt() {
|
|
let (server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
|
|
server
|
|
.mock_send_receipt(ReceiptType::Read)
|
|
.ok()
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(mark_read(&client, "!room:localhost", "$new:localhost").await);
|
|
|
|
assert_eq!(got["marked_read"], "$new:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_room_that_is_not_joined_or_not_a_room_is_refused() {
|
|
let (_server, client) = caught_up_room().await;
|
|
|
|
let unknown = err(send_message(&client, "!other:localhost", "hi").await);
|
|
assert!(unknown.contains("not joined"), "{unknown}");
|
|
let malformed = err(send_message(&client, "not a room", "hi").await);
|
|
assert!(malformed.contains("invalid room reference"), "{malformed}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_reaction_names_its_target_and_needs_no_catch_up() {
|
|
let (server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$reaction:localhost"))
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(send_reaction(&client, "!room:localhost", "$new:localhost", "👍").await);
|
|
|
|
assert_eq!(got["event_id"], "$reaction:localhost");
|
|
assert_eq!(got["target"], "$new:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_malformed_event_id_is_refused_before_the_server_is_asked() {
|
|
let (_server, client) = caught_up_room().await;
|
|
|
|
for response in [
|
|
send_reaction(&client, "!room:localhost", "nope", "👍").await,
|
|
mark_read(&client, "!room:localhost", "nope").await,
|
|
send_redact(&client, "!room:localhost", "nope", None).await,
|
|
] {
|
|
assert!(err(response).contains("invalid event_id"));
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_redaction_goes_to_the_server_and_names_its_target() {
|
|
let (server, client) = caught_up_room().await;
|
|
server
|
|
.mock_room_redact()
|
|
.ok(event_id!("$redaction:localhost"))
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(send_redact(&client, "!room:localhost", "$old:localhost", Some("spam")).await);
|
|
|
|
assert_eq!(got["event_id"], "$redaction:localhost");
|
|
assert_eq!(got["target"], "$old:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_server_refusal_reaches_the_agent_as_an_error() {
|
|
let (server, client) = caught_up_room().await;
|
|
server.mock_room_send().error500().mount().await;
|
|
|
|
let message = err(send_message(&client, "!room:localhost", "hello").await);
|
|
|
|
assert!(message.contains("send to !room:localhost"), "{message}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_joined_rooms_are_listed() {
|
|
let (_server, client) = caught_up_room().await;
|
|
|
|
let got = ok(list_rooms(&client).await);
|
|
|
|
let rooms = got.as_array().expect("a list");
|
|
assert_eq!(rooms.len(), 1);
|
|
assert_eq!(rooms[0]["room_id"], "!room:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_invite_is_listed_and_a_joined_room_is_not_an_invite() {
|
|
let (server, client) = caught_up_room().await;
|
|
server
|
|
.sync_room(
|
|
&client,
|
|
InvitedRoomBuilder::new(room_id!("!invited:localhost")),
|
|
)
|
|
.await;
|
|
|
|
let got = ok(list_invites(&client).await);
|
|
|
|
let invites = got.as_array().expect("a list");
|
|
assert_eq!(invites.len(), 1);
|
|
assert_eq!(invites[0]["room_id"], "!invited:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_invite_to_a_room_is_sent_to_the_server_and_a_bad_user_is_refused() {
|
|
let (server, client) = caught_up_room().await;
|
|
server.mock_invite_user_by_id().ok().expect(1).mount().await;
|
|
|
|
let got = ok(invite_user(&client, "!room:localhost", "@bob:localhost").await);
|
|
|
|
assert_eq!(got["invited"], true);
|
|
assert_eq!(got["user_id"], "@bob:localhost");
|
|
let bad = err(invite_user(&client, "!room:localhost", "bob").await);
|
|
assert!(bad.contains("invalid user_id"), "{bad}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rejecting_an_invite_leaves_the_room() {
|
|
let (server, client) = caught_up_room().await;
|
|
server
|
|
.sync_room(
|
|
&client,
|
|
InvitedRoomBuilder::new(room_id!("!invited:localhost")),
|
|
)
|
|
.await;
|
|
server
|
|
.mock_room_leave()
|
|
.ok(room_id!("!invited:localhost"))
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(resolve_invite(&client, "!invited:localhost", InviteAction::Reject).await);
|
|
|
|
assert_eq!(got["rejected"], true);
|
|
assert_eq!(got["room_id"], "!invited:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn leaving_a_joined_room_asks_the_server_and_an_unknown_room_is_refused() {
|
|
let (server, client) = caught_up_room().await;
|
|
server
|
|
.mock_room_leave()
|
|
.ok(room_id!("!room:localhost"))
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(leave_room(&client, "!room:localhost").await);
|
|
|
|
assert_eq!(got["left"], true);
|
|
assert_eq!(got["room_id"], "!room:localhost");
|
|
let unknown = err(leave_room(&client, "!never:localhost").await);
|
|
assert!(unknown.contains("not joined"), "{unknown}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reading_a_room_returns_its_events_newest_first_with_sender_and_body() {
|
|
let (_server, client) = room_with(vec![
|
|
bobs_message("$two:localhost", "second"),
|
|
bobs_message("$one:localhost", "first"),
|
|
])
|
|
.await;
|
|
|
|
let got = ok(read_room(&client, "!room:localhost", None, None, None).await);
|
|
|
|
let events = got.as_array().expect("a list");
|
|
assert_eq!(events.len(), 2);
|
|
assert_eq!(events[0]["event_id"], "$two:localhost");
|
|
assert_eq!(events[0]["sender"], "@bob:localhost");
|
|
assert_eq!(events[0]["body"], "second");
|
|
assert_eq!(events[1]["body"], "first");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_read_asks_for_the_limit_clamped_to_the_allowed_range() {
|
|
let server = MatrixMockServer::new().await;
|
|
let client = server.client_builder().build().await;
|
|
server
|
|
.sync_joined_room(&client, room_id!("!room:localhost"))
|
|
.await;
|
|
server.mock_room_state_encryption().plain().mount().await;
|
|
server
|
|
.mock_room_messages()
|
|
.match_limit(200)
|
|
.ok(RoomMessagesResponseTemplate::default())
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
ok(read_room(&client, "!room:localhost", Some(5000), None, None).await);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_read_with_both_anchors_or_a_bad_anchor_is_refused() {
|
|
let (_server, client) = caught_up_room().await;
|
|
|
|
let both = err(read_room(
|
|
&client,
|
|
"!room:localhost",
|
|
None,
|
|
Some("$a:localhost".to_owned()),
|
|
Some("$b:localhost".to_owned()),
|
|
)
|
|
.await);
|
|
assert!(both.contains("mutually exclusive"), "{both}");
|
|
let bad = err(read_room(
|
|
&client,
|
|
"!room:localhost",
|
|
None,
|
|
Some("nope".to_owned()),
|
|
None,
|
|
)
|
|
.await);
|
|
assert!(bad.contains("invalid event_id"), "{bad}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_joined_members_are_listed_with_their_display_names() {
|
|
let (server, client) = caught_up_room().await;
|
|
let f = EventFactory::new().room(room_id!("!room:localhost"));
|
|
server
|
|
.mock_get_members()
|
|
.ok(vec![
|
|
f.member(user_id!("@bob:localhost"))
|
|
.display_name("Bob")
|
|
.into_raw(),
|
|
])
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(list_room_members(&client, "!room:localhost").await);
|
|
|
|
let members = got.as_array().expect("a list");
|
|
assert_eq!(members.len(), 1);
|
|
assert_eq!(members[0]["user_id"], "@bob:localhost");
|
|
assert_eq!(members[0]["display_name"], "Bob");
|
|
}
|
|
|
|
/// `join_room_by_id_or_alias` posts to `/join/<room>`, which the SDK's mock
|
|
/// server has no prefab for (`mock_room_join` is `/rooms/<room>/join`).
|
|
async fn mock_join(server: &MatrixMockServer, room: &str) {
|
|
use wiremock::matchers::{method, path};
|
|
use wiremock::{Mock, ResponseTemplate};
|
|
Mock::given(method("POST"))
|
|
.and(path(format!("/_matrix/client/v3/join/{room}")))
|
|
.respond_with(
|
|
ResponseTemplate::new(200).set_body_json(serde_json::json!({"room_id": room})),
|
|
)
|
|
.expect(1)
|
|
.mount(server.server())
|
|
.await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn joining_a_room_asks_the_server_and_a_bad_reference_is_refused() {
|
|
let (server, client) = caught_up_room().await;
|
|
mock_join(&server, "!other:localhost").await;
|
|
|
|
let got = ok(join_room(&client, "!other:localhost").await);
|
|
|
|
assert_eq!(got["joined"], true);
|
|
assert_eq!(got["room_id"], "!other:localhost");
|
|
let bad = err(join_room(&client, "not a room").await);
|
|
assert!(bad.contains("invalid room reference"), "{bad}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn accepting_an_invite_is_joining_the_room() {
|
|
let (server, client) = caught_up_room().await;
|
|
mock_join(&server, "!invited:localhost").await;
|
|
|
|
let got = ok(resolve_invite(&client, "!invited:localhost", InviteAction::Accept).await);
|
|
|
|
assert_eq!(got["joined"], true);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_rooms_with_unread_messages_are_summarised_with_the_latest_sender() {
|
|
let (_server, client) = room_with(vec![bobs_message("$new:localhost", "are you there?")]).await;
|
|
|
|
let got = ok(unread_summary(&client).await);
|
|
|
|
let rooms = got.as_array().expect("a list");
|
|
assert_eq!(rooms.len(), 1);
|
|
assert_eq!(rooms[0]["label"], "!room:localhost");
|
|
assert_eq!(rooms[0]["last_sender"], "@bob:localhost");
|
|
assert_eq!(rooms[0]["last_body"], "are you there?");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_caught_up_room_has_nothing_to_summarise() {
|
|
let (_server, client) = caught_up_room().await;
|
|
|
|
let got = ok(unread_summary(&client).await);
|
|
|
|
assert_eq!(got.as_array().expect("a list").len(), 0);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_file_is_uploaded_and_posted_with_its_guessed_type_and_a_caption() {
|
|
let (server, client) = caught_up_room().await;
|
|
// The SDK asks for the server's upload limit before it uploads.
|
|
server
|
|
.mock_authenticated_media_config()
|
|
.ok_default()
|
|
.mount()
|
|
.await;
|
|
server
|
|
.mock_media_config()
|
|
.ok(matrix_sdk::ruma::UInt::from(10_000_000_u32))
|
|
.mount()
|
|
.await;
|
|
let (captured, upload) = server
|
|
.mock_upload()
|
|
.ok_with_capture(matrix_sdk::ruma::mxc_uri!("mxc://localhost/abc"));
|
|
upload.mount().await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$file:localhost"))
|
|
.expect(2)
|
|
.mount()
|
|
.await;
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("notes.txt");
|
|
std::fs::write(&path, b"hello").unwrap();
|
|
|
|
let got = ok(send_file(
|
|
&client,
|
|
"!room:localhost",
|
|
path.to_str().unwrap(),
|
|
Some("the notes"),
|
|
)
|
|
.await);
|
|
|
|
assert_eq!(got["filename"], "notes.txt");
|
|
assert_eq!(got["mime"], "text/plain");
|
|
assert_eq!(got["event_id"], "$file:localhost");
|
|
assert_eq!(captured.await.unwrap(), b"hello");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_file_that_is_missing_or_not_a_file_is_refused_before_any_upload() {
|
|
let (_server, client) = caught_up_room().await;
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
let missing = err(send_file(&client, "!room:localhost", "/nonexistent/file", None).await);
|
|
assert!(missing.contains("stat /nonexistent/file"), "{missing}");
|
|
let directory = err(send_file(
|
|
&client,
|
|
"!room:localhost",
|
|
dir.path().to_str().unwrap(),
|
|
None,
|
|
)
|
|
.await);
|
|
assert!(directory.contains("not a regular file"), "{directory}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_file_waits_for_the_agent_to_catch_up_like_a_message() {
|
|
let (_server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
|
|
|
|
let message = err(send_file(&client, "!room:localhost", "/nonexistent", None).await);
|
|
|
|
assert!(message.contains("unread message"), "{message}");
|
|
}
|
|
|
|
/// A client with one direct room, `!dm:localhost`, with `@bob:localhost` in
|
|
/// the `m.direct` account data, empty history, and `bob` as `bob_is` there.
|
|
async fn dm_with_bob(bob_is: Option<MembershipState>) -> (MatrixMockServer, Client) {
|
|
let server = MatrixMockServer::new().await;
|
|
let client = server.client_builder().build().await;
|
|
let direct: Raw<AnyGlobalAccountDataEvent> = serde_json::from_value(serde_json::json!({
|
|
"type": "m.direct",
|
|
"content": {"@bob:localhost": ["!dm:localhost"]},
|
|
}))
|
|
.unwrap();
|
|
server
|
|
.mock_sync()
|
|
.ok_and_run(&client, |builder| {
|
|
builder.add_joined_room(JoinedRoomBuilder::new(room_id!("!dm:localhost")));
|
|
builder.add_global_account_data(direct);
|
|
})
|
|
.await;
|
|
server.mock_room_state_encryption().plain().mount().await;
|
|
server
|
|
.mock_room_messages()
|
|
.ok(RoomMessagesResponseTemplate::default())
|
|
.mount()
|
|
.await;
|
|
let f = EventFactory::new().room(room_id!("!dm:localhost"));
|
|
let mut members = vec![f.member(user_id!("@example:localhost")).into_raw()];
|
|
if let Some(state) = bob_is {
|
|
members.push(
|
|
f.member(user_id!("@bob:localhost"))
|
|
.membership(state)
|
|
.into_raw(),
|
|
);
|
|
}
|
|
server.mock_get_members().ok(members).mount().await;
|
|
(server, client)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_dm_goes_to_the_existing_direct_room_and_says_the_recipient_is_in_it() {
|
|
let (server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$dm:localhost"))
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(send_dm(&client, "@bob:localhost", "hi").await);
|
|
|
|
assert_eq!(got["room_id"], "!dm:localhost");
|
|
assert_eq!(got["user_id"], "@bob:localhost");
|
|
assert_eq!(got["recipient"], "joined");
|
|
assert!(got.get("warning").is_none());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_bare_name_is_that_user_on_the_own_homeserver() {
|
|
let (server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$dm:localhost"))
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(send_dm(&client, "bob", "hi").await);
|
|
|
|
assert_eq!(got["user_id"], "@bob:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_dm_to_someone_who_has_not_joined_warns_that_they_will_see_it_later() {
|
|
let (server, client) = dm_with_bob(Some(MembershipState::Invite)).await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$dm:localhost"))
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(send_dm(&client, "@bob:localhost", "hi").await);
|
|
|
|
assert_eq!(got["recipient"], "invited");
|
|
assert!(got["warning"].as_str().unwrap().contains("not joined"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_dm_nobody_is_in_is_an_error_and_nothing_is_sent() {
|
|
let (server, client) = dm_with_bob(None).await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$dm:localhost"))
|
|
.never()
|
|
.mount()
|
|
.await;
|
|
|
|
let message = err(send_dm(&client, "@bob:localhost", "hi").await);
|
|
|
|
assert!(message.contains("is not in DM room"), "{message}");
|
|
assert!(message.contains("Nothing was sent"), "{message}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn opening_a_dm_names_the_room_and_sends_nothing() {
|
|
let (server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$dm:localhost"))
|
|
.never()
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(open_dm(&client, "@bob:localhost").await);
|
|
|
|
assert_eq!(got["room_id"], "!dm:localhost");
|
|
assert_eq!(got["recipient"], "joined");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_dm_with_a_malformed_user_is_refused() {
|
|
let (_server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
|
|
|
let message = err(send_dm(&client, "@bob", "hi").await);
|
|
|
|
assert!(message.contains("invalid user_id"), "{message}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_reply_names_its_target_and_is_sent() {
|
|
let (server, client) = caught_up_room().await;
|
|
server
|
|
.mock_room_event()
|
|
.ok(EventFactory::new()
|
|
.room(room_id!("!room:localhost"))
|
|
.sender(user_id!("@bob:localhost"))
|
|
.text_msg("question?")
|
|
.event_id(event_id!("$q:localhost"))
|
|
.into_event())
|
|
.mount()
|
|
.await;
|
|
server
|
|
.mock_room_send()
|
|
.ok(event_id!("$answer:localhost"))
|
|
.expect(1)
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(send_reply(&client, "!room:localhost", "$q:localhost", "yes").await);
|
|
|
|
assert_eq!(got["event_id"], "$answer:localhost");
|
|
assert_eq!(got["target"], "$q:localhost");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_reply_waits_for_the_catch_up_and_needs_a_real_event_id() {
|
|
let (_server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
|
|
let blocked = err(send_reply(&client, "!room:localhost", "$new:localhost", "yes").await);
|
|
assert!(blocked.contains("unread message"), "{blocked}");
|
|
|
|
let (_server, client) = caught_up_room().await;
|
|
let bad = err(send_reply(&client, "!room:localhost", "nope", "yes").await);
|
|
assert!(bad.contains("invalid event_id"), "{bad}");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reading_back_from_an_anchor_returns_the_anchor_then_the_older_events() {
|
|
let (server, client) = joined_room().await;
|
|
server
|
|
.mock_room_event_context()
|
|
.ok(RoomContextResponseTemplate::new(
|
|
EventFactory::new()
|
|
.room(room_id!("!room:localhost"))
|
|
.sender(user_id!("@bob:localhost"))
|
|
.text_msg("middle")
|
|
.event_id(event_id!("$anchor:localhost"))
|
|
.into_event(),
|
|
)
|
|
.start("before"))
|
|
.mount()
|
|
.await;
|
|
server
|
|
.mock_room_messages()
|
|
.ok(RoomMessagesResponseTemplate::default()
|
|
.events(vec![bobs_message("$older:localhost", "older")]))
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(read_room(
|
|
&client,
|
|
"!room:localhost",
|
|
None,
|
|
None,
|
|
Some("$anchor:localhost".to_owned()),
|
|
)
|
|
.await);
|
|
|
|
let bodies: Vec<&str> = got
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|e| e["body"].as_str().unwrap())
|
|
.collect();
|
|
assert_eq!(bodies, ["middle", "older"]);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reading_forward_from_the_newest_event_returns_just_that_event() {
|
|
let (server, client) = joined_room().await;
|
|
server
|
|
.mock_room_event_context()
|
|
.ok(RoomContextResponseTemplate::new(
|
|
EventFactory::new()
|
|
.room(room_id!("!room:localhost"))
|
|
.sender(user_id!("@bob:localhost"))
|
|
.text_msg("latest")
|
|
.event_id(event_id!("$latest:localhost"))
|
|
.into_event(),
|
|
))
|
|
.mount()
|
|
.await;
|
|
|
|
let got = ok(read_room(
|
|
&client,
|
|
"!room:localhost",
|
|
None,
|
|
Some("$latest:localhost".to_owned()),
|
|
None,
|
|
)
|
|
.await);
|
|
|
|
let events = got.as_array().unwrap();
|
|
assert_eq!(events.len(), 1);
|
|
assert_eq!(events[0]["body"], "latest");
|
|
}
|