Skip to content

Commit 516d066

Browse files
zecakehbnjbvr
authored andcommitted
test(sdk): Add a constructor for MockEndpoint on MatrixMockServer
Allows to reduce duplication and will allow to add common logic. Signed-off-by: Kévin Commaille <[email protected]>
1 parent fbcd5a7 commit 516d066

File tree

2 files changed

+59
-52
lines changed

2 files changed

+59
-52
lines changed

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

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ impl MatrixMockServer {
150150
/// Get an `OauthMockServer` that uses the same mock server as this one.
151151
#[cfg(feature = "experimental-oidc")]
152152
pub fn oauth(&self) -> oauth::OauthMockServer<'_> {
153-
oauth::OauthMockServer::new(self.server())
153+
oauth::OauthMockServer::new(self)
154+
}
155+
156+
/// Mock the given endpoint.
157+
fn mock_endpoint<T>(&self, mock: MockBuilder, endpoint: T) -> MockEndpoint<'_, T> {
158+
MockEndpoint::new(&self.server, mock, endpoint)
154159
}
155160

156161
/// Overrides the sync/ endpoint with knowledge that the given
@@ -286,11 +291,10 @@ impl MatrixMockServer {
286291
let mock = Mock::given(method("GET"))
287292
.and(path("/_matrix/client/v3/sync"))
288293
.and(header("authorization", "Bearer 1234"));
289-
MockEndpoint {
294+
self.mock_endpoint(
290295
mock,
291-
server: &self.server,
292-
endpoint: SyncEndpoint { sync_response_builder: self.sync_response_builder.clone() },
293-
}
296+
SyncEndpoint { sync_response_builder: self.sync_response_builder.clone() },
297+
)
294298
}
295299

296300
/// Creates a prebuilt mock for sending an event in a room.
@@ -334,7 +338,7 @@ impl MatrixMockServer {
334338
let mock = Mock::given(method("PUT"))
335339
.and(header("authorization", "Bearer 1234"))
336340
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/send/.*".to_owned()));
337-
MockEndpoint { mock, server: &self.server, endpoint: RoomSendEndpoint }
341+
self.mock_endpoint(mock, RoomSendEndpoint)
338342
}
339343

340344
/// Creates a prebuilt mock for sending a state event in a room.
@@ -384,7 +388,7 @@ impl MatrixMockServer {
384388
let mock = Mock::given(method("PUT"))
385389
.and(header("authorization", "Bearer 1234"))
386390
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/state/.*/.*"));
387-
MockEndpoint { mock, server: &self.server, endpoint: RoomSendStateEndpoint::default() }
391+
self.mock_endpoint(mock, RoomSendStateEndpoint::default())
388392
}
389393

390394
/// Creates a prebuilt mock for asking whether *a* room is encrypted or not.
@@ -416,7 +420,7 @@ impl MatrixMockServer {
416420
let mock = Mock::given(method("GET"))
417421
.and(header("authorization", "Bearer 1234"))
418422
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/state/m.*room.*encryption.?"));
419-
MockEndpoint { mock, server: &self.server, endpoint: EncryptionStateEndpoint }
423+
self.mock_endpoint(mock, EncryptionStateEndpoint)
420424
}
421425

422426
/// Creates a prebuilt mock for setting the room encryption state.
@@ -456,7 +460,7 @@ impl MatrixMockServer {
456460
let mock = Mock::given(method("PUT"))
457461
.and(header("authorization", "Bearer 1234"))
458462
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/state/m.*room.*encryption.?"));
459-
MockEndpoint { mock, server: &self.server, endpoint: SetEncryptionStateEndpoint }
463+
self.mock_endpoint(mock, SetEncryptionStateEndpoint)
460464
}
461465

