Skip to content

Commit 80cd44d

Browse files
committed
test(threads): add mocks for the thread subscription endpoint
1 parent aee686e commit 80cd44d

File tree

1 file changed

+144
-0
lines changed
  • crates/matrix-sdk/src/test_utils/mocks

1 file changed

+144
-0
lines changed

crates/matrix-sdk/src/test_utils/mocks/mod.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,32 @@ impl MatrixMockServer {
13281328
.and(query_param("animated", animated.to_string()));
13291329
self.mock_endpoint(mock, AuthedMediaThumbnailEndpoint).expect_default_access_token()
13301330
}
1331+
1332+
/// Create a prebuilt mock for the endpoint used to get a thread
1333+
/// subscription in a given room.
1334+
pub fn mock_get_thread_subscription(&self) -> MockEndpoint<'_, GetThreadSubscriptionEndpoint> {
1335+
let mock = Mock::given(method("GET"));
1336+
self.mock_endpoint(mock, GetThreadSubscriptionEndpoint::default())
1337+
.expect_default_access_token()
1338+
}
1339+
1340+
/// Create a prebuilt mock for the endpoint used to define a thread
1341+
/// subscription in a given room.
1342+
pub fn mock_put_thread_subscription(&self) -> MockEndpoint<'_, PutThreadSubscriptionEndpoint> {
1343+
let mock = Mock::given(method("PUT"));
1344+
self.mock_endpoint(mock, PutThreadSubscriptionEndpoint::default())
1345+
.expect_default_access_token()
1346+
}
1347+
1348+
/// Create a prebuilt mock for the endpoint used to delete a thread
1349+
/// subscription in a given room.
1350+
pub fn mock_delete_thread_subscription(
1351+
&self,
1352+
) -> MockEndpoint<'_, DeleteThreadSubscriptionEndpoint> {
1353+
let mock = Mock::given(method("DELETE"));
1354+
self.mock_endpoint(mock, DeleteThreadSubscriptionEndpoint::default())
1355+
.expect_default_access_token()
1356+
}
13311357
}
13321358

