Skip to content

Commit

Permalink
Implement new certificate list route
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraynaud committed May 26, 2023
1 parent f738a2e commit 51d33e2
Showing 1 changed file with 90 additions and 1 deletion.
91 changes: 90 additions & 1 deletion mithril-aggregator/src/http_server/routes/certificate_routes.rs
Expand Up @@ -7,6 +7,7 @@ pub fn routes(
dependency_manager: Arc<DependencyManager>,
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
certificate_pending(dependency_manager.clone())
.or(certificate_certificates(dependency_manager.clone()))
.or(certificate_certificate_hash(dependency_manager))
}

Expand All @@ -22,6 +23,16 @@ fn certificate_pending(
.and_then(handlers::certificate_pending)
}

/// GET /certificates
fn certificate_certificates(
dependency_manager: Arc<DependencyManager>,
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
warp::path!("certificates")
.and(warp::get())
.and(middlewares::with_certificate_store(dependency_manager))
.and_then(handlers::certificate_certificates)
}

/// GET /certificate/{certificate_hash}
fn certificate_certificate_hash(
dependency_manager: Arc<DependencyManager>,
Expand All @@ -34,13 +45,15 @@ fn certificate_certificate_hash(

mod handlers {
use crate::http_server::routes::reply;
use crate::message_adapters::ToCertificateMessageAdapter;
use crate::message_adapters::{ToCertificateListMessageAdapter, ToCertificateMessageAdapter};
use crate::{CertificatePendingStore, CertificateStore, ToCertificatePendingMessageAdapter};
use slog_scope::{debug, warn};
use std::convert::Infallible;
use std::sync::Arc;
use warp::http::StatusCode;

pub const LIST_MAX_ITEMS: usize = 20;

/// Certificate Pending
pub async fn certificate_pending(
certificate_pending_store: Arc<CertificatePendingStore>,
Expand All @@ -60,6 +73,24 @@ mod handlers {
}
}

/// List all Certificates
pub async fn certificate_certificates(
certificate_store: Arc<CertificateStore>,
) -> Result<impl warp::Reply, Infallible> {
debug!("⇄ HTTP SERVER: certificate_certificates",);

match certificate_store.get_list(LIST_MAX_ITEMS).await {
Ok(certificates) => Ok(reply::json(
&ToCertificateListMessageAdapter::adapt(certificates),
StatusCode::OK,
)),
Err(err) => {
warn!("certificate_certificates::error"; "error" => ?err);
Ok(reply::internal_server_error(err.to_string()))
}
}
}

/// Certificate by certificate hash
pub async fn certificate_certificate_hash(
certificate_hash: String,
Expand Down Expand Up @@ -178,6 +209,64 @@ mod tests {
);
}

#[tokio::test]
async fn test_certificate_certificates_get_ok() {
let dependency_manager = initialize_dependencies().await;
dependency_manager
.certificate_store
.save(fake_data::genesis_certificate(
"{certificate_hash}".to_string(),
))
.await
.expect("certificate store save should have succeeded");

let method = Method::GET.as_str();
let path = "/certificates";

let response = request()
.method(method)
.path(&format!("/{SERVER_BASE_PATH}{path}"))
.reply(&setup_router(Arc::new(dependency_manager)))
.await;

APISpec::verify_conformity(
APISpec::get_all_spec_files(),
method,
path,
"application/json",
&Null,
&response,
);
}

#[tokio::test]
async fn test_certificate_certificates_get_ko() {
let mut dependency_manager = initialize_dependencies().await;
let certificate_store = CertificateStore::new(Box::new(FailStoreAdapter::<
String,
entities::Certificate,
>::new()));
dependency_manager.certificate_store = Arc::new(certificate_store);

let method = Method::GET.as_str();
let path = "/certificates";

let response = request()
.method(method)
.path(&format!("/{SERVER_BASE_PATH}{path}"))
.reply(&setup_router(Arc::new(dependency_manager)))
.await;

APISpec::verify_conformity(
APISpec::get_all_spec_files(),
method,
path,
"application/json",
&Null,
&response,
);
}

#[tokio::test]
async fn test_certificate_certificate_hash_get_ok() {
let dependency_manager = initialize_dependencies().await;
Expand Down

0 comments on commit 51d33e2

Please sign in to comment.