From d1ee96562c6e211a9ef509c1619cb09d6dfed17c Mon Sep 17 00:00:00 2001 From: notV4l Date: Sun, 27 Aug 2023 22:10:06 +0200 Subject: [PATCH] chore: refactor tests and configure writers --- src/tests.cairo | 1 + src/tests/test_attributes.cairo | 19 +++- src/tests/test_box_nft.cairo | 24 ++--- src/tests/test_briq_factory.cairo | 12 +-- src/tests/test_set_nft.cairo | 160 ++++++++++++++++-------------- src/tests/test_utils.cairo | 130 ++++++++++++++++++++++-- src/tests/test_world_config.cairo | 93 +++++++++++++++++ 7 files changed, 329 insertions(+), 110 deletions(-) create mode 100644 src/tests/test_world_config.cairo diff --git a/src/tests.cairo b/src/tests.cairo index 4413371..4d29639 100644 --- a/src/tests.cairo +++ b/src/tests.cairo @@ -7,3 +7,4 @@ mod test_box_nft; mod test_briq_token; mod test_set_nft; mod test_briq_factory; +mod test_world_config; \ No newline at end of file diff --git a/src/tests/test_attributes.cairo b/src/tests/test_attributes.cairo index 67ef2ee..aa77e4c 100644 --- a/src/tests/test_attributes.cairo +++ b/src/tests/test_attributes.cairo @@ -6,7 +6,7 @@ use serde::Serde; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use briq_protocol::world_config::{WorldConfig, SYSTEM_CONFIG_ID}; -use briq_protocol::tests::test_utils::deploy_default_world; +use briq_protocol::tests::test_utils::{DEFAULT_OWNER, deploy_default_world, impersonate}; use dojo_erc::erc721::interface::IERC721DispatcherTrait; @@ -72,3 +72,20 @@ fn test_create_collection_collision() { world.execute('create_collection', (calldata)); } } + + +#[test] +#[available_gas(30000000)] +#[should_panic] +fn test_create_collection_with_non_world_admin() { + let DefaultWorld{world, .. } = deploy_default_world(); + + impersonate(DEFAULT_OWNER()); + + let mut calldata: Array = ArrayTrait::new(); + CreateCollectionData { + collection_id: 1, params: 2, admin_or_system: starknet::contract_address_const::<0xfafa>() + } + .serialize(ref calldata); + world.execute('create_collection', (calldata)); +} diff --git a/src/tests/test_box_nft.cairo b/src/tests/test_box_nft.cairo index 86e1148..b7ee98f 100644 --- a/src/tests/test_box_nft.cairo +++ b/src/tests/test_box_nft.cairo @@ -4,12 +4,13 @@ use result::ResultTrait; use array::ArrayTrait; use serde::Serde; -use starknet::testing::{set_caller_address, set_contract_address}; use starknet::ContractAddress; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use briq_protocol::world_config::{WorldConfig, SYSTEM_CONFIG_ID}; -use briq_protocol::tests::test_utils::{WORLD_ADMIN, DefaultWorld, deploy_default_world, mint_briqs}; +use briq_protocol::tests::test_utils::{ + WORLD_ADMIN, DEFAULT_OWNER, DefaultWorld, deploy_default_world, mint_briqs, impersonate +}; use dojo_erc::erc721::interface::IERC721DispatcherTrait; use dojo_erc::erc1155::interface::IERC1155DispatcherTrait; @@ -22,9 +23,6 @@ use debug::PrintTrait; use briq_protocol::world_config::get_world_config; -fn default_owner() -> ContractAddress { - starknet::contract_address_const::<0xcafe>() -} #[test] #[available_gas(300000000)] @@ -38,7 +36,7 @@ fn test_mint_and_unbox() { WORLD_ADMIN().into(), get_world_config(world).box.into(), 0, - default_owner().into(), + DEFAULT_OWNER().into(), 1, 0x1, 1, @@ -46,23 +44,23 @@ fn test_mint_and_unbox() { ]) ); - set_contract_address(default_owner()); + impersonate(DEFAULT_OWNER()); - assert(briq_token.balance_of(default_owner(), 1) == 0, 'bad balance'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 0, 'bad balance'); assert( booklet - .balance_of(default_owner(), 0x1000000000000000000000000000000000000000000000001) == 0, + .balance_of(DEFAULT_OWNER(), 0x1000000000000000000000000000000000000000000000001) == 0, 'bad balance 1' ); - assert(box_nft.balance_of(default_owner(), 1) == 1, 'bad balance 2'); + assert(box_nft.balance_of(DEFAULT_OWNER(), 1) == 1, 'bad balance 2'); world.execute('box_unboxing', (array![0x1,])); - assert(briq_token.balance_of(default_owner(), 1) == 434, 'bad balance 2.5'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 434, 'bad balance 2.5'); assert( booklet - .balance_of(default_owner(), 0x1000000000000000000000000000000000000000000000001) == 1, + .balance_of(DEFAULT_OWNER(), 0x1000000000000000000000000000000000000000000000001) == 1, 'bad balance 3' ); - assert(box_nft.balance_of(default_owner(), 1) == 0, 'bad balance 4'); + assert(box_nft.balance_of(DEFAULT_OWNER(), 1) == 0, 'bad balance 4'); } diff --git a/src/tests/test_briq_factory.cairo b/src/tests/test_briq_factory.cairo index 62ee54e..42fed25 100644 --- a/src/tests/test_briq_factory.cairo +++ b/src/tests/test_briq_factory.cairo @@ -11,7 +11,7 @@ use starknet::info::get_block_timestamp; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use briq_protocol::world_config::{WorldConfig, SYSTEM_CONFIG_ID}; -use briq_protocol::tests::test_utils::{WORLD_ADMIN, DefaultWorld, deploy_default_world, mint_briqs}; +use briq_protocol::tests::test_utils::{WORLD_ADMIN,ETH_ADDRESS, DefaultWorld, deploy_default_world, mint_briqs}; use dojo_erc::erc_common::utils::system_calldata; @@ -26,19 +26,13 @@ use briq_protocol::felt_math::{FeltOrd, FeltDiv}; use debug::PrintTrait; -fn default_owner() -> ContractAddress { - starknet::contract_address_const::<0xcafe>() -} -fn eth_address() -> ContractAddress { - starknet::contract_address_const::<0xeeee>() -} fn init_briq_factory(world: IWorldDispatcher, t: felt252, surge_t: felt252,) -> BriqFactoryStore { world .execute( 'BriqFactoryInitialize', - system_calldata(BriqFactoryInitializeParams { t, surge_t, buy_token: eth_address() }) + system_calldata(BriqFactoryInitializeParams { t, surge_t, buy_token: ETH_ADDRESS() }) ); BriqFactoryTrait::get_briq_factory(world) } @@ -49,7 +43,7 @@ fn test_briq_factory_init() { let DefaultWorld{world, .. } = deploy_default_world(); let briq_factory = init_briq_factory(world, DECIMALS(), DECIMALS()); - assert(briq_factory.buy_token == eth_address(), 'invalid buy_token'); + assert(briq_factory.buy_token == ETH_ADDRESS(), 'invalid buy_token'); assert(briq_factory.surge_t == DECIMALS(), 'invalid surge_t'); assert(briq_factory.last_stored_t == DECIMALS(), 'invalid last_stored_t'); } diff --git a/src/tests/test_set_nft.cairo b/src/tests/test_set_nft.cairo index 636b526..9691743 100644 --- a/src/tests/test_set_nft.cairo +++ b/src/tests/test_set_nft.cairo @@ -9,7 +9,9 @@ use starknet::ContractAddress; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use briq_protocol::world_config::{WorldConfig, SYSTEM_CONFIG_ID}; -use briq_protocol::tests::test_utils::{WORLD_ADMIN, DefaultWorld, deploy_default_world, mint_briqs}; +use briq_protocol::tests::test_utils::{ + WORLD_ADMIN, DEFAULT_OWNER, DefaultWorld, deploy_default_world, mint_briqs, impersonate +}; use dojo_erc::erc721::interface::IERC721DispatcherTrait; use dojo_erc::erc1155::interface::IERC1155DispatcherTrait; @@ -22,9 +24,6 @@ use briq_protocol::set_nft::systems::{assemble, disassemble}; use debug::PrintTrait; -fn default_owner() -> ContractAddress { - starknet::contract_address_const::<0xcafe>() -} #[test] #[available_gas(30000000)] @@ -32,12 +31,11 @@ fn default_owner() -> ContractAddress { fn test_empty_mint() { let DefaultWorld{world, briq_token, set_nft, .. } = deploy_default_world(); - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -49,7 +47,7 @@ fn test_empty_mint() { token_id == 0x1a61b367b44cb5cdc969ad212931f85c0dc5d31227cc1bdb8bf65238a722a6a, 'bad token id' ); - assert(default_owner() == set_nft.owner_of(token_id.into()), 'bad owner'); + assert(DEFAULT_OWNER() == set_nft.owner_of(token_id.into()), 'bad owner'); } #[test] @@ -57,14 +55,15 @@ fn test_empty_mint() { fn test_simple_mint_and_burn() { let DefaultWorld{world, briq_token, set_nft, .. } = deploy_default_world(); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -76,14 +75,14 @@ fn test_simple_mint_and_burn() { token_id == 0x3fa51acc2defe858e3cb515b7e29c6e3ba22da5657e7cc33885860a6470bfc2, 'bad token id' ); - assert(default_owner() == set_nft.owner_of(token_id.into()), 'bad owner'); - assert(set_nft.balance_of(default_owner()) == 1, 'bad balance'); + assert(DEFAULT_OWNER() == set_nft.owner_of(token_id.into()), 'bad owner'); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 1, 'bad balance'); assert(briq_token.balance_of(token_id.try_into().unwrap(), 1) == 1, 'bad balance'); - assert(briq_token.balance_of(default_owner(), 1) == 99, 'bad balance'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 99, 'bad balance'); - disassemble(world, default_owner(), token_id, array![FTSpec { token_id: 1, qty: 1 }], array![]); - assert(set_nft.balance_of(default_owner()) == 0, 'bad balance'); - assert(briq_token.balance_of(default_owner(), 1) == 100, 'bad balance'); + disassemble(world, DEFAULT_OWNER(), token_id, array![FTSpec { token_id: 1, qty: 1 }], array![]); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 0, 'bad balance'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 100, 'bad balance'); // TODO: validate that token ID balance asserts as it's 0 } @@ -93,12 +92,13 @@ fn test_simple_mint_and_burn() { fn test_simple_mint_and_burn_not_enough_briqs() { let DefaultWorld{world, briq_token, set_nft, .. } = deploy_default_world(); - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -113,14 +113,15 @@ fn test_simple_mint_and_burn_not_enough_briqs() { fn test_simple_mint_and_burn_2() { let DefaultWorld{world, briq_token, set_nft, .. } = deploy_default_world(); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -137,14 +138,14 @@ fn test_simple_mint_and_burn_2() { token_id == 0x2d4276d22e1b24bb462c255708ae8293302ff6b17691ed07f5057aee0d6eda3, 'bad token id' ); - assert(default_owner() == set_nft.owner_of(token_id.into()), 'bad owner'); - assert(set_nft.balance_of(default_owner()) == 1, 'bad balance'); + assert(DEFAULT_OWNER() == set_nft.owner_of(token_id.into()), 'bad owner'); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 1, 'bad balance'); assert(briq_token.balance_of(token_id.try_into().unwrap(), 1) == 4, 'bad token balance 1'); - assert(briq_token.balance_of(default_owner(), 1) == 96, 'bad briq balance 1'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 96, 'bad briq balance 1'); - disassemble(world, default_owner(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![]); - assert(set_nft.balance_of(default_owner()) == 0, 'bad balance'); - assert(briq_token.balance_of(default_owner(), 1) == 100, 'bad briq balance 2'); + disassemble(world, DEFAULT_OWNER(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![]); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 0, 'bad balance'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 100, 'bad briq balance 2'); // TODO: validate that token ID balance asserts as it's 0 } @@ -156,14 +157,15 @@ fn test_simple_mint_and_burn_2() { fn test_simple_mint_and_burn_not_enough_briqs_in_disassembly() { let DefaultWorld{world, briq_token, set_nft, .. } = deploy_default_world(); - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -180,16 +182,16 @@ fn test_simple_mint_and_burn_not_enough_briqs_in_disassembly() { token_id == 0x2d4276d22e1b24bb462c255708ae8293302ff6b17691ed07f5057aee0d6eda3, 'bad token id' ); - assert(default_owner() == set_nft.owner_of(token_id.into()), 'bad owner'); - assert(set_nft.balance_of(default_owner()) == 1, 'bad balance'); + assert(DEFAULT_OWNER() == set_nft.owner_of(token_id.into()), 'bad owner'); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 1, 'bad balance'); assert(briq_token.balance_of(token_id.try_into().unwrap(), 1) == 4, 'bad token balance 1'); - assert(briq_token.balance_of(default_owner(), 1) == 96, 'bad briq balance 1'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 96, 'bad briq balance 1'); - disassemble(world, default_owner(), token_id, array![FTSpec { token_id: 1, qty: 1 }], array![]); + disassemble(world, DEFAULT_OWNER(), token_id, array![FTSpec { token_id: 1, qty: 1 }], array![]); assert( starknet::contract_address_const::<0>() == set_nft.owner_of(token_id.into()), 'bad owner' ); - assert(briq_token.balance_of(default_owner(), 1) == 100, 'bad briq balance 2'); + assert(briq_token.balance_of(DEFAULT_OWNER(), 1) == 100, 'bad briq balance 2'); // TODO: validate that token ID balance asserts as it's 0 } @@ -200,14 +202,15 @@ fn test_simple_mint_and_burn_not_enough_briqs_in_disassembly() { fn test_simple_mint_attribute_not_exist() { let DefaultWorld{world, briq_token, set_nft, .. } = deploy_default_world(); - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -258,7 +261,7 @@ fn test_simple_mint_attribute_ok() { WORLD_ADMIN().into(), get!(world, (SYSTEM_CONFIG_ID), WorldConfig).booklet.into(), 0, - default_owner().into(), + DEFAULT_OWNER().into(), 1, 0x1, 1, @@ -266,14 +269,15 @@ fn test_simple_mint_attribute_ok() { ]) ); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); - set_caller_address(default_owner()); - set_contract_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -290,15 +294,15 @@ fn test_simple_mint_attribute_ok() { token_id == 0x2d4276d22e1b24bb462c255708ae8293302ff6b17691ed07f5057aee0d6eda3, 'bad token id' ); - assert(default_owner() == set_nft.owner_of(token_id.into()), 'bad owner'); - assert(set_nft.balance_of(default_owner()) == 1, 'bad balance'); + assert(DEFAULT_OWNER() == set_nft.owner_of(token_id.into()), 'bad owner'); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 1, 'bad balance'); assert(booklet.balance_of(token_id.try_into().unwrap(), 0x1) == 1, 'bad booklet balance 2'); // TODO validate booklet balance of owner to 0 disassemble( - world, default_owner(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![0x1] + world, DEFAULT_OWNER(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![0x1] ); - assert(booklet.balance_of(default_owner(), 0x1) == 1, 'bad booklet balance 3'); + assert(booklet.balance_of(DEFAULT_OWNER(), 0x1) == 1, 'bad booklet balance 3'); // TODO: validate that token ID balance asserts as it's 0 } @@ -341,14 +345,15 @@ fn test_simple_mint_attribute_dont_have_the_booklet() { world.execute('register_shape_verifier', (calldata)); } - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -365,16 +370,16 @@ fn test_simple_mint_attribute_dont_have_the_booklet() { token_id == 0x2d4276d22e1b24bb462c255708ae8293302ff6b17691ed07f5057aee0d6eda3, 'bad token id' ); - assert(default_owner() == set_nft.owner_of(token_id.into()), 'bad owner'); - assert(set_nft.balance_of(default_owner()) == 1, 'bad balance'); + assert(DEFAULT_OWNER() == set_nft.owner_of(token_id.into()), 'bad owner'); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 1, 'bad balance'); disassemble( - world, default_owner(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![0x1] + world, DEFAULT_OWNER(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![0x1] ); assert( starknet::contract_address_const::<0>() == set_nft.owner_of(token_id.into()), 'bad owner' ); - assert(set_nft.balance_of(default_owner()) == 0, 'bad balance'); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 0, 'bad balance'); } #[test] @@ -417,14 +422,15 @@ fn test_simple_mint_attribute_bad_shape_item() { world.execute('register_shape_verifier', (calldata)); } - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -479,14 +485,15 @@ fn test_simple_mint_attribute_shape_fts_mismatch() { world.execute('register_shape_verifier', (calldata)); } - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -529,14 +536,15 @@ fn test_simple_mint_attribute_forgot_in_disassembly() { world.execute('register_shape_verifier', (calldata)); } - set_contract_address(default_owner()); - set_caller_address(default_owner()); + impersonate(DEFAULT_OWNER()); + // assemble is not a contract, se we have to set_caller_address + set_caller_address(DEFAULT_OWNER()); - mint_briqs(world, default_owner(), 1, 100); + mint_briqs(world, DEFAULT_OWNER(), 1, 100); let token_id = assemble( world, - default_owner(), + DEFAULT_OWNER(), 0xfade, array![0xcafe], array![0xfade], @@ -553,8 +561,8 @@ fn test_simple_mint_attribute_forgot_in_disassembly() { token_id == 0x2d4276d22e1b24bb462c255708ae8293302ff6b17691ed07f5057aee0d6eda3, 'bad token id' ); - assert(default_owner() == set_nft.owner_of(token_id.into()), 'bad owner'); - assert(set_nft.balance_of(default_owner()) == 1, 'bad balance'); + assert(DEFAULT_OWNER() == set_nft.owner_of(token_id.into()), 'bad owner'); + assert(set_nft.balance_of(DEFAULT_OWNER()) == 1, 'bad balance'); - disassemble(world, default_owner(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![]); + disassemble(world, DEFAULT_OWNER(), token_id, array![FTSpec { token_id: 1, qty: 4 }], array![]); } diff --git a/src/tests/test_utils.cairo b/src/tests/test_utils.cairo index 17a61d6..c5d8096 100644 --- a/src/tests/test_utils.cairo +++ b/src/tests/test_utils.cairo @@ -4,7 +4,7 @@ use result::ResultTrait; use array::ArrayTrait; use serde::Serde; -use starknet::testing::{set_caller_address, set_contract_address}; +use starknet::testing::{set_caller_address, set_contract_address, set_account_contract_address}; use starknet::ContractAddress; use starknet::get_contract_address; use starknet::syscalls::deploy_syscall; @@ -21,43 +21,145 @@ use briq_protocol::world_config::{WorldConfig, SYSTEM_CONFIG_ID}; use dojo_erc::erc721::interface::IERC721Dispatcher; use dojo_erc::erc1155::interface::IERC1155Dispatcher; + +fn ETH_ADDRESS() -> ContractAddress { + starknet::contract_address_const::<0xeeee>() +} + fn WORLD_ADMIN() -> ContractAddress { - 0x420.try_into().unwrap() + starknet::contract_address_const::<0x420>() +} + +fn TREASURY() -> ContractAddress { + starknet::contract_address_const::<0x6969>() +} + +fn DEFAULT_OWNER() -> ContractAddress { + starknet::contract_address_const::<0xcafe>() +} + +fn impersonate(address: ContractAddress) { + set_contract_address(address); + set_account_contract_address(address); } fn spawn_world() -> IWorldDispatcher { // components let mut components = array![ + // world_config + briq_protocol::world_config::world_config::TEST_CLASS_HASH, + // erc1155 dojo_erc::erc1155::components::erc_1155_balance::TEST_CLASS_HASH, dojo_erc::erc1155::components::operator_approval::TEST_CLASS_HASH, + // erc721 dojo_erc::erc721::components::erc_721_balance::TEST_CLASS_HASH, dojo_erc::erc721::components::erc_721_owner::TEST_CLASS_HASH, dojo_erc::erc721::components::erc_721_token_approval::TEST_CLASS_HASH, + // collection briq_protocol::attributes::collection::collection::TEST_CLASS_HASH, + // shape_verifier briq_protocol::shape_verifier::shape_verifier::TEST_CLASS_HASH, - briq_protocol::world_config::world_config::TEST_CLASS_HASH, ]; // systems let mut systems = array![ + // world_config + briq_protocol::world_config::SetupWorld::TEST_CLASS_HASH, + // erc1155 dojo_erc::erc1155::systems::ERC1155SetApprovalForAll::TEST_CLASS_HASH, - briq_protocol::briq_token::systems::BriqTokenSafeTransferFrom::TEST_CLASS_HASH, - briq_protocol::briq_token::systems::BriqTokenSafeBatchTransferFrom::TEST_CLASS_HASH, briq_protocol::briq_token::systems::ERC1155MintBurn::TEST_CLASS_HASH, + // erc721 dojo_erc::erc721::systems::ERC721Approve::TEST_CLASS_HASH, dojo_erc::erc721::systems::ERC721SetApprovalForAll::TEST_CLASS_HASH, dojo_erc::erc721::systems::ERC721TransferFrom::TEST_CLASS_HASH, + // briq_token + briq_protocol::briq_token::systems::BriqTokenSafeTransferFrom::TEST_CLASS_HASH, + briq_protocol::briq_token::systems::BriqTokenSafeBatchTransferFrom::TEST_CLASS_HASH, + // set_nft briq_protocol::set_nft::systems::set_nft_assembly::TEST_CLASS_HASH, briq_protocol::set_nft::systems::set_nft_disassembly::TEST_CLASS_HASH, - briq_protocol::attributes::collection::create_collection::TEST_CLASS_HASH, + // shape_verifier briq_protocol::shape_verifier::register_shape_verifier::TEST_CLASS_HASH, briq_protocol::shape_verifier::shape_verifier_system::TEST_CLASS_HASH, - briq_protocol::box_nft::unboxing::box_unboxing::TEST_CLASS_HASH, - briq_protocol::world_config::SetupWorld::TEST_CLASS_HASH, + // briq_factory briq_protocol::briq_factory::systems::BriqFactoryMint::TEST_CLASS_HASH, briq_protocol::briq_factory::systems::BriqFactoryInitialize::TEST_CLASS_HASH, + // attributes + briq_protocol::attributes::collection::create_collection::TEST_CLASS_HASH, + // unboxing + briq_protocol::box_nft::unboxing::box_unboxing::TEST_CLASS_HASH, ]; let world = spawn_test_world(components, systems); + + // + // set-up writer rights + // + + // WARNING !! if uncommented --> anybody can SetupWorld + // world.grant_writer('WorldConfig', 'SetupWorld'); + + // WARNING !! if uncommented --> anybody can create_collection + // world.grant_writer('Collection', 'create_collection'); + + // *************************** + // **** erc1155 + // *************************** + + // erc_1155_balance + // world.grant_writer('ERC1155Balance', 'ERC1155SafeTransferFrom'); + // world.grant_writer('ERC1155Balance', 'ERC1155SafeBatchTransferFrom'); + world.grant_writer('ERC1155Balance', 'ERC1155MintBurn'); + + // operator_approval + world.grant_writer('OperatorApproval', 'ERC1155SetApprovalForAll'); + + // *************************** + // **** erc721 + // *************************** + + // erc_721_balance + world.grant_writer('ERC721Balance', 'ERC721TransferFrom'); + // world.grant_writer('ERC721Balance', 'ERC721Mint'); + // world.grant_writer('ERC721Balance', 'ERC721Burn'); + + // erc_721_owner + world.grant_writer('ERC721Owner', 'ERC721TransferFrom'); + // world.grant_writer('ERC721Owner', 'ERC721Mint'); + // world.grant_writer('ERC721Owner', 'ERC721Burn'); + + // erc_721_token_approval + world.grant_writer('ERC721TokenApproval', 'ERC721Approve'); + world.grant_writer('ERC721TokenApproval', 'ERC721TransferFrom'); + + // // operator_approval + // world.grant_writer('OperatorApproval', 'ERC721SetApprovalForAll'); + + // *************************** + // **** set_nft (erc721) + // *************************** + + // set_nft_assembly + world.grant_writer('ERC721Balance', 'set_nft_assembly'); + world.grant_writer('ERC721Owner', 'set_nft_assembly'); + world.grant_writer('ERC1155Balance', 'set_nft_assembly'); + + // set_nft_disassembly + world.grant_writer('ERC721Owner', 'set_nft_disassembly'); + world.grant_writer('ERC721Balance', 'set_nft_disassembly'); + world.grant_writer('ERC1155Balance', 'set_nft_disassembly'); + + // *************************** + // **** box_nft + // *************************** + + world.grant_writer('ERC1155Balance', 'box_unboxing'); + + // *************************** + // **** shape_verifier + // *************************** + + world.grant_writer('ERC1155Balance', 'shape_verifier_system'); + world } @@ -106,15 +208,21 @@ struct DefaultWorld { } fn deploy_default_world() -> DefaultWorld { - set_caller_address(WORLD_ADMIN()); - set_contract_address(WORLD_ADMIN()); + impersonate(WORLD_ADMIN()); let world = spawn_world(); let (briq, set, booklet, box) = deploy_contracts(world); world .execute( 'SetupWorld', - (array![WORLD_ADMIN().into(), briq.into(), set.into(), booklet.into(), box.into(),]) + (array![ + WORLD_ADMIN().into(), + TREASURY().into(), + briq.into(), + set.into(), + booklet.into(), + box.into(), + ]) ); DefaultWorld { world, diff --git a/src/tests/test_world_config.cairo b/src/tests/test_world_config.cairo new file mode 100644 index 0000000..c6b1c52 --- /dev/null +++ b/src/tests/test_world_config.cairo @@ -0,0 +1,93 @@ +use traits::{Into, TryInto, Default}; +use option::{Option, OptionTrait}; +use result::ResultTrait; +use array::ArrayTrait; +use serde::Serde; + +use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; +use briq_protocol::world_config::{WorldConfig, SYSTEM_CONFIG_ID}; +use briq_protocol::tests::test_utils::{ + DefaultWorld, spawn_world, deploy_contracts, WORLD_ADMIN, TREASURY, DEFAULT_OWNER, impersonate, + deploy_default_world +}; + + +use briq_protocol::types::{FTSpec, ShapeItem}; + +use debug::PrintTrait; + +use briq_protocol::attributes::collection::CreateCollectionData; + +#[test] +#[available_gas(30000000)] +fn test_world_admin_can_setup_world() { + impersonate(WORLD_ADMIN()); + + let world = spawn_world(); + let (briq, set, booklet, box) = deploy_contracts(world); + world + .execute( + 'SetupWorld', + (array![ + WORLD_ADMIN().into(), + TREASURY().into(), + briq.into(), + set.into(), + booklet.into(), + box.into(), + ]) + ); +} + + +#[test] +#[available_gas(30000000)] +#[should_panic] +fn test_not_world_admin_cannot_setup_world() { + impersonate(WORLD_ADMIN()); + + let world = spawn_world(); + let (briq, set, booklet, box) = deploy_contracts(world); + + impersonate(DEFAULT_OWNER()); + + world + .execute( + 'SetupWorld', + (array![ + WORLD_ADMIN().into(), + TREASURY().into(), + briq.into(), + set.into(), + booklet.into(), + box.into(), + ]) + ); +} + + +#[test] +#[available_gas(30000000)] +fn test_not_world_admin_can_setup_world_if_bad_writer_config() { + impersonate(WORLD_ADMIN()); + + let world = spawn_world(); + let (briq, set, booklet, box) = deploy_contracts(world); + + world.grant_writer('WorldConfig', 'SetupWorld'); + + impersonate(DEFAULT_OWNER()); + + world + .execute( + 'SetupWorld', + (array![ + WORLD_ADMIN().into(), + TREASURY().into(), + briq.into(), + set.into(), + booklet.into(), + box.into(), + ]) + ); +} \ No newline at end of file