diff --git a/web3_proxy/tests/common/app.rs b/web3_proxy/tests/common/app.rs index ed60ab07..9a77b2e1 100644 --- a/web3_proxy/tests/common/app.rs +++ b/web3_proxy/tests/common/app.rs @@ -8,6 +8,7 @@ use ethers::{ utils::{Anvil, AnvilInstance}, }; use hashbrown::HashMap; +use migration::sea_orm::DatabaseConnection; use parking_lot::Mutex; use std::{ env, @@ -32,6 +33,7 @@ use web3_proxy::{ #[derive(Clone)] pub struct DbData { + pub conn: Option, pub container_name: String, pub url: Option, } @@ -97,6 +99,7 @@ impl TestApp { // create the db_data as soon as the url is known // when this is dropped, the db will be stopped let mut db_data = DbData { + conn: None, container_name: db_container_name.clone(), url: None, }; @@ -195,7 +198,7 @@ impl TestApp { } // TODO: make sure mysql is actually ready for connections - sleep(Duration::from_secs(7)).await; + sleep(Duration::from_secs(1)).await; info!(%db_url, elapsed=%start.elapsed().as_secs_f32(), "db is ready for connections. Migrating now..."); @@ -207,15 +210,18 @@ impl TestApp { panic!("db took too long to start"); } - if let Err(err) = get_migrated_db(db_url.clone(), 1, 1).await { - // not connected. sleep and then try again - warn!(?err, "unable to migrate db"); - sleep(Duration::from_secs(1)).await; - continue; + match get_migrated_db(db_url.clone(), 1, 1).await { + Ok(x) => { + // it worked! yey! + db_data.conn = Some(x); + break; + } + Err(err) => { + // not connected. sleep and then try again + warn!(?err, "unable to migrate db"); + sleep(Duration::from_secs(1)).await; + } } - - // it worked! yey! - break; } info!(%db_url, elapsed=%start.elapsed().as_secs_f32(), "db is migrated"); @@ -302,6 +308,11 @@ impl TestApp { } } + #[allow(unused)] + pub fn db_conn(&self) -> &DatabaseConnection { + self.db.as_ref().unwrap().conn.as_ref().unwrap() + } + pub fn stop(&self) -> Result> { self.shutdown_sender.send(()) } diff --git a/web3_proxy/tests/test_admins.rs b/web3_proxy/tests/test_admins.rs index da65de6b..977c2b31 100644 --- a/web3_proxy/tests/test_admins.rs +++ b/web3_proxy/tests/test_admins.rs @@ -1,15 +1,15 @@ mod common; +use std::str::FromStr; +use std::time::Duration; + use crate::common::TestApp; -use ethers::abi::AbiEncode; use ethers::prelude::Signer; use ethers::types::Signature; use rust_decimal::Decimal; -use std::str::FromStr; -use tracing::{debug, info, trace, warn}; +use tracing::info; use web3_proxy::frontend::admin::AdminIncreaseBalancePost; use web3_proxy::frontend::users::authentication::{LoginPostResponse, PostLogin}; -use web3_proxy::relational_db::get_db; use web3_proxy::sub_commands::ChangeAdminStatusSubCommand; // #[cfg_attr(not(feature = "tests-needing-docker"), ignore)] @@ -26,17 +26,19 @@ async fn test_admin_imitate_user() { async fn test_admin_grant_credits() { info!("Starting admin grant credits test"); let x = TestApp::spawn(true).await; - let r = reqwest::Client::new(); + let r = reqwest::Client::builder() + .timeout(Duration::from_secs(3)) + .build() + .unwrap(); // Setup variables that will be used let login_post_url = format!("{}user/login", x.proxy_provider.url()); let increase_balance_post_url = format!("{}admin/increase_balance", x.proxy_provider.url()); - // TODO: I should make a wallet an admin wallet first ... let admin_wallet = x.wallet(1); let user_wallet = x.wallet(2); - // Login the admin + // Login the admin to create their account. they aren't an admin yet let admin_login_get_url = format!( "{}user/login/{:?}", x.proxy_provider.url(), @@ -44,18 +46,21 @@ async fn test_admin_grant_credits() { ); let admin_login_message = r.get(admin_login_get_url).send().await.unwrap(); let admin_login_message = admin_login_message.text().await.unwrap(); + // Sign the message and POST it to login as admin let admin_signed: Signature = admin_wallet .sign_message(&admin_login_message) .await .unwrap(); info!(?admin_signed); + let admin_post_login_data = PostLogin { msg: admin_login_message, sig: admin_signed.to_string(), referral_code: None, }; info!(?admin_post_login_data); + let admin_login_response = r .post(&login_post_url) .json(&admin_post_login_data) @@ -76,15 +81,17 @@ async fn test_admin_grant_credits() { let user_login_message = r.get(user_login_get_url).send().await.unwrap(); let user_login_message = user_login_message.text().await.unwrap(); - // Sign the message and POST it to login as admin + // Sign the message and POST it to login as the user let user_signed: Signature = user_wallet.sign_message(&user_login_message).await.unwrap(); info!(?user_signed); + let user_post_login_data = PostLogin { msg: user_login_message, sig: user_signed.to_string(), referral_code: None, }; info!(?user_post_login_data); + let user_login_response = r .post(&login_post_url) .json(&user_post_login_data) @@ -102,23 +109,11 @@ async fn test_admin_grant_credits() { address: format!("{:?}", admin_wallet.address()), should_be_admin: true, }; - info!("Admin status changer object is: "); info!(?admin_status_changer); - // I suppose I gotta create a new database connection - // Connect to the database using the connection getter - info!("Establishing the database connection"); - let db_conn = get_db( - x.db.as_ref().unwrap().url.as_ref().unwrap().to_string(), - 1, - 1, - ) - .await - .unwrap(); - info!("Changing the status of the admin_wallet to be an admin"); // Pass on the database into it ... - let _ = admin_status_changer.main(&db_conn).await.unwrap(); + admin_status_changer.main(x.db_conn()).await.unwrap(); // Login the admin again, because he was just signed out let admin_login_get_url = format!( @@ -128,18 +123,21 @@ async fn test_admin_grant_credits() { ); let admin_login_message = r.get(admin_login_get_url).send().await.unwrap(); let admin_login_message = admin_login_message.text().await.unwrap(); + // Sign the message and POST it to login as admin let admin_signed: Signature = admin_wallet .sign_message(&admin_login_message) .await .unwrap(); info!(?admin_signed); + let admin_post_login_data = PostLogin { msg: admin_login_message, sig: admin_signed.to_string(), referral_code: None, }; info!(?admin_post_login_data); + let admin_login_response = r .post(&login_post_url) .json(&admin_post_login_data) @@ -151,8 +149,6 @@ async fn test_admin_grant_credits() { .unwrap(); info!(?admin_login_response); - // Make the admin user an admin - info!("Increasing balance"); // Login the user // Use the bearer token of admin to increase user balance @@ -164,6 +160,7 @@ async fn test_admin_grant_credits() { info!(?increase_balance_post_url); info!(?increase_balance_data); info!(?admin_login_response.bearer_token); + let increase_balance_response = r .post(increase_balance_post_url) .json(&increase_balance_data) @@ -171,22 +168,23 @@ async fn test_admin_grant_credits() { .send() .await .unwrap(); - info!("Passed second checkpoint"); - info!(?increase_balance_response); - // let increase_balance_response = increase_balance_response - // .json::() - // .await - // .unwrap(); - // info!(?increase_balance_response); + info!("bug is on the line above. it never returns"); + info!(?increase_balance_response, "http response"); - // // Check if the response is as expected - // // assert_eq!(increase_balance_response["user"], user_wallet.address()); - // assert_eq!( - // Decimal::from_str(increase_balance_response["amount"].as_str().unwrap()).unwrap(), - // Decimal::from(100) - // ); - // - // x.wait().await; + let increase_balance_response = increase_balance_response + .json::() + .await + .unwrap(); + info!(?increase_balance_response, "json response"); + + // Check if the response is as expected + // TODO: assert_eq!(increase_balance_response["user"], user_wallet.address()); + assert_eq!( + Decimal::from_str(increase_balance_response["amount"].as_str().unwrap()).unwrap(), + Decimal::from(100) + ); + + x.wait().await; } // #[cfg_attr(not(feature = "tests-needing-docker"), ignore)]