From 17c42243d07f185a4f96844511039fc0b9d95c4b Mon Sep 17 00:00:00 2001 From: yenicelik Date: Fri, 30 Jun 2023 22:24:48 -0400 Subject: [PATCH] wont shutdown --- web3_proxy/src/frontend/admin.rs | 10 +- .../src/frontend/users/authentication.rs | 12 +- web3_proxy/tests/test_admins.rs | 111 +++++++++++++++++- web3_proxy/tests/test_users.rs | 12 +- 4 files changed, 124 insertions(+), 21 deletions(-) diff --git a/web3_proxy/src/frontend/admin.rs b/web3_proxy/src/frontend/admin.rs index 495205b4..92c8adad 100644 --- a/web3_proxy/src/frontend/admin.rs +++ b/web3_proxy/src/frontend/admin.rs @@ -28,7 +28,7 @@ use migration::sea_orm::{ self, ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, }; use migration::{Expr, OnConflict}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use serde_json::json; use siwe::{Message, VerificationOpts}; use std::ops::Add; @@ -38,11 +38,11 @@ use time_03::{Duration, OffsetDateTime}; use tracing::{info, trace, warn}; use ulid::Ulid; -#[derive(Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct AdminIncreaseBalancePost { - user_address: Address, - note: Option, - amount: Decimal, + pub user_address: Address, + pub note: Option, + pub amount: Decimal, } /// `POST /admin/increase_balance` -- As an admin, modify a user's user-tier diff --git a/web3_proxy/src/frontend/users/authentication.rs b/web3_proxy/src/frontend/users/authentication.rs index fd6c8ce0..27e00777 100644 --- a/web3_proxy/src/frontend/users/authentication.rs +++ b/web3_proxy/src/frontend/users/authentication.rs @@ -23,7 +23,7 @@ use migration::sea_orm::{ QueryFilter, TransactionTrait, }; use serde::{Deserialize, Serialize}; -use serde_json::json; +use serde_json::{json, Value}; use siwe::{Message, VerificationOpts}; use std::ops::Add; use std::str::FromStr; @@ -50,6 +50,16 @@ pub struct PostLogin { pub referral_code: Option, } +/// TODO: use this type in the frontend +#[derive(Debug, Deserialize)] +pub struct LoginPostResponse { + pub bearer_token: Ulid, + pub rpc_keys: Value, + /// unknown data gets put here + #[serde(flatten, default = "HashMap::default")] + pub extra: HashMap, +} + /// `GET /user/login/:user_address` or `GET /user/login/:user_address/:message_eip` -- Start the "Sign In with Ethereum" (siwe) login flow. /// /// `message_eip`s accepted: diff --git a/web3_proxy/tests/test_admins.rs b/web3_proxy/tests/test_admins.rs index c696609d..0a7e5455 100644 --- a/web3_proxy/tests/test_admins.rs +++ b/web3_proxy/tests/test_admins.rs @@ -1,6 +1,13 @@ mod common; use crate::common::TestApp; +use ethers::prelude::Signer; +use ethers::types::Signature; +use rust_decimal::Decimal; +use std::str::FromStr; +use tracing::{debug, info, trace, warn}; +use web3_proxy::frontend::admin::AdminIncreaseBalancePost; +use web3_proxy::frontend::users::authentication::{LoginPostResponse, PostLogin}; // #[cfg_attr(not(feature = "tests-needing-docker"), ignore)] #[ignore = "under construction"] @@ -11,13 +18,110 @@ async fn test_admin_imitate_user() { todo!(); } -// #[cfg_attr(not(feature = "tests-needing-docker"), ignore)] -#[ignore = "under construction"] +#[cfg_attr(not(feature = "tests-needing-docker"), ignore)] #[test_log::test(tokio::test)] async fn test_admin_grant_credits() { let x = TestApp::spawn(true).await; + let r = reqwest::Client::new(); - todo!(); + // 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 + let admin_login_get_url = format!( + "{}user/login/{:?}", + x.proxy_provider.url(), + admin_wallet.address() + ); + 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(); + debug!(?admin_signed); + let admin_post_login_data = PostLogin { + msg: admin_login_message, + sig: admin_signed.to_string(), + referral_code: None, + }; + debug!(?admin_post_login_data); + let admin_login_response = r + .post(&login_post_url) + .json(&admin_post_login_data) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + debug!(?admin_login_response); + + // Also login the user (to create the user) + 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 admin + let user_signed: Signature = user_wallet.sign_message(&user_login_message).await.unwrap(); + debug!(?user_signed); + let user_post_login_data = PostLogin { + msg: user_login_message, + sig: user_signed.to_string(), + referral_code: None, + }; + debug!(?user_post_login_data); + let user_login_response = r + .post(login_post_url) + .json(&user_post_login_data) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + debug!(?user_login_response); + + // Now I need to make the user an admin ... + + // Login the user + // Use the bearer token of admin to increase user balance + let increase_balance_data = AdminIncreaseBalancePost { + user_address: user_wallet.address(), // set user address to increase balance + amount: Decimal::from(100), // set amount to increase + note: Some("Test increasing balance".to_string()), + }; + let increase_balance_response = r + .post(increase_balance_post_url) + .json(&increase_balance_data) + .bearer_auth(admin_login_response.bearer_token) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + + debug!(?increase_balance_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; } // #[cfg_attr(not(feature = "tests-needing-docker"), ignore)] @@ -25,6 +129,5 @@ async fn test_admin_grant_credits() { #[test_log::test(tokio::test)] async fn test_admin_change_user_tier() { let x = TestApp::spawn(true).await; - todo!(); } diff --git a/web3_proxy/tests/test_users.rs b/web3_proxy/tests/test_users.rs index edae850d..7b42af94 100644 --- a/web3_proxy/tests/test_users.rs +++ b/web3_proxy/tests/test_users.rs @@ -8,17 +8,7 @@ use serde::Deserialize; use serde_json::Value; use tracing::{debug, info, trace}; use ulid::Ulid; -use web3_proxy::frontend::users::authentication::PostLogin; - -/// TODO: use this type in the frontend -#[derive(Debug, Deserialize)] -struct LoginPostResponse { - pub bearer_token: Ulid, - pub rpc_keys: Value, - /// unknown data gets put here - #[serde(flatten, default = "HashMap::default")] - pub extra: HashMap, -} +use web3_proxy::frontend::users::authentication::{LoginPostResponse, PostLogin}; /// TODO: 191 and the other message formats in another test #[cfg_attr(not(feature = "tests-needing-docker"), ignore)]