-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1204 from multiversx/forwarder-tests-migration
Forwarder tests migration
- Loading branch information
Showing
6 changed files
with
352 additions
and
86 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
contracts/feature-tests/composability/forwarder/tests/forwarder_blackbox_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use forwarder::nft::{Color, ProxyTrait as _}; | ||
|
||
use multiversx_sc_scenario::{ | ||
api::StaticApi, | ||
scenario_model::{ | ||
Account, CheckAccount, CheckStateStep, ScCallStep, SetStateStep, TransferStep, | ||
}, | ||
ContractInfo, ScenarioWorld, | ||
}; | ||
|
||
const USER_ADDRESS_EXPR: &str = "address:user"; | ||
const FORWARDER_ADDRESS_EXPR: &str = "sc:forwarder"; | ||
const FORWARDER_PATH_EXPR: &str = "file:output/forwarder.wasm"; | ||
|
||
const NFT_TOKEN_ID_EXPR: &str = "str:COOL-123456"; | ||
const NFT_TOKEN_ID: &[u8] = b"COOL-123456"; | ||
|
||
type ForwarderContract = ContractInfo<forwarder::Proxy<StaticApi>>; | ||
|
||
fn world() -> ScenarioWorld { | ||
let mut blockchain = ScenarioWorld::new(); | ||
blockchain.set_current_dir_from_workspace("contracts/composability/forwarder"); | ||
|
||
blockchain.register_contract(FORWARDER_PATH_EXPR, forwarder::ContractBuilder); | ||
blockchain | ||
} | ||
|
||
struct ForwarderTestState { | ||
world: ScenarioWorld, | ||
forwarder_contract: ForwarderContract, | ||
} | ||
|
||
impl ForwarderTestState { | ||
fn new() -> Self { | ||
let mut world = world(); | ||
|
||
let forwarder_code = world.code_expression(FORWARDER_PATH_EXPR); | ||
let roles = vec![ | ||
"ESDTRoleNFTCreate".to_string(), | ||
"ESDTRoleNFTUpdateAttributes".to_string(), | ||
]; | ||
|
||
world.set_state_step( | ||
SetStateStep::new() | ||
.put_account(USER_ADDRESS_EXPR, Account::new().nonce(1)) | ||
.put_account( | ||
FORWARDER_ADDRESS_EXPR, | ||
Account::new() | ||
.nonce(1) | ||
.code(forwarder_code.clone()) | ||
.esdt_roles(NFT_TOKEN_ID_EXPR, roles), | ||
), | ||
); | ||
|
||
let forwarder_contract = ForwarderContract::new(FORWARDER_ADDRESS_EXPR); | ||
|
||
Self { | ||
world, | ||
forwarder_contract, | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_nft_update_attributes_and_send() { | ||
let mut state = ForwarderTestState::new(); | ||
|
||
let original_attributes = Color { r: 0, g: 0, b: 0 }; | ||
|
||
state.world.sc_call( | ||
ScCallStep::new().from(USER_ADDRESS_EXPR).call( | ||
state | ||
.forwarder_contract | ||
.nft_create_compact(NFT_TOKEN_ID, 1u64, original_attributes), | ||
), | ||
); | ||
|
||
state.world.transfer_step( | ||
TransferStep::new() | ||
.from(FORWARDER_ADDRESS_EXPR) | ||
.to(USER_ADDRESS_EXPR) | ||
.esdt_transfer(NFT_TOKEN_ID, 1, "1"), | ||
); | ||
|
||
state | ||
.world | ||
.check_state_step(CheckStateStep::new().put_account( | ||
USER_ADDRESS_EXPR, | ||
CheckAccount::new().esdt_nft_balance_and_attributes( | ||
NFT_TOKEN_ID_EXPR, | ||
1, | ||
"1", | ||
Some(original_attributes), | ||
), | ||
)); | ||
|
||
let new_attributes = Color { | ||
r: 255, | ||
g: 255, | ||
b: 255, | ||
}; | ||
|
||
state.world.transfer_step( | ||
TransferStep::new() | ||
.from(USER_ADDRESS_EXPR) | ||
.to(FORWARDER_ADDRESS_EXPR) | ||
.esdt_transfer(NFT_TOKEN_ID, 1, "1"), | ||
); | ||
|
||
state.world.sc_call( | ||
ScCallStep::new().from(USER_ADDRESS_EXPR).call( | ||
state | ||
.forwarder_contract | ||
.nft_update_attributes(NFT_TOKEN_ID, 1u64, new_attributes), | ||
), | ||
); | ||
|
||
state.world.transfer_step( | ||
TransferStep::new() | ||
.from(FORWARDER_ADDRESS_EXPR) | ||
.to(USER_ADDRESS_EXPR) | ||
.esdt_transfer(NFT_TOKEN_ID, 1, "1"), | ||
); | ||
|
||
state | ||
.world | ||
.check_state_step(CheckStateStep::new().put_account( | ||
USER_ADDRESS_EXPR, | ||
CheckAccount::new().esdt_nft_balance_and_attributes( | ||
NFT_TOKEN_ID_EXPR, | ||
1, | ||
"1", | ||
Some(new_attributes), | ||
), | ||
)); | ||
} |
85 changes: 0 additions & 85 deletions
85
contracts/feature-tests/composability/forwarder/tests/forwarder_legacy_whitebox_test.rs
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
contracts/feature-tests/composability/forwarder/tests/forwarder_whitebox_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use forwarder::nft::{Color, ForwarderNftModule}; | ||
use multiversx_sc::{contract_base::ContractBase, types::Address}; | ||
use multiversx_sc_scenario::{ | ||
managed_address, managed_biguint, managed_token_id, rust_biguint, | ||
scenario_model::{ | ||
Account, AddressValue, CheckAccount, CheckStateStep, ScCallStep, SetStateStep, | ||
}, | ||
ScenarioWorld, WhiteboxContract, | ||
}; | ||
|
||
const USER_ADDRESS_EXPR: &str = "address:user"; | ||
const FORWARDER_ADDRESS_EXPR: &str = "sc:forwarder"; | ||
const FORWARDER_PATH_EXPR: &str = "file:output/forwarder.wasm"; | ||
|
||
const NFT_TOKEN_ID_EXPR: &str = "str:COOL-123456"; | ||
const NFT_TOKEN_ID: &[u8] = b"COOL-123456"; | ||
|
||
fn world() -> ScenarioWorld { | ||
let mut blockchain = ScenarioWorld::new(); | ||
blockchain.set_current_dir_from_workspace("contracts/composability/forwarder"); | ||
|
||
blockchain.register_contract(FORWARDER_PATH_EXPR, forwarder::ContractBuilder); | ||
blockchain | ||
} | ||
|
||
#[test] | ||
fn test_nft_update_attributes_and_send() { | ||
let mut world = world(); | ||
|
||
let forwarder_code = world.code_expression(FORWARDER_PATH_EXPR); | ||
let roles = vec![ | ||
"ESDTRoleNFTCreate".to_string(), | ||
"ESDTRoleNFTUpdateAttributes".to_string(), | ||
]; | ||
|
||
world.set_state_step( | ||
SetStateStep::new() | ||
.put_account(USER_ADDRESS_EXPR, Account::new().nonce(1)) | ||
.put_account( | ||
FORWARDER_ADDRESS_EXPR, | ||
Account::new() | ||
.nonce(1) | ||
.code(forwarder_code.clone()) | ||
.esdt_roles(NFT_TOKEN_ID_EXPR, roles), | ||
), | ||
); | ||
|
||
let forwarder_whitebox = WhiteboxContract::new(FORWARDER_ADDRESS_EXPR, forwarder::contract_obj); | ||
|
||
let original_attributes = Color { r: 0, g: 0, b: 0 }; | ||
|
||
world.whitebox_call( | ||
&forwarder_whitebox, | ||
ScCallStep::new().from(USER_ADDRESS_EXPR), | ||
|sc| { | ||
sc.nft_create_compact( | ||
managed_token_id!(NFT_TOKEN_ID), | ||
managed_biguint!(1), | ||
original_attributes, | ||
); | ||
|
||
sc.send().direct_esdt( | ||
&managed_address!(&address_expr_to_address(USER_ADDRESS_EXPR)), | ||
&managed_token_id!(NFT_TOKEN_ID), | ||
1, | ||
&managed_biguint!(1), | ||
); | ||
}, | ||
); | ||
|
||
world.check_state_step(CheckStateStep::new().put_account( | ||
USER_ADDRESS_EXPR, | ||
CheckAccount::new().esdt_nft_balance_and_attributes( | ||
NFT_TOKEN_ID_EXPR, | ||
1, | ||
"1", | ||
Some(original_attributes), | ||
), | ||
)); | ||
|
||
let new_attributes = Color { | ||
r: 255, | ||
g: 255, | ||
b: 255, | ||
}; | ||
|
||
world.whitebox_call( | ||
&forwarder_whitebox, | ||
ScCallStep::new() | ||
.from(USER_ADDRESS_EXPR) | ||
.esdt_transfer(NFT_TOKEN_ID, 1, rust_biguint!(1)), | ||
|sc| { | ||
sc.nft_update_attributes(managed_token_id!(NFT_TOKEN_ID), 1, new_attributes); | ||
|
||
sc.send().direct_esdt( | ||
&managed_address!(&address_expr_to_address(USER_ADDRESS_EXPR)), | ||
&managed_token_id!(NFT_TOKEN_ID), | ||
1, | ||
&managed_biguint!(1), | ||
); | ||
}, | ||
); | ||
|
||
world.check_state_step(CheckStateStep::new().put_account( | ||
USER_ADDRESS_EXPR, | ||
CheckAccount::new().esdt_nft_balance_and_attributes( | ||
NFT_TOKEN_ID_EXPR, | ||
1, | ||
"1", | ||
Some(new_attributes), | ||
), | ||
)); | ||
} | ||
|
||
fn address_expr_to_address(address_expr: &str) -> Address { | ||
AddressValue::from(address_expr).to_address() | ||
} |
Oops, something went wrong.