| 1 | // Copyright 2024 The Matrix.org Foundation C.I.C. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Mocks useful to reuse across different testing contexts. |
| 16 | |
| 17 | use wiremock::{ |
| 18 | Mock, MockServer, ResponseTemplate, |
| 19 | matchers::{header, method, path_regex}, |
| 20 | }; |
| 21 | |
| 22 | use crate::{event_factory::EventFactory, test_json}; |
| 23 | |
| 24 | /// Mount a Mock on the given server to handle the `GET |
| 25 | /// /rooms/.../state/m.room.encryption` endpoint with an option whether it |
| 26 | /// should return an encryption event or not. |
| 27 | pub async fn mock_encryption_state(server: &MockServer, is_encrypted: bool) { |
| 28 | let builder = Mock::given(method("GET")) |
| 29 | .and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.*room.*encryption.?")) |
| 30 | .and(header("authorization", "Bearer 1234")); |
| 31 | |
| 32 | if is_encrypted { |
| 33 | builder |
| 34 | .respond_with( |
| 35 | ResponseTemplate::new(200) |
| 36 | .set_body_json(EventFactory::new().room_encryption().into_content()), |
| 37 | ) |
| 38 | .mount(server) |
| 39 | .await; |
| 40 | } else { |
| 41 | builder |
| 42 | .respond_with(ResponseTemplate::new(404).set_body_json(&*test_json::NOT_FOUND)) |
| 43 | .mount(server) |
| 44 | .await; |
| 45 | } |
| 46 | } |