atagen

matrix-rust-sdk

public
Star0
Watch0
ssh://brewery@pls.lobotomise.me:33/atagen/matrix-rust-sdk.git

Blame: testing/matrix-sdk-test/src/mocks.rs

7ff8adafe9e4atagen2026-10-22 12:30 UTC1
// Copyright 2024 The Matrix.org Foundation C.I.C.
7ff8adafe9e4atagen2026-10-22 12:30 UTC2
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC3
// Licensed under the Apache License, Version 2.0 (the "License");
7ff8adafe9e4atagen2026-10-22 12:30 UTC4
// you may not use this file except in compliance with the License.
7ff8adafe9e4atagen2026-10-22 12:30 UTC5
// You may obtain a copy of the License at
7ff8adafe9e4atagen2026-10-22 12:30 UTC6
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC7
//     http://www.apache.org/licenses/LICENSE-2.0
7ff8adafe9e4atagen2026-10-22 12:30 UTC8
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC9
// Unless required by applicable law or agreed to in writing, software
7ff8adafe9e4atagen2026-10-22 12:30 UTC10
// distributed under the License is distributed on an "AS IS" BASIS,
7ff8adafe9e4atagen2026-10-22 12:30 UTC11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7ff8adafe9e4atagen2026-10-22 12:30 UTC12
// See the License for the specific language governing permissions and
7ff8adafe9e4atagen2026-10-22 12:30 UTC13
// limitations under the License.
7ff8adafe9e4atagen2026-10-22 12:30 UTC14
7ff8adafe9e4atagen2026-10-22 12:30 UTC15
//! Mocks useful to reuse across different testing contexts.
7ff8adafe9e4atagen2026-10-22 12:30 UTC16
7ff8adafe9e4atagen2026-10-22 12:30 UTC17
use wiremock::{
7ff8adafe9e4atagen2026-10-22 12:30 UTC18
    Mock, MockServer, ResponseTemplate,
7ff8adafe9e4atagen2026-10-22 12:30 UTC19
    matchers::{header, method, path_regex},
7ff8adafe9e4atagen2026-10-22 12:30 UTC20
};
7ff8adafe9e4atagen2026-10-22 12:30 UTC21
7ff8adafe9e4atagen2026-10-22 12:30 UTC22
use crate::{event_factory::EventFactory, test_json};
7ff8adafe9e4atagen2026-10-22 12:30 UTC23
7ff8adafe9e4atagen2026-10-22 12:30 UTC24
/// Mount a Mock on the given server to handle the `GET
7ff8adafe9e4atagen2026-10-22 12:30 UTC25
/// /rooms/.../state/m.room.encryption` endpoint with an option whether it
7ff8adafe9e4atagen2026-10-22 12:30 UTC26
/// should return an encryption event or not.
7ff8adafe9e4atagen2026-10-22 12:30 UTC27
pub async fn mock_encryption_state(server: &MockServer, is_encrypted: bool) {
7ff8adafe9e4atagen2026-10-22 12:30 UTC28
    let builder = Mock::given(method("GET"))
7ff8adafe9e4atagen2026-10-22 12:30 UTC29
        .and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.*room.*encryption.?"))
7ff8adafe9e4atagen2026-10-22 12:30 UTC30
        .and(header("authorization", "Bearer 1234"));
7ff8adafe9e4atagen2026-10-22 12:30 UTC31
7ff8adafe9e4atagen2026-10-22 12:30 UTC32
    if is_encrypted {
7ff8adafe9e4atagen2026-10-22 12:30 UTC33
        builder
7ff8adafe9e4atagen2026-10-22 12:30 UTC34
            .respond_with(
7ff8adafe9e4atagen2026-10-22 12:30 UTC35
                ResponseTemplate::new(200)
7ff8adafe9e4atagen2026-10-22 12:30 UTC36
                    .set_body_json(EventFactory::new().room_encryption().into_content()),
7ff8adafe9e4atagen2026-10-22 12:30 UTC37
            )
7ff8adafe9e4atagen2026-10-22 12:30 UTC38
            .mount(server)
7ff8adafe9e4atagen2026-10-22 12:30 UTC39
            .await;
7ff8adafe9e4atagen2026-10-22 12:30 UTC40
    } else {
7ff8adafe9e4atagen2026-10-22 12:30 UTC41
        builder
7ff8adafe9e4atagen2026-10-22 12:30 UTC42
            .respond_with(ResponseTemplate::new(404).set_body_json(&*test_json::NOT_FOUND))
7ff8adafe9e4atagen2026-10-22 12:30 UTC43
            .mount(server)
7ff8adafe9e4atagen2026-10-22 12:30 UTC44
            .await;
7ff8adafe9e4atagen2026-10-22 12:30 UTC45
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC46
}