462466
/// Creates a prebuilt mock for the room redact endpoint.
@@ -489,17 +493,13 @@ impl MatrixMockServer {
489493
let mock = Mock::given(method("PUT"))
490494
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/redact/.*?/.*?"))
491495
.and(header("authorization", "Bearer 1234"));
492-
MockEndpoint { mock, server: &self.server, endpoint: RoomRedactEndpoint }
496+
self.mock_endpoint(mock, RoomRedactEndpoint)
493497
}
494498

495499
/// Creates a prebuilt mock for retrieving an event with /room/.../event.
496500
pub fn mock_room_event(&self) -> MockEndpoint<'_, RoomEventEndpoint> {
497501
let mock = Mock::given(method("GET")).and(header("authorization", "Bearer 1234"));
498-
MockEndpoint {
499-
mock,
500-
server: &self.server,
501-
endpoint: RoomEventEndpoint { room: None, match_event_id: false },
502-
}
502+
self.mock_endpoint(mock, RoomEventEndpoint { room: None, match_event_id: false })
503503
}
504504

505505
/// Create a prebuild mock for paginating room message with the `/messages`
@@ -508,15 +508,15 @@ impl MatrixMockServer {
508508
let mock = Mock::given(method("GET"))
509509
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/messages$"))
510510
.and(header("authorization", "Bearer 1234"));
511-
MockEndpoint { mock, server: &self.server, endpoint: RoomMessagesEndpoint }
511+
self.mock_endpoint(mock, RoomMessagesEndpoint)
512512
}
513513

514514
/// Create a prebuilt mock for uploading media.
515515
pub fn mock_upload(&self) -> MockEndpoint<'_, UploadEndpoint> {
516516
let mock = Mock::given(method("POST"))
517517
.and(path("/_matrix/media/v3/upload"))
518518
.and(header("authorization", "Bearer 1234"));
519-
MockEndpoint { mock, server: &self.server, endpoint: UploadEndpoint }
519+
self.mock_endpoint(mock, UploadEndpoint)
520520
}
521521

522522
/// Create a prebuilt mock for resolving room aliases.
@@ -549,7 +549,7 @@ impl MatrixMockServer {
549549
pub fn mock_room_directory_resolve_alias(&self) -> MockEndpoint<'_, ResolveRoomAliasEndpoint> {
550550
let mock =
551551
Mock::given(method("GET")).and(path_regex(r"/_matrix/client/v3/directory/room/.*"));
552-
MockEndpoint { mock, server: &self.server, endpoint: ResolveRoomAliasEndpoint }
552+
self.mock_endpoint(mock, ResolveRoomAliasEndpoint)
553553
}
554554

555555
/// Create a prebuilt mock for publishing room aliases in the room
@@ -585,7 +585,7 @@ impl MatrixMockServer {
585585
) -> MockEndpoint<'_, CreateRoomAliasEndpoint> {
586586
let mock =
587587
Mock::given(method("PUT")).and(path_regex(r"/_matrix/client/v3/directory/room/.*"));
588-
MockEndpoint { mock, server: &self.server, endpoint: CreateRoomAliasEndpoint }
588+
self.mock_endpoint(mock, CreateRoomAliasEndpoint)
589589
}
590590

591591
/// Create a prebuilt mock for removing room aliases from the room
@@ -620,7 +620,7 @@ impl MatrixMockServer {
620620
) -> MockEndpoint<'_, RemoveRoomAliasEndpoint> {
621621
let mock =
622622
Mock::given(method("DELETE")).and(path_regex(r"/_matrix/client/v3/directory/room/.*"));
623-
MockEndpoint { mock, server: &self.server, endpoint: RemoveRoomAliasEndpoint }
623+
self.mock_endpoint(mock, RemoveRoomAliasEndpoint)
624624
}
625625

