should next write some simple end2end test

This commit is contained in:
yenicelik 2023-01-31 15:08:46 +01:00
parent d055febc2e
commit 4582cf3e37
5 changed files with 26 additions and 17 deletions

@ -6,11 +6,11 @@ pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { 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 manager
.alter_table( .alter_table(
Table::alter() Table::alter()
.table(Alias::new("login")) .table(Login::Table)
.add_column( .add_column(
ColumnDef::new(Login::ReadOnly) ColumnDef::new(Login::ReadOnly)
.boolean() .boolean()
@ -20,13 +20,12 @@ impl MigrationTrait for Migration {
} }
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
// Drop the column from the table ... // Drop the column from the table ...
manager manager
.alter_table( .alter_table(
Table::alter() Table::alter()
.table(Alias::new("login")) .table(Login::Table)
.drop_column(Alias::new("read_only")) .drop_column(Login::ReadOnly)
.to_owned() .to_owned()
).await ).await
} }

@ -6,11 +6,10 @@ pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
manager manager
.alter_table( .alter_table(
Table::alter() Table::alter()
.table(Alias::new("pending_login")) .table(PendingLogin::Table)
.add_column( .add_column(
ColumnDef::new(PendingLogin::ImitatingUser) ColumnDef::new(PendingLogin::ImitatingUser)
.big_unsigned() .big_unsigned()
@ -27,13 +26,12 @@ impl MigrationTrait for Migration {
} }
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
manager manager
.alter_table( .alter_table(
Table::alter() Table::alter()
.table(Alias::new("pending_login")) .table(PendingLogin::Table)
.drop_foreign_key(Alias::new("fk-pending_login-imitating_user")) .drop_foreign_key(Alias::new("fk-pending_login-imitating_user"))
.drop_column(Alias::new("imitating_user")) .drop_column(PendingLogin::ImitatingUser)
.to_owned() .to_owned()
).await ).await
} }

@ -15,6 +15,7 @@ use http::StatusCode;
use migration::sea_orm::{self, ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter}; use migration::sea_orm::{self, ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter};
use log::info; use log::info;
use redis_rate_limiter::redis::AsyncCommands; 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 // TODO: Add some logic to check if the operating user is an admin
// If he is, return true // If he is, return true
@ -79,7 +80,7 @@ pub async fn query_admin_modify_usertier<'a>(
.filter(admin::Column::UserId.eq(caller_id)) .filter(admin::Column::UserId.eq(caller_id))
.one(&db_conn) .one(&db_conn)
.await? .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 // 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)) .filter(user::Column::Address.eq(user_address))
.one(&db_conn) .one(&db_conn)
.await? .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 // Return early if the target user_tier_id is the same as the original user_tier_id
response_body.insert( response_body.insert(
"user_tier_title", "user_tier_title",
@ -96,11 +101,15 @@ pub async fn query_admin_modify_usertier<'a>(
); );
// Now we can modify the user's tier // 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())) .filter(user_tier::Column::Title.eq(user_tier_title.clone()))
.one(&db_conn) .one(&db_conn)
.await? .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 { if user.user_tier_id == new_user_tier.id {
info!("user already has that tier"); info!("user already has that tier");

@ -70,8 +70,8 @@ pub struct Web3ProxyCli {
#[argh(subcommand)] #[argh(subcommand)]
enum SubCommand { enum SubCommand {
ChangeUserAddress(change_user_address::ChangeUserAddressSubCommand), ChangeUserAddress(change_user_address::ChangeUserAddressSubCommand),
ChangeUserTier(change_user_tier::ChangeUserTierSubCommand),
ChangeUserAdminStatus(change_user_admin_status::ChangeUserAdminStatusSubCommand), ChangeUserAdminStatus(change_user_admin_status::ChangeUserAdminStatusSubCommand),
ChangeUserTier(change_user_tier::ChangeUserTierSubCommand),
ChangeUserTierByAddress(change_user_tier_by_address::ChangeUserTierByAddressSubCommand), ChangeUserTierByAddress(change_user_tier_by_address::ChangeUserTierByAddressSubCommand),
ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeySubCommand), ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeySubCommand),
CheckConfig(check_config::CheckConfigSubCommand), CheckConfig(check_config::CheckConfigSubCommand),

@ -173,8 +173,11 @@ pub async fn admin_login_get(
.filter(user::Column::Address.eq(user_address)) .filter(user::Column::Address.eq(user_address))
.one(db_replica.conn()) .one(db_replica.conn())
.await? .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? // 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 // I supposed if the user logs in, the admin would be logged out and vice versa