Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify exists_on_server tests #4355

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 38 additions & 70 deletions crates/matrix-sdk/src/encryption/backups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ mod test {
};

use super::*;
use crate::test_utils::logged_in_client;
use crate::test_utils::{logged_in_client, mocks::MatrixMockServer};

fn room_key() -> ExportedRoomKey {
let json = json!({
Expand Down Expand Up @@ -1119,93 +1119,61 @@ mod test {
}

#[async_test]
async fn test_exists_on_server() {
let server = MockServer::start().await;
let client = logged_in_client(Some(server.uri())).await;
async fn test_when_a_backup_exists_then_exists_on_server_returns_true() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

{
let _scope = Mock::given(method("GET"))
.and(path("_matrix/client/r0/room_keys/version"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
"auth_data": {
"public_key": "abcdefg",
"signatures": {},
},
"count": 42,
"etag": "anopaquestring",
"version": "1",
})))
.expect(1)
.mount_as_scoped(&server)
.await;

let exists = client
.encryption()
.backups()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");
server.mock_room_keys_version().exists().expect(1).mount().await;

assert!(exists, "We should deduce that a backup exist on the server");
}
let exists = client
.encryption()
.backups()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

{
let _scope = Mock::given(method("GET"))
.and(path("_matrix/client/r0/room_keys/version"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "No current backup version"
})))
.expect(1)
.mount_as_scoped(&server)
.await;

let exists = client
.encryption()
.backups()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");
assert!(exists, "We should deduce that a backup exists on the server");
}

assert!(!exists, "We should deduce that no backup exist on the server");
}
#[async_test]
async fn test_when_no_backup_exists_then_exists_on_server_returns_false() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

server.mock_room_keys_version().none().expect(1).mount().await;

let exists = client
.encryption()
.backups()
.exists_on_server()
.await
.expect("We should be able to check if backups exist on the server");

assert!(!exists, "We should deduce that no backup exists on the server");
}

#[async_test]
async fn test_when_server_returns_an_error_then_exists_on_server_returns_an_error() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

{
let _scope = Mock::given(method("GET"))
.and(path("_matrix/client/r0/room_keys/version"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(429).set_body_json(json!({
"errcode": "M_LIMIT_EXCEEDED",
"error": "Too many requests",
"retry_after_ms": 2000
})))
.expect(1)
.mount_as_scoped(&server)
.await;
let _scope =
server.mock_room_keys_version().error429().expect(1).mount_as_scoped().await;

client.encryption().backups().exists_on_server().await.expect_err(
"If the /version endpoint returns a non 404 error we should throw an error",
);
}

{
let _scope = Mock::given(method("GET"))
.and(path("_matrix/client/r0/room_keys/version"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404))
.expect(1)
.mount_as_scoped(&server)
.await;
let _scope =
server.mock_room_keys_version().error404().expect(1).mount_as_scoped().await;

client.encryption().backups().exists_on_server().await.expect_err(
"If the /version endpoint returns a non-Matrix 404 error we should throw an error",
);
}

server.verify().await;
}

#[async_test]
Expand Down
75 changes: 75 additions & 0 deletions crates/matrix-sdk/src/test_utils/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,36 @@ impl MatrixMockServer {
let mock = Mock::given(method("POST")).and(path_regex(r"/_matrix/client/v3/publicRooms"));
MockEndpoint { mock, server: &self.server, endpoint: PublicRoomsEndpoint }
}

/// Create a prebuilt mock for fetching information about key storage
/// backups.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "e2e-encryption")]
/// # {
/// # tokio_test::block_on(async {
/// use matrix_sdk::test_utils::mocks::MatrixMockServer;
///
/// let mock_server = MatrixMockServer::new().await;
/// let client = mock_server.client_builder().build().await;
///
/// mock_server.mock_room_keys_version().exists().expect(1).mount().await;
///
/// let exists =
/// client.encryption().backups().exists_on_server().await.unwrap();
///
/// assert!(exists);
/// # });
/// # }
/// ```
pub fn mock_room_keys_version(&self) -> MockEndpoint<'_, RoomKeysVersionEndpoint> {
let mock = Mock::given(method("GET"))
.and(path_regex(r"_matrix/client/v3/room_keys/version"))
.and(header("authorization", "Bearer 1234"));
MockEndpoint { mock, server: &self.server, endpoint: RoomKeysVersionEndpoint }
}
}

/// Parameter to [`MatrixMockServer::sync_room`].
Expand Down Expand Up @@ -1503,3 +1533,48 @@ impl<'a> MockEndpoint<'a, PublicRoomsEndpoint> {
MatrixMock { server: self.server, mock }
}
}

/// A prebuilt mock for `room_keys/version`: storage ("backup") of room keys.
pub struct RoomKeysVersionEndpoint;

impl<'a> MockEndpoint<'a, RoomKeysVersionEndpoint> {
/// Returns an endpoint that says there is a single room keys backup
pub fn exists(self) -> MatrixMock<'a> {
let mock = self.mock.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
"auth_data": {
"public_key": "abcdefg",
"signatures": {},
},
"count": 42,
"etag": "anopaquestring",
"version": "1",
})));
MatrixMock { server: self.server, mock }
}

/// Returns an endpoint that says there is no room keys backup
pub fn none(self) -> MatrixMock<'a> {
let mock = self.mock.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "No current backup version"
})));
MatrixMock { server: self.server, mock }
}

/// Returns an endpoint that 429 errors when we get it
pub fn error429(self) -> MatrixMock<'a> {
let mock = self.mock.respond_with(ResponseTemplate::new(429).set_body_json(json!({
"errcode": "M_LIMIT_EXCEEDED",
"error": "Too many requests",
"retry_after_ms": 2000
})));
MatrixMock { server: self.server, mock }
}

/// Returns an endpoint that 404 errors when we get it
pub fn error404(self) -> MatrixMock<'a> {
let mock = self.mock.respond_with(ResponseTemplate::new(404));
MatrixMock { server: self.server, mock }
}
}
Loading