626626
/// Create a prebuilt mock for listing public rooms.
@@ -663,7 +663,7 @@ impl MatrixMockServer {
663663
/// ```
664664
pub fn mock_public_rooms(&self) -> MockEndpoint<'_, PublicRoomsEndpoint> {
665665
let mock = Mock::given(method("POST")).and(path_regex(r"/_matrix/client/v3/publicRooms"));
666-
MockEndpoint { mock, server: &self.server, endpoint: PublicRoomsEndpoint }
666+
self.mock_endpoint(mock, PublicRoomsEndpoint)
667667
}
668668

669669
/// Create a prebuilt mock for setting a room's visibility in the room
@@ -701,7 +701,7 @@ impl MatrixMockServer {
701701
) -> MockEndpoint<'_, SetRoomVisibilityEndpoint> {
702702
let mock = Mock::given(method("PUT"))
703703
.and(path_regex(r"^/_matrix/client/v3/directory/list/room/.*$"));
704-
MockEndpoint { mock, server: &self.server, endpoint: SetRoomVisibilityEndpoint }
704+
self.mock_endpoint(mock, SetRoomVisibilityEndpoint)
705705
}
706706

707707
/// Create a prebuilt mock for getting a room's visibility in the room
@@ -741,7 +741,7 @@ impl MatrixMockServer {
741741
) -> MockEndpoint<'_, GetRoomVisibilityEndpoint> {
742742
let mock = Mock::given(method("GET"))
743743
.and(path_regex(r"^/_matrix/client/v3/directory/list/room/.*$"));
744-
MockEndpoint { mock, server: &self.server, endpoint: GetRoomVisibilityEndpoint }
744+
self.mock_endpoint(mock, GetRoomVisibilityEndpoint)
745745
}
746746

747747
/// Create a prebuilt mock for fetching information about key storage
@@ -771,23 +771,23 @@ impl MatrixMockServer {
771771
let mock = Mock::given(method("GET"))
772772
.and(path_regex(r"_matrix/client/v3/room_keys/version"))
773773
.and(header("authorization", "Bearer 1234"));
774-
MockEndpoint { mock, server: &self.server, endpoint: RoomKeysVersionEndpoint }
774+
self.mock_endpoint(mock, RoomKeysVersionEndpoint)
775775
}
776776

777777
/// Create a prebuilt mock for adding key storage backups via POST
778778
pub fn mock_add_room_keys_version(&self) -> MockEndpoint<'_, AddRoomKeysVersionEndpoint> {
779779
let mock = Mock::given(method("POST"))
780780
.and(path_regex(r"_matrix/client/v3/room_keys/version"))
781781
.and(header("authorization", "Bearer 1234"));
782-
MockEndpoint { mock, server: &self.server, endpoint: AddRoomKeysVersionEndpoint }
782+
self.mock_endpoint(mock, AddRoomKeysVersionEndpoint)
783783
}
784784

785785
/// Create a prebuilt mock for adding key storage backups via POST
786786
pub fn mock_delete_room_keys_version(&self) -> MockEndpoint<'_, DeleteRoomKeysVersionEndpoint> {
787787
let mock = Mock::given(method("DELETE"))
788788
.and(path_regex(r"_matrix/client/v3/room_keys/version/[^/]*"))
789789
.and(header("authorization", "Bearer 1234"));
790-
MockEndpoint { mock, server: &self.server, endpoint: DeleteRoomKeysVersionEndpoint }
790+
self.mock_endpoint(mock, DeleteRoomKeysVersionEndpoint)
791791
}
792792

793793
/// Create a prebuilt mock for getting the room members in a room.
@@ -836,7 +836,7 @@ impl MatrixMockServer {
836836
pub fn mock_get_members(&self) -> MockEndpoint<'_, GetRoomMembersEndpoint> {
837837
let mock =
838838
Mock::given(method("GET")).and(path_regex(r"^/_matrix/client/v3/rooms/.*/members$"));
839-
MockEndpoint { mock, server: &self.server, endpoint: GetRoomMembersEndpoint }
839+
self.mock_endpoint(mock, GetRoomMembersEndpoint)
840840
}
841841

