Skip to content

Commit

Permalink
Pinning Tests Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Jul 26, 2024
1 parent ee84daa commit d5f3c62
Showing 1 changed file with 104 additions and 173 deletions.
277 changes: 104 additions & 173 deletions crates/matrix-sdk-ui/tests/integration/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,235 +510,166 @@ async fn test_duplicate_maintains_correct_order() {

#[async_test]
async fn test_pin_event_is_sent_succesfully() {

Check warning on line 512 in crates/matrix-sdk-ui/tests/integration/timeline/mod.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"succesfully" should be "successfully".
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let f = EventFactory::new().sender(user_id!("@a:b.c"));
let mut sync_builder = SyncResponseBuilder::new();
let event_id = event_id!("$a");
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let room = client.get_room(room_id).unwrap();
let timeline = room.timeline().await.unwrap();

sync_builder.add_joined_room(
JoinedRoomBuilder::new(room_id)
.add_timeline_event(f.text_msg("A").event_id(event_id).into_raw_sync()),
);
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
let mut setup = PinningTestSetup::new().await;
let timeline = setup.timeline().await;

setup.mock_sync(false).await;
assert!(!timeline.items().await.is_empty());

// Pinning a remote event works.
Mock::given(method("PUT"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.room.pinned_events/.*?"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
// Pinning a remote event succeeds.
setup
.mock_response(ResponseTemplate::new(200).set_body_json(json!({
"event_id": "$42"
})))
.mount(&server)
.await;

let pin_event_result = timeline.pin_event(event_id).await.unwrap();
assert!(pin_event_result);
let event_id = setup.event_id();
assert!(timeline.pin_event(event_id).await.unwrap());

server.reset().await;
setup.reset_server().await;
}

#[async_test]
async fn test_pin_event_is_returning_false_because_is_already_pinned() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let f = EventFactory::new().sender(user_id!("@a:b.c"));
let mut sync_builder = SyncResponseBuilder::new();
let event_id = event_id!("$a");
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let room = client.get_room(room_id).unwrap();
let timeline = room.timeline().await.unwrap();

sync_builder.add_joined_room(
JoinedRoomBuilder::new(room_id)
.add_timeline_event(f.text_msg("A").event_id(event_id).into_raw_sync())
.add_state_event(StateTestEvent::RoomPinnedEvents),
);
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
let mut setup = PinningTestSetup::new().await;
let timeline = setup.timeline().await;

setup.mock_sync(true).await;
assert!(!timeline.items().await.is_empty());

let pin_event_result = timeline.pin_event(event_id).await.unwrap();
assert!(!pin_event_result);
let event_id = setup.event_id();
assert!(!timeline.pin_event(event_id).await.unwrap());

server.reset().await;
setup.reset_server().await;
}

#[async_test]
async fn test_pin_event_is_returning_an_error() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let f = EventFactory::new().sender(user_id!("@a:b.c"));
let mut sync_builder = SyncResponseBuilder::new();
let event_id = event_id!("$a");
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let room = client.get_room(room_id).unwrap();
let timeline = room.timeline().await.unwrap();

sync_builder.add_joined_room(
JoinedRoomBuilder::new(room_id)
.add_timeline_event(f.text_msg("A").event_id(event_id).into_raw_sync()),
);
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
let mut setup = PinningTestSetup::new().await;
let timeline = setup.timeline().await;

setup.mock_sync(false).await;
assert!(!timeline.items().await.is_empty());

// Pinning a remote event fails.
Mock::given(method("PUT"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.room.pinned_events/.*?"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(400))
.mount(&server)
.await;
setup.mock_response(ResponseTemplate::new(400)).await;

let event_id = setup.event_id();
assert!(timeline.pin_event(event_id).await.is_err());

server.reset().await;
setup.reset_server().await;
}

#[async_test]
async fn test_unpin_event_is_sent_succesfully() {

Check warning on line 564 in crates/matrix-sdk-ui/tests/integration/timeline/mod.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"succesfully" should be "successfully".
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let f = EventFactory::new().sender(user_id!("@a:b.c"));
let mut sync_builder = SyncResponseBuilder::new();
let event_id = event_id!("$a");
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let room = client.get_room(room_id).unwrap();
let timeline = room.timeline().await.unwrap();

sync_builder.add_joined_room(
JoinedRoomBuilder::new(room_id)
.add_timeline_event(f.text_msg("A").event_id(event_id).into_raw_sync())
.add_state_event(StateTestEvent::RoomPinnedEvents),
);
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
let mut setup = PinningTestSetup::new().await;
let timeline = setup.timeline().await;

setup.mock_sync(true).await;
assert!(!timeline.items().await.is_empty());

// Pinning a remote event works.
Mock::given(method("PUT"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.room.pinned_events/.*?"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
// Unpinning a remote event succeeds.
setup
.mock_response(ResponseTemplate::new(200).set_body_json(json!({
"event_id": "$42"
})))
.mount(&server)
.await;

let unpin_event_result = timeline.unpin_event(event_id).await.unwrap();
assert!(unpin_event_result);
let event_id = setup.event_id();
assert!(timeline.unpin_event(event_id).await.unwrap());

server.reset().await;
setup.reset_server().await;
}

#[async_test]
async fn test_unpin_event_is_returning_false_because_is_not_pinned() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let f = EventFactory::new().sender(user_id!("@a:b.c"));
let mut sync_builder = SyncResponseBuilder::new();
let event_id = event_id!("$a");
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let room = client.get_room(room_id).unwrap();
let timeline = room.timeline().await.unwrap();

sync_builder.add_joined_room(
JoinedRoomBuilder::new(room_id)
.add_timeline_event(f.text_msg("A").event_id(event_id).into_raw_sync()),
);
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
let mut setup = PinningTestSetup::new().await;
let timeline = setup.timeline().await;

setup.mock_sync(false).await;
assert!(!timeline.items().await.is_empty());

let unpin_event_result = timeline.unpin_event(event_id).await.unwrap();
assert!(!unpin_event_result);
let event_id = setup.event_id();
assert!(!timeline.unpin_event(event_id).await.unwrap());

server.reset().await;
setup.reset_server().await;
}

#[async_test]
async fn test_unpin_event_is_returning_an_error() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let f = EventFactory::new().sender(user_id!("@a:b.c"));
let mut sync_builder = SyncResponseBuilder::new();
let event_id = event_id!("$a");
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let room = client.get_room(room_id).unwrap();
let timeline = room.timeline().await.unwrap();

sync_builder.add_joined_room(
JoinedRoomBuilder::new(room_id)
.add_timeline_event(f.text_msg("A").event_id(event_id).into_raw_sync())
.add_state_event(StateTestEvent::RoomPinnedEvents),
);
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
let mut setup = PinningTestSetup::new().await;
let timeline = setup.timeline().await;

setup.mock_sync(true).await;
assert!(!timeline.items().await.is_empty());

// Unpinning a remote event fails.
Mock::given(method("PUT"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.room.pinned_events/.*?"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(400))
.mount(&server)
.await;
setup.mock_response(ResponseTemplate::new(400)).await;

let event_id = setup.event_id();
assert!(timeline.unpin_event(event_id).await.is_err());

server.reset().await;
setup.reset_server().await;
}

struct PinningTestSetup<'a> {
event_id: &'a ruma::EventId,
room_id: &'a ruma::RoomId,
client: matrix_sdk::Client,
server: wiremock::MockServer,
sync_settings: SyncSettings,
sync_builder: SyncResponseBuilder,
}

impl PinningTestSetup<'_> {
async fn new() -> Self {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let mut sync_builder = SyncResponseBuilder::new();
let event_id = event_id!("$a");
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

Self { event_id, room_id, client, server, sync_settings, sync_builder }
}

async fn timeline(&self) -> matrix_sdk_ui::Timeline {
let room = self.client.get_room(self.room_id).unwrap();
room.timeline().await.unwrap()
}

async fn reset_server(&self) {
self.server.reset().await;
}

async fn mock_response(&self, response: wiremock::ResponseTemplate) {
Mock::given(method("PUT"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.room.pinned_events/.*?"))
.and(header("authorization", "Bearer 1234"))
.respond_with(response)
.mount(&self.server)
.await;
}

async fn mock_sync(&mut self, is_using_pinned_state_event: bool) {
let f = EventFactory::new().sender(user_id!("@a:b.c"));
let mut joined_room_builder = JoinedRoomBuilder::new(self.room_id)
.add_timeline_event(f.text_msg("A").event_id(self.event_id).into_raw_sync());
if is_using_pinned_state_event {
joined_room_builder =
joined_room_builder.add_state_event(StateTestEvent::RoomPinnedEvents);
}
self.sync_builder.add_joined_room(joined_room_builder);
mock_sync(&self.server, self.sync_builder.build_json_sync_response(), None).await;
let _response = self.client.sync_once(self.sync_settings.clone()).await.unwrap();
}

fn event_id(&self) -> &ruma::EventId {
self.event_id
}
}

0 comments on commit d5f3c62

Please sign in to comment.