difftreelog
feature/CORE-296 Format
in: master
1 file changed
pallets/unique/src/tests.rsdiffbeforeafterboth1// Tests to be written here2use super::*;3use crate::mock::*;4use crate::{AccessMode, CollectionMode};5use up_data_structs::{6 COLLECTION_NUMBER_LIMIT, CollectionId, CreateItemData, CreateFungibleData, CreateNftData,7 CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission,8 TokenId, MAX_TOKEN_OWNERSHIP,9};10use frame_support::{assert_noop, assert_ok, assert_err};11use sp_std::convert::TryInto;12use pallet_balances;1314fn add_balance(user: u64, value: u64) {15 const RICH_USER: u64 = 999;16 assert_ok!(<pallet_balances::Pallet<Test>>::set_balance(Origin::root(), RICH_USER, value, 0));17 assert_ok!(<pallet_balances::Pallet<Test>>::force_transfer(Origin::root(), RICH_USER, user, value));18}1920fn default_nft_data() -> CreateNftData {21 CreateNftData {22 const_data: vec![1, 2, 3].try_into().unwrap(),23 variable_data: vec![3, 2, 1].try_into().unwrap(),24 }25}2627fn default_fungible_data() -> CreateFungibleData {28 CreateFungibleData { value: 5 }29}3031fn default_re_fungible_data() -> CreateReFungibleData {32 CreateReFungibleData {33 const_data: vec![1, 2, 3].try_into().unwrap(),34 variable_data: vec![3, 2, 1].try_into().unwrap(),35 pieces: 1023,36 }37}3839fn create_test_collection_for_owner(40 mode: &CollectionMode,41 owner: u64,42 id: CollectionId,43) -> CollectionId {44 add_balance(owner, CollectionCreationPrice::get() as u64 + 1);4546 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();47 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();48 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();4950 let origin1 = Origin::signed(owner);51 assert_ok!(TemplateModule::create_collection(52 origin1,53 col_name1.try_into().unwrap(),54 col_desc1.try_into().unwrap(),55 token_prefix1.try_into().unwrap(),56 mode.clone()57 ));5859 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();60 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();61 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();62 assert_eq!(63 <pallet_common::CollectionById<Test>>::get(id)64 .unwrap()65 .owner,66 owner67 );68 assert_eq!(69 <pallet_common::CollectionById<Test>>::get(id).unwrap().name,70 saved_col_name71 );72 assert_eq!(73 <pallet_common::CollectionById<Test>>::get(id).unwrap().mode,74 *mode75 );76 assert_eq!(77 <pallet_common::CollectionById<Test>>::get(id)78 .unwrap()79 .description,80 saved_description81 );82 assert_eq!(83 <pallet_common::CollectionById<Test>>::get(id)84 .unwrap()85 .token_prefix,86 saved_prefix87 );88 id89}9091fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {92 create_test_collection_for_owner(&mode, 1, id)93}9495fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {96 let origin1 = Origin::signed(1);97 assert_ok!(TemplateModule::create_item(98 origin1,99 collection_id,100 account(1),101 data.clone()102 ));103}104105fn account(sub: u64) -> TestCrossAccountId {106 TestCrossAccountId::from_sub(sub)107}108109// Use cases tests region110// #region111112#[test]113fn set_version_schema() {114 new_test_ext().execute_with(|| {115 let origin1 = Origin::signed(1);116 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));117118 assert_ok!(TemplateModule::set_schema_version(119 origin1,120 collection_id,121 SchemaVersion::Unique122 ));123 assert_eq!(124 <pallet_common::CollectionById<Test>>::get(collection_id)125 .unwrap()126 .schema_version,127 SchemaVersion::Unique128 );129 });130}131132#[test]133fn check_not_sufficient_founds() {134 new_test_ext().execute_with(|| {135 let acc: u64 = 1;136 <pallet_balances::Pallet<Test>>::set_balance(Origin::root(), acc, 0, 0).unwrap();137138 let name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();139 let description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();140 let token_prefix: Vec<u8> = b"token_prefix1\0".to_vec();141142 let data: CreateCollectionData<<Test as system::Config>::AccountId> = CreateCollectionData {143 name: name.try_into().unwrap(),144 description: description.try_into().unwrap(),145 token_prefix: token_prefix.try_into().unwrap(),146 mode: CollectionMode::NFT,147 ..Default::default()148 };149 150 let result = TemplateModule::create_collection_ex(Origin::signed(acc), data);151 assert_err!(result, <CommonError<Test>>::NotSufficientFounds);152 });153}154155#[test]156fn create_fungible_collection_fails_with_large_decimal_numbers() {157 new_test_ext().execute_with(|| {158 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();159 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();160 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();161162 let origin1 = Origin::signed(1);163 assert_noop!(164 TemplateModule::create_collection(165 origin1,166 col_name1.try_into().unwrap(),167 col_desc1.try_into().unwrap(),168 token_prefix1.try_into().unwrap(),169 CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1)170 ),171 Error::<Test>::CollectionDecimalPointLimitExceeded172 );173 });174}175176#[test]177fn create_nft_item() {178 new_test_ext().execute_with(|| {179 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));180181 let data = default_nft_data();182 create_test_item(collection_id, &data.clone().into());183184 let item = <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1)).unwrap();185 assert_eq!(item.const_data, data.const_data.into_inner());186 assert_eq!(item.variable_data, data.variable_data.into_inner());187 });188}189190// Use cases tests region191// #region192#[test]193fn create_nft_multiple_items() {194 new_test_ext().execute_with(|| {195 create_test_collection(&CollectionMode::NFT, CollectionId(1));196197 let origin1 = Origin::signed(1);198199 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];200201 assert_ok!(TemplateModule::create_multiple_items(202 origin1,203 CollectionId(1),204 account(1),205 items_data206 .clone()207 .into_iter()208 .map(|d| { d.into() })209 .collect()210 ));211 for (index, data) in items_data.into_iter().enumerate() {212 let item = <pallet_nonfungible::TokenData<Test>>::get((213 CollectionId(1),214 TokenId((index + 1) as u32),215 ))216 .unwrap();217 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());218 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());219 }220 });221}222223#[test]224fn create_refungible_item() {225 new_test_ext().execute_with(|| {226 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));227228 let data = default_re_fungible_data();229 create_test_item(collection_id, &data.clone().into());230 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));231 let balance =232 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));233 assert_eq!(item.const_data, data.const_data.into_inner());234 assert_eq!(item.variable_data, data.variable_data.into_inner());235 assert_eq!(balance, 1023);236 });237}238239#[test]240fn create_multiple_refungible_items() {241 new_test_ext().execute_with(|| {242 create_test_collection(&CollectionMode::ReFungible, CollectionId(1));243244 let origin1 = Origin::signed(1);245246 let items_data = vec![247 default_re_fungible_data(),248 default_re_fungible_data(),249 default_re_fungible_data(),250 ];251252 assert_ok!(TemplateModule::create_multiple_items(253 origin1,254 CollectionId(1),255 account(1),256 items_data257 .clone()258 .into_iter()259 .map(|d| { d.into() })260 .collect()261 ));262 for (index, data) in items_data.into_iter().enumerate() {263 let item = <pallet_refungible::TokenData<Test>>::get((264 CollectionId(1),265 TokenId((index + 1) as u32),266 ));267 let balance =268 <pallet_refungible::Balance<Test>>::get((CollectionId(1), TokenId(1), account(1)));269 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());270 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());271 assert_eq!(balance, 1023);272 }273 });274}275276#[test]277fn create_fungible_item() {278 new_test_ext().execute_with(|| {279 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));280281 let data = default_fungible_data();282 create_test_item(collection_id, &data.into());283284 assert_eq!(285 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),286 5287 );288 });289}290291//#[test]292// fn create_multiple_fungible_items() {293// new_test_ext().execute_with(|| {294// default_limits();295296// create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));297298// let origin1 = Origin::signed(1);299300// let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];301302// assert_ok!(TemplateModule::create_multiple_items(303// origin1.clone(),304// 1,305// 1,306// items_data.clone().into_iter().map(|d| { d.into() }).collect()307// ));308309// for (index, _) in items_data.iter().enumerate() {310// assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).value, 5);311// }312// assert_eq!(TemplateModule::balance_count(1, 1), 3000);313// assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);314// });315// }316317#[test]318fn transfer_fungible_item() {319 new_test_ext().execute_with(|| {320 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));321322 let origin1 = Origin::signed(1);323 let origin2 = Origin::signed(2);324325 let data = default_fungible_data();326 create_test_item(collection_id, &data.into());327328 assert_eq!(329 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),330 5331 );332333 // change owner scenario334 assert_ok!(TemplateModule::transfer(335 origin1,336 account(2),337 CollectionId(1),338 TokenId(0),339 5340 ));341 assert_eq!(342 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),343 0344 );345346 // split item scenario347 assert_ok!(TemplateModule::transfer(348 origin2.clone(),349 account(3),350 CollectionId(1),351 TokenId(0),352 3353 ));354355 // split item and new owner has account scenario356 assert_ok!(TemplateModule::transfer(357 origin2,358 account(3),359 CollectionId(1),360 TokenId(0),361 1362 ));363 assert_eq!(364 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(2))),365 1366 );367 assert_eq!(368 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(3))),369 4370 );371 });372}373374#[test]375fn transfer_refungible_item() {376 new_test_ext().execute_with(|| {377 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));378379 // Create RFT 1 in 1023 pieces for account 1380 let data = default_re_fungible_data();381 create_test_item(collection_id, &data.clone().into());382 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));383 assert_eq!(item.const_data, data.const_data.into_inner());384 assert_eq!(item.variable_data, data.variable_data.into_inner());385 assert_eq!(386 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),387 1388 );389 assert_eq!(390 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),391 1023392 );393 assert_eq!(394 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),395 true396 );397398 // Account 1 transfers all 1023 pieces of RFT 1 to account 2399 let origin1 = Origin::signed(1);400 let origin2 = Origin::signed(2);401 assert_ok!(TemplateModule::transfer(402 origin1,403 account(2),404 CollectionId(1),405 TokenId(1),406 1023407 ));408 assert_eq!(409 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),410 1023411 );412 assert_eq!(413 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),414 0415 );416 assert_eq!(417 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),418 1419 );420 assert_eq!(421 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),422 false423 );424 assert_eq!(425 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),426 true427 );428429 // Account 2 transfers 500 pieces of RFT 1 to account 3430 assert_ok!(TemplateModule::transfer(431 origin2.clone(),432 account(3),433 CollectionId(1),434 TokenId(1),435 500436 ));437 assert_eq!(438 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),439 523440 );441 assert_eq!(442 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),443 500444 );445 assert_eq!(446 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),447 1448 );449 assert_eq!(450 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),451 1452 );453 assert_eq!(454 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),455 true456 );457 assert_eq!(458 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),459 true460 );461462 // Account 2 transfers 200 more pieces of RFT 1 to account 3 with pre-existing balance463 assert_ok!(TemplateModule::transfer(464 origin2,465 account(3),466 CollectionId(1),467 TokenId(1),468 200469 ));470 assert_eq!(471 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),472 323473 );474 assert_eq!(475 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),476 700477 );478 assert_eq!(479 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),480 1481 );482 assert_eq!(483 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),484 1485 );486 assert_eq!(487 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),488 true489 );490 assert_eq!(491 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),492 true493 );494 });495}496497#[test]498fn transfer_nft_item() {499 new_test_ext().execute_with(|| {500 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));501502 let data = default_nft_data();503 create_test_item(collection_id, &data.into());504 assert_eq!(505 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),506 1507 );508 assert_eq!(509 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),510 true511 );512513 let origin1 = Origin::signed(1);514 // default scenario515 assert_ok!(TemplateModule::transfer(516 origin1,517 account(2),518 CollectionId(1),519 TokenId(1),520 1521 ));522 assert_eq!(523 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),524 0525 );526 assert_eq!(527 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),528 1529 );530 assert_eq!(531 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),532 false533 );534 assert_eq!(535 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),536 true537 );538 });539}540541#[test]542fn transfer_nft_item_wrong_value() {543 new_test_ext().execute_with(|| {544 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));545546 let data = default_nft_data();547 create_test_item(collection_id, &data.into());548 assert_eq!(549 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),550 1551 );552 assert_eq!(553 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),554 true555 );556557 let origin1 = Origin::signed(1);558559 assert_noop!(560 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 2)561 .map_err(|e| e.error),562 <pallet_nonfungible::Error::<Test>>::NonfungibleItemsHaveNoAmount563 );564 });565}566567#[test]568fn transfer_nft_item_zero_value() {569 new_test_ext().execute_with(|| {570 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));571572 let data = default_nft_data();573 create_test_item(collection_id, &data.into());574 assert_eq!(575 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),576 1577 );578 assert_eq!(579 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),580 true581 );582583 let origin1 = Origin::signed(1);584585 // Transferring 0 amount works on NFT...586 assert_ok!(TemplateModule::transfer(587 origin1,588 account(2),589 CollectionId(1),590 TokenId(1),591 0592 ));593 // ... and results in no transfer594 assert_eq!(595 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),596 1597 );598 assert_eq!(599 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),600 true601 );602 });603}604605#[test]606fn nft_approve_and_transfer_from() {607 new_test_ext().execute_with(|| {608 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));609610 let data = default_nft_data();611 create_test_item(collection_id, &data.into());612613 let origin1 = Origin::signed(1);614 let origin2 = Origin::signed(2);615616 assert_eq!(617 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),618 1619 );620 assert_eq!(621 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),622 true623 );624625 // neg transfer_from626 assert_noop!(627 TemplateModule::transfer_from(628 origin2.clone(),629 account(1),630 account(2),631 CollectionId(1),632 TokenId(1),633 1634 )635 .map_err(|e| e.error),636 CommonError::<Test>::ApprovedValueTooLow637 );638639 // do approve640 assert_ok!(TemplateModule::approve(641 origin1,642 account(2),643 CollectionId(1),644 TokenId(1),645 1646 ));647 assert_eq!(648 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),649 account(2)650 );651652 assert_ok!(TemplateModule::transfer_from(653 origin2,654 account(1),655 account(3),656 CollectionId(1),657 TokenId(1),658 1659 ));660 assert!(661 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()662 );663 });664}665666#[test]667fn nft_approve_and_transfer_from_allow_list() {668 new_test_ext().execute_with(|| {669 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));670671 let origin1 = Origin::signed(1);672 let origin2 = Origin::signed(2);673674 // Create NFT 1 for account 1675 let data = default_nft_data();676 create_test_item(collection_id, &data.clone().into());677 assert_eq!(678 &<pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))679 .unwrap()680 .const_data,681 &data.const_data.into_inner()682 );683 assert_eq!(684 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),685 1686 );687 assert_eq!(688 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),689 true690 );691692 // Allow allow-list users to mint and add accounts 1, 2, and 3 to allow-list693 assert_ok!(TemplateModule::set_mint_permission(694 origin1.clone(),695 CollectionId(1),696 true697 ));698 assert_ok!(TemplateModule::set_public_access_mode(699 origin1.clone(),700 CollectionId(1),701 AccessMode::AllowList702 ));703 assert_ok!(TemplateModule::add_to_allow_list(704 origin1.clone(),705 CollectionId(1),706 account(1)707 ));708 assert_ok!(TemplateModule::add_to_allow_list(709 origin1.clone(),710 CollectionId(1),711 account(2)712 ));713 assert_ok!(TemplateModule::add_to_allow_list(714 origin1.clone(),715 CollectionId(1),716 account(3)717 ));718719 // Account 1 approves account 2 for NFT 1720 assert_ok!(TemplateModule::approve(721 origin1.clone(),722 account(2),723 CollectionId(1),724 TokenId(1),725 1726 ));727 assert_eq!(728 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),729 account(2)730 );731732 // Account 2 transfers NFT 1 from account 1 to account 3733 assert_ok!(TemplateModule::transfer_from(734 origin2,735 account(1),736 account(3),737 CollectionId(1),738 TokenId(1),739 1740 ));741 assert!(742 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()743 );744 });745}746747#[test]748fn refungible_approve_and_transfer_from() {749 new_test_ext().execute_with(|| {750 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));751752 let origin1 = Origin::signed(1);753 let origin2 = Origin::signed(2);754755 // Create RFT 1 in 1023 pieces for account 1756 let data = default_re_fungible_data();757 create_test_item(collection_id, &data.into());758759 assert_eq!(760 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),761 1762 );763 assert_eq!(764 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),765 1023766 );767 assert_eq!(768 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),769 true770 );771772 // Allow public minting, enable allow-list and add accounts 1, 2, 3 to allow-list773 assert_ok!(TemplateModule::set_mint_permission(774 origin1.clone(),775 CollectionId(1),776 true777 ));778 assert_ok!(TemplateModule::set_public_access_mode(779 origin1.clone(),780 CollectionId(1),781 AccessMode::AllowList782 ));783 assert_ok!(TemplateModule::add_to_allow_list(784 origin1.clone(),785 CollectionId(1),786 account(1)787 ));788 assert_ok!(TemplateModule::add_to_allow_list(789 origin1.clone(),790 CollectionId(1),791 account(2)792 ));793 assert_ok!(TemplateModule::add_to_allow_list(794 origin1.clone(),795 CollectionId(1),796 account(3)797 ));798799 // Account 1 approves account 2 for 1023 pieces of RFT 1800 assert_ok!(TemplateModule::approve(801 origin1,802 account(2),803 CollectionId(1),804 TokenId(1),805 1023806 ));807 assert_eq!(808 <pallet_refungible::Allowance<Test>>::get((809 CollectionId(1),810 TokenId(1),811 account(1),812 account(2)813 )),814 1023815 );816817 // Account 2 transfers 100 pieces of RFT 1 from account 1 to account 3818 assert_ok!(TemplateModule::transfer_from(819 origin2,820 account(1),821 account(3),822 CollectionId(1),823 TokenId(1),824 100825 ));826 assert_eq!(827 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),828 1829 );830 assert_eq!(831 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),832 1833 );834 assert_eq!(835 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),836 923837 );838 assert_eq!(839 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),840 100841 );842 assert_eq!(843 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),844 true845 );846 assert_eq!(847 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),848 true849 );850 assert_eq!(851 <pallet_refungible::Allowance<Test>>::get((852 CollectionId(1),853 TokenId(1),854 account(1),855 account(2)856 )),857 923858 );859 });860}861862#[test]863fn fungible_approve_and_transfer_from() {864 new_test_ext().execute_with(|| {865 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));866867 let data = default_fungible_data();868 create_test_item(collection_id, &data.into());869870 let origin1 = Origin::signed(1);871 let origin2 = Origin::signed(2);872873 assert_ok!(TemplateModule::set_mint_permission(874 origin1.clone(),875 CollectionId(1),876 true877 ));878 assert_ok!(TemplateModule::set_public_access_mode(879 origin1.clone(),880 CollectionId(1),881 AccessMode::AllowList882 ));883 assert_ok!(TemplateModule::add_to_allow_list(884 origin1.clone(),885 CollectionId(1),886 account(1)887 ));888 assert_ok!(TemplateModule::add_to_allow_list(889 origin1.clone(),890 CollectionId(1),891 account(2)892 ));893 assert_ok!(TemplateModule::add_to_allow_list(894 origin1.clone(),895 CollectionId(1),896 account(3)897 ));898899 // do approve900 assert_ok!(TemplateModule::approve(901 origin1.clone(),902 account(2),903 CollectionId(1),904 TokenId(0),905 5906 ));907 assert_eq!(908 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),909 5910 );911 assert_ok!(TemplateModule::approve(912 origin1,913 account(3),914 CollectionId(1),915 TokenId(0),916 5917 ));918 assert_eq!(919 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),920 5921 );922 assert_eq!(923 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(3))),924 5925 );926927 assert_ok!(TemplateModule::transfer_from(928 origin2.clone(),929 account(1),930 account(3),931 CollectionId(1),932 TokenId(0),933 4934 ));935936 assert_eq!(937 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),938 1939 );940941 assert_noop!(942 TemplateModule::transfer_from(943 origin2,944 account(1),945 account(3),946 CollectionId(1),947 TokenId(0),948 4949 )950 .map_err(|e| e.error),951 CommonError::<Test>::ApprovedValueTooLow952 );953 });954}955956#[test]957fn change_collection_owner() {958 new_test_ext().execute_with(|| {959 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));960961 let origin1 = Origin::signed(1);962 assert_ok!(TemplateModule::change_collection_owner(963 origin1,964 collection_id,965 2966 ));967 assert_eq!(968 <pallet_common::CollectionById<Test>>::get(collection_id)969 .unwrap()970 .owner,971 2972 );973 });974}975976#[test]977fn destroy_collection() {978 new_test_ext().execute_with(|| {979 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));980981 let origin1 = Origin::signed(1);982 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));983 });984}985986#[test]987fn burn_nft_item() {988 new_test_ext().execute_with(|| {989 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));990991 let origin1 = Origin::signed(1);992993 let data = default_nft_data();994 create_test_item(collection_id, &data.into());995996 // check balance (collection with id = 1, user id = 1)997 assert_eq!(998 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),999 11000 );10011002 // burn item1003 assert_ok!(TemplateModule::burn_item(1004 origin1.clone(),1005 collection_id,1006 TokenId(1),1007 11008 ));1009 assert_eq!(1010 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1011 01012 );1013 });1014}10151016#[test]1017fn burn_same_nft_item_twice() {1018 new_test_ext().execute_with(|| {1019 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10201021 let origin1 = Origin::signed(1);10221023 let data = default_nft_data();1024 create_test_item(collection_id, &data.into());10251026 // check balance (collection with id = 1, user id = 1)1027 assert_eq!(1028 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1029 11030 );10311032 // burn item1033 assert_ok!(TemplateModule::burn_item(1034 origin1.clone(),1035 collection_id,1036 TokenId(1),1037 11038 ));10391040 // burn item again1041 assert_noop!(1042 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1).map_err(|e| e.error),1043 CommonError::<Test>::TokenNotFound1044 );10451046 assert_eq!(1047 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1048 01049 );1050 });1051}10521053#[test]1054fn burn_fungible_item() {1055 new_test_ext().execute_with(|| {1056 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));10571058 let origin1 = Origin::signed(1);1059 assert_ok!(TemplateModule::add_collection_admin(1060 origin1.clone(),1061 collection_id,1062 account(2)1063 ));10641065 let data = default_fungible_data();1066 create_test_item(collection_id, &data.into());10671068 // check balance (collection with id = 1, user id = 1)1069 assert_eq!(1070 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1071 51072 );10731074 // burn item1075 assert_ok!(TemplateModule::burn_item(1076 origin1.clone(),1077 CollectionId(1),1078 TokenId(0),1079 51080 ));1081 assert_noop!(1082 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(0), 5).map_err(|e| e.error),1083 CommonError::<Test>::TokenValueTooLow1084 );10851086 assert_eq!(1087 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1088 01089 );1090 });1091}10921093#[test]1094fn burn_fungible_item_with_token_id() {1095 new_test_ext().execute_with(|| {1096 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));10971098 let origin1 = Origin::signed(1);1099 assert_ok!(TemplateModule::add_collection_admin(1100 origin1.clone(),1101 collection_id,1102 account(2)1103 ));11041105 let data = default_fungible_data();1106 create_test_item(collection_id, &data.into());11071108 // check balance (collection with id = 1, user id = 1)1109 assert_eq!(1110 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1111 51112 );11131114 // Try to burn item using Token ID1115 assert_noop!(1116 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(1), 5).map_err(|e| e.error),1117 <pallet_fungible::Error::<Test>>::FungibleItemsHaveNoId1118 );1119 });1120}1121#[test]1122fn burn_refungible_item() {1123 new_test_ext().execute_with(|| {1124 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));1125 let origin1 = Origin::signed(1);11261127 assert_ok!(TemplateModule::set_mint_permission(1128 origin1.clone(),1129 collection_id,1130 true1131 ));1132 assert_ok!(TemplateModule::set_public_access_mode(1133 origin1.clone(),1134 collection_id,1135 AccessMode::AllowList1136 ));1137 assert_ok!(TemplateModule::add_to_allow_list(1138 origin1.clone(),1139 collection_id,1140 account(1)1141 ));11421143 assert_ok!(TemplateModule::add_collection_admin(1144 origin1.clone(),1145 collection_id,1146 account(2)1147 ));11481149 let data = default_re_fungible_data();1150 create_test_item(collection_id, &data.into());11511152 // check balance (collection with id = 1, user id = 2)1153 assert_eq!(1154 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),1155 11156 );1157 assert_eq!(1158 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1159 10231160 );11611162 // burn item1163 assert_ok!(TemplateModule::burn_item(1164 origin1.clone(),1165 collection_id,1166 TokenId(1),1167 10231168 ));1169 assert_noop!(1170 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1023)1171 .map_err(|e| e.error),1172 CommonError::<Test>::TokenValueTooLow1173 );11741175 assert_eq!(1176 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1177 01178 );1179 });1180}11811182#[test]1183fn add_collection_admin() {1184 new_test_ext().execute_with(|| {1185 let collection1_id =1186 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1187 let origin1 = Origin::signed(1);11881189 // Add collection admins1190 assert_ok!(TemplateModule::add_collection_admin(1191 origin1.clone(),1192 collection1_id,1193 account(2)1194 ));1195 assert_ok!(TemplateModule::add_collection_admin(1196 origin1,1197 collection1_id,1198 account(3)1199 ));12001201 // Owner is not an admin by default1202 assert_eq!(1203 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(1))),1204 false1205 );1206 assert!(<pallet_common::IsAdmin<Test>>::get((1207 CollectionId(1),1208 account(2)1209 )));1210 assert!(<pallet_common::IsAdmin<Test>>::get((1211 CollectionId(1),1212 account(3)1213 )));1214 });1215}12161217#[test]1218fn remove_collection_admin() {1219 new_test_ext().execute_with(|| {1220 let collection1_id =1221 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1222 let origin1 = Origin::signed(1);1223 let origin2 = Origin::signed(2);12241225 // Add collection admins 2 and 31226 assert_ok!(TemplateModule::add_collection_admin(1227 origin1.clone(),1228 collection1_id,1229 account(2)1230 ));1231 assert_ok!(TemplateModule::add_collection_admin(1232 origin1,1233 collection1_id,1234 account(3)1235 ));12361237 assert!(<pallet_common::IsAdmin<Test>>::get((1238 CollectionId(1),1239 account(2)1240 )));1241 assert!(<pallet_common::IsAdmin<Test>>::get((1242 CollectionId(1),1243 account(3)1244 )));12451246 // remove admin 31247 assert_ok!(TemplateModule::remove_collection_admin(1248 origin2,1249 CollectionId(1),1250 account(3)1251 ));12521253 // 2 is still admin, 3 is not an admin anymore1254 assert!(<pallet_common::IsAdmin<Test>>::get((1255 CollectionId(1),1256 account(2)1257 )));1258 assert_eq!(1259 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(3))),1260 false1261 );1262 });1263}12641265#[test]1266fn balance_of() {1267 new_test_ext().execute_with(|| {1268 let nft_collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1269 let fungible_collection_id =1270 create_test_collection(&CollectionMode::Fungible(3), CollectionId(2));1271 let re_fungible_collection_id =1272 create_test_collection(&CollectionMode::ReFungible, CollectionId(3));12731274 // check balance before1275 assert_eq!(1276 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1277 01278 );1279 assert_eq!(1280 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1281 01282 );1283 assert_eq!(1284 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1285 01286 );12871288 let nft_data = default_nft_data();1289 create_test_item(nft_collection_id, &nft_data.into());12901291 let fungible_data = default_fungible_data();1292 create_test_item(fungible_collection_id, &fungible_data.into());12931294 let re_fungible_data = default_re_fungible_data();1295 create_test_item(re_fungible_collection_id, &re_fungible_data.into());12961297 // check balance (collection with id = 1, user id = 1)1298 assert_eq!(1299 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1300 11301 );1302 assert_eq!(1303 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1304 51305 );1306 assert_eq!(1307 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1308 11309 );13101311 assert_eq!(1312 <pallet_nonfungible::Owned<Test>>::get((nft_collection_id, account(1), TokenId(1))),1313 true1314 );1315 assert_eq!(1316 <pallet_refungible::Owned<Test>>::get((1317 re_fungible_collection_id,1318 account(1),1319 TokenId(1)1320 )),1321 true1322 );1323 });1324}13251326#[test]1327fn approve() {1328 new_test_ext().execute_with(|| {1329 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));13301331 let data = default_nft_data();1332 create_test_item(collection_id, &data.into());13331334 let origin1 = Origin::signed(1);13351336 // approve1337 assert_ok!(TemplateModule::approve(1338 origin1,1339 account(2),1340 CollectionId(1),1341 TokenId(1),1342 11343 ));1344 assert_eq!(1345 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1346 account(2)1347 );1348 });1349}13501351#[test]1352fn transfer_from() {1353 new_test_ext().execute_with(|| {1354 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1355 let origin1 = Origin::signed(1);1356 let origin2 = Origin::signed(2);13571358 let data = default_nft_data();1359 create_test_item(collection_id, &data.into());13601361 // approve1362 assert_ok!(TemplateModule::approve(1363 origin1.clone(),1364 account(2),1365 CollectionId(1),1366 TokenId(1),1367 11368 ));1369 assert_eq!(1370 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1371 account(2)1372 );13731374 assert_ok!(TemplateModule::set_mint_permission(1375 origin1.clone(),1376 CollectionId(1),1377 true1378 ));1379 assert_ok!(TemplateModule::set_public_access_mode(1380 origin1.clone(),1381 CollectionId(1),1382 AccessMode::AllowList1383 ));1384 assert_ok!(TemplateModule::add_to_allow_list(1385 origin1.clone(),1386 CollectionId(1),1387 account(1)1388 ));1389 assert_ok!(TemplateModule::add_to_allow_list(1390 origin1.clone(),1391 CollectionId(1),1392 account(2)1393 ));1394 assert_ok!(TemplateModule::add_to_allow_list(1395 origin1,1396 CollectionId(1),1397 account(3)1398 ));13991400 assert_ok!(TemplateModule::transfer_from(1401 origin2,1402 account(1),1403 account(2),1404 CollectionId(1),1405 TokenId(1),1406 11407 ));14081409 // after transfer1410 assert_eq!(1411 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(1))),1412 01413 );1414 assert_eq!(1415 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(2))),1416 11417 );1418 });1419}14201421// #endregion14221423// Coverage tests region1424// #region14251426#[test]1427fn owner_can_add_address_to_allow_list() {1428 new_test_ext().execute_with(|| {1429 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14301431 let origin1 = Origin::signed(1);1432 assert_ok!(TemplateModule::add_to_allow_list(1433 origin1,1434 collection_id,1435 account(2)1436 ));1437 assert!(<pallet_common::Allowlist<Test>>::get((1438 collection_id,1439 account(2)1440 )));1441 });1442}14431444#[test]1445fn admin_can_add_address_to_allow_list() {1446 new_test_ext().execute_with(|| {1447 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1448 let origin1 = Origin::signed(1);1449 let origin2 = Origin::signed(2);14501451 assert_ok!(TemplateModule::add_collection_admin(1452 origin1,1453 collection_id,1454 account(2)1455 ));1456 assert_ok!(TemplateModule::add_to_allow_list(1457 origin2,1458 collection_id,1459 account(3)1460 ));1461 assert!(<pallet_common::Allowlist<Test>>::get((1462 collection_id,1463 account(3)1464 )));1465 });1466}14671468#[test]1469fn nonprivileged_user_cannot_add_address_to_allow_list() {1470 new_test_ext().execute_with(|| {1471 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14721473 let origin2 = Origin::signed(2);1474 assert_noop!(1475 TemplateModule::add_to_allow_list(origin2, collection_id, account(3)),1476 CommonError::<Test>::NoPermission1477 );1478 });1479}14801481#[test]1482fn nobody_can_add_address_to_allow_list_of_nonexisting_collection() {1483 new_test_ext().execute_with(|| {1484 let origin1 = Origin::signed(1);14851486 assert_noop!(1487 TemplateModule::add_to_allow_list(origin1, CollectionId(1), account(2)),1488 CommonError::<Test>::CollectionNotFound1489 );1490 });1491}14921493#[test]1494fn nobody_can_add_address_to_allow_list_of_deleted_collection() {1495 new_test_ext().execute_with(|| {1496 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14971498 let origin1 = Origin::signed(1);1499 assert_ok!(TemplateModule::destroy_collection(1500 origin1.clone(),1501 collection_id1502 ));1503 assert_noop!(1504 TemplateModule::add_to_allow_list(origin1, collection_id, account(2)),1505 CommonError::<Test>::CollectionNotFound1506 );1507 });1508}15091510// If address is already added to allow list, nothing happens1511#[test]1512fn address_is_already_added_to_allow_list() {1513 new_test_ext().execute_with(|| {1514 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1515 let origin1 = Origin::signed(1);15161517 assert_ok!(TemplateModule::add_to_allow_list(1518 origin1.clone(),1519 collection_id,1520 account(2)1521 ));1522 assert_ok!(TemplateModule::add_to_allow_list(1523 origin1,1524 collection_id,1525 account(2)1526 ));1527 assert!(<pallet_common::Allowlist<Test>>::get((1528 collection_id,1529 account(2)1530 )));1531 });1532}15331534#[test]1535fn owner_can_remove_address_from_allow_list() {1536 new_test_ext().execute_with(|| {1537 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15381539 let origin1 = Origin::signed(1);1540 assert_ok!(TemplateModule::add_to_allow_list(1541 origin1.clone(),1542 collection_id,1543 account(2)1544 ));1545 assert_ok!(TemplateModule::remove_from_allow_list(1546 origin1,1547 collection_id,1548 account(2)1549 ));1550 assert_eq!(1551 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1552 false1553 );1554 });1555}15561557#[test]1558fn admin_can_remove_address_from_allow_list() {1559 new_test_ext().execute_with(|| {1560 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1561 let origin1 = Origin::signed(1);1562 let origin2 = Origin::signed(2);15631564 // Owner adds admin1565 assert_ok!(TemplateModule::add_collection_admin(1566 origin1.clone(),1567 collection_id,1568 account(2)1569 ));15701571 // Owner adds address 3 to allow list1572 assert_ok!(TemplateModule::add_to_allow_list(1573 origin1,1574 collection_id,1575 account(3)1576 ));15771578 // Admin removes address 3 from allow list1579 assert_ok!(TemplateModule::remove_from_allow_list(1580 origin2,1581 collection_id,1582 account(3)1583 ));1584 assert_eq!(1585 <pallet_common::Allowlist<Test>>::get((collection_id, account(3))),1586 false1587 );1588 });1589}15901591#[test]1592fn nonprivileged_user_cannot_remove_address_from_allow_list() {1593 new_test_ext().execute_with(|| {1594 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1595 let origin1 = Origin::signed(1);1596 let origin2 = Origin::signed(2);15971598 assert_ok!(TemplateModule::add_to_allow_list(1599 origin1,1600 collection_id,1601 account(2)1602 ));1603 assert_noop!(1604 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1605 CommonError::<Test>::NoPermission1606 );1607 assert!(<pallet_common::Allowlist<Test>>::get((1608 collection_id,1609 account(2)1610 )));1611 });1612}16131614#[test]1615fn nobody_can_remove_address_from_allow_list_of_nonexisting_collection() {1616 new_test_ext().execute_with(|| {1617 let origin1 = Origin::signed(1);16181619 assert_noop!(1620 TemplateModule::remove_from_allow_list(origin1, CollectionId(1), account(2)),1621 CommonError::<Test>::CollectionNotFound1622 );1623 });1624}16251626#[test]1627fn nobody_can_remove_address_from_allow_list_of_deleted_collection() {1628 new_test_ext().execute_with(|| {1629 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1630 let origin1 = Origin::signed(1);1631 let origin2 = Origin::signed(2);16321633 // Add account 2 to allow list1634 assert_ok!(TemplateModule::add_to_allow_list(1635 origin1.clone(),1636 collection_id,1637 account(2)1638 ));16391640 // Account 2 is in collection allow-list1641 assert!(<pallet_common::Allowlist<Test>>::get((1642 collection_id,1643 account(2)1644 )));16451646 // Destroy collection1647 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));16481649 // Attempt to remove account 2 from collection allow-list => error1650 assert_noop!(1651 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1652 CommonError::<Test>::CollectionNotFound1653 );16541655 // Account 2 is not found in collection allow-list anyway1656 assert_eq!(1657 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1658 false1659 );1660 });1661}16621663// If address is already removed from allow list, nothing happens1664#[test]1665fn address_is_already_removed_from_allow_list() {1666 new_test_ext().execute_with(|| {1667 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1668 let origin1 = Origin::signed(1);16691670 assert_ok!(TemplateModule::add_to_allow_list(1671 origin1.clone(),1672 collection_id,1673 account(2)1674 ));1675 assert_ok!(TemplateModule::remove_from_allow_list(1676 origin1.clone(),1677 collection_id,1678 account(2)1679 ));1680 assert_eq!(1681 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1682 false1683 );1684 assert_ok!(TemplateModule::remove_from_allow_list(1685 origin1,1686 collection_id,1687 account(2)1688 ));1689 assert_eq!(1690 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1691 false1692 );1693 });1694}16951696// If Public Access mode is set to AllowList, tokens can’t be transferred from a non-allowlisted address with transfer or transferFrom (2 tests)1697#[test]1698fn allow_list_test_1() {1699 new_test_ext().execute_with(|| {1700 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17011702 let origin1 = Origin::signed(1);17031704 let data = default_nft_data();1705 create_test_item(collection_id, &data.into());17061707 assert_ok!(TemplateModule::set_public_access_mode(1708 origin1.clone(),1709 collection_id,1710 AccessMode::AllowList1711 ));1712 assert_ok!(TemplateModule::add_to_allow_list(1713 origin1.clone(),1714 collection_id,1715 account(2)1716 ));17171718 assert_noop!(1719 TemplateModule::transfer(origin1, account(3), CollectionId(1), TokenId(1), 1)1720 .map_err(|e| e.error),1721 CommonError::<Test>::AddressNotInAllowlist1722 );1723 });1724}17251726#[test]1727fn allow_list_test_2() {1728 new_test_ext().execute_with(|| {1729 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1730 let origin1 = Origin::signed(1);17311732 let data = default_nft_data();1733 create_test_item(collection_id, &data.into());17341735 assert_ok!(TemplateModule::set_public_access_mode(1736 origin1.clone(),1737 collection_id,1738 AccessMode::AllowList1739 ));1740 assert_ok!(TemplateModule::add_to_allow_list(1741 origin1.clone(),1742 collection_id,1743 account(1)1744 ));1745 assert_ok!(TemplateModule::add_to_allow_list(1746 origin1.clone(),1747 collection_id,1748 account(2)1749 ));17501751 // do approve1752 assert_ok!(TemplateModule::approve(1753 origin1.clone(),1754 account(1),1755 collection_id,1756 TokenId(1),1757 11758 ));1759 assert_eq!(1760 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1761 account(1)1762 );17631764 assert_ok!(TemplateModule::remove_from_allow_list(1765 origin1.clone(),1766 collection_id,1767 account(1)1768 ));17691770 assert_noop!(1771 TemplateModule::transfer_from(1772 origin1,1773 account(1),1774 account(3),1775 CollectionId(1),1776 TokenId(1),1777 11778 )1779 .map_err(|e| e.error),1780 CommonError::<Test>::AddressNotInAllowlist1781 );1782 });1783}17841785// If Public Access mode is set to AllowList, tokens can’t be transferred to a non-allowlisted address with transfer or transferFrom (2 tests)1786#[test]1787fn allow_list_test_3() {1788 new_test_ext().execute_with(|| {1789 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17901791 let origin1 = Origin::signed(1);17921793 let data = default_nft_data();1794 create_test_item(collection_id, &data.into());17951796 assert_ok!(TemplateModule::set_public_access_mode(1797 origin1.clone(),1798 collection_id,1799 AccessMode::AllowList1800 ));1801 assert_ok!(TemplateModule::add_to_allow_list(1802 origin1.clone(),1803 collection_id,1804 account(1)1805 ));18061807 assert_noop!(1808 TemplateModule::transfer(origin1, account(3), collection_id, TokenId(1), 1)1809 .map_err(|e| e.error),1810 CommonError::<Test>::AddressNotInAllowlist1811 );1812 });1813}18141815#[test]1816fn allow_list_test_4() {1817 new_test_ext().execute_with(|| {1818 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18191820 let origin1 = Origin::signed(1);18211822 let data = default_nft_data();1823 create_test_item(collection_id, &data.into());18241825 assert_ok!(TemplateModule::set_public_access_mode(1826 origin1.clone(),1827 collection_id,1828 AccessMode::AllowList1829 ));1830 assert_ok!(TemplateModule::add_to_allow_list(1831 origin1.clone(),1832 collection_id,1833 account(1)1834 ));1835 assert_ok!(TemplateModule::add_to_allow_list(1836 origin1.clone(),1837 collection_id,1838 account(2)1839 ));18401841 // do approve1842 assert_ok!(TemplateModule::approve(1843 origin1.clone(),1844 account(1),1845 collection_id,1846 TokenId(1),1847 11848 ));1849 assert_eq!(1850 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1851 account(1)1852 );18531854 assert_ok!(TemplateModule::remove_from_allow_list(1855 origin1.clone(),1856 collection_id,1857 account(2)1858 ));18591860 assert_noop!(1861 TemplateModule::transfer_from(1862 origin1,1863 account(1),1864 account(3),1865 collection_id,1866 TokenId(1),1867 11868 )1869 .map_err(|e| e.error),1870 CommonError::<Test>::AddressNotInAllowlist1871 );1872 });1873}18741875// If Public Access mode is set to AllowList, tokens can’t be destroyed by a non-allowlisted address (even if it owned them before enabling AllowList mode)1876#[test]1877fn allow_list_test_5() {1878 new_test_ext().execute_with(|| {1879 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18801881 let origin1 = Origin::signed(1);18821883 let data = default_nft_data();1884 create_test_item(collection_id, &data.into());18851886 assert_ok!(TemplateModule::set_public_access_mode(1887 origin1.clone(),1888 collection_id,1889 AccessMode::AllowList1890 ));1891 assert_noop!(1892 TemplateModule::burn_item(origin1.clone(), CollectionId(1), TokenId(1), 1)1893 .map_err(|e| e.error),1894 CommonError::<Test>::AddressNotInAllowlist1895 );1896 });1897}18981899// If Public Access mode is set to AllowList, token transfers can’t be Approved by a non-allowlisted address (see Approve method).1900#[test]1901fn allow_list_test_6() {1902 new_test_ext().execute_with(|| {1903 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19041905 let origin1 = Origin::signed(1);19061907 let data = default_nft_data();1908 create_test_item(collection_id, &data.into());19091910 assert_ok!(TemplateModule::set_public_access_mode(1911 origin1.clone(),1912 collection_id,1913 AccessMode::AllowList1914 ));19151916 // do approve1917 assert_noop!(1918 TemplateModule::approve(origin1, account(1), CollectionId(1), TokenId(1), 1)1919 .map_err(|e| e.error),1920 CommonError::<Test>::AddressNotInAllowlist1921 );1922 });1923}19241925// If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests) and1926// tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests)1927#[test]1928fn allow_list_test_7() {1929 new_test_ext().execute_with(|| {1930 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19311932 let data = default_nft_data();1933 create_test_item(collection_id, &data.into());19341935 let origin1 = Origin::signed(1);19361937 assert_ok!(TemplateModule::set_public_access_mode(1938 origin1.clone(),1939 collection_id,1940 AccessMode::AllowList1941 ));1942 assert_ok!(TemplateModule::add_to_allow_list(1943 origin1.clone(),1944 collection_id,1945 account(1)1946 ));1947 assert_ok!(TemplateModule::add_to_allow_list(1948 origin1.clone(),1949 collection_id,1950 account(2)1951 ));19521953 assert_ok!(TemplateModule::transfer(1954 origin1,1955 account(2),1956 CollectionId(1),1957 TokenId(1),1958 11959 ));1960 });1961}19621963#[test]1964fn allow_list_test_8() {1965 new_test_ext().execute_with(|| {1966 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19671968 // Create NFT for account 11969 let data = default_nft_data();1970 create_test_item(collection_id, &data.into());19711972 let origin1 = Origin::signed(1);19731974 // Toggle Allow List mode and add accounts 1 and 21975 assert_ok!(TemplateModule::set_public_access_mode(1976 origin1.clone(),1977 collection_id,1978 AccessMode::AllowList1979 ));1980 assert_ok!(TemplateModule::add_to_allow_list(1981 origin1.clone(),1982 collection_id,1983 account(1)1984 ));1985 assert_ok!(TemplateModule::add_to_allow_list(1986 origin1.clone(),1987 collection_id,1988 account(2)1989 ));19901991 // Sself-approve account 1 for NFT 11992 assert_ok!(TemplateModule::approve(1993 origin1.clone(),1994 account(1),1995 CollectionId(1),1996 TokenId(1),1997 11998 ));1999 assert_eq!(2000 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),2001 account(1)2002 );20032004 // Transfer from 1 to 22005 assert_ok!(TemplateModule::transfer_from(2006 origin1,2007 account(1),2008 account(2),2009 CollectionId(1),2010 TokenId(1),2011 12012 ));2013 });2014}20152016// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by owner.2017#[test]2018fn allow_list_test_9() {2019 new_test_ext().execute_with(|| {2020 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2021 let origin1 = Origin::signed(1);20222023 assert_ok!(TemplateModule::set_public_access_mode(2024 origin1.clone(),2025 collection_id,2026 AccessMode::AllowList2027 ));2028 assert_ok!(TemplateModule::set_mint_permission(2029 origin1,2030 collection_id,2031 false2032 ));20332034 let data = default_nft_data();2035 create_test_item(collection_id, &data.into());2036 });2037}20382039// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by admin.2040#[test]2041fn allow_list_test_10() {2042 new_test_ext().execute_with(|| {2043 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20442045 let origin1 = Origin::signed(1);2046 let origin2 = Origin::signed(2);20472048 assert_ok!(TemplateModule::set_public_access_mode(2049 origin1.clone(),2050 collection_id,2051 AccessMode::AllowList2052 ));2053 assert_ok!(TemplateModule::set_mint_permission(2054 origin1.clone(),2055 collection_id,2056 false2057 ));20582059 assert_ok!(TemplateModule::add_collection_admin(2060 origin1,2061 collection_id,2062 account(2)2063 ));20642065 assert_ok!(TemplateModule::create_item(2066 origin2,2067 collection_id,2068 account(2),2069 default_nft_data().into()2070 ));2071 });2072}20732074// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and allow listed address.2075#[test]2076fn allow_list_test_11() {2077 new_test_ext().execute_with(|| {2078 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20792080 let origin1 = Origin::signed(1);2081 let origin2 = Origin::signed(2);20822083 assert_ok!(TemplateModule::set_public_access_mode(2084 origin1.clone(),2085 collection_id,2086 AccessMode::AllowList2087 ));2088 assert_ok!(TemplateModule::set_mint_permission(2089 origin1.clone(),2090 collection_id,2091 false2092 ));2093 assert_ok!(TemplateModule::add_to_allow_list(2094 origin1,2095 collection_id,2096 account(2)2097 ));20982099 assert_noop!(2100 TemplateModule::create_item(2101 origin2,2102 CollectionId(1),2103 account(2),2104 default_nft_data().into()2105 )2106 .map_err(|e| e.error),2107 CommonError::<Test>::PublicMintingNotAllowed2108 );2109 });2110}21112112// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-allow listed address.2113#[test]2114fn allow_list_test_12() {2115 new_test_ext().execute_with(|| {2116 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21172118 let origin1 = Origin::signed(1);2119 let origin2 = Origin::signed(2);21202121 assert_ok!(TemplateModule::set_public_access_mode(2122 origin1.clone(),2123 collection_id,2124 AccessMode::AllowList2125 ));2126 assert_ok!(TemplateModule::set_mint_permission(2127 origin1,2128 collection_id,2129 false2130 ));21312132 assert_noop!(2133 TemplateModule::create_item(2134 origin2,2135 CollectionId(1),2136 account(2),2137 default_nft_data().into()2138 )2139 .map_err(|e| e.error),2140 CommonError::<Test>::PublicMintingNotAllowed2141 );2142 });2143}21442145// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by owner.2146#[test]2147fn allow_list_test_13() {2148 new_test_ext().execute_with(|| {2149 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21502151 let origin1 = Origin::signed(1);21522153 assert_ok!(TemplateModule::set_public_access_mode(2154 origin1.clone(),2155 collection_id,2156 AccessMode::AllowList2157 ));2158 assert_ok!(TemplateModule::set_mint_permission(2159 origin1,2160 collection_id,2161 true2162 ));21632164 let data = default_nft_data();2165 create_test_item(collection_id, &data.into());2166 });2167}21682169// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by admin.2170#[test]2171fn allow_list_test_14() {2172 new_test_ext().execute_with(|| {2173 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21742175 let origin1 = Origin::signed(1);2176 let origin2 = Origin::signed(2);21772178 assert_ok!(TemplateModule::set_public_access_mode(2179 origin1.clone(),2180 collection_id,2181 AccessMode::AllowList2182 ));2183 assert_ok!(TemplateModule::set_mint_permission(2184 origin1.clone(),2185 collection_id,2186 true2187 ));21882189 assert_ok!(TemplateModule::add_collection_admin(2190 origin1,2191 collection_id,2192 account(2)2193 ));21942195 assert_ok!(TemplateModule::create_item(2196 origin2,2197 collection_id,2198 account(2),2199 default_nft_data().into()2200 ));2201 });2202}22032204// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-allow listed address.2205#[test]2206fn allow_list_test_15() {2207 new_test_ext().execute_with(|| {2208 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22092210 let origin1 = Origin::signed(1);2211 let origin2 = Origin::signed(2);22122213 assert_ok!(TemplateModule::set_public_access_mode(2214 origin1.clone(),2215 collection_id,2216 AccessMode::AllowList2217 ));2218 assert_ok!(TemplateModule::set_mint_permission(2219 origin1,2220 collection_id,2221 true2222 ));22232224 assert_noop!(2225 TemplateModule::create_item(2226 origin2,2227 collection_id,2228 account(2),2229 default_nft_data().into()2230 )2231 .map_err(|e| e.error),2232 CommonError::<Test>::AddressNotInAllowlist2233 );2234 });2235}22362237// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by non-privileged and allow listed address.2238#[test]2239fn allow_list_test_16() {2240 new_test_ext().execute_with(|| {2241 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22422243 let origin1 = Origin::signed(1);2244 let origin2 = Origin::signed(2);22452246 assert_ok!(TemplateModule::set_public_access_mode(2247 origin1.clone(),2248 collection_id,2249 AccessMode::AllowList2250 ));2251 assert_ok!(TemplateModule::set_mint_permission(2252 origin1.clone(),2253 collection_id,2254 true2255 ));2256 assert_ok!(TemplateModule::add_to_allow_list(2257 origin1,2258 collection_id,2259 account(2)2260 ));22612262 assert_ok!(TemplateModule::create_item(2263 origin2,2264 collection_id,2265 account(2),2266 default_nft_data().into()2267 ));2268 });2269}22702271// Total number of collections. Positive test2272#[test]2273fn total_number_collections_bound() {2274 new_test_ext().execute_with(|| {2275 create_test_collection(&CollectionMode::NFT, CollectionId(1));2276 });2277}22782279#[test]2280fn create_max_collections() {2281 new_test_ext().execute_with(|| {2282 for i in 1..COLLECTION_NUMBER_LIMIT {2283 create_test_collection(&CollectionMode::NFT, CollectionId(i));2284 }2285 });2286}22872288// Total number of collections. Negative test2289#[test]2290fn total_number_collections_bound_neg() {2291 new_test_ext().execute_with(|| {2292 let origin1 = Origin::signed(1);22932294 for i in 1..=COLLECTION_NUMBER_LIMIT {2295 create_test_collection(&CollectionMode::NFT, CollectionId(i));2296 }22972298 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();2299 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();2300 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();23012302 // 11-th collection in chain. Expects error2303 assert_noop!(2304 TemplateModule::create_collection(2305 origin1,2306 col_name1.try_into().unwrap(),2307 col_desc1.try_into().unwrap(),2308 token_prefix1.try_into().unwrap(),2309 CollectionMode::NFT2310 ),2311 CommonError::<Test>::TotalCollectionsLimitExceeded2312 );2313 });2314}23152316// Owned tokens by a single address. Positive test2317#[test]2318fn owned_tokens_bound() {2319 new_test_ext().execute_with(|| {2320 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23212322 let data = default_nft_data();2323 create_test_item(collection_id, &data.clone().into());2324 create_test_item(collection_id, &data.into());2325 });2326}23272328// Owned tokens by a single address. Negotive test2329#[test]2330fn owned_tokens_bound_neg() {2331 new_test_ext().execute_with(|| {2332 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23332334 let origin1 = Origin::signed(1);23352336 for _ in 1..=MAX_TOKEN_OWNERSHIP {2337 let data = default_nft_data();2338 create_test_item(collection_id, &data.clone().into());2339 }23402341 let data = default_nft_data();2342 assert_noop!(2343 TemplateModule::create_item(origin1, CollectionId(1), account(1), data.into())2344 .map_err(|e| e.error),2345 CommonError::<Test>::AccountTokenLimitExceeded2346 );2347 });2348}23492350// Number of collection admins. Positive test2351#[test]2352fn collection_admins_bound() {2353 new_test_ext().execute_with(|| {2354 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23552356 let origin1 = Origin::signed(1);23572358 assert_ok!(TemplateModule::add_collection_admin(2359 origin1.clone(),2360 collection_id,2361 account(2)2362 ));2363 assert_ok!(TemplateModule::add_collection_admin(2364 origin1,2365 collection_id,2366 account(3)2367 ));2368 });2369}23702371// Number of collection admins. Negotive test2372#[test]2373fn collection_admins_bound_neg() {2374 new_test_ext().execute_with(|| {2375 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23762377 let origin1 = Origin::signed(1);23782379 for i in 0..COLLECTION_ADMINS_LIMIT {2380 assert_ok!(TemplateModule::add_collection_admin(2381 origin1.clone(),2382 collection_id,2383 account((2 + i).into())2384 ));2385 }2386 assert_noop!(2387 TemplateModule::add_collection_admin(2388 origin1,2389 collection_id,2390 account((3 + COLLECTION_ADMINS_LIMIT).into())2391 ),2392 CommonError::<Test>::CollectionAdminCountExceeded2393 );2394 });2395}2396// #endregion23972398#[test]2399fn set_const_on_chain_schema() {2400 new_test_ext().execute_with(|| {2401 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24022403 let origin1 = Origin::signed(1);2404 assert_ok!(TemplateModule::set_const_on_chain_schema(2405 origin1,2406 collection_id,2407 b"test const on chain schema".to_vec().try_into().unwrap()2408 ));24092410 assert_eq!(2411 <pallet_common::CollectionById<Test>>::get(collection_id)2412 .unwrap()2413 .const_on_chain_schema,2414 b"test const on chain schema".to_vec()2415 );2416 assert_eq!(2417 <pallet_common::CollectionById<Test>>::get(collection_id)2418 .unwrap()2419 .variable_on_chain_schema,2420 b"".to_vec()2421 );2422 });2423}24242425#[test]2426fn set_variable_on_chain_schema() {2427 new_test_ext().execute_with(|| {2428 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24292430 let origin1 = Origin::signed(1);2431 assert_ok!(TemplateModule::set_variable_on_chain_schema(2432 origin1,2433 collection_id,2434 b"test variable on chain schema"2435 .to_vec()2436 .try_into()2437 .unwrap()2438 ));24392440 assert_eq!(2441 <pallet_common::CollectionById<Test>>::get(collection_id)2442 .unwrap()2443 .const_on_chain_schema,2444 b"".to_vec()2445 );2446 assert_eq!(2447 <pallet_common::CollectionById<Test>>::get(collection_id)2448 .unwrap()2449 .variable_on_chain_schema,2450 b"test variable on chain schema".to_vec()2451 );2452 });2453}24542455#[test]2456fn set_variable_meta_data_on_nft_token_stores_variable_meta_data() {2457 new_test_ext().execute_with(|| {2458 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24592460 let origin1 = Origin::signed(1);24612462 let data = default_nft_data();2463 create_test_item(CollectionId(1), &data.into());24642465 let variable_data = b"test data".to_vec();2466 assert_ok!(TemplateModule::set_variable_meta_data(2467 origin1,2468 collection_id,2469 TokenId(1),2470 variable_data.clone().try_into().unwrap()2471 ));24722473 assert_eq!(2474 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2475 .unwrap()2476 .variable_data,2477 variable_data2478 );2479 });2480}24812482#[test]2483fn set_variable_meta_data_on_re_fungible_token_stores_variable_meta_data() {2484 new_test_ext().execute_with(|| {2485 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));24862487 let origin1 = Origin::signed(1);24882489 let data = default_re_fungible_data();2490 create_test_item(collection_id, &data.into());24912492 let variable_data = b"test data".to_vec();2493 assert_ok!(TemplateModule::set_variable_meta_data(2494 origin1,2495 collection_id,2496 TokenId(1),2497 variable_data.clone().try_into().unwrap()2498 ));24992500 assert_eq!(2501 <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1))).variable_data,2502 variable_data2503 );2504 });2505}25062507#[test]2508fn set_variable_meta_data_on_fungible_token_fails() {2509 new_test_ext().execute_with(|| {2510 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));25112512 let origin1 = Origin::signed(1);25132514 let data = default_fungible_data();2515 create_test_item(collection_id, &data.into());25162517 let variable_data = b"test data".to_vec();2518 assert_noop!(2519 TemplateModule::set_variable_meta_data(2520 origin1,2521 collection_id,2522 TokenId(0),2523 variable_data.try_into().unwrap()2524 )2525 .map_err(|e| e.error),2526 <pallet_fungible::Error<Test>>::FungibleItemsDontHaveData2527 );2528 });2529}25302531#[test]2532fn set_variable_meta_data_on_nft_with_item_owner_permission_flag() {2533 new_test_ext().execute_with(|| {2534 //default_limits();25352536 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));25372538 let origin1 = Origin::signed(1);25392540 let data = default_nft_data();2541 create_test_item(collection_id, &data.into());25422543 assert_ok!(TemplateModule::set_meta_update_permission_flag(2544 origin1.clone(),2545 collection_id,2546 MetaUpdatePermission::ItemOwner,2547 ));25482549 let variable_data = b"ten chars.".to_vec();2550 assert_ok!(TemplateModule::set_variable_meta_data(2551 origin1,2552 collection_id,2553 TokenId(1),2554 variable_data.clone().try_into().unwrap()2555 ));25562557 assert_eq!(2558 <pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))2559 .unwrap()2560 .variable_data,2561 variable_data2562 );2563 });2564}25652566#[test]2567fn collection_transfer_flag_works() {2568 new_test_ext().execute_with(|| {2569 let origin1 = Origin::signed(1);25702571 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2572 assert_ok!(TemplateModule::set_transfers_enabled_flag(2573 origin1,2574 collection_id,2575 true2576 ));25772578 let data = default_nft_data();2579 create_test_item(collection_id, &data.into());2580 assert_eq!(2581 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2582 12583 );2584 assert_eq!(2585 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2586 true2587 );25882589 let origin1 = Origin::signed(1);25902591 // default scenario2592 assert_ok!(TemplateModule::transfer(2593 origin1,2594 account(2),2595 collection_id,2596 TokenId(1),2597 12598 ));2599 assert_eq!(2600 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2601 false2602 );2603 assert_eq!(2604 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2605 true2606 );2607 assert_eq!(2608 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2609 02610 );2611 assert_eq!(2612 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2613 12614 );2615 });2616}26172618#[test]2619fn set_variable_meta_data_on_nft_with_admin_flag() {2620 new_test_ext().execute_with(|| {2621 // default_limits();26222623 let collection_id =2624 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26252626 let origin1 = Origin::signed(1);2627 let origin2 = Origin::signed(2);26282629 assert_ok!(TemplateModule::set_mint_permission(2630 origin2.clone(),2631 collection_id,2632 true2633 ));2634 assert_ok!(TemplateModule::add_to_allow_list(2635 origin2.clone(),2636 collection_id,2637 account(1)2638 ));26392640 assert_ok!(TemplateModule::add_collection_admin(2641 origin2.clone(),2642 collection_id,2643 account(1)2644 ));26452646 let data = default_nft_data();2647 create_test_item(collection_id, &data.into());26482649 assert_ok!(TemplateModule::set_meta_update_permission_flag(2650 origin2.clone(),2651 collection_id,2652 MetaUpdatePermission::Admin,2653 ));26542655 let variable_data = b"test.".to_vec();2656 assert_ok!(TemplateModule::set_variable_meta_data(2657 origin1,2658 collection_id,2659 TokenId(1),2660 variable_data.clone().try_into().unwrap()2661 ));26622663 assert_eq!(2664 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2665 .unwrap()2666 .variable_data,2667 variable_data2668 );2669 });2670}26712672#[test]2673fn set_variable_meta_data_on_nft_with_admin_flag_neg() {2674 new_test_ext().execute_with(|| {2675 // default_limits();26762677 let collection_id =2678 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26792680 let origin1 = Origin::signed(1);2681 let origin2 = Origin::signed(2);26822683 assert_ok!(TemplateModule::set_mint_permission(2684 origin2.clone(),2685 collection_id,2686 true2687 ));2688 assert_ok!(TemplateModule::add_to_allow_list(2689 origin2.clone(),2690 collection_id,2691 account(1)2692 ));26932694 let data = default_nft_data();2695 create_test_item(collection_id, &data.into());26962697 assert_ok!(TemplateModule::set_meta_update_permission_flag(2698 origin2.clone(),2699 collection_id,2700 MetaUpdatePermission::Admin,2701 ));27022703 let variable_data = b"test.".to_vec();2704 assert_noop!(2705 TemplateModule::set_variable_meta_data(2706 origin1,2707 collection_id,2708 TokenId(1),2709 variable_data.try_into().unwrap()2710 )2711 .map_err(|e| e.error),2712 CommonError::<Test>::NoPermission2713 );2714 });2715}27162717#[test]2718fn set_variable_meta_flag_after_freeze() {2719 new_test_ext().execute_with(|| {2720 // default_limits();27212722 let collection_id =2723 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));27242725 let origin2 = Origin::signed(2);27262727 assert_ok!(TemplateModule::set_meta_update_permission_flag(2728 origin2.clone(),2729 collection_id,2730 MetaUpdatePermission::None,2731 ));2732 assert_noop!(2733 TemplateModule::set_meta_update_permission_flag(2734 origin2.clone(),2735 collection_id,2736 MetaUpdatePermission::Admin2737 ),2738 CommonError::<Test>::MetadataFlagFrozen2739 );2740 });2741}27422743#[test]2744fn set_variable_meta_data_on_nft_with_none_flag_neg() {2745 new_test_ext().execute_with(|| {2746 // default_limits();27472748 let collection_id =2749 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));2750 let origin1 = Origin::signed(1);27512752 let data = default_nft_data();2753 create_test_item(collection_id, &data.into());27542755 assert_ok!(TemplateModule::set_meta_update_permission_flag(2756 origin1.clone(),2757 collection_id,2758 MetaUpdatePermission::None,2759 ));27602761 let variable_data = b"test.".to_vec();2762 assert_noop!(2763 TemplateModule::set_variable_meta_data(2764 origin1.clone(),2765 collection_id,2766 TokenId(1),2767 variable_data.try_into().unwrap()2768 )2769 .map_err(|e| e.error),2770 CommonError::<Test>::NoPermission2771 );2772 });2773}27742775#[test]2776fn collection_transfer_flag_works_neg() {2777 new_test_ext().execute_with(|| {2778 let origin1 = Origin::signed(1);27792780 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2781 assert_ok!(TemplateModule::set_transfers_enabled_flag(2782 origin1,2783 collection_id,2784 false2785 ));27862787 let data = default_nft_data();2788 create_test_item(collection_id, &data.into());2789 assert_eq!(2790 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2791 12792 );2793 assert_eq!(2794 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2795 true2796 );27972798 let origin1 = Origin::signed(1);27992800 // default scenario2801 assert_noop!(2802 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 1)2803 .map_err(|e| e.error),2804 CommonError::<Test>::TransferNotAllowed2805 );2806 assert_eq!(2807 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2808 12809 );2810 assert_eq!(2811 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2812 02813 );2814 assert_eq!(2815 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2816 true2817 );2818 assert_eq!(2819 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2820 false2821 );2822 });2823}1// Tests to be written here2use super::*;3use crate::mock::*;4use crate::{AccessMode, CollectionMode};5use up_data_structs::{6 COLLECTION_NUMBER_LIMIT, CollectionId, CreateItemData, CreateFungibleData, CreateNftData,7 CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission,8 TokenId, MAX_TOKEN_OWNERSHIP,9};10use frame_support::{assert_noop, assert_ok, assert_err};11use sp_std::convert::TryInto;12use pallet_balances;1314fn add_balance(user: u64, value: u64) {15 const RICH_USER: u64 = 999;16 assert_ok!(<pallet_balances::Pallet<Test>>::set_balance(17 Origin::root(),18 RICH_USER,19 value,20 021 ));22 assert_ok!(<pallet_balances::Pallet<Test>>::force_transfer(23 Origin::root(),24 RICH_USER,25 user,26 value27 ));28}2930fn default_nft_data() -> CreateNftData {31 CreateNftData {32 const_data: vec![1, 2, 3].try_into().unwrap(),33 variable_data: vec![3, 2, 1].try_into().unwrap(),34 }35}3637fn default_fungible_data() -> CreateFungibleData {38 CreateFungibleData { value: 5 }39}4041fn default_re_fungible_data() -> CreateReFungibleData {42 CreateReFungibleData {43 const_data: vec![1, 2, 3].try_into().unwrap(),44 variable_data: vec![3, 2, 1].try_into().unwrap(),45 pieces: 1023,46 }47}4849fn create_test_collection_for_owner(50 mode: &CollectionMode,51 owner: u64,52 id: CollectionId,53) -> CollectionId {54 add_balance(owner, CollectionCreationPrice::get() as u64 + 1);5556 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();57 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();58 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();5960 let origin1 = Origin::signed(owner);61 assert_ok!(TemplateModule::create_collection(62 origin1,63 col_name1.try_into().unwrap(),64 col_desc1.try_into().unwrap(),65 token_prefix1.try_into().unwrap(),66 mode.clone()67 ));6869 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();70 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();71 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();72 assert_eq!(73 <pallet_common::CollectionById<Test>>::get(id)74 .unwrap()75 .owner,76 owner77 );78 assert_eq!(79 <pallet_common::CollectionById<Test>>::get(id).unwrap().name,80 saved_col_name81 );82 assert_eq!(83 <pallet_common::CollectionById<Test>>::get(id).unwrap().mode,84 *mode85 );86 assert_eq!(87 <pallet_common::CollectionById<Test>>::get(id)88 .unwrap()89 .description,90 saved_description91 );92 assert_eq!(93 <pallet_common::CollectionById<Test>>::get(id)94 .unwrap()95 .token_prefix,96 saved_prefix97 );98 id99}100101fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {102 create_test_collection_for_owner(&mode, 1, id)103}104105fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {106 let origin1 = Origin::signed(1);107 assert_ok!(TemplateModule::create_item(108 origin1,109 collection_id,110 account(1),111 data.clone()112 ));113}114115fn account(sub: u64) -> TestCrossAccountId {116 TestCrossAccountId::from_sub(sub)117}118119// Use cases tests region120// #region121122#[test]123fn set_version_schema() {124 new_test_ext().execute_with(|| {125 let origin1 = Origin::signed(1);126 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));127128 assert_ok!(TemplateModule::set_schema_version(129 origin1,130 collection_id,131 SchemaVersion::Unique132 ));133 assert_eq!(134 <pallet_common::CollectionById<Test>>::get(collection_id)135 .unwrap()136 .schema_version,137 SchemaVersion::Unique138 );139 });140}141142#[test]143fn check_not_sufficient_founds() {144 new_test_ext().execute_with(|| {145 let acc: u64 = 1;146 <pallet_balances::Pallet<Test>>::set_balance(Origin::root(), acc, 0, 0).unwrap();147148 let name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();149 let description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();150 let token_prefix: Vec<u8> = b"token_prefix1\0".to_vec();151152 let data: CreateCollectionData<<Test as system::Config>::AccountId> =153 CreateCollectionData {154 name: name.try_into().unwrap(),155 description: description.try_into().unwrap(),156 token_prefix: token_prefix.try_into().unwrap(),157 mode: CollectionMode::NFT,158 ..Default::default()159 };160161 let result = TemplateModule::create_collection_ex(Origin::signed(acc), data);162 assert_err!(result, <CommonError<Test>>::NotSufficientFounds);163 });164}165166#[test]167fn create_fungible_collection_fails_with_large_decimal_numbers() {168 new_test_ext().execute_with(|| {169 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();170 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();171 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();172173 let origin1 = Origin::signed(1);174 assert_noop!(175 TemplateModule::create_collection(176 origin1,177 col_name1.try_into().unwrap(),178 col_desc1.try_into().unwrap(),179 token_prefix1.try_into().unwrap(),180 CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1)181 ),182 Error::<Test>::CollectionDecimalPointLimitExceeded183 );184 });185}186187#[test]188fn create_nft_item() {189 new_test_ext().execute_with(|| {190 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));191192 let data = default_nft_data();193 create_test_item(collection_id, &data.clone().into());194195 let item = <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1)).unwrap();196 assert_eq!(item.const_data, data.const_data.into_inner());197 assert_eq!(item.variable_data, data.variable_data.into_inner());198 });199}200201// Use cases tests region202// #region203#[test]204fn create_nft_multiple_items() {205 new_test_ext().execute_with(|| {206 create_test_collection(&CollectionMode::NFT, CollectionId(1));207208 let origin1 = Origin::signed(1);209210 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];211212 assert_ok!(TemplateModule::create_multiple_items(213 origin1,214 CollectionId(1),215 account(1),216 items_data217 .clone()218 .into_iter()219 .map(|d| { d.into() })220 .collect()221 ));222 for (index, data) in items_data.into_iter().enumerate() {223 let item = <pallet_nonfungible::TokenData<Test>>::get((224 CollectionId(1),225 TokenId((index + 1) as u32),226 ))227 .unwrap();228 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());229 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());230 }231 });232}233234#[test]235fn create_refungible_item() {236 new_test_ext().execute_with(|| {237 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));238239 let data = default_re_fungible_data();240 create_test_item(collection_id, &data.clone().into());241 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));242 let balance =243 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));244 assert_eq!(item.const_data, data.const_data.into_inner());245 assert_eq!(item.variable_data, data.variable_data.into_inner());246 assert_eq!(balance, 1023);247 });248}249250#[test]251fn create_multiple_refungible_items() {252 new_test_ext().execute_with(|| {253 create_test_collection(&CollectionMode::ReFungible, CollectionId(1));254255 let origin1 = Origin::signed(1);256257 let items_data = vec![258 default_re_fungible_data(),259 default_re_fungible_data(),260 default_re_fungible_data(),261 ];262263 assert_ok!(TemplateModule::create_multiple_items(264 origin1,265 CollectionId(1),266 account(1),267 items_data268 .clone()269 .into_iter()270 .map(|d| { d.into() })271 .collect()272 ));273 for (index, data) in items_data.into_iter().enumerate() {274 let item = <pallet_refungible::TokenData<Test>>::get((275 CollectionId(1),276 TokenId((index + 1) as u32),277 ));278 let balance =279 <pallet_refungible::Balance<Test>>::get((CollectionId(1), TokenId(1), account(1)));280 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());281 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());282 assert_eq!(balance, 1023);283 }284 });285}286287#[test]288fn create_fungible_item() {289 new_test_ext().execute_with(|| {290 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));291292 let data = default_fungible_data();293 create_test_item(collection_id, &data.into());294295 assert_eq!(296 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),297 5298 );299 });300}301302//#[test]303// fn create_multiple_fungible_items() {304// new_test_ext().execute_with(|| {305// default_limits();306307// create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));308309// let origin1 = Origin::signed(1);310311// let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];312313// assert_ok!(TemplateModule::create_multiple_items(314// origin1.clone(),315// 1,316// 1,317// items_data.clone().into_iter().map(|d| { d.into() }).collect()318// ));319320// for (index, _) in items_data.iter().enumerate() {321// assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).value, 5);322// }323// assert_eq!(TemplateModule::balance_count(1, 1), 3000);324// assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);325// });326// }327328#[test]329fn transfer_fungible_item() {330 new_test_ext().execute_with(|| {331 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));332333 let origin1 = Origin::signed(1);334 let origin2 = Origin::signed(2);335336 let data = default_fungible_data();337 create_test_item(collection_id, &data.into());338339 assert_eq!(340 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),341 5342 );343344 // change owner scenario345 assert_ok!(TemplateModule::transfer(346 origin1,347 account(2),348 CollectionId(1),349 TokenId(0),350 5351 ));352 assert_eq!(353 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),354 0355 );356357 // split item scenario358 assert_ok!(TemplateModule::transfer(359 origin2.clone(),360 account(3),361 CollectionId(1),362 TokenId(0),363 3364 ));365366 // split item and new owner has account scenario367 assert_ok!(TemplateModule::transfer(368 origin2,369 account(3),370 CollectionId(1),371 TokenId(0),372 1373 ));374 assert_eq!(375 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(2))),376 1377 );378 assert_eq!(379 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(3))),380 4381 );382 });383}384385#[test]386fn transfer_refungible_item() {387 new_test_ext().execute_with(|| {388 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));389390 // Create RFT 1 in 1023 pieces for account 1391 let data = default_re_fungible_data();392 create_test_item(collection_id, &data.clone().into());393 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));394 assert_eq!(item.const_data, data.const_data.into_inner());395 assert_eq!(item.variable_data, data.variable_data.into_inner());396 assert_eq!(397 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),398 1399 );400 assert_eq!(401 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),402 1023403 );404 assert_eq!(405 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),406 true407 );408409 // Account 1 transfers all 1023 pieces of RFT 1 to account 2410 let origin1 = Origin::signed(1);411 let origin2 = Origin::signed(2);412 assert_ok!(TemplateModule::transfer(413 origin1,414 account(2),415 CollectionId(1),416 TokenId(1),417 1023418 ));419 assert_eq!(420 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),421 1023422 );423 assert_eq!(424 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),425 0426 );427 assert_eq!(428 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),429 1430 );431 assert_eq!(432 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),433 false434 );435 assert_eq!(436 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),437 true438 );439440 // Account 2 transfers 500 pieces of RFT 1 to account 3441 assert_ok!(TemplateModule::transfer(442 origin2.clone(),443 account(3),444 CollectionId(1),445 TokenId(1),446 500447 ));448 assert_eq!(449 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),450 523451 );452 assert_eq!(453 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),454 500455 );456 assert_eq!(457 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),458 1459 );460 assert_eq!(461 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),462 1463 );464 assert_eq!(465 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),466 true467 );468 assert_eq!(469 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),470 true471 );472473 // Account 2 transfers 200 more pieces of RFT 1 to account 3 with pre-existing balance474 assert_ok!(TemplateModule::transfer(475 origin2,476 account(3),477 CollectionId(1),478 TokenId(1),479 200480 ));481 assert_eq!(482 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),483 323484 );485 assert_eq!(486 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),487 700488 );489 assert_eq!(490 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),491 1492 );493 assert_eq!(494 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),495 1496 );497 assert_eq!(498 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),499 true500 );501 assert_eq!(502 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),503 true504 );505 });506}507508#[test]509fn transfer_nft_item() {510 new_test_ext().execute_with(|| {511 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));512513 let data = default_nft_data();514 create_test_item(collection_id, &data.into());515 assert_eq!(516 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),517 1518 );519 assert_eq!(520 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),521 true522 );523524 let origin1 = Origin::signed(1);525 // default scenario526 assert_ok!(TemplateModule::transfer(527 origin1,528 account(2),529 CollectionId(1),530 TokenId(1),531 1532 ));533 assert_eq!(534 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),535 0536 );537 assert_eq!(538 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),539 1540 );541 assert_eq!(542 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),543 false544 );545 assert_eq!(546 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),547 true548 );549 });550}551552#[test]553fn transfer_nft_item_wrong_value() {554 new_test_ext().execute_with(|| {555 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));556557 let data = default_nft_data();558 create_test_item(collection_id, &data.into());559 assert_eq!(560 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),561 1562 );563 assert_eq!(564 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),565 true566 );567568 let origin1 = Origin::signed(1);569570 assert_noop!(571 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 2)572 .map_err(|e| e.error),573 <pallet_nonfungible::Error::<Test>>::NonfungibleItemsHaveNoAmount574 );575 });576}577578#[test]579fn transfer_nft_item_zero_value() {580 new_test_ext().execute_with(|| {581 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));582583 let data = default_nft_data();584 create_test_item(collection_id, &data.into());585 assert_eq!(586 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),587 1588 );589 assert_eq!(590 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),591 true592 );593594 let origin1 = Origin::signed(1);595596 // Transferring 0 amount works on NFT...597 assert_ok!(TemplateModule::transfer(598 origin1,599 account(2),600 CollectionId(1),601 TokenId(1),602 0603 ));604 // ... and results in no transfer605 assert_eq!(606 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),607 1608 );609 assert_eq!(610 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),611 true612 );613 });614}615616#[test]617fn nft_approve_and_transfer_from() {618 new_test_ext().execute_with(|| {619 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));620621 let data = default_nft_data();622 create_test_item(collection_id, &data.into());623624 let origin1 = Origin::signed(1);625 let origin2 = Origin::signed(2);626627 assert_eq!(628 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),629 1630 );631 assert_eq!(632 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),633 true634 );635636 // neg transfer_from637 assert_noop!(638 TemplateModule::transfer_from(639 origin2.clone(),640 account(1),641 account(2),642 CollectionId(1),643 TokenId(1),644 1645 )646 .map_err(|e| e.error),647 CommonError::<Test>::ApprovedValueTooLow648 );649650 // do approve651 assert_ok!(TemplateModule::approve(652 origin1,653 account(2),654 CollectionId(1),655 TokenId(1),656 1657 ));658 assert_eq!(659 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),660 account(2)661 );662663 assert_ok!(TemplateModule::transfer_from(664 origin2,665 account(1),666 account(3),667 CollectionId(1),668 TokenId(1),669 1670 ));671 assert!(672 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()673 );674 });675}676677#[test]678fn nft_approve_and_transfer_from_allow_list() {679 new_test_ext().execute_with(|| {680 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));681682 let origin1 = Origin::signed(1);683 let origin2 = Origin::signed(2);684685 // Create NFT 1 for account 1686 let data = default_nft_data();687 create_test_item(collection_id, &data.clone().into());688 assert_eq!(689 &<pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))690 .unwrap()691 .const_data,692 &data.const_data.into_inner()693 );694 assert_eq!(695 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),696 1697 );698 assert_eq!(699 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),700 true701 );702703 // Allow allow-list users to mint and add accounts 1, 2, and 3 to allow-list704 assert_ok!(TemplateModule::set_mint_permission(705 origin1.clone(),706 CollectionId(1),707 true708 ));709 assert_ok!(TemplateModule::set_public_access_mode(710 origin1.clone(),711 CollectionId(1),712 AccessMode::AllowList713 ));714 assert_ok!(TemplateModule::add_to_allow_list(715 origin1.clone(),716 CollectionId(1),717 account(1)718 ));719 assert_ok!(TemplateModule::add_to_allow_list(720 origin1.clone(),721 CollectionId(1),722 account(2)723 ));724 assert_ok!(TemplateModule::add_to_allow_list(725 origin1.clone(),726 CollectionId(1),727 account(3)728 ));729730 // Account 1 approves account 2 for NFT 1731 assert_ok!(TemplateModule::approve(732 origin1.clone(),733 account(2),734 CollectionId(1),735 TokenId(1),736 1737 ));738 assert_eq!(739 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),740 account(2)741 );742743 // Account 2 transfers NFT 1 from account 1 to account 3744 assert_ok!(TemplateModule::transfer_from(745 origin2,746 account(1),747 account(3),748 CollectionId(1),749 TokenId(1),750 1751 ));752 assert!(753 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()754 );755 });756}757758#[test]759fn refungible_approve_and_transfer_from() {760 new_test_ext().execute_with(|| {761 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));762763 let origin1 = Origin::signed(1);764 let origin2 = Origin::signed(2);765766 // Create RFT 1 in 1023 pieces for account 1767 let data = default_re_fungible_data();768 create_test_item(collection_id, &data.into());769770 assert_eq!(771 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),772 1773 );774 assert_eq!(775 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),776 1023777 );778 assert_eq!(779 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),780 true781 );782783 // Allow public minting, enable allow-list and add accounts 1, 2, 3 to allow-list784 assert_ok!(TemplateModule::set_mint_permission(785 origin1.clone(),786 CollectionId(1),787 true788 ));789 assert_ok!(TemplateModule::set_public_access_mode(790 origin1.clone(),791 CollectionId(1),792 AccessMode::AllowList793 ));794 assert_ok!(TemplateModule::add_to_allow_list(795 origin1.clone(),796 CollectionId(1),797 account(1)798 ));799 assert_ok!(TemplateModule::add_to_allow_list(800 origin1.clone(),801 CollectionId(1),802 account(2)803 ));804 assert_ok!(TemplateModule::add_to_allow_list(805 origin1.clone(),806 CollectionId(1),807 account(3)808 ));809810 // Account 1 approves account 2 for 1023 pieces of RFT 1811 assert_ok!(TemplateModule::approve(812 origin1,813 account(2),814 CollectionId(1),815 TokenId(1),816 1023817 ));818 assert_eq!(819 <pallet_refungible::Allowance<Test>>::get((820 CollectionId(1),821 TokenId(1),822 account(1),823 account(2)824 )),825 1023826 );827828 // Account 2 transfers 100 pieces of RFT 1 from account 1 to account 3829 assert_ok!(TemplateModule::transfer_from(830 origin2,831 account(1),832 account(3),833 CollectionId(1),834 TokenId(1),835 100836 ));837 assert_eq!(838 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),839 1840 );841 assert_eq!(842 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),843 1844 );845 assert_eq!(846 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),847 923848 );849 assert_eq!(850 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),851 100852 );853 assert_eq!(854 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),855 true856 );857 assert_eq!(858 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),859 true860 );861 assert_eq!(862 <pallet_refungible::Allowance<Test>>::get((863 CollectionId(1),864 TokenId(1),865 account(1),866 account(2)867 )),868 923869 );870 });871}872873#[test]874fn fungible_approve_and_transfer_from() {875 new_test_ext().execute_with(|| {876 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));877878 let data = default_fungible_data();879 create_test_item(collection_id, &data.into());880881 let origin1 = Origin::signed(1);882 let origin2 = Origin::signed(2);883884 assert_ok!(TemplateModule::set_mint_permission(885 origin1.clone(),886 CollectionId(1),887 true888 ));889 assert_ok!(TemplateModule::set_public_access_mode(890 origin1.clone(),891 CollectionId(1),892 AccessMode::AllowList893 ));894 assert_ok!(TemplateModule::add_to_allow_list(895 origin1.clone(),896 CollectionId(1),897 account(1)898 ));899 assert_ok!(TemplateModule::add_to_allow_list(900 origin1.clone(),901 CollectionId(1),902 account(2)903 ));904 assert_ok!(TemplateModule::add_to_allow_list(905 origin1.clone(),906 CollectionId(1),907 account(3)908 ));909910 // do approve911 assert_ok!(TemplateModule::approve(912 origin1.clone(),913 account(2),914 CollectionId(1),915 TokenId(0),916 5917 ));918 assert_eq!(919 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),920 5921 );922 assert_ok!(TemplateModule::approve(923 origin1,924 account(3),925 CollectionId(1),926 TokenId(0),927 5928 ));929 assert_eq!(930 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),931 5932 );933 assert_eq!(934 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(3))),935 5936 );937938 assert_ok!(TemplateModule::transfer_from(939 origin2.clone(),940 account(1),941 account(3),942 CollectionId(1),943 TokenId(0),944 4945 ));946947 assert_eq!(948 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),949 1950 );951952 assert_noop!(953 TemplateModule::transfer_from(954 origin2,955 account(1),956 account(3),957 CollectionId(1),958 TokenId(0),959 4960 )961 .map_err(|e| e.error),962 CommonError::<Test>::ApprovedValueTooLow963 );964 });965}966967#[test]968fn change_collection_owner() {969 new_test_ext().execute_with(|| {970 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));971972 let origin1 = Origin::signed(1);973 assert_ok!(TemplateModule::change_collection_owner(974 origin1,975 collection_id,976 2977 ));978 assert_eq!(979 <pallet_common::CollectionById<Test>>::get(collection_id)980 .unwrap()981 .owner,982 2983 );984 });985}986987#[test]988fn destroy_collection() {989 new_test_ext().execute_with(|| {990 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));991992 let origin1 = Origin::signed(1);993 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));994 });995}996997#[test]998fn burn_nft_item() {999 new_test_ext().execute_with(|| {1000 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10011002 let origin1 = Origin::signed(1);10031004 let data = default_nft_data();1005 create_test_item(collection_id, &data.into());10061007 // check balance (collection with id = 1, user id = 1)1008 assert_eq!(1009 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1010 11011 );10121013 // burn item1014 assert_ok!(TemplateModule::burn_item(1015 origin1.clone(),1016 collection_id,1017 TokenId(1),1018 11019 ));1020 assert_eq!(1021 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1022 01023 );1024 });1025}10261027#[test]1028fn burn_same_nft_item_twice() {1029 new_test_ext().execute_with(|| {1030 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10311032 let origin1 = Origin::signed(1);10331034 let data = default_nft_data();1035 create_test_item(collection_id, &data.into());10361037 // check balance (collection with id = 1, user id = 1)1038 assert_eq!(1039 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1040 11041 );10421043 // burn item1044 assert_ok!(TemplateModule::burn_item(1045 origin1.clone(),1046 collection_id,1047 TokenId(1),1048 11049 ));10501051 // burn item again1052 assert_noop!(1053 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1).map_err(|e| e.error),1054 CommonError::<Test>::TokenNotFound1055 );10561057 assert_eq!(1058 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1059 01060 );1061 });1062}10631064#[test]1065fn burn_fungible_item() {1066 new_test_ext().execute_with(|| {1067 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));10681069 let origin1 = Origin::signed(1);1070 assert_ok!(TemplateModule::add_collection_admin(1071 origin1.clone(),1072 collection_id,1073 account(2)1074 ));10751076 let data = default_fungible_data();1077 create_test_item(collection_id, &data.into());10781079 // check balance (collection with id = 1, user id = 1)1080 assert_eq!(1081 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1082 51083 );10841085 // burn item1086 assert_ok!(TemplateModule::burn_item(1087 origin1.clone(),1088 CollectionId(1),1089 TokenId(0),1090 51091 ));1092 assert_noop!(1093 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(0), 5).map_err(|e| e.error),1094 CommonError::<Test>::TokenValueTooLow1095 );10961097 assert_eq!(1098 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1099 01100 );1101 });1102}11031104#[test]1105fn burn_fungible_item_with_token_id() {1106 new_test_ext().execute_with(|| {1107 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11081109 let origin1 = Origin::signed(1);1110 assert_ok!(TemplateModule::add_collection_admin(1111 origin1.clone(),1112 collection_id,1113 account(2)1114 ));11151116 let data = default_fungible_data();1117 create_test_item(collection_id, &data.into());11181119 // check balance (collection with id = 1, user id = 1)1120 assert_eq!(1121 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1122 51123 );11241125 // Try to burn item using Token ID1126 assert_noop!(1127 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(1), 5).map_err(|e| e.error),1128 <pallet_fungible::Error::<Test>>::FungibleItemsHaveNoId1129 );1130 });1131}1132#[test]1133fn burn_refungible_item() {1134 new_test_ext().execute_with(|| {1135 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));1136 let origin1 = Origin::signed(1);11371138 assert_ok!(TemplateModule::set_mint_permission(1139 origin1.clone(),1140 collection_id,1141 true1142 ));1143 assert_ok!(TemplateModule::set_public_access_mode(1144 origin1.clone(),1145 collection_id,1146 AccessMode::AllowList1147 ));1148 assert_ok!(TemplateModule::add_to_allow_list(1149 origin1.clone(),1150 collection_id,1151 account(1)1152 ));11531154 assert_ok!(TemplateModule::add_collection_admin(1155 origin1.clone(),1156 collection_id,1157 account(2)1158 ));11591160 let data = default_re_fungible_data();1161 create_test_item(collection_id, &data.into());11621163 // check balance (collection with id = 1, user id = 2)1164 assert_eq!(1165 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),1166 11167 );1168 assert_eq!(1169 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1170 10231171 );11721173 // burn item1174 assert_ok!(TemplateModule::burn_item(1175 origin1.clone(),1176 collection_id,1177 TokenId(1),1178 10231179 ));1180 assert_noop!(1181 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1023)1182 .map_err(|e| e.error),1183 CommonError::<Test>::TokenValueTooLow1184 );11851186 assert_eq!(1187 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1188 01189 );1190 });1191}11921193#[test]1194fn add_collection_admin() {1195 new_test_ext().execute_with(|| {1196 let collection1_id =1197 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1198 let origin1 = Origin::signed(1);11991200 // Add collection admins1201 assert_ok!(TemplateModule::add_collection_admin(1202 origin1.clone(),1203 collection1_id,1204 account(2)1205 ));1206 assert_ok!(TemplateModule::add_collection_admin(1207 origin1,1208 collection1_id,1209 account(3)1210 ));12111212 // Owner is not an admin by default1213 assert_eq!(1214 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(1))),1215 false1216 );1217 assert!(<pallet_common::IsAdmin<Test>>::get((1218 CollectionId(1),1219 account(2)1220 )));1221 assert!(<pallet_common::IsAdmin<Test>>::get((1222 CollectionId(1),1223 account(3)1224 )));1225 });1226}12271228#[test]1229fn remove_collection_admin() {1230 new_test_ext().execute_with(|| {1231 let collection1_id =1232 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1233 let origin1 = Origin::signed(1);1234 let origin2 = Origin::signed(2);12351236 // Add collection admins 2 and 31237 assert_ok!(TemplateModule::add_collection_admin(1238 origin1.clone(),1239 collection1_id,1240 account(2)1241 ));1242 assert_ok!(TemplateModule::add_collection_admin(1243 origin1,1244 collection1_id,1245 account(3)1246 ));12471248 assert!(<pallet_common::IsAdmin<Test>>::get((1249 CollectionId(1),1250 account(2)1251 )));1252 assert!(<pallet_common::IsAdmin<Test>>::get((1253 CollectionId(1),1254 account(3)1255 )));12561257 // remove admin 31258 assert_ok!(TemplateModule::remove_collection_admin(1259 origin2,1260 CollectionId(1),1261 account(3)1262 ));12631264 // 2 is still admin, 3 is not an admin anymore1265 assert!(<pallet_common::IsAdmin<Test>>::get((1266 CollectionId(1),1267 account(2)1268 )));1269 assert_eq!(1270 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(3))),1271 false1272 );1273 });1274}12751276#[test]1277fn balance_of() {1278 new_test_ext().execute_with(|| {1279 let nft_collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1280 let fungible_collection_id =1281 create_test_collection(&CollectionMode::Fungible(3), CollectionId(2));1282 let re_fungible_collection_id =1283 create_test_collection(&CollectionMode::ReFungible, CollectionId(3));12841285 // check balance before1286 assert_eq!(1287 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1288 01289 );1290 assert_eq!(1291 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1292 01293 );1294 assert_eq!(1295 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1296 01297 );12981299 let nft_data = default_nft_data();1300 create_test_item(nft_collection_id, &nft_data.into());13011302 let fungible_data = default_fungible_data();1303 create_test_item(fungible_collection_id, &fungible_data.into());13041305 let re_fungible_data = default_re_fungible_data();1306 create_test_item(re_fungible_collection_id, &re_fungible_data.into());13071308 // check balance (collection with id = 1, user id = 1)1309 assert_eq!(1310 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1311 11312 );1313 assert_eq!(1314 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1315 51316 );1317 assert_eq!(1318 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1319 11320 );13211322 assert_eq!(1323 <pallet_nonfungible::Owned<Test>>::get((nft_collection_id, account(1), TokenId(1))),1324 true1325 );1326 assert_eq!(1327 <pallet_refungible::Owned<Test>>::get((1328 re_fungible_collection_id,1329 account(1),1330 TokenId(1)1331 )),1332 true1333 );1334 });1335}13361337#[test]1338fn approve() {1339 new_test_ext().execute_with(|| {1340 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));13411342 let data = default_nft_data();1343 create_test_item(collection_id, &data.into());13441345 let origin1 = Origin::signed(1);13461347 // approve1348 assert_ok!(TemplateModule::approve(1349 origin1,1350 account(2),1351 CollectionId(1),1352 TokenId(1),1353 11354 ));1355 assert_eq!(1356 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1357 account(2)1358 );1359 });1360}13611362#[test]1363fn transfer_from() {1364 new_test_ext().execute_with(|| {1365 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1366 let origin1 = Origin::signed(1);1367 let origin2 = Origin::signed(2);13681369 let data = default_nft_data();1370 create_test_item(collection_id, &data.into());13711372 // approve1373 assert_ok!(TemplateModule::approve(1374 origin1.clone(),1375 account(2),1376 CollectionId(1),1377 TokenId(1),1378 11379 ));1380 assert_eq!(1381 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1382 account(2)1383 );13841385 assert_ok!(TemplateModule::set_mint_permission(1386 origin1.clone(),1387 CollectionId(1),1388 true1389 ));1390 assert_ok!(TemplateModule::set_public_access_mode(1391 origin1.clone(),1392 CollectionId(1),1393 AccessMode::AllowList1394 ));1395 assert_ok!(TemplateModule::add_to_allow_list(1396 origin1.clone(),1397 CollectionId(1),1398 account(1)1399 ));1400 assert_ok!(TemplateModule::add_to_allow_list(1401 origin1.clone(),1402 CollectionId(1),1403 account(2)1404 ));1405 assert_ok!(TemplateModule::add_to_allow_list(1406 origin1,1407 CollectionId(1),1408 account(3)1409 ));14101411 assert_ok!(TemplateModule::transfer_from(1412 origin2,1413 account(1),1414 account(2),1415 CollectionId(1),1416 TokenId(1),1417 11418 ));14191420 // after transfer1421 assert_eq!(1422 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(1))),1423 01424 );1425 assert_eq!(1426 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(2))),1427 11428 );1429 });1430}14311432// #endregion14331434// Coverage tests region1435// #region14361437#[test]1438fn owner_can_add_address_to_allow_list() {1439 new_test_ext().execute_with(|| {1440 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14411442 let origin1 = Origin::signed(1);1443 assert_ok!(TemplateModule::add_to_allow_list(1444 origin1,1445 collection_id,1446 account(2)1447 ));1448 assert!(<pallet_common::Allowlist<Test>>::get((1449 collection_id,1450 account(2)1451 )));1452 });1453}14541455#[test]1456fn admin_can_add_address_to_allow_list() {1457 new_test_ext().execute_with(|| {1458 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1459 let origin1 = Origin::signed(1);1460 let origin2 = Origin::signed(2);14611462 assert_ok!(TemplateModule::add_collection_admin(1463 origin1,1464 collection_id,1465 account(2)1466 ));1467 assert_ok!(TemplateModule::add_to_allow_list(1468 origin2,1469 collection_id,1470 account(3)1471 ));1472 assert!(<pallet_common::Allowlist<Test>>::get((1473 collection_id,1474 account(3)1475 )));1476 });1477}14781479#[test]1480fn nonprivileged_user_cannot_add_address_to_allow_list() {1481 new_test_ext().execute_with(|| {1482 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14831484 let origin2 = Origin::signed(2);1485 assert_noop!(1486 TemplateModule::add_to_allow_list(origin2, collection_id, account(3)),1487 CommonError::<Test>::NoPermission1488 );1489 });1490}14911492#[test]1493fn nobody_can_add_address_to_allow_list_of_nonexisting_collection() {1494 new_test_ext().execute_with(|| {1495 let origin1 = Origin::signed(1);14961497 assert_noop!(1498 TemplateModule::add_to_allow_list(origin1, CollectionId(1), account(2)),1499 CommonError::<Test>::CollectionNotFound1500 );1501 });1502}15031504#[test]1505fn nobody_can_add_address_to_allow_list_of_deleted_collection() {1506 new_test_ext().execute_with(|| {1507 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15081509 let origin1 = Origin::signed(1);1510 assert_ok!(TemplateModule::destroy_collection(1511 origin1.clone(),1512 collection_id1513 ));1514 assert_noop!(1515 TemplateModule::add_to_allow_list(origin1, collection_id, account(2)),1516 CommonError::<Test>::CollectionNotFound1517 );1518 });1519}15201521// If address is already added to allow list, nothing happens1522#[test]1523fn address_is_already_added_to_allow_list() {1524 new_test_ext().execute_with(|| {1525 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1526 let origin1 = Origin::signed(1);15271528 assert_ok!(TemplateModule::add_to_allow_list(1529 origin1.clone(),1530 collection_id,1531 account(2)1532 ));1533 assert_ok!(TemplateModule::add_to_allow_list(1534 origin1,1535 collection_id,1536 account(2)1537 ));1538 assert!(<pallet_common::Allowlist<Test>>::get((1539 collection_id,1540 account(2)1541 )));1542 });1543}15441545#[test]1546fn owner_can_remove_address_from_allow_list() {1547 new_test_ext().execute_with(|| {1548 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15491550 let origin1 = Origin::signed(1);1551 assert_ok!(TemplateModule::add_to_allow_list(1552 origin1.clone(),1553 collection_id,1554 account(2)1555 ));1556 assert_ok!(TemplateModule::remove_from_allow_list(1557 origin1,1558 collection_id,1559 account(2)1560 ));1561 assert_eq!(1562 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1563 false1564 );1565 });1566}15671568#[test]1569fn admin_can_remove_address_from_allow_list() {1570 new_test_ext().execute_with(|| {1571 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1572 let origin1 = Origin::signed(1);1573 let origin2 = Origin::signed(2);15741575 // Owner adds admin1576 assert_ok!(TemplateModule::add_collection_admin(1577 origin1.clone(),1578 collection_id,1579 account(2)1580 ));15811582 // Owner adds address 3 to allow list1583 assert_ok!(TemplateModule::add_to_allow_list(1584 origin1,1585 collection_id,1586 account(3)1587 ));15881589 // Admin removes address 3 from allow list1590 assert_ok!(TemplateModule::remove_from_allow_list(1591 origin2,1592 collection_id,1593 account(3)1594 ));1595 assert_eq!(1596 <pallet_common::Allowlist<Test>>::get((collection_id, account(3))),1597 false1598 );1599 });1600}16011602#[test]1603fn nonprivileged_user_cannot_remove_address_from_allow_list() {1604 new_test_ext().execute_with(|| {1605 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1606 let origin1 = Origin::signed(1);1607 let origin2 = Origin::signed(2);16081609 assert_ok!(TemplateModule::add_to_allow_list(1610 origin1,1611 collection_id,1612 account(2)1613 ));1614 assert_noop!(1615 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1616 CommonError::<Test>::NoPermission1617 );1618 assert!(<pallet_common::Allowlist<Test>>::get((1619 collection_id,1620 account(2)1621 )));1622 });1623}16241625#[test]1626fn nobody_can_remove_address_from_allow_list_of_nonexisting_collection() {1627 new_test_ext().execute_with(|| {1628 let origin1 = Origin::signed(1);16291630 assert_noop!(1631 TemplateModule::remove_from_allow_list(origin1, CollectionId(1), account(2)),1632 CommonError::<Test>::CollectionNotFound1633 );1634 });1635}16361637#[test]1638fn nobody_can_remove_address_from_allow_list_of_deleted_collection() {1639 new_test_ext().execute_with(|| {1640 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1641 let origin1 = Origin::signed(1);1642 let origin2 = Origin::signed(2);16431644 // Add account 2 to allow list1645 assert_ok!(TemplateModule::add_to_allow_list(1646 origin1.clone(),1647 collection_id,1648 account(2)1649 ));16501651 // Account 2 is in collection allow-list1652 assert!(<pallet_common::Allowlist<Test>>::get((1653 collection_id,1654 account(2)1655 )));16561657 // Destroy collection1658 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));16591660 // Attempt to remove account 2 from collection allow-list => error1661 assert_noop!(1662 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1663 CommonError::<Test>::CollectionNotFound1664 );16651666 // Account 2 is not found in collection allow-list anyway1667 assert_eq!(1668 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1669 false1670 );1671 });1672}16731674// If address is already removed from allow list, nothing happens1675#[test]1676fn address_is_already_removed_from_allow_list() {1677 new_test_ext().execute_with(|| {1678 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1679 let origin1 = Origin::signed(1);16801681 assert_ok!(TemplateModule::add_to_allow_list(1682 origin1.clone(),1683 collection_id,1684 account(2)1685 ));1686 assert_ok!(TemplateModule::remove_from_allow_list(1687 origin1.clone(),1688 collection_id,1689 account(2)1690 ));1691 assert_eq!(1692 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1693 false1694 );1695 assert_ok!(TemplateModule::remove_from_allow_list(1696 origin1,1697 collection_id,1698 account(2)1699 ));1700 assert_eq!(1701 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1702 false1703 );1704 });1705}17061707// If Public Access mode is set to AllowList, tokens can’t be transferred from a non-allowlisted address with transfer or transferFrom (2 tests)1708#[test]1709fn allow_list_test_1() {1710 new_test_ext().execute_with(|| {1711 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17121713 let origin1 = Origin::signed(1);17141715 let data = default_nft_data();1716 create_test_item(collection_id, &data.into());17171718 assert_ok!(TemplateModule::set_public_access_mode(1719 origin1.clone(),1720 collection_id,1721 AccessMode::AllowList1722 ));1723 assert_ok!(TemplateModule::add_to_allow_list(1724 origin1.clone(),1725 collection_id,1726 account(2)1727 ));17281729 assert_noop!(1730 TemplateModule::transfer(origin1, account(3), CollectionId(1), TokenId(1), 1)1731 .map_err(|e| e.error),1732 CommonError::<Test>::AddressNotInAllowlist1733 );1734 });1735}17361737#[test]1738fn allow_list_test_2() {1739 new_test_ext().execute_with(|| {1740 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1741 let origin1 = Origin::signed(1);17421743 let data = default_nft_data();1744 create_test_item(collection_id, &data.into());17451746 assert_ok!(TemplateModule::set_public_access_mode(1747 origin1.clone(),1748 collection_id,1749 AccessMode::AllowList1750 ));1751 assert_ok!(TemplateModule::add_to_allow_list(1752 origin1.clone(),1753 collection_id,1754 account(1)1755 ));1756 assert_ok!(TemplateModule::add_to_allow_list(1757 origin1.clone(),1758 collection_id,1759 account(2)1760 ));17611762 // do approve1763 assert_ok!(TemplateModule::approve(1764 origin1.clone(),1765 account(1),1766 collection_id,1767 TokenId(1),1768 11769 ));1770 assert_eq!(1771 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1772 account(1)1773 );17741775 assert_ok!(TemplateModule::remove_from_allow_list(1776 origin1.clone(),1777 collection_id,1778 account(1)1779 ));17801781 assert_noop!(1782 TemplateModule::transfer_from(1783 origin1,1784 account(1),1785 account(3),1786 CollectionId(1),1787 TokenId(1),1788 11789 )1790 .map_err(|e| e.error),1791 CommonError::<Test>::AddressNotInAllowlist1792 );1793 });1794}17951796// If Public Access mode is set to AllowList, tokens can’t be transferred to a non-allowlisted address with transfer or transferFrom (2 tests)1797#[test]1798fn allow_list_test_3() {1799 new_test_ext().execute_with(|| {1800 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18011802 let origin1 = Origin::signed(1);18031804 let data = default_nft_data();1805 create_test_item(collection_id, &data.into());18061807 assert_ok!(TemplateModule::set_public_access_mode(1808 origin1.clone(),1809 collection_id,1810 AccessMode::AllowList1811 ));1812 assert_ok!(TemplateModule::add_to_allow_list(1813 origin1.clone(),1814 collection_id,1815 account(1)1816 ));18171818 assert_noop!(1819 TemplateModule::transfer(origin1, account(3), collection_id, TokenId(1), 1)1820 .map_err(|e| e.error),1821 CommonError::<Test>::AddressNotInAllowlist1822 );1823 });1824}18251826#[test]1827fn allow_list_test_4() {1828 new_test_ext().execute_with(|| {1829 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18301831 let origin1 = Origin::signed(1);18321833 let data = default_nft_data();1834 create_test_item(collection_id, &data.into());18351836 assert_ok!(TemplateModule::set_public_access_mode(1837 origin1.clone(),1838 collection_id,1839 AccessMode::AllowList1840 ));1841 assert_ok!(TemplateModule::add_to_allow_list(1842 origin1.clone(),1843 collection_id,1844 account(1)1845 ));1846 assert_ok!(TemplateModule::add_to_allow_list(1847 origin1.clone(),1848 collection_id,1849 account(2)1850 ));18511852 // do approve1853 assert_ok!(TemplateModule::approve(1854 origin1.clone(),1855 account(1),1856 collection_id,1857 TokenId(1),1858 11859 ));1860 assert_eq!(1861 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1862 account(1)1863 );18641865 assert_ok!(TemplateModule::remove_from_allow_list(1866 origin1.clone(),1867 collection_id,1868 account(2)1869 ));18701871 assert_noop!(1872 TemplateModule::transfer_from(1873 origin1,1874 account(1),1875 account(3),1876 collection_id,1877 TokenId(1),1878 11879 )1880 .map_err(|e| e.error),1881 CommonError::<Test>::AddressNotInAllowlist1882 );1883 });1884}18851886// If Public Access mode is set to AllowList, tokens can’t be destroyed by a non-allowlisted address (even if it owned them before enabling AllowList mode)1887#[test]1888fn allow_list_test_5() {1889 new_test_ext().execute_with(|| {1890 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18911892 let origin1 = Origin::signed(1);18931894 let data = default_nft_data();1895 create_test_item(collection_id, &data.into());18961897 assert_ok!(TemplateModule::set_public_access_mode(1898 origin1.clone(),1899 collection_id,1900 AccessMode::AllowList1901 ));1902 assert_noop!(1903 TemplateModule::burn_item(origin1.clone(), CollectionId(1), TokenId(1), 1)1904 .map_err(|e| e.error),1905 CommonError::<Test>::AddressNotInAllowlist1906 );1907 });1908}19091910// If Public Access mode is set to AllowList, token transfers can’t be Approved by a non-allowlisted address (see Approve method).1911#[test]1912fn allow_list_test_6() {1913 new_test_ext().execute_with(|| {1914 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19151916 let origin1 = Origin::signed(1);19171918 let data = default_nft_data();1919 create_test_item(collection_id, &data.into());19201921 assert_ok!(TemplateModule::set_public_access_mode(1922 origin1.clone(),1923 collection_id,1924 AccessMode::AllowList1925 ));19261927 // do approve1928 assert_noop!(1929 TemplateModule::approve(origin1, account(1), CollectionId(1), TokenId(1), 1)1930 .map_err(|e| e.error),1931 CommonError::<Test>::AddressNotInAllowlist1932 );1933 });1934}19351936// If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests) and1937// tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests)1938#[test]1939fn allow_list_test_7() {1940 new_test_ext().execute_with(|| {1941 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19421943 let data = default_nft_data();1944 create_test_item(collection_id, &data.into());19451946 let origin1 = Origin::signed(1);19471948 assert_ok!(TemplateModule::set_public_access_mode(1949 origin1.clone(),1950 collection_id,1951 AccessMode::AllowList1952 ));1953 assert_ok!(TemplateModule::add_to_allow_list(1954 origin1.clone(),1955 collection_id,1956 account(1)1957 ));1958 assert_ok!(TemplateModule::add_to_allow_list(1959 origin1.clone(),1960 collection_id,1961 account(2)1962 ));19631964 assert_ok!(TemplateModule::transfer(1965 origin1,1966 account(2),1967 CollectionId(1),1968 TokenId(1),1969 11970 ));1971 });1972}19731974#[test]1975fn allow_list_test_8() {1976 new_test_ext().execute_with(|| {1977 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19781979 // Create NFT for account 11980 let data = default_nft_data();1981 create_test_item(collection_id, &data.into());19821983 let origin1 = Origin::signed(1);19841985 // Toggle Allow List mode and add accounts 1 and 21986 assert_ok!(TemplateModule::set_public_access_mode(1987 origin1.clone(),1988 collection_id,1989 AccessMode::AllowList1990 ));1991 assert_ok!(TemplateModule::add_to_allow_list(1992 origin1.clone(),1993 collection_id,1994 account(1)1995 ));1996 assert_ok!(TemplateModule::add_to_allow_list(1997 origin1.clone(),1998 collection_id,1999 account(2)2000 ));20012002 // Sself-approve account 1 for NFT 12003 assert_ok!(TemplateModule::approve(2004 origin1.clone(),2005 account(1),2006 CollectionId(1),2007 TokenId(1),2008 12009 ));2010 assert_eq!(2011 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),2012 account(1)2013 );20142015 // Transfer from 1 to 22016 assert_ok!(TemplateModule::transfer_from(2017 origin1,2018 account(1),2019 account(2),2020 CollectionId(1),2021 TokenId(1),2022 12023 ));2024 });2025}20262027// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by owner.2028#[test]2029fn allow_list_test_9() {2030 new_test_ext().execute_with(|| {2031 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2032 let origin1 = Origin::signed(1);20332034 assert_ok!(TemplateModule::set_public_access_mode(2035 origin1.clone(),2036 collection_id,2037 AccessMode::AllowList2038 ));2039 assert_ok!(TemplateModule::set_mint_permission(2040 origin1,2041 collection_id,2042 false2043 ));20442045 let data = default_nft_data();2046 create_test_item(collection_id, &data.into());2047 });2048}20492050// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by admin.2051#[test]2052fn allow_list_test_10() {2053 new_test_ext().execute_with(|| {2054 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20552056 let origin1 = Origin::signed(1);2057 let origin2 = Origin::signed(2);20582059 assert_ok!(TemplateModule::set_public_access_mode(2060 origin1.clone(),2061 collection_id,2062 AccessMode::AllowList2063 ));2064 assert_ok!(TemplateModule::set_mint_permission(2065 origin1.clone(),2066 collection_id,2067 false2068 ));20692070 assert_ok!(TemplateModule::add_collection_admin(2071 origin1,2072 collection_id,2073 account(2)2074 ));20752076 assert_ok!(TemplateModule::create_item(2077 origin2,2078 collection_id,2079 account(2),2080 default_nft_data().into()2081 ));2082 });2083}20842085// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and allow listed address.2086#[test]2087fn allow_list_test_11() {2088 new_test_ext().execute_with(|| {2089 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20902091 let origin1 = Origin::signed(1);2092 let origin2 = Origin::signed(2);20932094 assert_ok!(TemplateModule::set_public_access_mode(2095 origin1.clone(),2096 collection_id,2097 AccessMode::AllowList2098 ));2099 assert_ok!(TemplateModule::set_mint_permission(2100 origin1.clone(),2101 collection_id,2102 false2103 ));2104 assert_ok!(TemplateModule::add_to_allow_list(2105 origin1,2106 collection_id,2107 account(2)2108 ));21092110 assert_noop!(2111 TemplateModule::create_item(2112 origin2,2113 CollectionId(1),2114 account(2),2115 default_nft_data().into()2116 )2117 .map_err(|e| e.error),2118 CommonError::<Test>::PublicMintingNotAllowed2119 );2120 });2121}21222123// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-allow listed address.2124#[test]2125fn allow_list_test_12() {2126 new_test_ext().execute_with(|| {2127 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21282129 let origin1 = Origin::signed(1);2130 let origin2 = Origin::signed(2);21312132 assert_ok!(TemplateModule::set_public_access_mode(2133 origin1.clone(),2134 collection_id,2135 AccessMode::AllowList2136 ));2137 assert_ok!(TemplateModule::set_mint_permission(2138 origin1,2139 collection_id,2140 false2141 ));21422143 assert_noop!(2144 TemplateModule::create_item(2145 origin2,2146 CollectionId(1),2147 account(2),2148 default_nft_data().into()2149 )2150 .map_err(|e| e.error),2151 CommonError::<Test>::PublicMintingNotAllowed2152 );2153 });2154}21552156// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by owner.2157#[test]2158fn allow_list_test_13() {2159 new_test_ext().execute_with(|| {2160 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21612162 let origin1 = Origin::signed(1);21632164 assert_ok!(TemplateModule::set_public_access_mode(2165 origin1.clone(),2166 collection_id,2167 AccessMode::AllowList2168 ));2169 assert_ok!(TemplateModule::set_mint_permission(2170 origin1,2171 collection_id,2172 true2173 ));21742175 let data = default_nft_data();2176 create_test_item(collection_id, &data.into());2177 });2178}21792180// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by admin.2181#[test]2182fn allow_list_test_14() {2183 new_test_ext().execute_with(|| {2184 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21852186 let origin1 = Origin::signed(1);2187 let origin2 = Origin::signed(2);21882189 assert_ok!(TemplateModule::set_public_access_mode(2190 origin1.clone(),2191 collection_id,2192 AccessMode::AllowList2193 ));2194 assert_ok!(TemplateModule::set_mint_permission(2195 origin1.clone(),2196 collection_id,2197 true2198 ));21992200 assert_ok!(TemplateModule::add_collection_admin(2201 origin1,2202 collection_id,2203 account(2)2204 ));22052206 assert_ok!(TemplateModule::create_item(2207 origin2,2208 collection_id,2209 account(2),2210 default_nft_data().into()2211 ));2212 });2213}22142215// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-allow listed address.2216#[test]2217fn allow_list_test_15() {2218 new_test_ext().execute_with(|| {2219 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22202221 let origin1 = Origin::signed(1);2222 let origin2 = Origin::signed(2);22232224 assert_ok!(TemplateModule::set_public_access_mode(2225 origin1.clone(),2226 collection_id,2227 AccessMode::AllowList2228 ));2229 assert_ok!(TemplateModule::set_mint_permission(2230 origin1,2231 collection_id,2232 true2233 ));22342235 assert_noop!(2236 TemplateModule::create_item(2237 origin2,2238 collection_id,2239 account(2),2240 default_nft_data().into()2241 )2242 .map_err(|e| e.error),2243 CommonError::<Test>::AddressNotInAllowlist2244 );2245 });2246}22472248// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by non-privileged and allow listed address.2249#[test]2250fn allow_list_test_16() {2251 new_test_ext().execute_with(|| {2252 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22532254 let origin1 = Origin::signed(1);2255 let origin2 = Origin::signed(2);22562257 assert_ok!(TemplateModule::set_public_access_mode(2258 origin1.clone(),2259 collection_id,2260 AccessMode::AllowList2261 ));2262 assert_ok!(TemplateModule::set_mint_permission(2263 origin1.clone(),2264 collection_id,2265 true2266 ));2267 assert_ok!(TemplateModule::add_to_allow_list(2268 origin1,2269 collection_id,2270 account(2)2271 ));22722273 assert_ok!(TemplateModule::create_item(2274 origin2,2275 collection_id,2276 account(2),2277 default_nft_data().into()2278 ));2279 });2280}22812282// Total number of collections. Positive test2283#[test]2284fn total_number_collections_bound() {2285 new_test_ext().execute_with(|| {2286 create_test_collection(&CollectionMode::NFT, CollectionId(1));2287 });2288}22892290#[test]2291fn create_max_collections() {2292 new_test_ext().execute_with(|| {2293 for i in 1..COLLECTION_NUMBER_LIMIT {2294 create_test_collection(&CollectionMode::NFT, CollectionId(i));2295 }2296 });2297}22982299// Total number of collections. Negative test2300#[test]2301fn total_number_collections_bound_neg() {2302 new_test_ext().execute_with(|| {2303 let origin1 = Origin::signed(1);23042305 for i in 1..=COLLECTION_NUMBER_LIMIT {2306 create_test_collection(&CollectionMode::NFT, CollectionId(i));2307 }23082309 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();2310 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();2311 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();23122313 // 11-th collection in chain. Expects error2314 assert_noop!(2315 TemplateModule::create_collection(2316 origin1,2317 col_name1.try_into().unwrap(),2318 col_desc1.try_into().unwrap(),2319 token_prefix1.try_into().unwrap(),2320 CollectionMode::NFT2321 ),2322 CommonError::<Test>::TotalCollectionsLimitExceeded2323 );2324 });2325}23262327// Owned tokens by a single address. Positive test2328#[test]2329fn owned_tokens_bound() {2330 new_test_ext().execute_with(|| {2331 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23322333 let data = default_nft_data();2334 create_test_item(collection_id, &data.clone().into());2335 create_test_item(collection_id, &data.into());2336 });2337}23382339// Owned tokens by a single address. Negotive test2340#[test]2341fn owned_tokens_bound_neg() {2342 new_test_ext().execute_with(|| {2343 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23442345 let origin1 = Origin::signed(1);23462347 for _ in 1..=MAX_TOKEN_OWNERSHIP {2348 let data = default_nft_data();2349 create_test_item(collection_id, &data.clone().into());2350 }23512352 let data = default_nft_data();2353 assert_noop!(2354 TemplateModule::create_item(origin1, CollectionId(1), account(1), data.into())2355 .map_err(|e| e.error),2356 CommonError::<Test>::AccountTokenLimitExceeded2357 );2358 });2359}23602361// Number of collection admins. Positive test2362#[test]2363fn collection_admins_bound() {2364 new_test_ext().execute_with(|| {2365 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23662367 let origin1 = Origin::signed(1);23682369 assert_ok!(TemplateModule::add_collection_admin(2370 origin1.clone(),2371 collection_id,2372 account(2)2373 ));2374 assert_ok!(TemplateModule::add_collection_admin(2375 origin1,2376 collection_id,2377 account(3)2378 ));2379 });2380}23812382// Number of collection admins. Negotive test2383#[test]2384fn collection_admins_bound_neg() {2385 new_test_ext().execute_with(|| {2386 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23872388 let origin1 = Origin::signed(1);23892390 for i in 0..COLLECTION_ADMINS_LIMIT {2391 assert_ok!(TemplateModule::add_collection_admin(2392 origin1.clone(),2393 collection_id,2394 account((2 + i).into())2395 ));2396 }2397 assert_noop!(2398 TemplateModule::add_collection_admin(2399 origin1,2400 collection_id,2401 account((3 + COLLECTION_ADMINS_LIMIT).into())2402 ),2403 CommonError::<Test>::CollectionAdminCountExceeded2404 );2405 });2406}2407// #endregion24082409#[test]2410fn set_const_on_chain_schema() {2411 new_test_ext().execute_with(|| {2412 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24132414 let origin1 = Origin::signed(1);2415 assert_ok!(TemplateModule::set_const_on_chain_schema(2416 origin1,2417 collection_id,2418 b"test const on chain schema".to_vec().try_into().unwrap()2419 ));24202421 assert_eq!(2422 <pallet_common::CollectionById<Test>>::get(collection_id)2423 .unwrap()2424 .const_on_chain_schema,2425 b"test const on chain schema".to_vec()2426 );2427 assert_eq!(2428 <pallet_common::CollectionById<Test>>::get(collection_id)2429 .unwrap()2430 .variable_on_chain_schema,2431 b"".to_vec()2432 );2433 });2434}24352436#[test]2437fn set_variable_on_chain_schema() {2438 new_test_ext().execute_with(|| {2439 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24402441 let origin1 = Origin::signed(1);2442 assert_ok!(TemplateModule::set_variable_on_chain_schema(2443 origin1,2444 collection_id,2445 b"test variable on chain schema"2446 .to_vec()2447 .try_into()2448 .unwrap()2449 ));24502451 assert_eq!(2452 <pallet_common::CollectionById<Test>>::get(collection_id)2453 .unwrap()2454 .const_on_chain_schema,2455 b"".to_vec()2456 );2457 assert_eq!(2458 <pallet_common::CollectionById<Test>>::get(collection_id)2459 .unwrap()2460 .variable_on_chain_schema,2461 b"test variable on chain schema".to_vec()2462 );2463 });2464}24652466#[test]2467fn set_variable_meta_data_on_nft_token_stores_variable_meta_data() {2468 new_test_ext().execute_with(|| {2469 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24702471 let origin1 = Origin::signed(1);24722473 let data = default_nft_data();2474 create_test_item(CollectionId(1), &data.into());24752476 let variable_data = b"test data".to_vec();2477 assert_ok!(TemplateModule::set_variable_meta_data(2478 origin1,2479 collection_id,2480 TokenId(1),2481 variable_data.clone().try_into().unwrap()2482 ));24832484 assert_eq!(2485 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2486 .unwrap()2487 .variable_data,2488 variable_data2489 );2490 });2491}24922493#[test]2494fn set_variable_meta_data_on_re_fungible_token_stores_variable_meta_data() {2495 new_test_ext().execute_with(|| {2496 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));24972498 let origin1 = Origin::signed(1);24992500 let data = default_re_fungible_data();2501 create_test_item(collection_id, &data.into());25022503 let variable_data = b"test data".to_vec();2504 assert_ok!(TemplateModule::set_variable_meta_data(2505 origin1,2506 collection_id,2507 TokenId(1),2508 variable_data.clone().try_into().unwrap()2509 ));25102511 assert_eq!(2512 <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1))).variable_data,2513 variable_data2514 );2515 });2516}25172518#[test]2519fn set_variable_meta_data_on_fungible_token_fails() {2520 new_test_ext().execute_with(|| {2521 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));25222523 let origin1 = Origin::signed(1);25242525 let data = default_fungible_data();2526 create_test_item(collection_id, &data.into());25272528 let variable_data = b"test data".to_vec();2529 assert_noop!(2530 TemplateModule::set_variable_meta_data(2531 origin1,2532 collection_id,2533 TokenId(0),2534 variable_data.try_into().unwrap()2535 )2536 .map_err(|e| e.error),2537 <pallet_fungible::Error<Test>>::FungibleItemsDontHaveData2538 );2539 });2540}25412542#[test]2543fn set_variable_meta_data_on_nft_with_item_owner_permission_flag() {2544 new_test_ext().execute_with(|| {2545 //default_limits();25462547 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));25482549 let origin1 = Origin::signed(1);25502551 let data = default_nft_data();2552 create_test_item(collection_id, &data.into());25532554 assert_ok!(TemplateModule::set_meta_update_permission_flag(2555 origin1.clone(),2556 collection_id,2557 MetaUpdatePermission::ItemOwner,2558 ));25592560 let variable_data = b"ten chars.".to_vec();2561 assert_ok!(TemplateModule::set_variable_meta_data(2562 origin1,2563 collection_id,2564 TokenId(1),2565 variable_data.clone().try_into().unwrap()2566 ));25672568 assert_eq!(2569 <pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))2570 .unwrap()2571 .variable_data,2572 variable_data2573 );2574 });2575}25762577#[test]2578fn collection_transfer_flag_works() {2579 new_test_ext().execute_with(|| {2580 let origin1 = Origin::signed(1);25812582 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2583 assert_ok!(TemplateModule::set_transfers_enabled_flag(2584 origin1,2585 collection_id,2586 true2587 ));25882589 let data = default_nft_data();2590 create_test_item(collection_id, &data.into());2591 assert_eq!(2592 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2593 12594 );2595 assert_eq!(2596 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2597 true2598 );25992600 let origin1 = Origin::signed(1);26012602 // default scenario2603 assert_ok!(TemplateModule::transfer(2604 origin1,2605 account(2),2606 collection_id,2607 TokenId(1),2608 12609 ));2610 assert_eq!(2611 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2612 false2613 );2614 assert_eq!(2615 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2616 true2617 );2618 assert_eq!(2619 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2620 02621 );2622 assert_eq!(2623 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2624 12625 );2626 });2627}26282629#[test]2630fn set_variable_meta_data_on_nft_with_admin_flag() {2631 new_test_ext().execute_with(|| {2632 // default_limits();26332634 let collection_id =2635 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26362637 let origin1 = Origin::signed(1);2638 let origin2 = Origin::signed(2);26392640 assert_ok!(TemplateModule::set_mint_permission(2641 origin2.clone(),2642 collection_id,2643 true2644 ));2645 assert_ok!(TemplateModule::add_to_allow_list(2646 origin2.clone(),2647 collection_id,2648 account(1)2649 ));26502651 assert_ok!(TemplateModule::add_collection_admin(2652 origin2.clone(),2653 collection_id,2654 account(1)2655 ));26562657 let data = default_nft_data();2658 create_test_item(collection_id, &data.into());26592660 assert_ok!(TemplateModule::set_meta_update_permission_flag(2661 origin2.clone(),2662 collection_id,2663 MetaUpdatePermission::Admin,2664 ));26652666 let variable_data = b"test.".to_vec();2667 assert_ok!(TemplateModule::set_variable_meta_data(2668 origin1,2669 collection_id,2670 TokenId(1),2671 variable_data.clone().try_into().unwrap()2672 ));26732674 assert_eq!(2675 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2676 .unwrap()2677 .variable_data,2678 variable_data2679 );2680 });2681}26822683#[test]2684fn set_variable_meta_data_on_nft_with_admin_flag_neg() {2685 new_test_ext().execute_with(|| {2686 // default_limits();26872688 let collection_id =2689 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26902691 let origin1 = Origin::signed(1);2692 let origin2 = Origin::signed(2);26932694 assert_ok!(TemplateModule::set_mint_permission(2695 origin2.clone(),2696 collection_id,2697 true2698 ));2699 assert_ok!(TemplateModule::add_to_allow_list(2700 origin2.clone(),2701 collection_id,2702 account(1)2703 ));27042705 let data = default_nft_data();2706 create_test_item(collection_id, &data.into());27072708 assert_ok!(TemplateModule::set_meta_update_permission_flag(2709 origin2.clone(),2710 collection_id,2711 MetaUpdatePermission::Admin,2712 ));27132714 let variable_data = b"test.".to_vec();2715 assert_noop!(2716 TemplateModule::set_variable_meta_data(2717 origin1,2718 collection_id,2719 TokenId(1),2720 variable_data.try_into().unwrap()2721 )2722 .map_err(|e| e.error),2723 CommonError::<Test>::NoPermission2724 );2725 });2726}27272728#[test]2729fn set_variable_meta_flag_after_freeze() {2730 new_test_ext().execute_with(|| {2731 // default_limits();27322733 let collection_id =2734 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));27352736 let origin2 = Origin::signed(2);27372738 assert_ok!(TemplateModule::set_meta_update_permission_flag(2739 origin2.clone(),2740 collection_id,2741 MetaUpdatePermission::None,2742 ));2743 assert_noop!(2744 TemplateModule::set_meta_update_permission_flag(2745 origin2.clone(),2746 collection_id,2747 MetaUpdatePermission::Admin2748 ),2749 CommonError::<Test>::MetadataFlagFrozen2750 );2751 });2752}27532754#[test]2755fn set_variable_meta_data_on_nft_with_none_flag_neg() {2756 new_test_ext().execute_with(|| {2757 // default_limits();27582759 let collection_id =2760 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));2761 let origin1 = Origin::signed(1);27622763 let data = default_nft_data();2764 create_test_item(collection_id, &data.into());27652766 assert_ok!(TemplateModule::set_meta_update_permission_flag(2767 origin1.clone(),2768 collection_id,2769 MetaUpdatePermission::None,2770 ));27712772 let variable_data = b"test.".to_vec();2773 assert_noop!(2774 TemplateModule::set_variable_meta_data(2775 origin1.clone(),2776 collection_id,2777 TokenId(1),2778 variable_data.try_into().unwrap()2779 )2780 .map_err(|e| e.error),2781 CommonError::<Test>::NoPermission2782 );2783 });2784}27852786#[test]2787fn collection_transfer_flag_works_neg() {2788 new_test_ext().execute_with(|| {2789 let origin1 = Origin::signed(1);27902791 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2792 assert_ok!(TemplateModule::set_transfers_enabled_flag(2793 origin1,2794 collection_id,2795 false2796 ));27972798 let data = default_nft_data();2799 create_test_item(collection_id, &data.into());2800 assert_eq!(2801 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2802 12803 );2804 assert_eq!(2805 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2806 true2807 );28082809 let origin1 = Origin::signed(1);28102811 // default scenario2812 assert_noop!(2813 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 1)2814 .map_err(|e| e.error),2815 CommonError::<Test>::TransferNotAllowed2816 );2817 assert_eq!(2818 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2819 12820 );2821 assert_eq!(2822 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2823 02824 );2825 assert_eq!(2826 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2827 true2828 );2829 assert_eq!(2830 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2831 false2832 );2833 });2834}