842842
/// Creates a prebuilt mock for inviting a user to a room by its id.
@@ -866,7 +866,7 @@ impl MatrixMockServer {
866866
pub fn mock_invite_user_by_id(&self) -> MockEndpoint<'_, InviteUserByIdEndpoint> {
867867
let mock =
868868
Mock::given(method("POST")).and(path_regex(r"^/_matrix/client/v3/rooms/.*/invite$"));
869-
MockEndpoint { mock, server: &self.server, endpoint: InviteUserByIdEndpoint }
869+
self.mock_endpoint(mock, InviteUserByIdEndpoint)
870870
}
871871

872872
/// Creates a prebuilt mock for kicking a user from a room.
@@ -896,7 +896,7 @@ impl MatrixMockServer {
896896
pub fn mock_kick_user(&self) -> MockEndpoint<'_, KickUserEndpoint> {
897897
let mock =
898898
Mock::given(method("POST")).and(path_regex(r"^/_matrix/client/v3/rooms/.*/kick"));
899-
MockEndpoint { mock, server: &self.server, endpoint: KickUserEndpoint }
899+
self.mock_endpoint(mock, KickUserEndpoint)
900900
}
901901

902902
/// Creates a prebuilt mock for banning a user from a room.
@@ -925,20 +925,20 @@ impl MatrixMockServer {
925925
/// ```
926926
pub fn mock_ban_user(&self) -> MockEndpoint<'_, BanUserEndpoint> {
927927
let mock = Mock::given(method("POST")).and(path_regex(r"^/_matrix/client/v3/rooms/.*/ban"));
928-
MockEndpoint { mock, server: &self.server, endpoint: BanUserEndpoint }
928+
self.mock_endpoint(mock, BanUserEndpoint)
929929
}
930930

931931
/// Creates a prebuilt mock for the `/_matrix/client/versions` endpoint.
932932
pub fn mock_versions(&self) -> MockEndpoint<'_, VersionsEndpoint> {
933933
let mock = Mock::given(method("GET")).and(path_regex(r"^/_matrix/client/versions"));
934-
MockEndpoint { mock, server: &self.server, endpoint: VersionsEndpoint }
934+
self.mock_endpoint(mock, VersionsEndpoint)
935935
}
936936

937937
/// Creates a prebuilt mock for the room summary endpoint [MSC3266](https://github.com/matrix-org/matrix-spec-proposals/pull/3266).
938938
pub fn mock_room_summary(&self) -> MockEndpoint<'_, RoomSummaryEndpoint> {
939939
let mock = Mock::given(method("GET"))
940940
.and(path_regex(r"^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary"));
941-
MockEndpoint { mock, server: &self.server, endpoint: RoomSummaryEndpoint }
941+
self.mock_endpoint(mock, RoomSummaryEndpoint)
942942
}
943943

944944
/// Creates a prebuilt mock for the endpoint used to set a room's pinned
@@ -947,7 +947,7 @@ impl MatrixMockServer {
947947
let mock = Mock::given(method("PUT"))
948948
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/state/m.room.pinned_events/.*?"))
949949
.and(header("authorization", "Bearer 1234"));
950-
MockEndpoint { mock, server: &self.server, endpoint: SetRoomPinnedEventsEndpoint }
950+
self.mock_endpoint(mock, SetRoomPinnedEventsEndpoint)
951951
}
952952

953953
/// Creates a prebuilt mock for the endpoint used to get information about
@@ -958,11 +958,7 @@ impl MatrixMockServer {
958958
pub fn mock_who_am_i(&self) -> MockEndpoint<'_, WhoAmIEndpoint> {
959959
let mock =
960960
Mock::given(method("GET")).and(path_regex(r"^/_matrix/client/v3/account/whoami"));
961-
MockEndpoint {
962-
mock,
963-
server: &self.server,
964-
endpoint: WhoAmIEndpoint { expected_access_token: "1234" },
965-
}
961+
self.mock_endpoint(mock, WhoAmIEndpoint { expected_access_token: "1234" })
966962
}
967963

