chore: migrate tests to also run with mariadb

This commit is contained in:
realaravinth 2022-07-22 19:14:18 +05:30
parent 912b342e0e
commit 249b6461ee
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
24 changed files with 352 additions and 59 deletions

View file

@ -126,8 +126,7 @@ test: frontend-test frontend ## Run all available tests
cd db/db-sqlx-maria &&\
DATABASE_URL=${MARIA_DATABASE_URL}\
cargo test --no-fail-fast
DATABASE_URL=${MARIA_DATABASE_URL} cargo test --no-fail-fast
DATABASE_URL=${POSTGRES_DATABASE_URL} cargo test --no-fail-fast
cargo test --no-fail-fast
# ./scripts/tests.sh
xml-test-coverage: migrate ## Generate code coverage report in XML format

16
db/db-sqlx-maria/.gitignore vendored Normal file
View file

@ -0,0 +1,16 @@
/target
tarpaulin-report.html
.env
.env
cobertura.xml
prod/
node_modules/
/static-assets/bundle
static/cache/bundle
./templates/**/*.js
/static-assets/bundle/*
src/cache_buster_data.json
coverage
dist
assets
yarn-error.log

View file

@ -33,16 +33,16 @@ pub fn map_row_not_found_err(e: Error, row_not_found: DBError) -> DBError {
/// map postgres errors to [DBError](DBError) types
pub fn map_register_err(e: Error) -> DBError {
if let Error::Database(err) = e {
if err.code() == Some(Cow::from("23505")) {
if err.code() == Some(Cow::from("1602")) {
let msg = err.message();
println!("{}", msg);
if msg.contains("mcaptcha_users_name_key") {
if msg.contains("name") {
DBError::UsernameTaken
} else if msg.contains("mcaptcha_users_email_key") {
} else if msg.contains("email") {
DBError::EmailTaken
} else if msg.contains("mcaptcha_users_secret_key") {
} else if msg.contains("secret") {
DBError::SecretTaken
} else if msg.contains("mcaptcha_config_key_key") {
} else if msg.contains("captcha_key") {
DBError::CaptchaKeyTaken
} else {
DBError::DBError(Box::new(Error::Database(err)))

View file

@ -106,12 +106,22 @@ pub mod tests {
use crate::tests::*;
#[actix_rt::test]
pub async fn update_password_works() {
async fn update_password_works_pg() {
let data = crate::tests::pg::get_data().await;
update_password_works(data).await;
}
#[actix_rt::test]
async fn update_password_works_maria() {
let data = crate::tests::maria::get_data().await;
update_password_works(data).await;
}
pub async fn update_password_works(data: ArcData) {
const NAME: &str = "updatepassuser";
const PASSWORD: &str = "longpassword2";
const EMAIL: &str = "updatepassuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -29,11 +29,21 @@ use crate::errors::*;
use crate::tests::*;
#[actix_rt::test]
pub async fn uname_email_exists_works() {
async fn uname_email_exists_works_pg() {
let data = crate::tests::pg::get_data().await;
uname_email_exists_works(data).await;
}
#[actix_rt::test]
async fn uname_email_exists_works_maria() {
let data = crate::tests::maria::get_data().await;
uname_email_exists_works(data).await;
}
pub async fn uname_email_exists_works(data: ArcData) {
const NAME: &str = "testuserexists";
const PASSWORD: &str = "longpassword2";
const EMAIL: &str = "testuserexists@a.com2";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;
@ -115,14 +125,24 @@ pub async fn uname_email_exists_works() {
}
#[actix_rt::test]
pub async fn email_udpate_password_validation_del_userworks() {
async fn email_udpate_password_validation_del_userworks_pg() {
let data = crate::tests::pg::get_data().await;
email_udpate_password_validation_del_userworks(data).await;
}
#[actix_rt::test]
async fn email_udpate_password_validation_del_userworks_maria() {
let data = crate::tests::maria::get_data().await;
email_udpate_password_validation_del_userworks(data).await;
}
pub async fn email_udpate_password_validation_del_userworks(data: ArcData) {
const NAME: &str = "testuser2";
const PASSWORD: &str = "longpassword2";
const EMAIL: &str = "testuser1@a.com2";
const NAME2: &str = "eupdauser";
const EMAIL2: &str = "eupdauser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;
delete_user(data, NAME2).await;
@ -198,7 +218,18 @@ pub async fn email_udpate_password_validation_del_userworks() {
}
#[actix_rt::test]
pub async fn username_update_works() {
async fn username_update_works_pg() {
let data = crate::tests::pg::get_data().await;
username_update_works(data).await;
}
#[actix_rt::test]
async fn username_update_works_maria() {
let data = crate::tests::maria::get_data().await;
username_update_works(data).await;
}
pub async fn username_update_works(data: ArcData) {
const NAME: &str = "testuserupda";
const EMAIL: &str = "testuserupda@sss.com";
const EMAIL2: &str = "testuserupda2@sss.com";
@ -206,7 +237,6 @@ pub async fn username_update_works() {
const NAME2: &str = "terstusrtds";
const NAME_CHANGE: &str = "terstusrtdsxx";
let data = get_data().await;
let data = &data;
futures::join!(

View file

@ -263,11 +263,21 @@ pub mod tests {
}
#[actix_rt::test]
pub async fn easy_works() {
async fn easy_works_pg() {
let data = crate::tests::pg::get_data().await;
easy_works(data).await;
}
#[actix_rt::test]
async fn easy_works_maria() {
let data = crate::tests::maria::get_data().await;
easy_works(data).await;
}
pub async fn easy_works(data: ArcData) {
const NAME: &str = "defaultuserconfgworks";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "defaultuserconfgworks@a.com";
let data = crate::tests::get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -37,11 +37,21 @@ const L2: Level = Level {
};
#[actix_rt::test]
pub async fn level_routes_work() {
async fn level_routes_work_pg() {
let data = crate::tests::pg::get_data().await;
level_routes_work(data).await;
}
#[actix_rt::test]
async fn level_routes_work_maria() {
let data = crate::tests::maria::get_data().await;
level_routes_work(data).await;
}
pub async fn level_routes_work(data: ArcData) {
const NAME: &str = "testuserlevelroutes";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "testuserlevelrouts@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -155,11 +155,21 @@ mod tests {
use crate::*;
#[actix_rt::test]
async fn update_and_get_mcaptcha_works() {
async fn update_and_get_mcaptcha_works_pg() {
let data = crate::tests::pg::get_data().await;
update_and_get_mcaptcha_works(data).await;
}
#[actix_rt::test]
async fn update_and_get_mcaptcha_works_maria() {
let data = crate::tests::maria::get_data().await;
update_and_get_mcaptcha_works(data).await;
}
async fn update_and_get_mcaptcha_works(data: ArcData) {
const NAME: &str = "updateusermcaptcha";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "testupdateusermcaptcha@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -121,9 +121,19 @@ pub mod tests {
}
#[actix_rt::test]
pub async fn health_works() {
async fn health_works_pg() {
let data = crate::tests::pg::get_data().await;
health_works(data).await;
}
#[actix_rt::test]
async fn health_works_maria() {
let data = crate::tests::maria::get_data().await;
health_works(data).await;
}
pub async fn health_works(data: ArcData) {
println!("{}", V1_API_ROUTES.meta.health);
let data = crate::tests::get_data().await;
let data = &data;
let app = get_app!(data).await;

View file

@ -66,14 +66,24 @@ pub mod tests {
use crate::*;
#[actix_rt::test]
pub async fn notification_works() {
async fn notification_works_pg() {
let data = pg::get_data().await;
notification_works(data).await;
}
#[actix_rt::test]
async fn notification_works_maria() {
let data = maria::get_data().await;
notification_works(data).await;
}
pub async fn notification_works(data: ArcData) {
const NAME1: &str = "notifuser1";
const NAME2: &str = "notiuser2";
const PASSWORD: &str = "longpassworddomain";
const EMAIL1: &str = "testnotification1@a.com";
const EMAIL2: &str = "testnotification2@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME1).await;

View file

@ -86,7 +86,18 @@ pub mod tests {
use crate::*;
#[actix_rt::test]
pub async fn notification_get_works() {
async fn notification_get_works_pg() {
let data = pg::get_data().await;
notification_get_works(data).await;
}
#[actix_rt::test]
async fn notification_get_works_maria() {
let data = maria::get_data().await;
notification_get_works(data).await;
}
pub async fn notification_get_works(data: ArcData) {
const NAME1: &str = "notifuser12";
const NAME2: &str = "notiuser22";
const PASSWORD: &str = "longpassworddomain";
@ -95,7 +106,6 @@ pub mod tests {
const HEADING: &str = "testing notifications get";
const MESSAGE: &str = "testing notifications get message";
let data = get_data().await;
let data = &data;
delete_user(data, NAME1).await;

View file

@ -60,7 +60,18 @@ pub mod tests {
use crate::*;
#[actix_rt::test]
pub async fn notification_mark_read_works() {
async fn notification_mark_read_works_pg() {
let data = pg::get_data().await;
notification_mark_read_works(data).await;
}
#[actix_rt::test]
async fn notification_mark_read_works_maria() {
let data = maria::get_data().await;
notification_mark_read_works(data).await;
}
pub async fn notification_mark_read_works(data: ArcData) {
const NAME1: &str = "notifuser122";
const NAME2: &str = "notiuser222";
const PASSWORD: &str = "longpassworddomain";
@ -68,7 +79,6 @@ pub mod tests {
const EMAIL2: &str = "testnotification222@a.com";
const HEADING: &str = "testing notifications get";
const MESSAGE: &str = "testing notifications get message";
let data = get_data().await;
let data = &data;
delete_user(data, NAME1).await;

View file

@ -147,7 +147,18 @@ pub mod tests {
use libmcaptcha::pow::PoWConfig;
#[actix_rt::test]
pub async fn get_pow_config_works() {
async fn get_pow_config_works_pg() {
let data = crate::tests::pg::get_data().await;
get_pow_config_works(data).await;
}
#[actix_rt::test]
async fn get_pow_config_works_maria() {
let data = crate::tests::maria::get_data().await;
get_pow_config_works(data).await;
}
pub async fn get_pow_config_works(data: ArcData) {
use super::*;
use crate::tests::*;
use crate::*;
@ -157,7 +168,6 @@ pub mod tests {
const PASSWORD: &str = "testingpas";
const EMAIL: &str = "randomuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;
@ -186,7 +196,20 @@ pub mod tests {
}
#[actix_rt::test]
pub async fn pow_difficulty_factor_increases_on_visitor_count_increase() {
async fn pow_difficulty_factor_increases_on_visitor_count_increase_pg() {
let data = crate::tests::pg::get_data().await;
pow_difficulty_factor_increases_on_visitor_count_increase(data).await;
}
#[actix_rt::test]
async fn pow_difficulty_factor_increases_on_visitor_count_increase_maria() {
let data = crate::tests::maria::get_data().await;
pow_difficulty_factor_increases_on_visitor_count_increase(data).await;
}
pub async fn pow_difficulty_factor_increases_on_visitor_count_increase(
data: ArcData,
) {
use super::*;
use crate::tests::*;
use crate::*;
@ -214,7 +237,6 @@ pub mod tests {
visitor_threshold: 30,
};
let data = get_data().await;
let data = &data;
let levels = [L1, L2, L3];

View file

@ -59,11 +59,21 @@ pub mod tests {
use crate::*;
#[actix_rt::test]
pub async fn verify_pow_works() {
async fn verify_pow_works_pg() {
let data = crate::tests::pg::get_data().await;
verify_pow_works(data).await;
}
#[actix_rt::test]
async fn verify_pow_works_maria() {
let data = crate::tests::maria::get_data().await;
verify_pow_works(data).await;
}
pub async fn verify_pow_works(data: ArcData) {
const NAME: &str = "powverifyusr";
const PASSWORD: &str = "testingpas";
const EMAIL: &str = "verifyuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -80,7 +80,18 @@ pub mod tests {
use crate::*;
#[actix_rt::test]
pub async fn validate_captcha_token_works() {
async fn validate_captcha_token_works_pg() {
let data = crate::tests::pg::get_data().await;
validate_captcha_token_works(data).await;
}
#[actix_rt::test]
async fn validate_captcha_token_works_maria() {
let data = crate::tests::maria::get_data().await;
validate_captcha_token_works(data).await;
}
pub async fn validate_captcha_token_works(data: ArcData) {
const NAME: &str = "enterprisetken";
const PASSWORD: &str = "testingpas";
const EMAIL: &str = "verifyuser@enter.com";
@ -89,7 +100,6 @@ pub mod tests {
const VERIFY_TOKEN_URL: &str = "/api/v1/pow/siteverify";
// const UPDATE_URL: &str = "/api/v1/mcaptcha/domain/token/duration/update";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -26,12 +26,22 @@ use crate::*;
use crate::tests::*;
#[actix_rt::test]
pub async fn auth_works() {
async fn auth_works_pg_test() {
let data = pg::get_data().await;
auth_works(data).await;
}
#[actix_rt::test]
async fn auth_works_maria_test() {
let data = maria::get_data().await;
auth_works(data).await;
}
pub async fn auth_works(data: ArcData) {
const NAME: &str = "testuser";
const PASSWORD: &str = "longpassword";
const EMAIL: &str = "testuser1@a.com";
let data = get_data().await;
let data = &data;
let app = get_app!(data).await;
@ -143,11 +153,21 @@ pub async fn auth_works() {
}
#[actix_rt::test]
pub async fn serverside_password_validation_works() {
async fn serverside_password_validation_works_pg() {
let data = pg::get_data().await;
serverside_password_validation_works(data).await;
}
#[actix_rt::test]
async fn serverside_password_validation_works_maria() {
let data = maria::get_data().await;
serverside_password_validation_works(data).await;
}
pub async fn serverside_password_validation_works(data: ArcData) {
const NAME: &str = "testuser542";
const PASSWORD: &str = "longpassword2";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -23,11 +23,21 @@ use crate::*;
use crate::tests::*;
#[actix_rt::test]
async fn protected_routes_work() {
async fn protected_routes_work_pg() {
let data = pg::get_data().await;
protected_routes_work(data).await;
}
#[actix_rt::test]
async fn protected_routes_work_maria() {
let data = maria::get_data().await;
protected_routes_work(data).await;
}
async fn protected_routes_work(data: ArcData) {
const NAME: &str = "testuser619";
const PASSWORD: &str = "longpassword2";
const EMAIL: &str = "testuser119@a.com2";
let data = get_data().await;
let data = &data;
let _post_protected_urls = [

View file

@ -114,8 +114,18 @@ mod tests {
const DURATION: u64 = 5;
#[actix_rt::test]
async fn demo_account_works() {
let data_inner = get_data().await;
async fn demo_account_works_pg() {
let data = crate::tests::pg::get_data().await;
demo_account_works(data).await;
}
#[actix_rt::test]
async fn demo_account_works_maria() {
let data = crate::tests::maria::get_data().await;
demo_account_works(data).await;
}
async fn demo_account_works(data_inner: ArcData) {
let data_inner = &data_inner;
let data = AppData::new(data_inner.clone());
crate::tests::delete_user(data_inner, DEMO_USER).await;

View file

@ -101,10 +101,20 @@ mod tests {
use awc::Client;
#[actix_rt::test]
async fn email_verification_works() {
async fn email_verification_works_pg() {
let data = crate::tests::pg::get_data().await;
email_verification_works(data).await;
}
#[actix_rt::test]
async fn email_verification_works_maria() {
let data = crate::tests::maria::get_data().await;
email_verification_works(data).await;
}
async fn email_verification_works(data: crate::ArcData) {
const TO_ADDR: &str = "Hello <realaravinth@localhost>";
const VERIFICATION_LINK: &str = "https://localhost";
let data = crate::tests::get_data().await;
let settings = &data.settings;
verification(&data, TO_ADDR, VERIFICATION_LINK)
.await

View file

@ -47,12 +47,22 @@ mod tests {
use crate::*;
#[actix_rt::test]
async fn protected_pages_templates_work() {
async fn protected_pages_templates_work_pg_test() {
let data = pg::get_data().await;
protected_pages_templates_work(data).await;
}
#[actix_rt::test]
async fn protected_pages_templates_work_maria_test() {
let data = maria::get_data().await;
protected_pages_templates_work(data).await;
}
async fn protected_pages_templates_work(data: ArcData) {
const NAME: &str = "templateuser";
const PASSWORD: &str = "longpassword";
const EMAIL: &str = "templateuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -143,11 +143,21 @@ mod test {
use crate::*;
#[actix_rt::test]
async fn edit_sitekey_work() {
async fn edit_sitekey_work_pg_test() {
let data = pg::get_data().await;
edit_sitekey_work(data).await;
}
#[actix_rt::test]
async fn edit_sitekey_work_maria_test() {
let data = maria::get_data().await;
edit_sitekey_work(data).await;
}
async fn edit_sitekey_work(data: ArcData) {
const NAME: &str = "editsitekeyuser";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "editsitekeyuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -62,12 +62,22 @@ mod test {
use crate::*;
#[actix_rt::test]
async fn list_sitekeys_work() {
async fn list_sitekeys_work_pg() {
let data = pg::get_data().await;
list_sitekeys_work(data).await;
}
#[actix_rt::test]
async fn protected_routes_work_maria() {
let data = maria::get_data().await;
list_sitekeys_work(data).await;
}
async fn list_sitekeys_work(data: ArcData) {
const NAME: &str = "listsitekeyuser";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "listsitekeyuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -89,12 +89,22 @@ mod test {
use crate::*;
#[actix_rt::test]
async fn view_sitekey_work() {
async fn view_sitekey_work_pg_test() {
let data = pg::get_data().await;
view_sitekey_work(data).await;
}
#[actix_rt::test]
async fn view_sitekey_work_maria_test() {
let data = maria::get_data().await;
view_sitekey_work(data).await;
}
async fn view_sitekey_work(data: ArcData) {
const NAME: &str = "viewsitekeyuser";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "viewsitekeyuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;

View file

@ -37,11 +37,47 @@ use crate::ArcData;
pub fn get_settings() -> Settings {
Settings::new().unwrap()
}
pub async fn get_data() -> ArcData {
let settings = get_settings();
let data = Data::new(&settings).await;
data
pub mod pg {
use std::env;
use crate::data::Data;
use crate::settings::*;
use crate::ArcData;
use super::get_settings;
pub async fn get_data() -> ArcData {
let url = env::var("POSTGRES_DATABASE_URL").unwrap();
let mut settings = get_settings();
settings.database.url = url.clone();
settings.database.database_type = DBType::Postgres;
let data = Data::new(&settings).await;
data
}
}
pub mod maria {
use std::env;
use crate::data::Data;
use crate::settings::*;
use crate::ArcData;
use super::get_settings;
pub async fn get_data() -> ArcData {
let url = env::var("MARIA_DATABASE_URL").unwrap();
let mut settings = get_settings();
settings.database.url = url.clone();
settings.database.database_type = DBType::Maria;
let data = Data::new(&settings).await;
data
}
}
//pub async fn get_data() -> ArcData {
// let settings = get_settings();
// let data = Data::new(&settings).await;
// data
//}
#[macro_export]
macro_rules! get_cookie {