2023-07-21 04:21:49 +03:00
|
|
|
use super::TestApp;
|
2023-07-12 10:35:07 +03:00
|
|
|
use entities::{user, user_tier};
|
2023-07-06 08:18:39 +03:00
|
|
|
use ethers::prelude::{LocalWallet, Signer};
|
|
|
|
use ethers::types::Signature;
|
2023-07-15 04:30:01 +03:00
|
|
|
use http::StatusCode;
|
2023-07-12 10:35:07 +03:00
|
|
|
use migration::sea_orm::{
|
2023-07-14 00:56:17 +03:00
|
|
|
self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel,
|
|
|
|
QueryFilter,
|
2023-07-12 10:35:07 +03:00
|
|
|
};
|
2023-07-15 04:30:01 +03:00
|
|
|
use tracing::{info, trace};
|
2023-07-12 10:35:07 +03:00
|
|
|
use web3_proxy::errors::Web3ProxyResult;
|
2023-07-06 08:18:39 +03:00
|
|
|
use web3_proxy::frontend::users::authentication::{LoginPostResponse, PostLogin};
|
|
|
|
|
|
|
|
/// Helper function to create an "ordinary" user
|
|
|
|
#[allow(unused)]
|
|
|
|
pub async fn create_user(
|
|
|
|
x: &TestApp,
|
|
|
|
r: &reqwest::Client,
|
|
|
|
user_wallet: &LocalWallet,
|
|
|
|
referral_code: Option<String>,
|
|
|
|
) -> (LoginPostResponse) {
|
|
|
|
let login_post_url = format!("{}user/login", x.proxy_provider.url());
|
|
|
|
let user_login_get_url = format!(
|
|
|
|
"{}user/login/{:?}",
|
|
|
|
x.proxy_provider.url(),
|
|
|
|
user_wallet.address()
|
|
|
|
);
|
|
|
|
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 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,
|
|
|
|
};
|
|
|
|
info!(?user_post_login_data);
|
|
|
|
|
2023-07-15 04:30:01 +03:00
|
|
|
let user_login_response = r
|
2023-07-06 08:18:39 +03:00
|
|
|
.post(&login_post_url)
|
|
|
|
.json(&user_post_login_data)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-07-15 04:30:01 +03:00
|
|
|
trace!(?user_login_response);
|
|
|
|
|
2023-07-15 04:31:00 +03:00
|
|
|
assert_eq!(user_login_response.status(), StatusCode::CREATED);
|
2023-07-15 04:30:01 +03:00
|
|
|
|
|
|
|
let user_login_text = user_login_response.text().await.unwrap();
|
|
|
|
trace!("user_login_text: {:#}", user_login_text);
|
|
|
|
|
|
|
|
let user_login_response: LoginPostResponse = serde_json::from_str(&user_login_text).unwrap();
|
2023-07-06 08:18:39 +03:00
|
|
|
info!(?user_login_response);
|
|
|
|
|
|
|
|
user_login_response
|
|
|
|
}
|
2023-07-12 10:35:07 +03:00
|
|
|
|
|
|
|
/// TODO: use an admin user to do this instead
|
|
|
|
#[allow(unused)]
|
|
|
|
pub async fn set_user_tier(
|
|
|
|
x: &TestApp,
|
2023-07-14 00:56:17 +03:00
|
|
|
db_conn: &DatabaseConnection,
|
2023-07-12 10:35:07 +03:00
|
|
|
user: user::Model,
|
|
|
|
tier_name: &str,
|
|
|
|
) -> Web3ProxyResult<user_tier::Model> {
|
|
|
|
let ut = user_tier::Entity::find()
|
|
|
|
.filter(user_tier::Column::Title.like(tier_name))
|
|
|
|
.one(db_conn)
|
|
|
|
.await?
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut user = user.into_active_model();
|
|
|
|
|
|
|
|
user.user_tier_id = sea_orm::Set(ut.id);
|
|
|
|
|
|
|
|
user.save(db_conn).await?;
|
|
|
|
|
|
|
|
Ok(ut)
|
|
|
|
}
|