13331359
/// Parameter to [`MatrixMockServer::sync_room`].
@@ -3736,3 +3762,121 @@ impl<'a> MockEndpoint<'a, JoinRoomEndpoint> {
37363762
})))
37373763
}
37383764
}
3765+
3766+
#[derive(Default)]
3767+
struct ThreadSubscriptionMatchers {
3768+
/// Optional room id to match in the query.
3769+
room_id: Option<OwnedRoomId>,
3770+
/// Optional thread root event id to match in the query.
3771+
thread_root: Option<OwnedEventId>,
3772+
}
3773+
3774+
impl ThreadSubscriptionMatchers {
3775+
/// Match the request parameter against a specific room id.
3776+
fn match_room_id(mut self, room_id: OwnedRoomId) -> Self {
3777+
self.room_id = Some(room_id);
3778+
self
3779+
}
3780+
3781+
/// Match the request parameter against a specific thread root event id.
3782+
fn match_thread_id(mut self, thread_root: OwnedEventId) -> Self {
3783+
self.thread_root = Some(thread_root);
3784+
self
3785+
}
3786+
3787+
/// Compute the final URI for the thread subscription endpoint.
3788+
fn endpoint_regexp_uri(&self) -> String {
3789+
if self.room_id.is_some() || self.thread_root.is_some() {
3790+
format!(
3791+
"^/_matrix/client/unstable/io.element.msc4306/rooms/{}/thread/{}/subscription$",
3792+
self.room_id.as_deref().map(|s| s.as_str()).unwrap_or(".*"),
3793+
self.thread_root.as_deref().map(|s| s.as_str()).unwrap_or(".*").replace("$", "\\$")
3794+
)
3795+
} else {
3796+
"^/_matrix/client/unstable/io.element.msc4306/rooms/.*/thread/.*/subscription$"
3797+
.to_owned()
3798+
}
3799+
}
3800+
}
3801+
3802+
/// A prebuilt mock for `GET
3803+
/// /client/*/rooms/{room_id}/threads/{thread_root}/subscription`
3804+
#[derive(Default)]
3805+
pub struct GetThreadSubscriptionEndpoint {
3806+
matchers: ThreadSubscriptionMatchers,
3807+
}
3808+
3809+
impl<'a> MockEndpoint<'a, GetThreadSubscriptionEndpoint> {
3810+
/// Returns a successful response for the given thread subscription.
3811+
pub fn ok(mut self, automatic: bool) -> MatrixMock<'a> {
3812+
self.mock = self.mock.and(path_regex(self.endpoint.matchers.endpoint_regexp_uri()));
3813+
self.respond_with(ResponseTemplate::new(200).set_body_json(json!({
3814+
"automatic": automatic
3815+
})))
3816+
}
3817+
3818+
/// Match the request parameter against a specific room id.
3819+
pub fn match_room_id(mut self, room_id: OwnedRoomId) -> Self {
3820+
self.endpoint.matchers = self.endpoint.matchers.match_room_id(room_id);
3821+
self
3822+
}
3823+
/// Match the request parameter against a specific thread root event id.
3824+
pub fn match_thread_id(mut self, thread_root: OwnedEventId) -> Self {
3825+
self.endpoint.matchers = self.endpoint.matchers.match_thread_id(thread_root);
3826+
self
3827+
}
3828+
}
3829+
3830+
/// A prebuilt mock for `PUT
3831+
/// /client/*/rooms/{room_id}/threads/{thread_root}/subscription`
3832+
#[derive(Default)]
3833+
pub struct PutThreadSubscriptionEndpoint {
3834+
matchers: ThreadSubscriptionMatchers,
3835+
}
3836+
3837+
impl<'a> MockEndpoint<'a, PutThreadSubscriptionEndpoint> {
3838+
/// Returns a successful response for the given setting of thread
3839+
/// subscription.
3840+
pub fn ok(mut self) -> MatrixMock<'a> {
3841+
self.mock = self.mock.and(path_regex(self.endpoint.matchers.endpoint_regexp_uri()));
3842+
self.respond_with(ResponseTemplate::new(200))
3843+
}
3844+
3845+
/// Match the request parameter against a specific room id.
3846+
pub fn match_room_id(mut self, room_id: OwnedRoomId) -> Self {
3847+
self.endpoint.matchers = self.endpoint.matchers.match_room_id(room_id);
3848+
self
3849+
}
3850+
/// Match the request parameter against a specific thread root event id.
3851+
pub fn match_thread_id(mut self, thread_root: OwnedEventId) -> Self {
3852+
self.endpoint.matchers = self.endpoint.matchers.match_thread_id(thread_root);
3853+
self
3854+
}
3855+
}
3856+
3857+
/// A prebuilt mock for `DELETE
3858+
/// /client/*/rooms/{room_id}/threads/{thread_root}/subscription`
3859+
#[derive(Default)]
3860+
pub struct DeleteThreadSubscriptionEndpoint {
3861+
matchers: ThreadSubscriptionMatchers,
3862+
}
3863+
3864+
impl<'a> MockEndpoint<'a, DeleteThreadSubscriptionEndpoint> {
3865+
/// Returns a successful response for the deletion of a given thread
3866+
/// subscription.
3867+
pub fn ok(mut self) -> MatrixMock<'a> {
3868+
self.mock = self.mock.and(path_regex(self.endpoint.matchers.endpoint_regexp_uri()));
3869+
self.respond_with(ResponseTemplate::new(200))
3870+
}
3871+
3872+
/// Match the request parameter against a specific room id.
3873+
pub fn match_room_id(mut self, room_id: OwnedRoomId) -> Self {
3874+
self.endpoint.matchers = self.endpoint.matchers.match_room_id(room_id);
3875+
self
3876+
}
3877+
/// Match the request parameter against a specific thread root event id.
3878+
pub fn match_thread_id(mut self, thread_root: OwnedEventId) -> Self {
3879+
self.endpoint.matchers = self.endpoint.matchers.match_thread_id(thread_root);
3880+
self
3881+
}
3882+
}

0 commit comments

Comments
 (0)