2022-02-05 06:11:24 +00:00
|
|
|
use actix_web::Scope;
|
|
|
|
use actix_web_httpauth::middleware::HttpAuthentication;
|
2022-02-04 19:36:54 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
use crate::{api, auth};
|
|
|
|
|
2022-02-03 21:55:10 +00:00
|
|
|
#[macro_export]
|
2022-02-04 19:36:54 +00:00
|
|
|
#[cfg(test)]
|
2022-02-03 21:55:10 +00:00
|
|
|
macro_rules! call_endpoint {
|
2022-02-04 19:36:54 +00:00
|
|
|
($req:ident, $state:ident) => {{
|
|
|
|
// let subscriber = tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt::with(
|
|
|
|
// tracing_subscriber::registry(),
|
|
|
|
// tracing_subscriber::Layer::with_filter(
|
|
|
|
// tracing_subscriber::fmt::Layer::new()
|
|
|
|
// .pretty()
|
|
|
|
// .with_writer(std::io::stdout)
|
|
|
|
// .with_ansi(true),
|
|
|
|
// tracing_subscriber::filter::LevelFilter::DEBUG,
|
|
|
|
// ),
|
|
|
|
// );
|
|
|
|
// tracing::subscriber::set_global_default(subscriber)
|
|
|
|
// .expect("Unable to set a global collector");
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
let jwt_secret = crate::auth::get_secret(&$state.db).await?;
|
|
|
|
let token = jsonwebtoken::encode(
|
|
|
|
&jsonwebtoken::Header::default(),
|
|
|
|
&crate::auth::AuthClaims {
|
|
|
|
exp: 10_000_000_000,
|
|
|
|
},
|
|
|
|
&jsonwebtoken::EncodingKey::from_secret(&jwt_secret),
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
crate::error::Error::new(
|
|
|
|
crate::error::ErrorCode::Internal,
|
|
|
|
&format!("Token error: {} ", e),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
$req.headers_mut().append(
|
|
|
|
actix_web::http::header::AUTHORIZATION,
|
|
|
|
actix_web::http::header::HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
|
|
|
|
);
|
|
|
|
|
2022-02-03 21:55:10 +00:00
|
|
|
let a = App::new()
|
2022-02-04 19:36:54 +00:00
|
|
|
.wrap(tracing_actix_web::TracingLogger::default())
|
|
|
|
.app_data($state.clone())
|
2022-02-05 06:11:24 +00:00
|
|
|
.service(routes());
|
2022-02-04 19:36:54 +00:00
|
|
|
let app = actix_web::test::init_service(a).await;
|
|
|
|
let resp = actix_web::test::call_service(&app, $req).await;
|
2022-02-03 21:55:10 +00:00
|
|
|
resp
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2022-02-04 19:36:54 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
macro_rules! get_response {
|
|
|
|
($resp: ident, $type:ty) => {{
|
|
|
|
let body = test::read_body($resp).await.to_vec();
|
|
|
|
serde_json::from_slice::<$type>(&body).unwrap()
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2022-02-04 20:28:53 +00:00
|
|
|
pub mod application_categories;
|
2022-02-03 21:55:10 +00:00
|
|
|
pub mod applications;
|
2022-02-05 06:11:24 +00:00
|
|
|
pub mod authorization;
|
2022-02-04 20:28:53 +00:00
|
|
|
pub mod bookmark_categories;
|
|
|
|
pub mod bookmarks;
|
2022-02-03 21:55:10 +00:00
|
|
|
|
|
|
|
mod api_prelude {
|
2022-02-04 19:36:54 +00:00
|
|
|
pub use super::ListObjects;
|
2022-02-03 21:55:10 +00:00
|
|
|
pub use crate::entity::prelude::*;
|
|
|
|
pub use crate::entity::*;
|
|
|
|
pub use crate::AppState;
|
2022-02-04 19:36:54 +00:00
|
|
|
pub use actix_web::{delete, get, post, put, web, Error, HttpResponse, Scope};
|
2022-02-03 21:55:10 +00:00
|
|
|
pub use sea_orm::prelude::*;
|
2022-02-04 19:36:54 +00:00
|
|
|
pub use sea_orm::{NotSet, Set};
|
2022-02-05 06:11:24 +00:00
|
|
|
pub use serde::{Deserialize, Serialize};
|
2022-02-03 21:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2022-02-05 06:11:24 +00:00
|
|
|
pub mod test_prelude {
|
2022-02-12 06:23:07 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-02-04 19:36:54 +00:00
|
|
|
pub use super::ListObjects;
|
2022-02-05 06:11:24 +00:00
|
|
|
use crate::auth;
|
2022-02-03 21:55:10 +00:00
|
|
|
pub use crate::entity::*;
|
|
|
|
pub use crate::AppState;
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
pub use super::routes;
|
2022-02-04 19:36:54 +00:00
|
|
|
pub use crate::error::Result;
|
2022-02-03 21:55:10 +00:00
|
|
|
pub use actix_web::dev::ServiceResponse;
|
|
|
|
pub use actix_web::{test, web, App};
|
|
|
|
pub use sea_orm::{
|
2022-02-04 19:36:54 +00:00
|
|
|
entity::prelude::*, entity::*, tests_cfg::*, DatabaseBackend, MockDatabase, MockExecResult,
|
|
|
|
Transaction,
|
2022-02-03 21:55:10 +00:00
|
|
|
};
|
2022-02-12 06:23:07 +00:00
|
|
|
use tokio::sync::Mutex;
|
2022-02-04 19:36:54 +00:00
|
|
|
|
|
|
|
/// Sets up a testing state with an in-memory database and creates the scheme.
|
|
|
|
pub async fn setup_state() -> Result<actix_web::web::Data<AppState>> {
|
2022-02-12 08:03:27 +00:00
|
|
|
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await?;
|
|
|
|
sqlx::migrate!("./migrations").run(&pool).await?;
|
2022-02-04 19:36:54 +00:00
|
|
|
|
2022-02-12 08:03:27 +00:00
|
|
|
let db = sea_orm::SqlxSqliteConnector::from_sqlx_sqlite_pool(pool);
|
2022-02-05 06:11:24 +00:00
|
|
|
|
|
|
|
auth::generate_secret(&db).await?;
|
|
|
|
|
2022-02-12 06:23:07 +00:00
|
|
|
Ok(actix_web::web::Data::new(AppState {
|
|
|
|
db,
|
|
|
|
healthcheck_status: Mutex::new(HashMap::new()),
|
|
|
|
}))
|
2022-02-04 19:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ListObjects<T>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
items: Vec<T>,
|
|
|
|
total: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Serialize> ListObjects<T> {
|
|
|
|
pub fn new(items: Vec<T>, total: usize) -> Self {
|
|
|
|
Self { items, total }
|
|
|
|
}
|
2022-02-03 21:55:10 +00:00
|
|
|
}
|
2022-02-05 06:11:24 +00:00
|
|
|
|
|
|
|
pub fn routes() -> Scope {
|
|
|
|
let auth_handler = HttpAuthentication::bearer(auth::validator);
|
|
|
|
let protected_routes = Scope::new("")
|
|
|
|
.wrap(auth_handler)
|
|
|
|
.service(api::applications::routes())
|
|
|
|
.service(api::application_categories::routes())
|
|
|
|
.service(api::bookmarks::routes())
|
|
|
|
.service(api::bookmark_categories::routes())
|
|
|
|
.service(api::authorization::update_password);
|
|
|
|
|
2022-02-08 04:04:45 +00:00
|
|
|
Scope::new("api")
|
2022-02-05 06:11:24 +00:00
|
|
|
.service(api::authorization::authorize)
|
|
|
|
.service(api::authorization::initial_setup)
|
|
|
|
.service(protected_routes)
|
|
|
|
}
|