From 4582cf3e37af5859fc66b80ab80c48e12acadbb9 Mon Sep 17 00:00:00 2001 From: yenicelik Date: Tue, 31 Jan 2023 15:08:46 +0100 Subject: [PATCH] should next write some simple end2end test --- .../m20230130_124740_read_only_login_logic.rs | 9 ++++----- ..._165144_prepare_admin_imitation_pre_login.rs | 8 +++----- web3_proxy/src/admin_queries.rs | 17 +++++++++++++---- web3_proxy/src/bin/web3_proxy_cli/main.rs | 2 +- web3_proxy/src/frontend/admin.rs | 7 +++++-- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/migration/src/m20230130_124740_read_only_login_logic.rs b/migration/src/m20230130_124740_read_only_login_logic.rs index 2e47dec4..064dc683 100644 --- a/migration/src/m20230130_124740_read_only_login_logic.rs +++ b/migration/src/m20230130_124740_read_only_login_logic.rs @@ -6,11 +6,11 @@ pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { - // Replace the sample below with your own migration scripts + // Add a read-only column to the table manager .alter_table( Table::alter() - .table(Alias::new("login")) + .table(Login::Table) .add_column( ColumnDef::new(Login::ReadOnly) .boolean() @@ -20,13 +20,12 @@ impl MigrationTrait for Migration { } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - // Replace the sample below with your own migration scripts // Drop the column from the table ... manager .alter_table( Table::alter() - .table(Alias::new("login")) - .drop_column(Alias::new("read_only")) + .table(Login::Table) + .drop_column(Login::ReadOnly) .to_owned() ).await } diff --git a/migration/src/m20230130_165144_prepare_admin_imitation_pre_login.rs b/migration/src/m20230130_165144_prepare_admin_imitation_pre_login.rs index 64a2a068..ff6ec868 100644 --- a/migration/src/m20230130_165144_prepare_admin_imitation_pre_login.rs +++ b/migration/src/m20230130_165144_prepare_admin_imitation_pre_login.rs @@ -6,11 +6,10 @@ pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { - // Replace the sample below with your own migration scripts manager .alter_table( Table::alter() - .table(Alias::new("pending_login")) + .table(PendingLogin::Table) .add_column( ColumnDef::new(PendingLogin::ImitatingUser) .big_unsigned() @@ -27,13 +26,12 @@ impl MigrationTrait for Migration { } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - // Replace the sample below with your own migration scripts manager .alter_table( Table::alter() - .table(Alias::new("pending_login")) + .table(PendingLogin::Table) .drop_foreign_key(Alias::new("fk-pending_login-imitating_user")) - .drop_column(Alias::new("imitating_user")) + .drop_column(PendingLogin::ImitatingUser) .to_owned() ).await } diff --git a/web3_proxy/src/admin_queries.rs b/web3_proxy/src/admin_queries.rs index 58bd32e1..83305808 100644 --- a/web3_proxy/src/admin_queries.rs +++ b/web3_proxy/src/admin_queries.rs @@ -15,6 +15,7 @@ use http::StatusCode; use migration::sea_orm::{self, ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter}; use log::info; use redis_rate_limiter::redis::AsyncCommands; +use crate::frontend::errors::FrontendErrorResponse::AccessDenied; // TODO: Add some logic to check if the operating user is an admin // If he is, return true @@ -79,7 +80,7 @@ pub async fn query_admin_modify_usertier<'a>( .filter(admin::Column::UserId.eq(caller_id)) .one(&db_conn) .await? - .context("This user is not registered as an admin")?; + .ok_or(AccessDenied.into())?; // If we are here, that means an admin was found, and we can safely proceed @@ -88,7 +89,11 @@ pub async fn query_admin_modify_usertier<'a>( .filter(user::Column::Address.eq(user_address)) .one(&db_conn) .await? - .context("No user with this id found as the change")?; + .ok_or(FrontendErrorResponse::StatusCode( + StatusCode::BAD_REQUEST, + "No user with this id found".to_string(), + None, + ))?; // Return early if the target user_tier_id is the same as the original user_tier_id response_body.insert( "user_tier_title", @@ -96,11 +101,15 @@ pub async fn query_admin_modify_usertier<'a>( ); // Now we can modify the user's tier - let new_user_tier: user_tier::Model = user_tier::Entity::find() + let new_user_tier: user_tier::Model = !user_tier::Entity::find() .filter(user_tier::Column::Title.eq(user_tier_title.clone())) .one(&db_conn) .await? - .context("No user tier found with that name")?; + .ok_or(|| FrontendErrorResponse::StatusCode( + StatusCode::BAD_REQUEST, + "User Tier name was not found".to_string(), + None, + ))?; if user.user_tier_id == new_user_tier.id { info!("user already has that tier"); diff --git a/web3_proxy/src/bin/web3_proxy_cli/main.rs b/web3_proxy/src/bin/web3_proxy_cli/main.rs index ff7dd61b..73710f7a 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/main.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/main.rs @@ -70,8 +70,8 @@ pub struct Web3ProxyCli { #[argh(subcommand)] enum SubCommand { ChangeUserAddress(change_user_address::ChangeUserAddressSubCommand), - ChangeUserTier(change_user_tier::ChangeUserTierSubCommand), ChangeUserAdminStatus(change_user_admin_status::ChangeUserAdminStatusSubCommand), + ChangeUserTier(change_user_tier::ChangeUserTierSubCommand), ChangeUserTierByAddress(change_user_tier_by_address::ChangeUserTierByAddressSubCommand), ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeySubCommand), CheckConfig(check_config::CheckConfigSubCommand), diff --git a/web3_proxy/src/frontend/admin.rs b/web3_proxy/src/frontend/admin.rs index c15a103f..6532d312 100644 --- a/web3_proxy/src/frontend/admin.rs +++ b/web3_proxy/src/frontend/admin.rs @@ -173,8 +173,11 @@ pub async fn admin_login_get( .filter(user::Column::Address.eq(user_address)) .one(db_replica.conn()) .await? - .context("fetching admin from db by user_id")?; - + .ok_or(FrontendErrorResponse::StatusCode( + StatusCode::BAD_REQUEST, + "Could not find user in db".to_string(), + None, + ))?; // Can there be two login-sessions at the same time? // I supposed if the user logs in, the admin would be logged out and vice versa