From cfc459dde11a3dba20c85b038b8a945735c54d75 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Thu, 2 Nov 2023 04:00:56 +0530 Subject: [PATCH] fix: use individual database for each test --- ...6b74151a20532ceebc8ee41d079decee3acb5.json | 23 ++++++++++++++ ...66184ccff75583e21df5ddf5e8872f710d3d2.json | 22 ++++++++++++++ Cargo.lock | 10 +++++++ src/api/v1/admin/account/password.rs | 6 ++-- src/api/v1/admin/account/test.rs | 20 +++++++------ src/api/v1/admin/campaigns.rs | 7 +++-- src/api/v1/admin/tests/auth.rs | 9 ++++-- src/api/v1/admin/tests/protected.rs | 4 +-- src/api/v1/stats.rs | 6 ++-- src/archive.rs | 6 ++-- src/pages/mod.rs | 2 +- src/pages/panel/campaigns/delete.rs | 2 +- src/pages/panel/campaigns/mod.rs | 2 +- src/pages/panel/campaigns/new.rs | 2 +- src/pages/panel/export.rs | 2 +- src/tests.rs | 30 ++++++++++++------- 16 files changed, 114 insertions(+), 39 deletions(-) create mode 100644 .sqlx/query-52c16c2c0759140af6348ef7de56b74151a20532ceebc8ee41d079decee3acb5.json create mode 100644 .sqlx/query-63370a30a4ff6d31292a3cb632c66184ccff75583e21df5ddf5e8872f710d3d2.json diff --git a/.sqlx/query-52c16c2c0759140af6348ef7de56b74151a20532ceebc8ee41d079decee3acb5.json b/.sqlx/query-52c16c2c0759140af6348ef7de56b74151a20532ceebc8ee41d079decee3acb5.json new file mode 100644 index 0000000..a028bd6 --- /dev/null +++ b/.sqlx/query-52c16c2c0759140af6348ef7de56b74151a20532ceebc8ee41d079decee3acb5.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT\n difficulty\n FROM\n survey_benches\n WHERE\n duration <= $1\n ORDER BY difficulty ASC LIMIT 1 OFFSET $2;", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "difficulty", + "type_info": "Int4" + } + ], + "parameters": { + "Left": [ + "Float4", + "Int8" + ] + }, + "nullable": [ + false + ] + }, + "hash": "52c16c2c0759140af6348ef7de56b74151a20532ceebc8ee41d079decee3acb5" +} diff --git a/.sqlx/query-63370a30a4ff6d31292a3cb632c66184ccff75583e21df5ddf5e8872f710d3d2.json b/.sqlx/query-63370a30a4ff6d31292a3cb632c66184ccff75583e21df5ddf5e8872f710d3d2.json new file mode 100644 index 0000000..47be8d7 --- /dev/null +++ b/.sqlx/query-63370a30a4ff6d31292a3cb632c66184ccff75583e21df5ddf5e8872f710d3d2.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT COUNT(difficulty) FROM survey_benches WHERE duration <= $1;", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "count", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Float4" + ] + }, + "nullable": [ + null + ] + }, + "hash": "63370a30a4ff6d31292a3cb632c66184ccff75583e21df5ddf5e8872f710d3d2" +} diff --git a/Cargo.lock b/Cargo.lock index 8ed4986..1abca35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2000,6 +2000,15 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-src" +version = "300.1.6+3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439fac53e092cd7442a3660c85dde4643ab3b5bd39040912388dcdabf6b88085" +dependencies = [ + "cc", +] + [[package]] name = "openssl-sys" version = "0.9.93" @@ -2008,6 +2017,7 @@ checksum = "db4d56a4c0478783083cfafcc42493dd4a981d41669da64b4572a2a089b51b1d" dependencies = [ "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] diff --git a/src/api/v1/admin/account/password.rs b/src/api/v1/admin/account/password.rs index dc8f062..2e8e197 100644 --- a/src/api/v1/admin/account/password.rs +++ b/src/api/v1/admin/account/password.rs @@ -115,12 +115,12 @@ mod tests { const PASSWORD: &str = "longpassword2"; const EMAIL: &str = "updatepassuser@a.com"; + let data = get_test_data().await; { - let data = get_test_data().await; delete_user(NAME, &data).await; } - let (data, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; @@ -153,6 +153,7 @@ mod tests { }; bad_post_req_test( + &data, NAME, new_password, ROUTES.admin.account.update_password, @@ -168,6 +169,7 @@ mod tests { }; bad_post_req_test( + &data, NAME, new_password, ROUTES.admin.account.update_password, diff --git a/src/api/v1/admin/account/test.rs b/src/api/v1/admin/account/test.rs index 22e8c36..3b0dd05 100644 --- a/src/api/v1/admin/account/test.rs +++ b/src/api/v1/admin/account/test.rs @@ -22,12 +22,12 @@ async fn uname_email_exists_works() { const PASSWORD: &str = "longpassword2"; const EMAIL: &str = "testuserexists@a.com2"; + let data = get_test_data().await; { - let data = get_test_data().await; delete_user(NAME, &data).await; } - let (data, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; @@ -112,14 +112,14 @@ async fn email_udpate_password_validation_del_userworks() { const NAME2: &str = "eupdauser"; const EMAIL2: &str = "eupdauser@a.com"; + let data = get_test_data().await; { - let data = get_test_data().await; delete_user(NAME, &data).await; delete_user(NAME2, &data).await; } - let _ = register_and_signin(NAME2, EMAIL2, PASSWORD).await; - let (data, _creds, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let _ = register_and_signin(&data, NAME2, EMAIL2, PASSWORD).await; + let (_creds, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; @@ -140,6 +140,7 @@ async fn email_udpate_password_validation_del_userworks() { // check duplicate email while duplicate email email_payload.email = EMAIL2.into(); bad_post_req_test( + &data, NAME, PASSWORD, ROUTES.admin.account.update_email, @@ -153,6 +154,7 @@ async fn email_udpate_password_validation_del_userworks() { password: NAME.into(), }; bad_post_req_test( + &data, NAME, PASSWORD, ROUTES.admin.account.delete, @@ -195,9 +197,8 @@ async fn username_update_works() { const NAME2: &str = "terstusrtds"; const NAME_CHANGE: &str = "terstusrtdsxx"; + let data = get_test_data().await; { - let data = get_test_data().await; - futures::join!( delete_user(NAME, &data), delete_user(NAME2, &data), @@ -205,8 +206,8 @@ async fn username_update_works() { ); } - let _ = register_and_signin(NAME2, EMAIL2, PASSWORD).await; - let (data, _creds, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let _ = register_and_signin(&data, NAME2, EMAIL2, PASSWORD).await; + let (_creds, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; @@ -226,6 +227,7 @@ async fn username_update_works() { // check duplicate username with duplicate username username_udpate.username = NAME2.into(); bad_post_req_test( + &data, NAME_CHANGE, PASSWORD, ROUTES.admin.account.update_username, diff --git a/src/api/v1/admin/campaigns.rs b/src/api/v1/admin/campaigns.rs index a2448f2..aa7e68d 100644 --- a/src/api/v1/admin/campaigns.rs +++ b/src/api/v1/admin/campaigns.rs @@ -574,13 +574,13 @@ mod tests { const DEVICE_SOFTWARE_RECOGNISED: &str = "Foobar.v2"; const THREADS: i32 = 4; + let data = get_test_data().await; { - let data = get_test_data().await; delete_user(NAME, &data).await; } - let (data, _creds, signin_resp) = - register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_creds, signin_resp) = + register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let survey = get_survey_user(data.clone()).await; let survey_cookie = get_cookie!(survey); @@ -671,6 +671,7 @@ mod tests { assert_eq!(responses, res); bad_post_req_test_witout_payload( + &data, NAME, PASSWORD, &V1_API_ROUTES.admin.campaign.delete.replace("{uuid}", NAME), diff --git a/src/api/v1/admin/tests/auth.rs b/src/api/v1/admin/tests/auth.rs index dc46ff0..934d32a 100644 --- a/src/api/v1/admin/tests/auth.rs +++ b/src/api/v1/admin/tests/auth.rs @@ -41,11 +41,11 @@ async fn auth_works() { delete_user(NAME, &data).await; // 1. Register and signin - let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); // Sign in with email - signin(EMAIL, PASSWORD).await; + signin(&data, EMAIL, PASSWORD).await; // 2. check if duplicate username is allowed let mut msg = Register { @@ -55,6 +55,7 @@ async fn auth_works() { email: Some(EMAIL.into()), }; bad_post_req_test( + &data, NAME, PASSWORD, ROUTES.admin.auth.register, @@ -66,6 +67,7 @@ async fn auth_works() { let name = format!("{}dupemail", NAME); msg.username = name; bad_post_req_test( + &data, NAME, PASSWORD, ROUTES.admin.auth.register, @@ -80,6 +82,7 @@ async fn auth_works() { password: msg.password.clone(), }; bad_post_req_test( + &data, NAME, PASSWORD, ROUTES.admin.auth.login, @@ -90,6 +93,7 @@ async fn auth_works() { creds.login = "nonexistantuser@example.com".into(); bad_post_req_test( + &data, NAME, PASSWORD, ROUTES.admin.auth.login, @@ -103,6 +107,7 @@ async fn auth_works() { creds.password = NAME.into(); bad_post_req_test( + &data, NAME, PASSWORD, ROUTES.admin.auth.login, diff --git a/src/api/v1/admin/tests/protected.rs b/src/api/v1/admin/tests/protected.rs index 3998fa3..7fc99f9 100644 --- a/src/api/v1/admin/tests/protected.rs +++ b/src/api/v1/admin/tests/protected.rs @@ -17,13 +17,13 @@ async fn protected_routes_work() { const EMAIL: &str = "testuser119@a.com2"; let get_protected_urls = [V1_API_ROUTES.admin.auth.logout]; + let data = get_test_data().await; { - let data = get_test_data().await; delete_user(NAME, &data).await; } - let (data, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; diff --git a/src/api/v1/stats.rs b/src/api/v1/stats.rs index 50600eb..125cfcf 100644 --- a/src/api/v1/stats.rs +++ b/src/api/v1/stats.rs @@ -165,13 +165,13 @@ mod tests { const DEVICE_SOFTWARE_RECOGNISED: &str = "Foobar.v2"; const THREADS: i32 = 4; + let data = get_test_data().await; { - let data = get_test_data().await; delete_user(NAME, &data).await; } - let (data, _creds, signin_resp) = - register_and_signin(NAME, EMAIL, PASSWORD).await; + let (creds, signin_resp) = + register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; diff --git a/src/archive.rs b/src/archive.rs index 17c0fb7..9c21762 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -338,8 +338,8 @@ mod tests { const DEVICE_SOFTWARE_RECOGNISED: &str = "Foobar.v2"; const THREADS: i32 = 4; + let data = get_test_data().await; { - let data = get_test_data().await; delete_user(NAME, &data).await; } @@ -348,8 +348,8 @@ mod tests { //self.write_campaign_file(&campaign, &archive).await?; //self.write_benchmark_file(&campaign, &archive, data).await?; - let (data, _creds, signin_resp) = - register_and_signin(NAME, EMAIL, PASSWORD).await; + let (creds, signin_resp) = + register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let survey = get_survey_user(data.clone()).await; let survey_cookie = get_cookie!(survey); diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 222d236..9985741 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -226,7 +226,7 @@ mod tests { delete_user(NAME, &data).await; } - let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let campaign = diff --git a/src/pages/panel/campaigns/delete.rs b/src/pages/panel/campaigns/delete.rs index 7593e85..940a6d4 100644 --- a/src/pages/panel/campaigns/delete.rs +++ b/src/pages/panel/campaigns/delete.rs @@ -170,7 +170,7 @@ mod tests { let data = get_test_data().await; let app = get_app!(data).await; delete_user(NAME, &data).await; - let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let uuid = diff --git a/src/pages/panel/campaigns/mod.rs b/src/pages/panel/campaigns/mod.rs index 26dd25c..93896bd 100644 --- a/src/pages/panel/campaigns/mod.rs +++ b/src/pages/panel/campaigns/mod.rs @@ -252,7 +252,7 @@ mod tests { delete_user(NAME, &data).await; } - let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let survey = get_survey_user(data.clone()).await; let survey_cookie = get_cookie!(survey); diff --git a/src/pages/panel/campaigns/new.rs b/src/pages/panel/campaigns/new.rs index 76698f6..03228c9 100644 --- a/src/pages/panel/campaigns/new.rs +++ b/src/pages/panel/campaigns/new.rs @@ -127,7 +127,7 @@ mod tests { let data = get_test_data().await; let app = get_app!(data).await; delete_user(NAME, &data).await; - let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let mut difficulties = String::new(); diff --git a/src/pages/panel/export.rs b/src/pages/panel/export.rs index 530afc2..9d31314 100644 --- a/src/pages/panel/export.rs +++ b/src/pages/panel/export.rs @@ -112,7 +112,7 @@ mod tests { let data = get_test_data().await; let app = get_app!(data).await; delete_user(NAME, &data).await; - let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; + let (_, signin_resp) = register_and_signin(&data, NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); let uuid = diff --git a/src/tests.rs b/src/tests.rs index 1248787..fba03a8 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -18,6 +18,7 @@ use mktemp::Temp; use lazy_static::lazy_static; use serde::Serialize; +use sqlx::migrate::MigrateDatabase; use sqlx::types::Uuid; use super::*; @@ -47,8 +48,15 @@ pub async fn get_test_data() -> Arc { let tmp_dir = Temp::new_dir().unwrap(); settings.publish.dir = tmp_dir.join("base_path").to_str().unwrap().into(); settings.allow_registration = true; + let mut url = url::Url::parse(&settings.database.url).unwrap(); + url.set_path(&crate::api::v1::get_random(16)); + settings.database.url = url.to_string(); + sqlx::Postgres::create_database(&settings.database.url) + .await + .unwrap(); let test_mcaptcha = Box::new(crate::mcaptcha::tests::TestClient::default()); let data = Data::new(settings, test_mcaptcha).await; + db::migrate_db(&data.db).await.unwrap(); data } @@ -149,17 +157,17 @@ macro_rules! get_app { /// register and signin utility pub async fn register_and_signin( + data: &Arc, name: &str, email: &str, password: &str, -) -> (Arc, Login, ServiceResponse>) { - register(name, email, password).await; - signin(name, password).await +) -> (Login, ServiceResponse>) { + register(data, name, email, password).await; + signin(data, name, password).await } /// register utility -pub async fn register(name: &str, email: &str, password: &str) { - let data = get_test_data().await; +pub async fn register(data: &Arc, name: &str, email: &str, password: &str) { let app = get_app!(data).await; // 1. Register @@ -184,10 +192,10 @@ pub async fn register(name: &str, email: &str, password: &str) { /// signin util pub async fn signin( + data: &Arc, name: &str, password: &str, -) -> (Arc, Login, ServiceResponse>) { - let data = get_test_data().await; +) -> (Login, ServiceResponse>) { let app = get_app!(data.clone()).await; // 2. signin @@ -201,18 +209,19 @@ pub async fn signin( ) .await; assert_eq!(signin_resp.status(), StatusCode::OK); - (data, creds, signin_resp) + (creds, signin_resp) } /// pub duplicate test pub async fn bad_post_req_test( + data: &Arc, name: &str, password: &str, url: &str, payload: &T, err: ServiceError, ) { - let (data, _, signin_resp) = signin(name, password).await; + let (_, signin_resp) = signin(data, name, password).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; @@ -231,12 +240,13 @@ pub async fn bad_post_req_test( /// bad post req test without payload pub async fn bad_post_req_test_witout_payload( + data: &Arc, name: &str, password: &str, url: &str, err: ServiceError, ) { - let (data, _, signin_resp) = signin(name, password).await; + let (_, signin_resp) = signin(data, name, password).await; let cookies = get_cookie!(signin_resp); let app = get_app!(data).await; -- 2.39.5