mithril_aggregator_client/query/get/
get_certificates_list.rs

1use async_trait::async_trait;
2use reqwest::StatusCode;
3
4use mithril_common::messages::CertificateListMessage;
5
6use crate::AggregatorHttpClientResult;
7use crate::query::{AggregatorQuery, QueryContext, QueryMethod, ResponseExt};
8
9/// Query to get a list of certificates
10pub struct GetCertificatesListQuery {}
11
12impl GetCertificatesListQuery {
13    /// Instantiate a query to get the latest certificates list
14    pub fn latest() -> Self {
15        Self {}
16    }
17}
18
19#[cfg_attr(target_family = "wasm", async_trait(?Send))]
20#[cfg_attr(not(target_family = "wasm"), async_trait)]
21impl AggregatorQuery for GetCertificatesListQuery {
22    type Response = CertificateListMessage;
23    type Body = ();
24
25    fn method() -> QueryMethod {
26        QueryMethod::Get
27    }
28
29    fn route(&self) -> String {
30        "certificates".to_string()
31    }
32
33    async fn handle_response(
34        &self,
35        context: QueryContext,
36    ) -> AggregatorHttpClientResult<Self::Response> {
37        match context.response.status() {
38            StatusCode::OK => context.response.parse_json().await,
39            _ => Err(context.unhandled_status_code().await),
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use serde_json::json;
47
48    use mithril_common::messages::CertificateListItemMessage;
49    use mithril_common::test::double::Dummy;
50
51    use crate::AggregatorHttpClientError;
52    use crate::test::{assert_error_matches, setup_server_and_client};
53
54    use super::*;
55
56    #[tokio::test]
57    async fn test_latest_certificates_list_ok_200() {
58        let (server, client) = setup_server_and_client();
59        let expected_list = vec![
60            CertificateListItemMessage::dummy(),
61            CertificateListItemMessage::dummy(),
62        ];
63        let _server_mock = server.mock(|when, then| {
64            when.path("/certificates");
65            then.status(200).body(json!(expected_list).to_string());
66        });
67
68        let fetched_list = client.send(GetCertificatesListQuery::latest()).await.unwrap();
69
70        assert_eq!(expected_list, fetched_list);
71    }
72
73    #[tokio::test]
74    async fn test_latest_certificates_list_ko_500() {
75        let (server, client) = setup_server_and_client();
76        let _server_mock = server.mock(|when, then| {
77            when.any_request();
78            then.status(500).body("an error occurred");
79        });
80
81        let error = client.send(GetCertificatesListQuery::latest()).await.unwrap_err();
82
83        assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_));
84    }
85}