968964
/// Creates a prebuilt mock for the endpoint used to publish end-to-end
@@ -971,7 +967,7 @@ impl MatrixMockServer {
971967
let mock = Mock::given(method("POST"))
972968
.and(path_regex(r"^/_matrix/client/v3/keys/upload"))
973969
.and(header("authorization", "Bearer 1234"));
974-
MockEndpoint { mock, server: &self.server, endpoint: UploadKeysEndpoint }
970+
self.mock_endpoint(mock, UploadKeysEndpoint)
975971
}
976972

977973
/// Creates a prebuilt mock for the endpoint used to query end-to-end
@@ -980,14 +976,14 @@ impl MatrixMockServer {
980976
let mock = Mock::given(method("POST"))
981977
.and(path_regex(r"^/_matrix/client/v3/keys/query"))
982978
.and(header("authorization", "Bearer 1234"));
983-
MockEndpoint { mock, server: &self.server, endpoint: QueryKeysEndpoint }
979+
self.mock_endpoint(mock, QueryKeysEndpoint)
984980
}
985981

986982
/// Creates a prebuilt mock for the endpoint used to discover the URL of a
987983
/// homeserver.
988984
pub fn mock_well_known(&self) -> MockEndpoint<'_, WellKnownEndpoint> {
989985
let mock = Mock::given(method("GET")).and(path_regex(r"^/.well-known/matrix/client"));
990-
MockEndpoint { mock, server: &self.server, endpoint: WellKnownEndpoint }
986+
self.mock_endpoint(mock, WellKnownEndpoint)
991987
}
992988

993989
/// Creates a prebuilt mock for the endpoint used to publish cross-signing
@@ -998,7 +994,7 @@ impl MatrixMockServer {
998994
let mock = Mock::given(method("POST"))
999995
.and(path_regex(r"^/_matrix/client/v3/keys/device_signing/upload"))
1000996
.and(header("authorization", "Bearer 1234"));
1001-
MockEndpoint { mock, server: &self.server, endpoint: UploadCrossSigningKeysEndpoint }
997+
self.mock_endpoint(mock, UploadCrossSigningKeysEndpoint)
1002998
}
1003999

10041000
/// Creates a prebuilt mock for the endpoint used to publish cross-signing
@@ -1009,7 +1005,7 @@ impl MatrixMockServer {
10091005
let mock = Mock::given(method("POST"))
10101006
.and(path_regex(r"^/_matrix/client/v3/keys/signatures/upload"))
10111007
.and(header("authorization", "Bearer 1234"));
1012-
MockEndpoint { mock, server: &self.server, endpoint: UploadCrossSigningSignaturesEndpoint }
1008+
self.mock_endpoint(mock, UploadCrossSigningSignaturesEndpoint)
10131009
}
10141010
}
10151011

@@ -1169,6 +1165,10 @@ pub struct MockEndpoint<'a, T> {
11691165
}
11701166

11711167
impl<'a, T> MockEndpoint<'a, T> {
1168+
fn new(server: &'a MockServer, mock: MockBuilder, endpoint: T) -> Self {
1169+
Self { server, mock, endpoint }
1170+
}
1171+
11721172
/// Specify how to respond to a query (viz., like
11731173
/// [`MockBuilder::respond_with`] does), when other predefined responses
11741174
/// aren't sufficient.

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ use serde_json::json;
2222
use url::Url;
2323
use wiremock::{
2424
matchers::{method, path_regex},
25-
Mock, MockServer, ResponseTemplate,
25+
Mock, MockBuilder, ResponseTemplate,
2626
};
2727

28-
use super::{MatrixMock, MockEndpoint};
28+
use super::{MatrixMock, MatrixMockServer, MockEndpoint};
2929

3030
/// A [`wiremock`] [`MockServer`] along with useful methods to help mocking
3131
/// OAuth 2.0 API endpoints easily.
@@ -51,14 +51,21 @@ use super::{MatrixMock, MockEndpoint};
5151
/// curried, so one doesn't have to pass it around when calling
5252
/// [`MatrixMock::mount()`] or [`MatrixMock::mount_as_scoped()`]. As such, it
5353
/// mostly defers its implementations to [`wiremock::Mock`] under the hood.
54+
///
55+
/// [`MockServer`]: wiremock::MockServer
5456
pub struct OauthMockServer<'a> {
55-
server: &'a MockServer,
57+
server: &'a MatrixMockServer,
5658
}
5759

