You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.4 KiB

#[macro_use]
extern crate log;
3 years ago
#[cfg_attr(test, macro_use)]
extern crate mockall;
3 years ago
#[cfg_attr(test, macro_use)]
extern crate mockall_double;
3 years ago
// due to linking errors
extern crate openssl;
3 years ago
// don't touch anything
extern crate diesel;
3 years ago
// in this block
mod db;
mod handlers;
mod init;
use init::*;
use serde::Deserialize;
3 years ago
use std::path::PathBuf;
use u_lib::{config::MASTER_PORT, utils::Env};
use warp::Filter;
#[derive(Deserialize)]
struct ServEnv {
admin_auth_token: String,
}
//TODO: tracing-subscriber
pub async fn serve() -> Result<(), String> {
init_logger();
prefill_jobs();
let env = Env::<ServEnv>::init().map_err(|e| e.to_string())?;
let routes = init_filters(&env.inner.admin_auth_token);
3 years ago
let certs_dir = PathBuf::from("certs");
warp::serve(routes.with(warp::log("warp")))
3 years ago
.tls()
3 years ago
.cert_path(certs_dir.join("server.crt"))
.key_path(certs_dir.join("server.key"))
.client_auth_required_path(certs_dir.join("ca.crt"))
.run(([0, 0, 0, 0], MASTER_PORT))
.await;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[double]
use crate::handlers::Endpoints;
use handlers::build_ok;
use mockall::predicate::*;
use test_case::test_case;
use u_lib::messaging::{AsMsg, BaseMessage, Reportable};
use uuid::Uuid;
use warp::test::request;
#[test_case(Some(Uuid::new_v4()))]
#[test_case(None => panics)]
#[tokio::test]
async fn test_get_agent_jobs_unauthorized(uid: Option<Uuid>) {
let mock = Endpoints::get_agent_jobs_context();
3 years ago
mock.expect().with(eq(uid)).returning(|_| Ok(build_ok("")));
request()
.path(&format!(
"/get_agent_jobs/{}",
uid.map(|u| u.simple().to_string()).unwrap_or(String::new())
))
.method("GET")
.filter(&init_filters(""))
.await
.unwrap();
mock.checkpoint();
}
3 years ago
#[tokio::test]
async fn test_report_unauth_successful() {
let mock = Endpoints::report_context();
mock.expect()
.withf(|msg: &BaseMessage<'_, Vec<Reportable>>| msg.inner_ref()[0] == Reportable::Dummy)
3 years ago
.returning(|_| Ok(build_ok("")));
request()
.path("/report/")
.method("POST")
.json(&vec![Reportable::Dummy].as_message())
.filter(&init_filters(""))
3 years ago
.await
.unwrap();
mock.checkpoint();
}
}