Rohan Sircar
3 years ago
12 changed files with 335 additions and 195 deletions
-
9.env
-
16.github/workflows/ci.yml
-
49Cargo.lock
-
31Cargo.toml
-
14src/errors/domain_error.rs
-
96src/lib.rs
-
137src/main.rs
-
8src/models/errors.rs
-
12src/routes/auth.rs
-
22src/routes/users.rs
-
89tests/integration/common/mod.rs
-
47tests/integration/main.rs
@ -1,3 +1,8 @@ |
|||
#this is required for diesel_cli to work |
|||
DATABASE_URL=data/app.db |
|||
BIND_ADDRESS=127.0.0.1:7800 |
|||
HASH_COST=8 |
|||
ACTIX_DEMO_DATABASE_URL=${DATABASE_URL} |
|||
ACTIX_DEMO_RUST_LOG=actix_demo=debug |
|||
ACTIX_DEMO_TEST_RUST_LOG=debug |
|||
ACTIX_DEMO_HTTP_HOST=127.0.0.1 |
|||
ACTIX_DEMO_HASH_COST=8 |
|||
|
@ -0,0 +1,96 @@ |
|||
#[macro_use]
|
|||
extern crate diesel;
|
|||
#[macro_use]
|
|||
extern crate derive_new;
|
|||
#[macro_use]
|
|||
extern crate log;
|
|||
extern crate bcrypt;
|
|||
extern crate custom_error;
|
|||
extern crate regex;
|
|||
extern crate validator;
|
|||
|
|||
mod actions;
|
|||
mod errors;
|
|||
mod middlewares;
|
|||
pub mod models;
|
|||
mod routes;
|
|||
mod schema;
|
|||
mod services;
|
|||
mod types;
|
|||
mod utils;
|
|||
|
|||
use actix_files as fs;
|
|||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
|||
use actix_web::{cookie::SameSite, middleware, web, App, HttpServer};
|
|||
use actix_web::{middleware::Logger, web::ServiceConfig};
|
|||
use rand::Rng;
|
|||
use serde::Deserialize;
|
|||
use types::DbPool;
|
|||
|
|||
#[derive(Deserialize, Debug, Clone)]
|
|||
pub struct EnvConfig {
|
|||
pub database_url: String,
|
|||
pub http_host: String,
|
|||
#[serde(default = "default_hash_cost")]
|
|||
pub hash_cost: u8,
|
|||
}
|
|||
|
|||
#[derive(Deserialize, Debug, Clone)]
|
|||
pub struct AppConfig {
|
|||
pub hash_cost: u8,
|
|||
}
|
|||
|
|||
#[derive(Clone)]
|
|||
pub struct AppData {
|
|||
pub config: AppConfig,
|
|||
pub pool: DbPool,
|
|||
}
|
|||
|
|||
pub fn default_hash_cost() -> u8 {
|
|||
8
|
|||
}
|
|||
|
|||
pub fn configure_app(app_data: AppData) -> Box<dyn Fn(&mut ServiceConfig)> {
|
|||
Box::new(move |cfg: &mut ServiceConfig| {
|
|||
cfg.data(app_data.clone())
|
|||
.service(
|
|||
web::scope("/api")
|
|||
.service(routes::users::get_user)
|
|||
.service(routes::users::get_all_users),
|
|||
)
|
|||
// .route("/api/users/get", web::get().to(user_controller.get_user.into()))
|
|||
.service(web::scope("/api/public")) // public endpoint - not implemented yet
|
|||
.service(routes::auth::login)
|
|||
.service(routes::auth::logout)
|
|||
.service(routes::auth::index)
|
|||
.service(routes::users::add_user)
|
|||
.service(fs::Files::new("/", "./static"));
|
|||
})
|
|||
}
|
|||
|
|||
pub fn id_service(
|
|||
private_key: &[u8],
|
|||
) -> actix_identity::IdentityService<CookieIdentityPolicy> {
|
|||
IdentityService::new(
|
|||
CookieIdentityPolicy::new(&private_key)
|
|||
.name("my-app-auth")
|
|||
.secure(false)
|
|||
.same_site(SameSite::Lax),
|
|||
)
|
|||
}
|
|||
|
|||
pub fn app_logger() -> Logger {
|
|||
middleware::Logger::default()
|
|||
}
|
|||
|
|||
pub async fn run(addr: String, app_data: AppData) -> std::io::Result<()> {
|
|||
info!("Starting server at {}", addr);
|
|||
let private_key = rand::thread_rng().gen::<[u8; 32]>();
|
|||
let app = move || {
|
|||
App::new()
|
|||
.configure(configure_app(app_data.clone()))
|
|||
.wrap(id_service(&private_key))
|
|||
.wrap(app_logger())
|
|||
};
|
|||
HttpServer::new(app).bind(addr)?.run().await
|
|||
}
|
@ -0,0 +1,89 @@ |
|||
extern crate actix_demo;
|
|||
use actix_demo::{AppConfig, AppData};
|
|||
use actix_web::test;
|
|||
use actix_web::App;
|
|||
use diesel::SqliteConnection;
|
|||
|
|||
use diesel::r2d2::{self, ConnectionManager};
|
|||
use env_logger::Env;
|
|||
use std::io;
|
|||
use std::io::ErrorKind;
|
|||
|
|||
use actix_demo::configure_app;
|
|||
|
|||
use actix_http::Request;
|
|||
use actix_web::{dev as ax_dev, Error as AxError};
|
|||
|
|||
pub async fn test_app() -> impl ax_dev::Service<
|
|||
Request = Request,
|
|||
Response = ax_dev::ServiceResponse<impl ax_dev::MessageBody>,
|
|||
Error = AxError,
|
|||
> {
|
|||
let _ = dotenv::dotenv()
|
|||
.map_err(|err| {
|
|||
io::Error::new(
|
|||
ErrorKind::Other,
|
|||
format!("Failed to set up env: {:?}", err),
|
|||
)
|
|||
})
|
|||
.unwrap();
|
|||
let _ = env_logger::builder()
|
|||
.is_test(true)
|
|||
.parse_env(Env::default().filter("ACTIX_DEMO_TEST_RUST_LOG"))
|
|||
.try_init();
|
|||
|
|||
let connspec = ":memory:";
|
|||
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
|||
let pool = r2d2::Pool::builder()
|
|||
.build(manager)
|
|||
.map_err(|err| {
|
|||
io::Error::new(
|
|||
ErrorKind::Other,
|
|||
format!("Failed to create pool: {:?}", err),
|
|||
)
|
|||
})
|
|||
.unwrap();
|
|||
|
|||
let _ = {
|
|||
let conn = &pool
|
|||
.get()
|
|||
.map_err(|err| {
|
|||
io::Error::new(
|
|||
ErrorKind::Other,
|
|||
format!("Failed to get connection: {:?}", err),
|
|||
)
|
|||
})
|
|||
.unwrap();
|
|||
|
|||
let migrations_dir = diesel_migrations::find_migrations_directory()
|
|||
.map_err(|err| {
|
|||
io::Error::new(
|
|||
ErrorKind::Other,
|
|||
format!("Error finding migrations dir: {:?}", err),
|
|||
)
|
|||
})
|
|||
.unwrap();
|
|||
let _ = diesel_migrations::run_pending_migrations_in_directory(
|
|||
conn,
|
|||
&migrations_dir,
|
|||
&mut io::sink(),
|
|||
)
|
|||
.map_err(|err| {
|
|||
io::Error::new(
|
|||
ErrorKind::Other,
|
|||
format!("Error running migrations: {:?}", err),
|
|||
)
|
|||
})
|
|||
.unwrap();
|
|||
};
|
|||
|
|||
test::init_service(
|
|||
App::new()
|
|||
.configure(configure_app(AppData {
|
|||
config: AppConfig { hash_cost: 8 },
|
|||
pool,
|
|||
}))
|
|||
.wrap(actix_web::middleware::Logger::default()),
|
|||
)
|
|||
.await
|
|||
}
|
@ -0,0 +1,47 @@ |
|||
mod common;
|
|||
|
|||
#[cfg(test)]
|
|||
mod tests {
|
|||
|
|||
use super::*;
|
|||
extern crate actix_demo;
|
|||
use actix_demo::models::ErrorModel;
|
|||
use actix_web::dev::Service as _;
|
|||
use actix_web::http::StatusCode;
|
|||
use actix_web::test;
|
|||
|
|||
#[actix_rt::test]
|
|||
async fn get_users_api_should_succeed() {
|
|||
let req = test::TestRequest::get().uri("/api/get/users").to_request();
|
|||
let resp = common::test_app().await.call(req).await.unwrap();
|
|||
assert_eq!(resp.status(), StatusCode::ACCEPTED);
|
|||
let body: ErrorModel = test::read_body_json(resp).await;
|
|||
assert_eq!(
|
|||
body,
|
|||
ErrorModel {
|
|||
success: false,
|
|||
reason: "Entity does not exist - No users available".to_owned()
|
|||
}
|
|||
);
|
|||
log::debug!("{:?}", body);
|
|||
}
|
|||
|
|||
#[actix_rt::test]
|
|||
async fn get_user_api_should_succeed() {
|
|||
let req = test::TestRequest::get()
|
|||
.uri("/api/get/users/1")
|
|||
.to_request();
|
|||
let resp = common::test_app().await.call(req).await.unwrap();
|
|||
assert_eq!(resp.status(), StatusCode::ACCEPTED);
|
|||
let body: ErrorModel = test::read_body_json(resp).await;
|
|||
assert_eq!(
|
|||
body,
|
|||
ErrorModel {
|
|||
success: false,
|
|||
reason: "Entity does not exist - No user found with uid: 1"
|
|||
.to_owned()
|
|||
}
|
|||
);
|
|||
log::debug!("{:?}", body);
|
|||
}
|
|||
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue