Skip to content

Commit 883789a

Browse files
committed
test(aggregator-client): add tests for CertificateRetriver impl
1 parent c5822d4 commit 883789a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

internal/mithril-aggregator-client/src/external_trait_impls.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,66 @@ impl CertificateRetriever for AggregatorHttpClient {
2828
message.try_into().map_err(CertificateRetrieverError)
2929
}
3030
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use mithril_common::{
35+
entities::ServerError, messages::CertificateMessage, test::double::Dummy,
36+
};
37+
38+
use super::*;
39+
40+
#[tokio::test]
41+
async fn test_retrieve_certificate_that_exist() {
42+
let certificate_message = CertificateMessage::dummy();
43+
let expected_certificate = certificate_message.clone().try_into().unwrap();
44+
45+
let (server, client) = crate::test::setup_server_and_client();
46+
server.mock(|when, then| {
47+
when.method(httpmock::Method::GET)
48+
.path(format!("/certificate/{}", certificate_message.hash));
49+
then.status(200)
50+
.body(serde_json::to_string(&certificate_message).unwrap());
51+
});
52+
53+
let certificate = client
54+
.get_certificate_details(&certificate_message.hash)
55+
.await
56+
.unwrap();
57+
58+
assert_eq!(certificate, expected_certificate);
59+
}
60+
61+
#[tokio::test]
62+
async fn test_retrieve_certificate_that_does_not_exist() {
63+
let (server, client) = crate::test::setup_server_and_client();
64+
server.mock(|when, then| {
65+
when.method(httpmock::Method::GET);
66+
then.status(404);
67+
});
68+
69+
let error = client.get_certificate_details("whatever").await.unwrap_err();
70+
71+
assert!(
72+
format!("{error:?}").contains("Certificate does not exist"),
73+
"Error message should contain 'Certificate does not exist', error:\n{error:?}",
74+
);
75+
}
76+
77+
#[tokio::test]
78+
async fn test_retrieve_certificate_when_request_fails() {
79+
let (server, client) = crate::test::setup_server_and_client();
80+
server.mock(|when, then| {
81+
when.method(httpmock::Method::GET);
82+
then.status(500)
83+
.body(serde_json::to_string(&ServerError::new("an error")).unwrap());
84+
});
85+
86+
let error = client.get_certificate_details("whatever").await.unwrap_err();
87+
88+
assert!(
89+
format!("{error:?}").contains("Failed to retrieve certificate with hash"),
90+
"Error message should contain 'Failed to retrieve certificate with hash', error:\n{error:?}",
91+
);
92+
}
93+
}

0 commit comments

Comments
 (0)