fix: use individual database for each test #21

Merged
realaravinth merged 1 commit from fix-flaky-tests into master 2023-11-02 04:29:40 +05:30
16 changed files with 114 additions and 39 deletions

View file

@ -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"
}

View file

@ -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"
}

10
Cargo.lock generated
View file

@ -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",
]

View file

@ -115,12 +115,12 @@ mod tests {
const PASSWORD: &str = "longpassword2";
const EMAIL: &str = "updatepassuser@a.com";
{
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,

View file

@ -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;
{
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;
{
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;
{
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,

View file

@ -574,13 +574,13 @@ mod tests {
const DEVICE_SOFTWARE_RECOGNISED: &str = "Foobar.v2";
const THREADS: i32 = 4;
{
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),

View file

@ -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,

View file

@ -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;

View file

@ -165,13 +165,13 @@ mod tests {
const DEVICE_SOFTWARE_RECOGNISED: &str = "Foobar.v2";
const THREADS: i32 = 4;
{
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;

View file

@ -338,8 +338,8 @@ mod tests {
const DEVICE_SOFTWARE_RECOGNISED: &str = "Foobar.v2";
const THREADS: i32 = 4;
{
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);

View file

@ -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 =

View file

@ -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 =

View file

@ -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);

View file

@ -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();

View file

@ -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 =

View file

@ -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<Data> {
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<Data>,
name: &str,
email: &str,
password: &str,
) -> (Arc<Data>, Login, ServiceResponse<EitherBody<BoxBody>>) {
register(name, email, password).await;
signin(name, password).await
) -> (Login, ServiceResponse<EitherBody<BoxBody>>) {
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<Data>, 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<Data>,
name: &str,
password: &str,
) -> (Arc<Data>, Login, ServiceResponse<EitherBody<BoxBody>>) {
let data = get_test_data().await;
) -> (Login, ServiceResponse<EitherBody<BoxBody>>) {
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<T: Serialize>(
data: &Arc<Data>,
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<T: Serialize>(
/// bad post req test without payload
pub async fn bad_post_req_test_witout_payload(
data: &Arc<Data>,
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;