use actix_web::Scope; use actix_web_httpauth::middleware::HttpAuthentication; use serde::{Deserialize, Serialize}; use crate::{api, auth}; #[macro_export] #[cfg(test)] macro_rules! call_endpoint { ($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"); 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(), ); let a = App::new() .wrap(tracing_actix_web::TracingLogger::default()) .app_data($state.clone()) .service(routes()); let app = actix_web::test::init_service(a).await; let resp = actix_web::test::call_service(&app, $req).await; resp }}; } #[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() }}; } pub mod application_categories; pub mod applications; pub mod authorization; pub mod bookmark_categories; pub mod bookmarks; mod api_prelude { pub use super::ListObjects; pub use crate::entity::prelude::*; pub use crate::entity::*; pub use crate::AppState; pub use actix_web::{delete, get, post, put, web, Error, HttpResponse, Scope}; pub use sea_orm::prelude::*; pub use sea_orm::{NotSet, Set}; pub use serde::{Deserialize, Serialize}; } #[cfg(test)] pub mod test_prelude { pub use super::ListObjects; use crate::auth; pub use crate::entity::*; pub use crate::AppState; pub use super::routes; pub use crate::error::Result; pub use actix_web::dev::ServiceResponse; pub use actix_web::{test, web, App}; use sea_orm::sea_query::TableCreateStatement; use sea_orm::ConnectionTrait; use sea_orm::Database; use sea_orm::DbBackend; use sea_orm::Schema; pub use sea_orm::{ entity::prelude::*, entity::*, tests_cfg::*, DatabaseBackend, MockDatabase, MockExecResult, Transaction, }; /// Sets up a testing state with an in-memory database and creates the scheme. pub async fn setup_state() -> Result> { let db = Database::connect("sqlite::memory:").await?; let schema = Schema::new(DbBackend::Sqlite); let stmt: TableCreateStatement = schema.create_table_from_entity(application::Entity); db.execute(db.get_database_backend().build(&stmt)).await?; let stmt: TableCreateStatement = schema.create_table_from_entity(application_category::Entity); db.execute(db.get_database_backend().build(&stmt)).await?; let stmt: TableCreateStatement = schema.create_table_from_entity(bookmark::Entity); db.execute(db.get_database_backend().build(&stmt)).await?; let stmt: TableCreateStatement = schema.create_table_from_entity(bookmark_category::Entity); db.execute(db.get_database_backend().build(&stmt)).await?; let stmt: TableCreateStatement = schema.create_table_from_entity(setting::Entity); db.execute(db.get_database_backend().build(&stmt)).await?; auth::generate_secret(&db).await?; Ok(actix_web::web::Data::new(AppState { db })) } } #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ListObjects where T: Serialize, { items: Vec, total: usize, } impl ListObjects { pub fn new(items: Vec, total: usize) -> Self { Self { items, total } } } 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); Scope::new("api") .service(api::authorization::authorize) .service(api::authorization::initial_setup) .service(protected_routes) }