|
| 1 | +use async_trait::async_trait; |
| 2 | +use reqwest::StatusCode; |
| 3 | +use slog::debug; |
| 4 | + |
| 5 | +use mithril_common::messages::RegisterSignatureMessageHttp; |
| 6 | + |
| 7 | +use crate::query::{AggregatorQuery, QueryContext, QueryMethod}; |
| 8 | +use crate::{AggregatorHttpClientError, AggregatorHttpClientResult}; |
| 9 | + |
| 10 | +/// Query to register a signature to a Mithril Aggregator. |
| 11 | +pub struct PostRegisterSignatureQuery { |
| 12 | + message: RegisterSignatureMessageHttp, |
| 13 | +} |
| 14 | + |
| 15 | +impl PostRegisterSignatureQuery { |
| 16 | + /// Instantiate a new query to register a signature |
| 17 | + pub fn new(message: RegisterSignatureMessageHttp) -> Self { |
| 18 | + Self { message } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +#[cfg_attr(target_family = "wasm", async_trait(?Send))] |
| 23 | +#[cfg_attr(not(target_family = "wasm"), async_trait)] |
| 24 | +impl AggregatorQuery for PostRegisterSignatureQuery { |
| 25 | + type Response = (); |
| 26 | + type Body = RegisterSignatureMessageHttp; |
| 27 | + |
| 28 | + fn method() -> QueryMethod { |
| 29 | + QueryMethod::Post |
| 30 | + } |
| 31 | + |
| 32 | + fn route(&self) -> String { |
| 33 | + "register-signatures".to_string() |
| 34 | + } |
| 35 | + |
| 36 | + fn body(&self) -> Option<Self::Body> { |
| 37 | + Some(self.message.clone()) |
| 38 | + } |
| 39 | + |
| 40 | + async fn handle_response( |
| 41 | + &self, |
| 42 | + context: QueryContext, |
| 43 | + ) -> AggregatorHttpClientResult<Self::Response> { |
| 44 | + debug!(context.logger, "Register signature"; "signed_entity" => ?self.message.signed_entity_type); |
| 45 | + |
| 46 | + match context.response.status() { |
| 47 | + StatusCode::CREATED | StatusCode::ACCEPTED => Ok(()), |
| 48 | + StatusCode::GONE => { |
| 49 | + let root_cause = AggregatorHttpClientError::get_root_cause(context.response).await; |
| 50 | + debug!(context.logger, "Message already certified or expired"; "details" => &root_cause); |
| 51 | + |
| 52 | + Ok(()) |
| 53 | + } |
| 54 | + _ => Err(context.unhandled_status_code().await), |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use httpmock::Method::POST; |
| 62 | + |
| 63 | + use mithril_common::entities::ClientError; |
| 64 | + use mithril_common::test::double::Dummy; |
| 65 | + |
| 66 | + use crate::test::{TestLogger, assert_error_matches, setup_server_and_client}; |
| 67 | + |
| 68 | + use super::*; |
| 69 | + |
| 70 | + #[tokio::test] |
| 71 | + async fn test_register_signature_ok_201() { |
| 72 | + let expected_message = RegisterSignatureMessageHttp::dummy(); |
| 73 | + let (server, client) = setup_server_and_client(); |
| 74 | + let _server_mock = server.mock(|when, then| { |
| 75 | + when.method(POST) |
| 76 | + .path("/register-signatures") |
| 77 | + .body(serde_json::to_string(&expected_message).unwrap()); |
| 78 | + then.status(201); |
| 79 | + }); |
| 80 | + |
| 81 | + let register_signature = |
| 82 | + client.send(PostRegisterSignatureQuery::new(expected_message)).await; |
| 83 | + register_signature.expect("unexpected error"); |
| 84 | + } |
| 85 | + |
| 86 | + #[tokio::test] |
| 87 | + async fn test_register_signature_ok_202() { |
| 88 | + let (server, client) = setup_server_and_client(); |
| 89 | + let _server_mock = server.mock(|when, then| { |
| 90 | + when.method(POST).path("/register-signatures"); |
| 91 | + then.status(202); |
| 92 | + }); |
| 93 | + |
| 94 | + let register_signature = client |
| 95 | + .send(PostRegisterSignatureQuery::new( |
| 96 | + RegisterSignatureMessageHttp::dummy(), |
| 97 | + )) |
| 98 | + .await; |
| 99 | + register_signature.expect("unexpected error"); |
| 100 | + } |
| 101 | + |
| 102 | + #[tokio::test] |
| 103 | + async fn test_register_signature_ko_400() { |
| 104 | + let (server, client) = setup_server_and_client(); |
| 105 | + let _server_mock = server.mock(|when, then| { |
| 106 | + when.method(POST).path("/register-signatures"); |
| 107 | + then.status(400).body( |
| 108 | + serde_json::to_vec(&ClientError::new( |
| 109 | + "error".to_string(), |
| 110 | + "an error".to_string(), |
| 111 | + )) |
| 112 | + .unwrap(), |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + let error = client |
| 117 | + .send(PostRegisterSignatureQuery::new( |
| 118 | + RegisterSignatureMessageHttp::dummy(), |
| 119 | + )) |
| 120 | + .await |
| 121 | + .unwrap_err(); |
| 122 | + |
| 123 | + assert_error_matches!(error, AggregatorHttpClientError::RemoteServerLogical(_)); |
| 124 | + } |
| 125 | + |
| 126 | + #[tokio::test] |
| 127 | + async fn test_register_signature_ok_410_log_response_body() { |
| 128 | + let (logger, log_inspector) = TestLogger::memory(); |
| 129 | + |
| 130 | + let (server, mut client) = setup_server_and_client(); |
| 131 | + client.logger = logger; |
| 132 | + let _server_mock = server.mock(|when, then| { |
| 133 | + when.method(POST).path("/register-signatures"); |
| 134 | + then.status(410).body( |
| 135 | + serde_json::to_vec(&ClientError::new( |
| 136 | + "already_aggregated".to_string(), |
| 137 | + "too late".to_string(), |
| 138 | + )) |
| 139 | + .unwrap(), |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + client |
| 144 | + .send(PostRegisterSignatureQuery::new( |
| 145 | + RegisterSignatureMessageHttp::dummy(), |
| 146 | + )) |
| 147 | + .await |
| 148 | + .expect("Should not fail when status is 410 (GONE)"); |
| 149 | + |
| 150 | + assert!(log_inspector.contains_log("already_aggregated")); |
| 151 | + assert!(log_inspector.contains_log("too late")); |
| 152 | + } |
| 153 | + |
| 154 | + #[tokio::test] |
| 155 | + async fn test_register_signature_ko_409() { |
| 156 | + let (server, client) = setup_server_and_client(); |
| 157 | + let _server_mock = server.mock(|when, then| { |
| 158 | + when.method(POST).path("/register-signatures"); |
| 159 | + then.status(409); |
| 160 | + }); |
| 161 | + |
| 162 | + let error = client |
| 163 | + .send(PostRegisterSignatureQuery::new( |
| 164 | + RegisterSignatureMessageHttp::dummy(), |
| 165 | + )) |
| 166 | + .await |
| 167 | + .unwrap_err(); |
| 168 | + |
| 169 | + assert_error_matches!(error, AggregatorHttpClientError::RemoteServerLogical(_)); |
| 170 | + } |
| 171 | + |
| 172 | + #[tokio::test] |
| 173 | + async fn test_register_signature_ko_500() { |
| 174 | + let (server, client) = setup_server_and_client(); |
| 175 | + let _server_mock = server.mock(|when, then| { |
| 176 | + when.method(POST).path("/register-signatures"); |
| 177 | + then.status(500).body("an error occurred"); |
| 178 | + }); |
| 179 | + |
| 180 | + let error = client |
| 181 | + .send(PostRegisterSignatureQuery::new( |
| 182 | + RegisterSignatureMessageHttp::dummy(), |
| 183 | + )) |
| 184 | + .await |
| 185 | + .unwrap_err(); |
| 186 | + |
| 187 | + assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_)); |
| 188 | + } |
| 189 | +} |
0 commit comments