Skip to content

Commit

Permalink
task(backup_tests): Use helper functions to shorten exists_on_server …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
andybalaam committed Dec 3, 2024
1 parent e76b8f7 commit 4c5ef03
Showing 1 changed file with 63 additions and 47 deletions.
110 changes: 63 additions & 47 deletions crates/matrix-sdk/src/encryption/backups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ mod test {
use serde_json::json;
use wiremock::{
matchers::{header, method, path},
Mock, MockServer, ResponseTemplate,
Mock, MockGuard, MockServer, ResponseTemplate,
};

use super::*;
Expand Down Expand Up @@ -1124,22 +1124,7 @@ mod test {
let client = logged_in_client(Some(server.uri())).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 _scope = mock_backup_exists(&server).await;

let exists = client
.encryption()
Expand All @@ -1148,20 +1133,11 @@ mod test {
.await
.expect("We should be able to check if backups exist on the server");

assert!(exists, "We should deduce that a backup exist on the server");
assert!(exists, "We should deduce that a backup exists 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 _scope = mock_backup_none(&server).await;

let exists = client
.encryption()
Expand All @@ -1170,35 +1146,19 @@ mod test {
.await
.expect("We should be able to check if backups exist on the server");

assert!(!exists, "We should deduce that no backup exist on the server");
assert!(!exists, "We should deduce that no backup exists 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(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 = mock_backup_too_many_requests(&server).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 = mock_backup_404(&server);

client.encryption().backups().exists_on_server().await.expect_err(
"If the /version endpoint returns a non-Matrix 404 error we should throw an error",
Expand Down Expand Up @@ -1282,4 +1242,60 @@ mod test {

server.verify().await;
}

async fn mock_backup_exists(server: &MockServer) -> MockGuard {
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
}

async fn mock_backup_none(server: &MockServer) -> MockGuard {
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
}

async fn mock_backup_too_many_requests(server: &MockServer) -> MockGuard {
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
}

async fn mock_backup_404(server: &MockServer) -> MockGuard {
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
}
}

0 comments on commit 4c5ef03

Please sign in to comment.