From 0369c9d316a909c825d6f6bb5358349b765f6a5b Mon Sep 17 00:00:00 2001 From: Marcin Nowak-Liebiediew Date: Thu, 18 Jan 2024 14:26:01 +0100 Subject: [PATCH] refactor: move command, and e2e test --- e2e/tests-dfx/cycles-ledger.bash | 42 ++++++++++++++++++- e2e/tests-dfx/wallet.bash | 40 ------------------ src/dfx/src/commands/cycles/mod.rs | 3 ++ .../redeem_faucet_coupon.rs | 0 src/dfx/src/commands/wallet/mod.rs | 5 +-- 5 files changed, 45 insertions(+), 45 deletions(-) rename src/dfx/src/commands/{wallet => cycles}/redeem_faucet_coupon.rs (100%) diff --git a/e2e/tests-dfx/cycles-ledger.bash b/e2e/tests-dfx/cycles-ledger.bash index 0d12dd70a0..06f4d8258f 100644 --- a/e2e/tests-dfx/cycles-ledger.bash +++ b/e2e/tests-dfx/cycles-ledger.bash @@ -544,4 +544,44 @@ current_time_nanoseconds() { assert_command dfx canister delete "${FRONTEND_ID}" assert_command dfx cycles balance assert_eq "22.379 TC (trillion cycles)." -} \ No newline at end of file +} + +@test "redeem-faucet-coupon can set a new wallet and top up an existing one" { + dfx_new hello + install_asset faucet + dfx deploy + dfx ledger fabricate-cycles --canister faucet --t 1000 + + dfx identity new --storage-mode plaintext faucet_testing + dfx identity use faucet_testing + + # prepare wallet to hand out + dfx wallet balance # this creates a new wallet with user faucet_testing as controller + dfx canister call faucet set_wallet_to_hand_out "(principal \"$(dfx identity get-wallet)\")" # register the wallet as the wallet that the faucet will return + rm "$E2E_SHARED_LOCAL_NETWORK_DATA_DIRECTORY/wallets.json" # forget about the currently configured wallet + + # assert: no wallet configured + export DFX_DISABLE_AUTO_WALLET=1 + assert_command_fail dfx wallet balance + assert_match "No wallet configured" + + assert_command dfx cycles redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'valid-coupon' + assert_match "Redeemed coupon valid-coupon for a new wallet" + assert_match "New wallet set." + + # only succeeds if wallet is correctly set + assert_command dfx wallet balance + # checking only balance before the dot, rest may fluctuate + # balance may be 99.??? TC if cycles accounting is done, or 100.000 TC if not + assert_match "99\.|100\." + + unset DFX_DISABLE_AUTO_WALLET + + assert_command dfx cycles redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'another-valid-coupon' + assert_match "Redeemed coupon code another-valid-coupon for 10.000 TC" + + assert_command dfx wallet balance + # checking only balance before the dot, rest may fluctuate + # balance may be 109.??? TC if cycles accounting is done, or 110.000 TC if not + assert_match "109\.|110\." +} diff --git a/e2e/tests-dfx/wallet.bash b/e2e/tests-dfx/wallet.bash index 66bd979f98..37656ab91e 100644 --- a/e2e/tests-dfx/wallet.bash +++ b/e2e/tests-dfx/wallet.bash @@ -184,43 +184,3 @@ teardown() { assert_match "There is no wallet defined for identity 'default' on network 'local'. Nothing to do." } -@test "redeem-faucet-coupon can set a new wallet and top up an existing one" { - dfx_new hello - dfx_start - install_asset faucet - dfx deploy - dfx ledger fabricate-cycles --canister faucet --t 1000 - - dfx identity new --storage-mode plaintext faucet_testing - dfx identity use faucet_testing - - # prepare wallet to hand out - dfx wallet balance # this creates a new wallet with user faucet_testing as controller - dfx canister call faucet set_wallet_to_hand_out "(principal \"$(dfx identity get-wallet)\")" # register the wallet as the wallet that the faucet will return - rm "$E2E_SHARED_LOCAL_NETWORK_DATA_DIRECTORY/wallets.json" # forget about the currently configured wallet - - # assert: no wallet configured - export DFX_DISABLE_AUTO_WALLET=1 - assert_command_fail dfx wallet balance - assert_match "No wallet configured" - - assert_command dfx wallet redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'valid-coupon' - assert_match "Redeemed coupon valid-coupon for a new wallet" - assert_match "New wallet set." - - # only succeeds if wallet is correctly set - assert_command dfx wallet balance - # checking only balance before the dot, rest may fluctuate - # balance may be 99.??? TC if cycles accounting is done, or 100.000 TC if not - assert_match "99\.|100\." - - unset DFX_DISABLE_AUTO_WALLET - - assert_command dfx wallet redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'another-valid-coupon' - assert_match "Redeemed coupon code another-valid-coupon for 10.000 TC" - - assert_command dfx wallet balance - # checking only balance before the dot, rest may fluctuate - # balance may be 109.??? TC if cycles accounting is done, or 110.000 TC if not - assert_match "109\.|110\." -} diff --git a/src/dfx/src/commands/cycles/mod.rs b/src/dfx/src/commands/cycles/mod.rs index 8e1b14228a..7b2c530fb3 100644 --- a/src/dfx/src/commands/cycles/mod.rs +++ b/src/dfx/src/commands/cycles/mod.rs @@ -6,6 +6,7 @@ use clap::Parser; use tokio::runtime::Runtime; mod balance; +mod redeem_faucet_coupon; pub mod top_up; mod transfer; @@ -25,6 +26,7 @@ enum SubCommand { Balance(balance::CyclesBalanceOpts), TopUp(top_up::TopUpOpts), Transfer(transfer::TransferOpts), + RedeemFaucetCoupon(redeem_faucet_coupon::RedeemFaucetCouponOpts), } pub fn exec(env: &dyn Environment, opts: CyclesOpts) -> DfxResult { @@ -35,6 +37,7 @@ pub fn exec(env: &dyn Environment, opts: CyclesOpts) -> DfxResult { SubCommand::Balance(v) => balance::exec(&agent_env, v).await, SubCommand::TopUp(v) => top_up::exec(&agent_env, v).await, SubCommand::Transfer(v) => transfer::exec(&agent_env, v).await, + SubCommand::RedeemFaucetCoupon(v) => redeem_faucet_coupon::exec(&agent_env, v).await, } }) } diff --git a/src/dfx/src/commands/wallet/redeem_faucet_coupon.rs b/src/dfx/src/commands/cycles/redeem_faucet_coupon.rs similarity index 100% rename from src/dfx/src/commands/wallet/redeem_faucet_coupon.rs rename to src/dfx/src/commands/cycles/redeem_faucet_coupon.rs diff --git a/src/dfx/src/commands/wallet/mod.rs b/src/dfx/src/commands/wallet/mod.rs index bbc2538f4f..0a5f96ff75 100644 --- a/src/dfx/src/commands/wallet/mod.rs +++ b/src/dfx/src/commands/wallet/mod.rs @@ -21,7 +21,6 @@ mod custodians; mod deauthorize; mod list_addresses; mod name; -mod redeem_faucet_coupon; mod remove_controller; mod send; mod set_name; @@ -48,7 +47,6 @@ enum SubCommand { Custodians(custodians::CustodiansOpts), Deauthorize(deauthorize::DeauthorizeOpts), Name(name::NameOpts), - RedeemFaucetCoupon(redeem_faucet_coupon::RedeemFaucetCouponOpts), RemoveController(remove_controller::RemoveControllerOpts), Send(send::SendOpts), SetName(set_name::SetNameOpts), @@ -68,7 +66,6 @@ pub fn exec(env: &dyn Environment, opts: WalletOpts) -> DfxResult { SubCommand::Custodians(v) => custodians::exec(&agent_env, v).await, SubCommand::Deauthorize(v) => deauthorize::exec(&agent_env, v).await, SubCommand::Name(v) => name::exec(&agent_env, v).await, - SubCommand::RedeemFaucetCoupon(v) => redeem_faucet_coupon::exec(&agent_env, v).await, SubCommand::RemoveController(v) => remove_controller::exec(&agent_env, v).await, SubCommand::Send(v) => send::exec(&agent_env, v).await, SubCommand::SetName(v) => set_name::exec(&agent_env, v).await, @@ -118,7 +115,7 @@ where } #[context("Failed to setup wallet caller.")] -async fn get_wallet(env: &dyn Environment) -> DfxResult> { +pub(crate) async fn get_wallet(env: &dyn Environment) -> DfxResult> { let identity_name = env .get_selected_identity() .expect("No selected identity.")