5860
impl<'a> OauthMockServer<'a> {
59-
pub(super) fn new(server: &'a MockServer) -> Self {
61+
pub(super) fn new(server: &'a MatrixMockServer) -> Self {
6062
Self { server }
6163
}
64+
65+
/// Mock the given endpoint.
66+
fn mock_endpoint<T>(&self, mock: MockBuilder, endpoint: T) -> MockEndpoint<'a, T> {
67+
self.server.mock_endpoint(mock, endpoint)
68+
}
6269
}
6370

6471
// Specific mount endpoints.
@@ -75,35 +82,35 @@ impl OauthMockServer<'_> {
7582
pub fn mock_server_metadata(&self) -> MockEndpoint<'_, ServerMetadataEndpoint> {
7683
let mock = Mock::given(method("GET"))
7784
.and(path_regex(r"^/_matrix/client/unstable/org.matrix.msc2965/auth_metadata"));
78-
MockEndpoint { mock, server: self.server, endpoint: ServerMetadataEndpoint }
85+
self.mock_endpoint(mock, ServerMetadataEndpoint)
7986
}
8087

8188
/// Creates a prebuilt mock for the OAuth 2.0 endpoint used to register a
8289
/// new client.
8390
pub fn mock_registration(&self) -> MockEndpoint<'_, RegistrationEndpoint> {
8491
let mock = Mock::given(method("POST")).and(path_regex(r"^/oauth2/registration"));
85-
MockEndpoint { mock, server: self.server, endpoint: RegistrationEndpoint }
92+
self.mock_endpoint(mock, RegistrationEndpoint)
8693
}
8794

8895
/// Creates a prebuilt mock for the OAuth 2.0 endpoint used to authorize a
8996
/// device.
9097
pub fn mock_device_authorization(&self) -> MockEndpoint<'_, DeviceAuthorizationEndpoint> {
9198
let mock = Mock::given(method("POST")).and(path_regex(r"^/oauth2/device"));
92-
MockEndpoint { mock, server: self.server, endpoint: DeviceAuthorizationEndpoint }
99+
self.mock_endpoint(mock, DeviceAuthorizationEndpoint)
93100
}
94101

95102
/// Creates a prebuilt mock for the OAuth 2.0 endpoint used to request an
96103
/// access token.
97104
pub fn mock_token(&self) -> MockEndpoint<'_, TokenEndpoint> {
98105
let mock = Mock::given(method("POST")).and(path_regex(r"^/oauth2/token"));
99-
MockEndpoint { mock, server: self.server, endpoint: TokenEndpoint }
106+
self.mock_endpoint(mock, TokenEndpoint)
100107
}
101108

102109
/// Creates a prebuilt mock for the OAuth 2.0 endpoint used to revoke a
103110
/// token.
104111
pub fn mock_revocation(&self) -> MockEndpoint<'_, RevocationEndpoint> {
105112
let mock = Mock::given(method("POST")).and(path_regex(r"^/oauth2/revoke"));
106-
MockEndpoint { mock, server: self.server, endpoint: RevocationEndpoint }
113+
self.mock_endpoint(mock, RevocationEndpoint)
107114
}
108115
}
109116

0 commit comments

Comments
 (0)