pub mod auth;
pub mod session;
pub mod wrappers;
#[cfg(test)]
mod session_test {
use crate::auth;
use crate::auth::AddUserReturn;
use crate::session;
use crate::session::generate_session;
use actix_web::{http::header::ContentType, test, App, HttpMessage};
async fn connect_to_pool() -> sqlx::Pool<sqlx::Postgres> {
match auth::connect_to_db_get_pool().await {
Ok(pool) => pool,
Err(_) => panic!("could not connect to db"),
}
}
async fn complete_migrations(pool: &sqlx::Pool<sqlx::Postgres>) -> () {
let sqlx_migrator = sqlx::migrate!();
let _migration_undo = match sqlx_migrator.undo(pool, 0).await {
Ok(_) => true,
Err(err) => return assert!(false, "migrator failed with err: {}", err.to_string()),
};
let _migration_run = match sqlx_migrator.run(pool).await {
Ok(_) => true,
Err(_) => return assert!(false, "migrator failed run"),
};
}
use super::*;
#[actix_web::test]
async fn test_gen_and_valid_session_based() {
let postgres_pool = match auth::connect_to_db_get_pool().await {
Ok(pool) => pool,
Err(_err) => panic!("cound not connect to db"),
};
complete_migrations(&postgres_pool).await;
let creds = auth::Credentials {
user_name: "test_user_session_based".to_string().to_owned(),
password: "mypass".to_string().to_owned(),
realm: "user".to_string().to_owned(),
};
let _user_added = match auth::add_user(&creds, &postgres_pool).await {
AddUserReturn::Good() => (),
_ => panic!("Add user failed"),
};
let app = test::init_service(
App::new()
.app_data(actix_web::web::Data::new(postgres_pool.clone()))
.wrap(actix_session::SessionMiddleware::new(
actix_session::storage::CookieSessionStore::default(),
actix_web::cookie::Key::from(
"wfjro2f2ofj293fj23f2dfljw;fljf2lkfskjdf;slkdfjsd;lkfjsd;lfksjflkdjj23fkj3".as_bytes(),
),
))
.route(
"/generate_session",
actix_web::web::get().to(session::generate_session_web_resp),
)
.route(
"/validate_session",
actix_web::web::get().to(session::validate_session_web_resp),
),
)
.await;
let gen_sesh_req = test::TestRequest::get()
.uri("/generate_session")
.set_json(&creds)
.to_request();
let resp = test::call_service(&app, gen_sesh_req).await;
let cookies = match resp.response().cookies().next() {
Some(cookie) => cookie,
None => panic!("Cookie was not set")
};
let validate_session_request = test::TestRequest::get()
.uri("/validate_session")
.cookie(cookies)
.to_request();
let resp = test::call_service(&app, validate_session_request).await;
println!("{:?}", &resp.status());
println!("{:?}", &resp.response().body());
assert!(resp.status().is_success());
}
}
#[cfg(test)]
mod auth_tests {
use crate::auth;
async fn connect_to_pool() -> sqlx::Pool<sqlx::Postgres> {
match auth::connect_to_db_get_pool().await {
Ok(pool) => pool,
Err(_) => panic!("could not connect to db"),
}
}
async fn complete_migrations(pool: &sqlx::Pool<sqlx::Postgres>) -> () {
let sqlx_migrator = sqlx::migrate!();
let _migration_undo = match sqlx_migrator.undo(pool, 0).await {
Ok(_) => true,
Err(err) => return assert!(false, "migrator failed with err: {}", err.to_string()),
};
let _migration_run = match sqlx_migrator.run(pool).await {
Ok(_) => true,
Err(_) => return assert!(false, "migrator failed run"),
};
}
#[actix_web::test]
async fn get_session() {
let pool = connect_to_pool().await;
complete_migrations(&pool).await;
let creds = auth::Credentials {
user_name: "test_user".to_string().to_owned(),
password: "my_pass".to_string().to_owned(),
realm: "user".to_string().to_owned(),
};
let _add_user_result = match auth::add_user(&creds, &pool).await {
auth::AddUserReturn::Good() => (),
_ => (), };
match auth::generate_session(&creds, &pool, auth::SESSION_VALID_FOR_SECONDS).await {
Ok(session) => {
assert!(session.user_name == creds.user_name && session.session_token != "")
}
Err(err) => panic!("the test for get session failed with: {:?}", err),
}
}
#[actix_web::test]
async fn verify_session() {
let pool = connect_to_pool().await;
complete_migrations(&pool).await;
let creds = auth::Credentials {
user_name: "test_user_verify_session".to_string().to_owned(),
password: "mypass".to_string().to_owned(),
realm: "user".to_string().to_owned(),
};
let _add_user_result = match auth::add_user(&creds, &pool).await {
auth::AddUserReturn::Good() => (),
_ => panic!("Add user failed"),
};
let session = match auth::generate_session(&creds, &pool, auth::SESSION_VALID_FOR_SECONDS).await {
Ok(session) => session,
Err(err) => {
return assert!(
false,
"Generate session got error:{:?}\non user:{:?}",
err, creds
)
}
};
match auth::validate_session(&session, &pool).await {
auth::SessionValidated::ValidSession() => assert!(true, "Session validated"),
auth::SessionValidated::InvalidSession() => {
assert!(false, "Session wrongly invalidated")
}
}
}
#[actix_web::test]
async fn verify_session_invalid_token_end() {
let pool = connect_to_pool().await;
complete_migrations(&pool).await;
let creds = auth::Credentials {
user_name: "test_user".to_string().to_owned(),
password: "my_pass".to_string().to_owned(),
realm: "user".to_string().to_owned(),
};
let _add_user_result = match auth::add_user(&creds, &pool).await {
auth::AddUserReturn::Good() => (),
_ => (),
};
let mut session = match auth::generate_session(&creds, &pool, auth::SESSION_VALID_FOR_SECONDS).await {
Ok(session) => session,
Err(err) => return assert!(false, "{:?}", err),
};
let replace_last_char_with = match session.session_token.pop() {
Some(c) => {
if c == 'a' {
'b'
} else {
'a'
}
}
None => 'a',
};
session.session_token.push(replace_last_char_with);
match auth::validate_session(&session, &pool).await {
auth::SessionValidated::ValidSession() => assert!(false, "Session validated wrongly"),
auth::SessionValidated::InvalidSession() => {
assert!(true, "Session correctly invalidated")
}
}
}
#[actix_web::test]
async fn verify_session_invalid_user_name() {
let pool = connect_to_pool().await;
complete_migrations(&pool).await;
let creds = auth::Credentials {
user_name: "test_user".to_string().to_owned(),
password: "my_pass".to_string().to_owned(),
realm: "user".to_string().to_owned(),
};
let _add_user_result = match auth::add_user(&creds, &pool).await {
auth::AddUserReturn::Good() => (),
_ => (),
};
let mut session = match auth::generate_session(&creds, &pool, auth::SESSION_VALID_FOR_SECONDS).await {
Ok(session) => session,
Err(err) => return assert!(false, "{:?}", err),
};
session.user_name = "".to_string();
match auth::validate_session(&session, &pool).await {
auth::SessionValidated::ValidSession() => assert!(false, "Session validated wrongly"),
auth::SessionValidated::InvalidSession() => {
assert!(true, "Session correctly invalidated")
}
}
}
#[actix_web::test]
async fn invalidate_session_test() {
let pool = connect_to_pool().await;
complete_migrations(&pool).await;
let creds = auth::Credentials {
user_name: "test_user".to_string().to_owned(),
password: "my_pass".to_string().to_owned(),
realm: "user".to_string().to_owned(),
};
let _add_user_result = match auth::add_user(&creds, &pool).await {
auth::AddUserReturn::Good() => (),
_ => (),
};
let session = match auth::generate_session(&creds, &pool, auth::SESSION_VALID_FOR_SECONDS).await {
Ok(session) => session,
Err(err) => return assert!(false, "{:?}", err),
};
match auth::invalidate_session(&session, &pool).await {
auth::SessionInvalided::SucessfullyInvalidated() => {
match auth::validate_session(&session, &pool).await {
auth::SessionValidated::ValidSession() => {
panic!("Session was reported invalidated but was still returning as valid")
}
auth::SessionValidated::InvalidSession() => {
assert!(true, "Session invalidated correctly")
}
}
}
_ => assert!(false, "Session invalidated error"),
}
}
}