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.

118 lines
3.0 KiB

#[macro_use]
extern crate log;
#[macro_use]
extern crate mockall;
#[macro_use]
extern crate mockall_double;
// because of linking errors
extern crate openssl;
#[macro_use]
extern crate diesel;
//
mod db;
mod filters;
mod handlers;
use db::UDB;
use filters::make_filters;
use u_lib::{config::MASTER_PORT, models::*, utils::init_env};
use warp::Filter;
const LOGFILE: &str = "u_server.log";
fn prefill_jobs() {
let agent_hello = JobMeta::builder()
.with_type(misc::JobType::Manage)
.with_alias("agent_hello")
.build()
.unwrap();
UDB::lock_db().insert_jobs(&[agent_hello]).ok();
}
fn init_logger() {
use simplelog::*;
use std::fs::OpenOptions;
let log_cfg = ConfigBuilder::new()
.set_time_format_str("%x %X")
.set_time_to_local(true)
.build();
let logfile = OpenOptions::new()
.append(true)
.create(true)
.open(LOGFILE)
.unwrap();
3 years ago
let level = LevelFilter::Info;
let loggers = vec![
3 years ago
WriteLogger::new(level, log_cfg.clone(), logfile) as Box<dyn SharedLogger>,
TermLogger::new(level, log_cfg, TerminalMode::Stderr, ColorChoice::Auto),
];
CombinedLogger::init(loggers).unwrap();
}
fn init_all() {
init_logger();
init_env();
prefill_jobs();
}
pub async fn serve() {
init_all();
let routes = make_filters();
warp::serve(routes.with(warp::log("warp")))
3 years ago
.tls()
.cert_path("./certs/server.crt")
.key_path("./certs/server.key")
.client_auth_required_path("./certs/ca.crt")
.run(([0, 0, 0, 0], MASTER_PORT))
.await;
}
#[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(&make_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())
3 years ago
.filter(&make_filters())
.await
.unwrap();
mock.checkpoint();
}
}