From d5f3c625d42532534ff9445f239ca5a5a51a6e99 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Fri, 26 Jul 2024 13:55:33 +0200 Subject: [PATCH] Pinning Tests Setup --- .../tests/integration/timeline/mod.rs | 277 +++++++----------- 1 file changed, 104 insertions(+), 173 deletions(-) diff --git a/crates/matrix-sdk-ui/tests/integration/timeline/mod.rs b/crates/matrix-sdk-ui/tests/integration/timeline/mod.rs index c364761c898..c6c6a1e2fe5 100644 --- a/crates/matrix-sdk-ui/tests/integration/timeline/mod.rs +++ b/crates/matrix-sdk-ui/tests/integration/timeline/mod.rs @@ -510,235 +510,166 @@ async fn test_duplicate_maintains_correct_order() { #[async_test] async fn test_pin_event_is_sent_succesfully() { - 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() { - 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 + } }