difftreelog
fix cargo fmt
in: master
1 file changed
pallets/nft/src/tests.rsdiffbeforeafterboth1// Tests to be written here2use super::*;3use crate::mock::*;4use crate::{AccessMode, CollectionMode, Ownership, CreateItemData};5use nft_data_structs::{6 CreateNftData, CreateFungibleData, CreateReFungibleData, CollectionId, TokenId,7 MAX_DECIMAL_POINTS,8};9use frame_support::{assert_noop, assert_ok};10use sp_std::convert::TryInto;1112fn default_nft_data() -> CreateNftData {13 CreateNftData {14 const_data: vec![1, 2, 3].try_into().unwrap(),15 variable_data: vec![3, 2, 1].try_into().unwrap(),16 }17}1819fn default_fungible_data() -> CreateFungibleData {20 CreateFungibleData { value: 5 }21}2223fn default_re_fungible_data() -> CreateReFungibleData {24 CreateReFungibleData {25 const_data: vec![1, 2, 3].try_into().unwrap(),26 variable_data: vec![3, 2, 1].try_into().unwrap(),27 pieces: 1023,28 }29}3031fn create_test_collection_for_owner(32 mode: &CollectionMode,33 owner: u64,34 id: CollectionId,35) -> CollectionId {36 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();37 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();38 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();3940 let origin1 = Origin::signed(owner);41 assert_ok!(TemplateModule::create_collection(42 origin1,43 col_name1,44 col_desc1,45 token_prefix1,46 mode.clone()47 ));4849 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();50 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();51 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();52 assert_eq!(TemplateModule::collection_id(id).unwrap().owner, owner);53 assert_eq!(54 TemplateModule::collection_id(id).unwrap().name,55 saved_col_name56 );57 assert_eq!(TemplateModule::collection_id(id).unwrap().mode, *mode);58 assert_eq!(59 TemplateModule::collection_id(id).unwrap().description,60 saved_description61 );62 assert_eq!(63 TemplateModule::collection_id(id).unwrap().token_prefix,64 saved_prefix65 );66 id67}6869fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {70 create_test_collection_for_owner(&mode, 1, id)71}7273fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {74 let origin1 = Origin::signed(1);75 assert_ok!(TemplateModule::create_item(76 origin1,77 collection_id,78 account(1),79 data.clone()80 ));81}8283fn account(sub: u64) -> TestCrossAccountId {84 TestCrossAccountId::from_sub(sub)85}8687// Use cases tests region88// #region8990#[test]91fn set_version_schema() {92 new_test_ext().execute_with(|| {93 let origin1 = Origin::signed(1);94 let collection_id = create_test_collection(&CollectionMode::NFT, 1);9596 assert_ok!(TemplateModule::set_schema_version(97 origin1,98 collection_id,99 SchemaVersion::Unique100 ));101 assert_eq!(102 TemplateModule::collection_id(collection_id)103 .unwrap()104 .schema_version,105 SchemaVersion::Unique106 );107 });108}109110#[test]111fn create_fungible_collection_fails_with_large_decimal_numbers() {112 new_test_ext().execute_with(|| {113 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();114 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();115 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();116117 let origin1 = Origin::signed(1);118 assert_noop!(119 TemplateModule::create_collection(120 origin1,121 col_name1,122 col_desc1,123 token_prefix1,124 CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1)125 ),126 Error::<Test>::CollectionDecimalPointLimitExceeded127 );128 });129}130131#[test]132fn create_nft_item() {133 new_test_ext().execute_with(|| {134 let collection_id = create_test_collection(&CollectionMode::NFT, 1);135136 let data = default_nft_data();137 create_test_item(collection_id, &data.clone().into());138 let item = TemplateModule::nft_item_id(collection_id, 1).unwrap();139 assert_eq!(item.const_data, data.const_data.into_inner());140 assert_eq!(item.variable_data, data.variable_data.into_inner());141 });142}143144// Use cases tests region145// #region146#[test]147fn create_nft_multiple_items() {148 new_test_ext().execute_with(|| {149 create_test_collection(&CollectionMode::NFT, 1);150151 let origin1 = Origin::signed(1);152153 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];154155 assert_ok!(TemplateModule::create_multiple_items(156 origin1,157 1,158 account(1),159 items_data160 .clone()161 .into_iter()162 .map(|d| { d.into() })163 .collect()164 ));165 for (index, data) in items_data.into_iter().enumerate() {166 let item = TemplateModule::nft_item_id(1, (index + 1) as TokenId).unwrap();167 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());168 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());169 }170 });171}172173#[test]174fn create_refungible_item() {175 new_test_ext().execute_with(|| {176 let collection_id = create_test_collection(&CollectionMode::ReFungible, 1);177178 let data = default_re_fungible_data();179 create_test_item(collection_id, &data.clone().into());180 let item = TemplateModule::refungible_item_id(collection_id, 1).unwrap();181 assert_eq!(item.const_data, data.const_data.into_inner());182 assert_eq!(item.variable_data, data.variable_data.into_inner());183 assert_eq!(184 item.owner[0],185 Ownership {186 owner: account(1),187 fraction: 1023188 }189 );190 });191}192193#[test]194fn create_multiple_refungible_items() {195 new_test_ext().execute_with(|| {196 create_test_collection(&CollectionMode::ReFungible, 1);197198 let origin1 = Origin::signed(1);199200 let items_data = vec![201 default_re_fungible_data(),202 default_re_fungible_data(),203 default_re_fungible_data(),204 ];205206 assert_ok!(TemplateModule::create_multiple_items(207 origin1,208 1,209 account(1),210 items_data211 .clone()212 .into_iter()213 .map(|d| { d.into() })214 .collect()215 ));216 for (index, data) in items_data.into_iter().enumerate() {217 let item = TemplateModule::refungible_item_id(1, (index + 1) as TokenId).unwrap();218 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());219 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());220 assert_eq!(221 item.owner[0],222 Ownership {223 owner: account(1),224 fraction: 1023225 }226 );227 }228 });229}230231#[test]232fn create_fungible_item() {233 new_test_ext().execute_with(|| {234 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);235236 let data = default_fungible_data();237 create_test_item(collection_id, &data.into());238239 assert_eq!(TemplateModule::fungible_item_id(collection_id, 1).value, 5);240 });241}242243//#[test]244// fn create_multiple_fungible_items() {245// new_test_ext().execute_with(|| {246// default_limits();247248// create_test_collection(&CollectionMode::Fungible(3), 1);249250// let origin1 = Origin::signed(1);251252// let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];253254// assert_ok!(TemplateModule::create_multiple_items(255// origin1.clone(),256// 1,257// 1,258// items_data.clone().into_iter().map(|d| { d.into() }).collect()259// ));260261// for (index, _) in items_data.iter().enumerate() {262// assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).value, 5);263// }264// assert_eq!(TemplateModule::balance_count(1, 1), 3000);265// assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);266// });267// }268269#[test]270fn transfer_fungible_item() {271 new_test_ext().execute_with(|| {272 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);273274 let origin1 = Origin::signed(1);275 let origin2 = Origin::signed(2);276277 let data = default_fungible_data();278 create_test_item(collection_id, &data.into());279280 assert_eq!(TemplateModule::fungible_item_id(1, 1).value, 5);281 assert_eq!(TemplateModule::balance_count(1, 1), 5);282283 // change owner scenario284 assert_ok!(TemplateModule::transfer(origin1, account(2), 1, 1, 5));285 assert_eq!(TemplateModule::fungible_item_id(1, 1).value, 0);286 assert_eq!(TemplateModule::balance_count(1, 1), 0);287 assert_eq!(TemplateModule::balance_count(1, 2), 5);288289 // split item scenario290 assert_ok!(TemplateModule::transfer(291 origin2.clone(),292 account(3),293 1,294 1,295 3296 ));297 assert_eq!(TemplateModule::balance_count(1, 2), 2);298 assert_eq!(TemplateModule::balance_count(1, 3), 3);299300 // split item and new owner has account scenario301 assert_ok!(TemplateModule::transfer(origin2, account(3), 1, 1, 1));302 assert_eq!(TemplateModule::fungible_item_id(1, 2).value, 1);303 assert_eq!(TemplateModule::fungible_item_id(1, 3).value, 4);304 assert_eq!(TemplateModule::balance_count(1, 2), 1);305 assert_eq!(TemplateModule::balance_count(1, 3), 4);306 });307}308309#[test]310fn transfer_refungible_item() {311 new_test_ext().execute_with(|| {312 let collection_id = create_test_collection(&CollectionMode::ReFungible, 1);313314 let data = default_re_fungible_data();315 create_test_item(collection_id, &data.clone().into());316317 let origin1 = Origin::signed(1);318 let origin2 = Origin::signed(2);319 {320 let item = TemplateModule::refungible_item_id(collection_id, 1).unwrap();321 assert_eq!(item.const_data, data.const_data.into_inner());322 assert_eq!(item.variable_data, data.variable_data.into_inner());323 assert_eq!(324 item.owner[0],325 Ownership {326 owner: account(1),327 fraction: 1023328 }329 );330 }331 assert_eq!(TemplateModule::balance_count(1, 1), 1023);332 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);333334 // change owner scenario335 assert_ok!(TemplateModule::transfer(origin1, account(2), 1, 1, 1023));336 assert_eq!(337 TemplateModule::refungible_item_id(1, 1).unwrap().owner[0],338 Ownership {339 owner: account(2),340 fraction: 1023341 }342 );343 assert_eq!(TemplateModule::balance_count(1, 1), 0);344 assert_eq!(TemplateModule::balance_count(1, 2), 1023);345 // assert_eq!(TemplateModule::address_tokens(1, 1), []);346 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);347348 // split item scenario349 assert_ok!(TemplateModule::transfer(350 origin2.clone(),351 account(3),352 1,353 1,354 500355 ));356 {357 let item = TemplateModule::refungible_item_id(1, 1).unwrap();358 assert_eq!(359 item.owner[0],360 Ownership {361 owner: account(2),362 fraction: 523363 }364 );365 assert_eq!(366 item.owner[1],367 Ownership {368 owner: account(3),369 fraction: 500370 }371 );372 }373 assert_eq!(TemplateModule::balance_count(1, 2), 523);374 assert_eq!(TemplateModule::balance_count(1, 3), 500);375 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);376 assert_eq!(TemplateModule::address_tokens(1, 3), [1]);377378 // split item and new owner has account scenario379 assert_ok!(TemplateModule::transfer(origin2, account(3), 1, 1, 200));380 {381 let item = TemplateModule::refungible_item_id(1, 1).unwrap();382 assert_eq!(383 item.owner[0],384 Ownership {385 owner: account(2),386 fraction: 323387 }388 );389 assert_eq!(390 item.owner[1],391 Ownership {392 owner: account(3),393 fraction: 700394 }395 );396 }397 assert_eq!(TemplateModule::balance_count(1, 2), 323);398 assert_eq!(TemplateModule::balance_count(1, 3), 700);399 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);400 assert_eq!(TemplateModule::address_tokens(1, 3), [1]);401 });402}403404#[test]405fn transfer_nft_item() {406 new_test_ext().execute_with(|| {407 let collection_id = create_test_collection(&CollectionMode::NFT, 1);408409 let data = default_nft_data();410 create_test_item(collection_id, &data.into());411 assert_eq!(TemplateModule::balance_count(1, 1), 1);412 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);413414 let origin1 = Origin::signed(1);415 // default scenario416 assert_ok!(TemplateModule::transfer(origin1, account(2), 1, 1, 1000));417 assert_eq!(TemplateModule::nft_item_id(1, 1).unwrap().owner, account(2));418 assert_eq!(TemplateModule::balance_count(1, 1), 0);419 assert_eq!(TemplateModule::balance_count(1, 2), 1);420 // assert_eq!(TemplateModule::address_tokens(1, 1), []);421 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);422 });423}424425#[test]426fn nft_approve_and_transfer_from() {427 new_test_ext().execute_with(|| {428 let collection_id = create_test_collection(&CollectionMode::NFT, 1);429430 let data = default_nft_data();431 create_test_item(collection_id, &data.into());432433 let origin1 = Origin::signed(1);434 let origin2 = Origin::signed(2);435436 assert_eq!(TemplateModule::balance_count(1, 1), 1);437 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);438439 // neg transfer440 assert_noop!(441 TemplateModule::transfer_from(origin2.clone(), account(1), account(2), 1, 1, 1),442 Error::<Test>::NoPermission443 );444445 // do approve446 assert_ok!(TemplateModule::approve(origin1, account(2), 1, 1, 5));447 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 5);448 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 5);449450 assert_ok!(TemplateModule::transfer_from(451 origin2,452 account(1),453 account(3),454 1,455 1,456 1457 ));458 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 4);459 });460}461462#[test]463fn nft_approve_and_transfer_from_white_list() {464 new_test_ext().execute_with(|| {465 let collection_id = create_test_collection(&CollectionMode::NFT, 1);466467 let origin1 = Origin::signed(1);468 let origin2 = Origin::signed(2);469470 let data = default_nft_data();471 create_test_item(collection_id, &data.clone().into());472473 assert_eq!(474 &TemplateModule::nft_item_id(1, 1).unwrap().const_data,475 &data.const_data.into_inner()476 );477 assert_eq!(TemplateModule::balance_count(1, 1), 1);478 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);479480 assert_ok!(TemplateModule::set_mint_permission(481 origin1.clone(),482 1,483 true484 ));485 assert_ok!(TemplateModule::set_public_access_mode(486 origin1.clone(),487 1,488 AccessMode::WhiteList489 ));490 assert_ok!(TemplateModule::add_to_white_list(491 origin1.clone(),492 1,493 account(1)494 ));495 assert_ok!(TemplateModule::add_to_white_list(496 origin1.clone(),497 1,498 account(2)499 ));500 assert_ok!(TemplateModule::add_to_white_list(501 origin1.clone(),502 1,503 account(3)504 ));505506 // do approve507 assert_ok!(TemplateModule::approve(508 origin1.clone(),509 account(2),510 1,511 1,512 5513 ));514 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 5);515 assert_ok!(TemplateModule::approve(origin1, account(3), 1, 1, 5));516 assert_eq!(TemplateModule::approved(1, (1, 1, 3)), 5);517518 assert_ok!(TemplateModule::transfer_from(519 origin2,520 account(1),521 account(3),522 1,523 1,524 1525 ));526 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 4);527 });528}529530#[test]531fn refungible_approve_and_transfer_from() {532 new_test_ext().execute_with(|| {533 let collection_id = create_test_collection(&CollectionMode::ReFungible, 1);534535 let origin1 = Origin::signed(1);536 let origin2 = Origin::signed(2);537538 let data = default_re_fungible_data();539 create_test_item(collection_id, &data.into());540541 assert_eq!(TemplateModule::balance_count(1, 1), 1023);542 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);543544 assert_ok!(TemplateModule::set_mint_permission(545 origin1.clone(),546 1,547 true548 ));549 assert_ok!(TemplateModule::set_public_access_mode(550 origin1.clone(),551 1,552 AccessMode::WhiteList553 ));554 assert_ok!(TemplateModule::add_to_white_list(555 origin1.clone(),556 1,557 account(1)558 ));559 assert_ok!(TemplateModule::add_to_white_list(560 origin1.clone(),561 1,562 account(2)563 ));564 assert_ok!(TemplateModule::add_to_white_list(565 origin1.clone(),566 1,567 account(3)568 ));569570 // do approve571 assert_ok!(TemplateModule::approve(origin1, account(2), 1, 1, 1023));572 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 1023);573574 assert_ok!(TemplateModule::transfer_from(575 origin2,576 account(1),577 account(3),578 1,579 1,580 100581 ));582 assert_eq!(TemplateModule::balance_count(1, 1), 923);583 assert_eq!(TemplateModule::balance_count(1, 3), 100);584 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);585 assert_eq!(TemplateModule::address_tokens(1, 3), [1]);586587 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 923);588 });589}590591#[test]592fn fungible_approve_and_transfer_from() {593 new_test_ext().execute_with(|| {594 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);595596 let data = default_fungible_data();597 create_test_item(collection_id, &data.into());598599 let origin1 = Origin::signed(1);600 let origin2 = Origin::signed(2);601602 assert_eq!(TemplateModule::balance_count(1, 1), 5);603604 assert_ok!(TemplateModule::set_mint_permission(605 origin1.clone(),606 1,607 true608 ));609 assert_ok!(TemplateModule::set_public_access_mode(610 origin1.clone(),611 1,612 AccessMode::WhiteList613 ));614 assert_ok!(TemplateModule::add_to_white_list(615 origin1.clone(),616 1,617 account(1)618 ));619 assert_ok!(TemplateModule::add_to_white_list(620 origin1.clone(),621 1,622 account(2)623 ));624 assert_ok!(TemplateModule::add_to_white_list(625 origin1.clone(),626 1,627 account(3)628 ));629630 // do approve631 assert_ok!(TemplateModule::approve(632 origin1.clone(),633 account(2),634 1,635 1,636 5637 ));638 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 5);639 assert_ok!(TemplateModule::approve(origin1, account(3), 1, 1, 5));640 assert_eq!(TemplateModule::approved(1, (1, 1, 3)), 5);641 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 5);642643 assert_ok!(TemplateModule::transfer_from(644 origin2.clone(),645 account(1),646 account(3),647 1,648 1,649 4650 ));651 assert_eq!(TemplateModule::balance_count(1, 1), 1);652 assert_eq!(TemplateModule::balance_count(1, 3), 4);653654 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 1);655656 assert_noop!(657 TemplateModule::transfer_from(origin2, account(1), account(3), 1, 1, 4),658 Error::<Test>::NoPermission659 );660 });661}662663#[test]664fn change_collection_owner() {665 new_test_ext().execute_with(|| {666 let collection_id = create_test_collection(&CollectionMode::NFT, 1);667668 let origin1 = Origin::signed(1);669 assert_ok!(TemplateModule::change_collection_owner(670 origin1,671 collection_id,672 2673 ));674 assert_eq!(675 TemplateModule::collection_id(collection_id).unwrap().owner,676 2677 );678 });679}680681#[test]682fn destroy_collection() {683 new_test_ext().execute_with(|| {684 let collection_id = create_test_collection(&CollectionMode::NFT, 1);685686 let origin1 = Origin::signed(1);687 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));688 });689}690691#[test]692fn burn_nft_item() {693 new_test_ext().execute_with(|| {694 let collection_id = create_test_collection(&CollectionMode::NFT, 1);695696 let origin1 = Origin::signed(1);697 assert_ok!(TemplateModule::add_collection_admin(698 origin1.clone(),699 collection_id,700 account(2)701 ));702703 let data = default_nft_data();704 create_test_item(collection_id, &data.into());705706 // check balance (collection with id = 1, user id = 1)707 assert_eq!(TemplateModule::balance_count(1, 1), 1);708709 // burn item710 assert_ok!(TemplateModule::burn_item(711 origin1.clone(),712 collection_id,713 1,714 1715 ));716 assert_noop!(717 TemplateModule::burn_item(origin1, collection_id, 1, 1),718 Error::<Test>::TokenNotFound719 );720721 assert_eq!(TemplateModule::balance_count(1, 1), 0);722 });723}724725#[test]726fn burn_fungible_item() {727 new_test_ext().execute_with(|| {728 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);729730 let origin1 = Origin::signed(1);731 assert_ok!(TemplateModule::add_collection_admin(732 origin1.clone(),733 collection_id,734 account(2)735 ));736737 let data = default_fungible_data();738 create_test_item(collection_id, &data.into());739740 // check balance (collection with id = 1, user id = 1)741 assert_eq!(TemplateModule::balance_count(1, 1), 5);742743 // burn item744 assert_ok!(TemplateModule::burn_item(745 origin1.clone(),746 1,747 1,748 5749 ));750 assert_noop!(751 TemplateModule::burn_item(origin1, 1, 1, 5),752 Error::<Test>::TokenValueNotEnough753 );754755 assert_eq!(TemplateModule::balance_count(1, 1), 0);756 });757}758759#[test]760fn burn_refungible_item() {761 new_test_ext().execute_with(|| {762 let collection_id = create_test_collection(&CollectionMode::ReFungible, 1);763 let origin1 = Origin::signed(1);764765 assert_ok!(TemplateModule::set_mint_permission(766 origin1.clone(),767 collection_id,768 true769 ));770 assert_ok!(TemplateModule::set_public_access_mode(771 origin1.clone(),772 collection_id,773 AccessMode::WhiteList774 ));775 assert_ok!(TemplateModule::add_to_white_list(776 origin1.clone(),777 1,778 account(1)779 ));780781 assert_ok!(TemplateModule::add_collection_admin(782 origin1.clone(),783 1,784 account(2)785 ));786787 let data = default_re_fungible_data();788 create_test_item(collection_id, &data.into());789790 // check balance (collection with id = 1, user id = 2)791 assert_eq!(TemplateModule::balance_count(1, 1), 1023);792793 // burn item794 assert_ok!(TemplateModule::burn_item(795 origin1.clone(),796 1,797 1,798 1023799 ));800 assert_noop!(801 TemplateModule::burn_item(origin1, 1, 1, 1023),802 Error::<Test>::TokenNotFound803 );804805 assert_eq!(TemplateModule::balance_count(1, 1), 0);806 });807}808809#[test]810fn add_collection_admin() {811 new_test_ext().execute_with(|| {812 let collection1_id = create_test_collection_for_owner(&CollectionMode::NFT, 1, 1);813 create_test_collection_for_owner(&CollectionMode::NFT, 2, 2);814 create_test_collection_for_owner(&CollectionMode::NFT, 3, 3);815816 let origin1 = Origin::signed(1);817818 // collection admin819 assert_ok!(TemplateModule::add_collection_admin(820 origin1.clone(),821 collection1_id,822 account(2)823 ));824 assert_ok!(TemplateModule::add_collection_admin(825 origin1,826 collection1_id,827 account(3)828 ));829830 assert!(TemplateModule::admin_list_collection(collection1_id).contains(&account(2)),);831 assert!(TemplateModule::admin_list_collection(collection1_id).contains(&account(3)),);832 });833}834835#[test]836fn remove_collection_admin() {837 new_test_ext().execute_with(|| {838 let collection1_id = create_test_collection_for_owner(&CollectionMode::NFT, 1, 1);839 create_test_collection_for_owner(&CollectionMode::NFT, 2, 2);840 create_test_collection_for_owner(&CollectionMode::NFT, 3, 3);841842 let origin1 = Origin::signed(1);843 let origin2 = Origin::signed(2);844845 // collection admin846 assert_ok!(TemplateModule::add_collection_admin(847 origin1.clone(),848 collection1_id,849 account(2)850 ));851 assert_ok!(TemplateModule::add_collection_admin(852 origin1,853 collection1_id,854 account(3)855 ));856857 assert!(TemplateModule::admin_list_collection(1).contains(&account(2)),);858 assert!(TemplateModule::admin_list_collection(1).contains(&account(3)),);859860 // remove admin861 assert_ok!(TemplateModule::remove_collection_admin(862 origin2,863 1,864 account(3)865 ));866 assert!(!TemplateModule::admin_list_collection(1).contains(&account(3)),);867 });868}869870#[test]871fn balance_of() {872 new_test_ext().execute_with(|| {873 let nft_collection_id = create_test_collection(&CollectionMode::NFT, 1);874 let fungible_collection_id = create_test_collection(&CollectionMode::Fungible(3), 2);875 let re_fungible_collection_id = create_test_collection(&CollectionMode::ReFungible, 3);876877 // check balance before878 assert_eq!(TemplateModule::balance_count(nft_collection_id, 1), 0);879 assert_eq!(TemplateModule::balance_count(fungible_collection_id, 1), 0);880 assert_eq!(881 TemplateModule::balance_count(re_fungible_collection_id, 1),882 0883 );884885 let nft_data = default_nft_data();886 create_test_item(nft_collection_id, &nft_data.into());887888 let fungible_data = default_fungible_data();889 create_test_item(fungible_collection_id, &fungible_data.into());890891 let re_fungible_data = default_re_fungible_data();892 create_test_item(re_fungible_collection_id, &re_fungible_data.into());893894 // check balance (collection with id = 1, user id = 1)895 assert_eq!(TemplateModule::balance_count(nft_collection_id, 1), 1);896 assert_eq!(TemplateModule::balance_count(fungible_collection_id, 1), 5);897 assert_eq!(898 TemplateModule::balance_count(re_fungible_collection_id, 1),899 1023900 );901 assert_eq!(902 TemplateModule::nft_item_id(nft_collection_id, 1)903 .unwrap()904 .owner,905 account(1)906 );907 assert_eq!(908 TemplateModule::fungible_item_id(fungible_collection_id, 1).value,909 5910 );911 assert_eq!(912 TemplateModule::refungible_item_id(re_fungible_collection_id, 1)913 .unwrap()914 .owner[0]915 .owner,916 account(1)917 );918 });919}920921#[test]922fn approve() {923 new_test_ext().execute_with(|| {924 let collection_id = create_test_collection(&CollectionMode::NFT, 1);925926 let data = default_nft_data();927 create_test_item(collection_id, &data.into());928929 let origin1 = Origin::signed(1);930931 // approve932 assert_ok!(TemplateModule::approve(origin1, account(2), 1, 1, 1));933 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 1);934 });935}936937#[test]938fn transfer_from() {939 new_test_ext().execute_with(|| {940 let collection_id = create_test_collection(&CollectionMode::NFT, 1);941 let origin1 = Origin::signed(1);942 let origin2 = Origin::signed(2);943944 let data = default_nft_data();945 create_test_item(collection_id, &data.into());946947 // approve948 assert_ok!(TemplateModule::approve(949 origin1.clone(),950 account(2),951 1,952 1,953 1954 ));955 assert_eq!(TemplateModule::approved(1, (1, 1, 2)), 1);956957 assert_ok!(TemplateModule::set_mint_permission(958 origin1.clone(),959 1,960 true961 ));962 assert_ok!(TemplateModule::set_public_access_mode(963 origin1.clone(),964 1,965 AccessMode::WhiteList966 ));967 assert_ok!(TemplateModule::add_to_white_list(968 origin1.clone(),969 1,970 account(1)971 ));972 assert_ok!(TemplateModule::add_to_white_list(973 origin1.clone(),974 1,975 account(2)976 ));977 assert_ok!(TemplateModule::add_to_white_list(origin1, 1, account(3)));978979 assert_ok!(TemplateModule::transfer_from(980 origin2,981 account(1),982 account(2),983 1,984 1,985 1986 ));987988 // after transfer989 assert_eq!(TemplateModule::balance_count(1, 1), 0);990 assert_eq!(TemplateModule::balance_count(1, 2), 1);991 });992}993994// #endregion995996// Coverage tests region997// #region998999#[test]1000fn owner_can_add_address_to_white_list() {1001 new_test_ext().execute_with(|| {1002 let collection_id = create_test_collection(&CollectionMode::NFT, 1);10031004 let origin1 = Origin::signed(1);1005 assert_ok!(TemplateModule::add_to_white_list(1006 origin1,1007 collection_id,1008 account(2)1009 ));1010 assert!(TemplateModule::white_list(collection_id, 2));1011 });1012}10131014#[test]1015fn admin_can_add_address_to_white_list() {1016 new_test_ext().execute_with(|| {1017 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1018 let origin1 = Origin::signed(1);1019 let origin2 = Origin::signed(2);10201021 assert_ok!(TemplateModule::add_collection_admin(1022 origin1,1023 collection_id,1024 account(2)1025 ));1026 assert_ok!(TemplateModule::add_to_white_list(1027 origin2,1028 collection_id,1029 account(3)1030 ));1031 assert!(TemplateModule::white_list(collection_id, 3));1032 });1033}10341035#[test]1036fn nonprivileged_user_cannot_add_address_to_white_list() {1037 new_test_ext().execute_with(|| {1038 let collection_id = create_test_collection(&CollectionMode::NFT, 1);10391040 let origin2 = Origin::signed(2);1041 assert_noop!(1042 TemplateModule::add_to_white_list(origin2, collection_id, account(3)),1043 Error::<Test>::NoPermission1044 );1045 });1046}10471048#[test]1049fn nobody_can_add_address_to_white_list_of_nonexisting_collection() {1050 new_test_ext().execute_with(|| {1051 let origin1 = Origin::signed(1);10521053 assert_noop!(1054 TemplateModule::add_to_white_list(origin1, 1, account(2)),1055 Error::<Test>::CollectionNotFound1056 );1057 });1058}10591060#[test]1061fn nobody_can_add_address_to_white_list_of_deleted_collection() {1062 new_test_ext().execute_with(|| {1063 let collection_id = create_test_collection(&CollectionMode::NFT, 1);10641065 let origin1 = Origin::signed(1);1066 assert_ok!(TemplateModule::destroy_collection(1067 origin1.clone(),1068 collection_id1069 ));1070 assert_noop!(1071 TemplateModule::add_to_white_list(origin1, collection_id, account(2)),1072 Error::<Test>::CollectionNotFound1073 );1074 });1075}10761077// If address is already added to white list, nothing happens1078#[test]1079fn address_is_already_added_to_white_list() {1080 new_test_ext().execute_with(|| {1081 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1082 let origin1 = Origin::signed(1);10831084 assert_ok!(TemplateModule::add_to_white_list(1085 origin1.clone(),1086 collection_id,1087 account(2)1088 ));1089 assert_ok!(TemplateModule::add_to_white_list(1090 origin1,1091 collection_id,1092 account(2)1093 ));1094 assert!(TemplateModule::white_list(collection_id, 2));1095 });1096}10971098#[test]1099fn owner_can_remove_address_from_white_list() {1100 new_test_ext().execute_with(|| {1101 let collection_id = create_test_collection(&CollectionMode::NFT, 1);11021103 let origin1 = Origin::signed(1);1104 assert_ok!(TemplateModule::add_to_white_list(1105 origin1.clone(),1106 collection_id,1107 account(2)1108 ));1109 assert_ok!(TemplateModule::remove_from_white_list(1110 origin1,1111 collection_id,1112 account(2)1113 ));1114 assert!(!TemplateModule::white_list(collection_id, 2));1115 });1116}11171118#[test]1119fn admin_can_remove_address_from_white_list() {1120 new_test_ext().execute_with(|| {1121 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1122 let origin1 = Origin::signed(1);1123 let origin2 = Origin::signed(2);11241125 assert_ok!(TemplateModule::add_collection_admin(1126 origin1.clone(),1127 collection_id,1128 account(2)1129 ));11301131 assert_ok!(TemplateModule::add_to_white_list(1132 origin1,1133 collection_id,1134 account(3)1135 ));1136 assert_ok!(TemplateModule::remove_from_white_list(1137 origin2,1138 collection_id,1139 account(3)1140 ));1141 assert!(!TemplateModule::white_list(collection_id, 3));1142 });1143}11441145#[test]1146fn nonprivileged_user_cannot_remove_address_from_white_list() {1147 new_test_ext().execute_with(|| {1148 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1149 let origin1 = Origin::signed(1);1150 let origin2 = Origin::signed(2);11511152 assert_ok!(TemplateModule::add_to_white_list(1153 origin1,1154 collection_id,1155 account(2)1156 ));1157 assert_noop!(1158 TemplateModule::remove_from_white_list(origin2, collection_id, account(2)),1159 Error::<Test>::NoPermission1160 );1161 assert!(TemplateModule::white_list(collection_id, 2));1162 });1163}11641165#[test]1166fn nobody_can_remove_address_from_white_list_of_nonexisting_collection() {1167 new_test_ext().execute_with(|| {1168 let origin1 = Origin::signed(1);11691170 assert_noop!(1171 TemplateModule::remove_from_white_list(origin1, 1, account(2)),1172 Error::<Test>::CollectionNotFound1173 );1174 });1175}11761177#[test]1178fn nobody_can_remove_address_from_white_list_of_deleted_collection() {1179 new_test_ext().execute_with(|| {1180 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1181 let origin1 = Origin::signed(1);1182 let origin2 = Origin::signed(2);11831184 assert_ok!(TemplateModule::add_to_white_list(1185 origin1.clone(),1186 collection_id,1187 account(2)1188 ));1189 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));1190 assert_noop!(1191 TemplateModule::remove_from_white_list(origin2, collection_id, account(2)),1192 Error::<Test>::CollectionNotFound1193 );1194 assert!(!TemplateModule::white_list(collection_id, 2));1195 });1196}11971198// If address is already removed from white list, nothing happens1199#[test]1200fn address_is_already_removed_from_white_list() {1201 new_test_ext().execute_with(|| {1202 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1203 let origin1 = Origin::signed(1);12041205 assert_ok!(TemplateModule::add_to_white_list(1206 origin1.clone(),1207 collection_id,1208 account(2)1209 ));1210 assert_ok!(TemplateModule::remove_from_white_list(1211 origin1.clone(),1212 collection_id,1213 account(2)1214 ));1215 assert_ok!(TemplateModule::remove_from_white_list(1216 origin1,1217 collection_id,1218 account(2)1219 ));1220 assert!(!TemplateModule::white_list(collection_id, 2));1221 });1222}12231224// If Public Access mode is set to WhiteList, tokens can’t be transferred from a non-whitelisted address with transfer or transferFrom (2 tests)1225#[test]1226fn white_list_test_1() {1227 new_test_ext().execute_with(|| {1228 let collection_id = create_test_collection(&CollectionMode::NFT, 1);12291230 let origin1 = Origin::signed(1);12311232 let data = default_nft_data();1233 create_test_item(collection_id, &data.into());12341235 assert_ok!(TemplateModule::set_public_access_mode(1236 origin1.clone(),1237 collection_id,1238 AccessMode::WhiteList1239 ));1240 assert_ok!(TemplateModule::add_to_white_list(1241 origin1.clone(),1242 collection_id,1243 account(2)1244 ));12451246 assert_noop!(1247 TemplateModule::transfer(origin1, account(3), 1, 1, 1),1248 Error::<Test>::AddresNotInWhiteList1249 );1250 });1251}12521253#[test]1254fn white_list_test_2() {1255 new_test_ext().execute_with(|| {1256 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1257 let origin1 = Origin::signed(1);12581259 let data = default_nft_data();1260 create_test_item(collection_id, &data.into());12611262 assert_ok!(TemplateModule::set_public_access_mode(1263 origin1.clone(),1264 collection_id,1265 AccessMode::WhiteList1266 ));1267 assert_ok!(TemplateModule::add_to_white_list(1268 origin1.clone(),1269 1,1270 account(1)1271 ));1272 assert_ok!(TemplateModule::add_to_white_list(1273 origin1.clone(),1274 1,1275 account(2)1276 ));12771278 // do approve1279 assert_ok!(TemplateModule::approve(1280 origin1.clone(),1281 account(1),1282 1,1283 1,1284 11285 ));1286 assert_eq!(TemplateModule::approved(1, (1, 1, 1)), 1);12871288 assert_ok!(TemplateModule::remove_from_white_list(1289 origin1.clone(),1290 1,1291 account(1)1292 ));12931294 assert_noop!(1295 TemplateModule::transfer_from(origin1, account(1), account(3), 1, 1, 1),1296 Error::<Test>::AddresNotInWhiteList1297 );1298 });1299}13001301// If Public Access mode is set to WhiteList, tokens can’t be transferred to a non-whitelisted address with transfer or transferFrom (2 tests)1302#[test]1303fn white_list_test_3() {1304 new_test_ext().execute_with(|| {1305 let collection_id = create_test_collection(&CollectionMode::NFT, 1);13061307 let origin1 = Origin::signed(1);13081309 let data = default_nft_data();1310 create_test_item(collection_id, &data.into());13111312 assert_ok!(TemplateModule::set_public_access_mode(1313 origin1.clone(),1314 collection_id,1315 AccessMode::WhiteList1316 ));1317 assert_ok!(TemplateModule::add_to_white_list(1318 origin1.clone(),1319 1,1320 account(1)1321 ));13221323 assert_noop!(1324 TemplateModule::transfer(origin1, account(3), 1, 1, 1),1325 Error::<Test>::AddresNotInWhiteList1326 );1327 });1328}13291330#[test]1331fn white_list_test_4() {1332 new_test_ext().execute_with(|| {1333 let collection_id = create_test_collection(&CollectionMode::NFT, 1);13341335 let origin1 = Origin::signed(1);13361337 let data = default_nft_data();1338 create_test_item(collection_id, &data.into());13391340 assert_ok!(TemplateModule::set_public_access_mode(1341 origin1.clone(),1342 collection_id,1343 AccessMode::WhiteList1344 ));1345 assert_ok!(TemplateModule::add_to_white_list(1346 origin1.clone(),1347 collection_id,1348 account(1)1349 ));1350 assert_ok!(TemplateModule::add_to_white_list(1351 origin1.clone(),1352 collection_id,1353 account(2)1354 ));13551356 // do approve1357 assert_ok!(TemplateModule::approve(1358 origin1.clone(),1359 account(1),1360 1,1361 1,1362 11363 ));1364 assert_eq!(TemplateModule::approved(1, (1, 1, 1)), 1);13651366 assert_ok!(TemplateModule::remove_from_white_list(1367 origin1.clone(),1368 collection_id,1369 account(2)1370 ));13711372 assert_noop!(1373 TemplateModule::transfer_from(origin1, account(1), account(3), 1, 1, 1),1374 Error::<Test>::AddresNotInWhiteList1375 );1376 });1377}13781379// If Public Access mode is set to WhiteList, tokens can’t be destroyed by a non-whitelisted address (even if it owned them before enabling WhiteList mode)1380#[test]1381fn white_list_test_5() {1382 new_test_ext().execute_with(|| {1383 let collection_id = create_test_collection(&CollectionMode::NFT, 1);13841385 let origin1 = Origin::signed(1);13861387 let data = default_nft_data();1388 create_test_item(collection_id, &data.into());13891390 assert_ok!(TemplateModule::set_public_access_mode(1391 origin1.clone(),1392 collection_id,1393 AccessMode::WhiteList1394 ));1395 assert_noop!(1396 TemplateModule::burn_item(origin1.clone(), 1, 1, 5),1397 Error::<Test>::AddresNotInWhiteList1398 );1399 });1400}14011402// If Public Access mode is set to WhiteList, token transfers can’t be Approved by a non-whitelisted address (see Approve method).1403#[test]1404fn white_list_test_6() {1405 new_test_ext().execute_with(|| {1406 let collection_id = create_test_collection(&CollectionMode::NFT, 1);14071408 let origin1 = Origin::signed(1);14091410 let data = default_nft_data();1411 create_test_item(collection_id, &data.into());14121413 assert_ok!(TemplateModule::set_public_access_mode(1414 origin1.clone(),1415 collection_id,1416 AccessMode::WhiteList1417 ));14181419 // do approve1420 assert_noop!(1421 TemplateModule::approve(origin1, account(1), 1, 1, 5),1422 Error::<Test>::AddresNotInWhiteList1423 );1424 });1425}14261427// If Public Access mode is set to WhiteList, tokens can be transferred from a whitelisted address with transfer or transferFrom (2 tests) and1428// tokens can be transferred from a whitelisted address with transfer or transferFrom (2 tests)1429#[test]1430fn white_list_test_7() {1431 new_test_ext().execute_with(|| {1432 let collection_id = create_test_collection(&CollectionMode::NFT, 1);14331434 let data = default_nft_data();1435 create_test_item(collection_id, &data.into());14361437 let origin1 = Origin::signed(1);14381439 assert_ok!(TemplateModule::set_public_access_mode(1440 origin1.clone(),1441 collection_id,1442 AccessMode::WhiteList1443 ));1444 assert_ok!(TemplateModule::add_to_white_list(1445 origin1.clone(),1446 collection_id,1447 account(1)1448 ));1449 assert_ok!(TemplateModule::add_to_white_list(1450 origin1.clone(),1451 collection_id,1452 account(2)1453 ));14541455 assert_ok!(TemplateModule::transfer(origin1, account(2), 1, 1, 1));1456 });1457}14581459#[test]1460fn white_list_test_8() {1461 new_test_ext().execute_with(|| {1462 let collection_id = create_test_collection(&CollectionMode::NFT, 1);14631464 let data = default_nft_data();1465 create_test_item(collection_id, &data.into());14661467 let origin1 = Origin::signed(1);14681469 assert_ok!(TemplateModule::set_public_access_mode(1470 origin1.clone(),1471 collection_id,1472 AccessMode::WhiteList1473 ));1474 assert_ok!(TemplateModule::add_to_white_list(1475 origin1.clone(),1476 collection_id,1477 account(1)1478 ));1479 assert_ok!(TemplateModule::add_to_white_list(1480 origin1.clone(),1481 collection_id,1482 account(2)1483 ));14841485 // do approve1486 assert_ok!(TemplateModule::approve(1487 origin1.clone(),1488 account(1),1489 1,1490 1,1491 51492 ));1493 assert_eq!(TemplateModule::approved(1, (1, 1, 1)), 5);14941495 assert_ok!(TemplateModule::transfer_from(1496 origin1,1497 account(1),1498 account(2),1499 1,1500 1,1501 11502 ));1503 });1504}15051506// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by owner.1507#[test]1508fn white_list_test_9() {1509 new_test_ext().execute_with(|| {1510 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1511 let origin1 = Origin::signed(1);15121513 assert_ok!(TemplateModule::set_public_access_mode(1514 origin1.clone(),1515 collection_id,1516 AccessMode::WhiteList1517 ));1518 assert_ok!(TemplateModule::set_mint_permission(1519 origin1,1520 collection_id,1521 false1522 ));15231524 let data = default_nft_data();1525 create_test_item(collection_id, &data.into());1526 });1527}15281529// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by admin.1530#[test]1531fn white_list_test_10() {1532 new_test_ext().execute_with(|| {1533 let collection_id = create_test_collection(&CollectionMode::NFT, 1);15341535 let origin1 = Origin::signed(1);1536 let origin2 = Origin::signed(2);15371538 assert_ok!(TemplateModule::set_public_access_mode(1539 origin1.clone(),1540 collection_id,1541 AccessMode::WhiteList1542 ));1543 assert_ok!(TemplateModule::set_mint_permission(1544 origin1.clone(),1545 collection_id,1546 false1547 ));15481549 assert_ok!(TemplateModule::add_collection_admin(1550 origin1,1551 collection_id,1552 account(2)1553 ));15541555 assert_ok!(TemplateModule::create_item(1556 origin2,1557 collection_id,1558 account(2),1559 default_nft_data().into()1560 ));1561 });1562}15631564// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and white listed address.1565#[test]1566fn white_list_test_11() {1567 new_test_ext().execute_with(|| {1568 let collection_id = create_test_collection(&CollectionMode::NFT, 1);15691570 let origin1 = Origin::signed(1);1571 let origin2 = Origin::signed(2);15721573 assert_ok!(TemplateModule::set_public_access_mode(1574 origin1.clone(),1575 collection_id,1576 AccessMode::WhiteList1577 ));1578 assert_ok!(TemplateModule::set_mint_permission(1579 origin1.clone(),1580 collection_id,1581 false1582 ));1583 assert_ok!(TemplateModule::add_to_white_list(1584 origin1,1585 collection_id,1586 account(2)1587 ));15881589 assert_noop!(1590 TemplateModule::create_item(origin2, 1, account(2), default_nft_data().into()),1591 Error::<Test>::PublicMintingNotAllowed1592 );1593 });1594}15951596// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-white listed address.1597#[test]1598fn white_list_test_12() {1599 new_test_ext().execute_with(|| {1600 let collection_id = create_test_collection(&CollectionMode::NFT, 1);16011602 let origin1 = Origin::signed(1);1603 let origin2 = Origin::signed(2);16041605 assert_ok!(TemplateModule::set_public_access_mode(1606 origin1.clone(),1607 collection_id,1608 AccessMode::WhiteList1609 ));1610 assert_ok!(TemplateModule::set_mint_permission(1611 origin1,1612 collection_id,1613 false1614 ));16151616 assert_noop!(1617 TemplateModule::create_item(origin2, 1, account(2), default_nft_data().into()),1618 Error::<Test>::PublicMintingNotAllowed1619 );1620 });1621}16221623// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by owner.1624#[test]1625fn white_list_test_13() {1626 new_test_ext().execute_with(|| {1627 let collection_id = create_test_collection(&CollectionMode::NFT, 1);16281629 let origin1 = Origin::signed(1);16301631 assert_ok!(TemplateModule::set_public_access_mode(1632 origin1.clone(),1633 collection_id,1634 AccessMode::WhiteList1635 ));1636 assert_ok!(TemplateModule::set_mint_permission(1637 origin1,1638 collection_id,1639 true1640 ));16411642 let data = default_nft_data();1643 create_test_item(collection_id, &data.into());1644 });1645}16461647// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by admin.1648#[test]1649fn white_list_test_14() {1650 new_test_ext().execute_with(|| {1651 let collection_id = create_test_collection(&CollectionMode::NFT, 1);16521653 let origin1 = Origin::signed(1);1654 let origin2 = Origin::signed(2);16551656 assert_ok!(TemplateModule::set_public_access_mode(1657 origin1.clone(),1658 collection_id,1659 AccessMode::WhiteList1660 ));1661 assert_ok!(TemplateModule::set_mint_permission(1662 origin1.clone(),1663 collection_id,1664 true1665 ));16661667 assert_ok!(TemplateModule::add_collection_admin(1668 origin1,1669 collection_id,1670 account(2)1671 ));16721673 assert_ok!(TemplateModule::create_item(1674 origin2,1675 1,1676 account(2),1677 default_nft_data().into()1678 ));1679 });1680}16811682// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-white listed address.1683#[test]1684fn white_list_test_15() {1685 new_test_ext().execute_with(|| {1686 let collection_id = create_test_collection(&CollectionMode::NFT, 1);16871688 let origin1 = Origin::signed(1);1689 let origin2 = Origin::signed(2);16901691 assert_ok!(TemplateModule::set_public_access_mode(1692 origin1.clone(),1693 collection_id,1694 AccessMode::WhiteList1695 ));1696 assert_ok!(TemplateModule::set_mint_permission(1697 origin1,1698 collection_id,1699 true1700 ));17011702 assert_noop!(1703 TemplateModule::create_item(origin2, 1, account(2), default_nft_data().into()),1704 Error::<Test>::AddresNotInWhiteList1705 );1706 });1707}17081709// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by non-privileged and white listed address.1710#[test]1711fn white_list_test_16() {1712 new_test_ext().execute_with(|| {1713 let collection_id = create_test_collection(&CollectionMode::NFT, 1);17141715 let origin1 = Origin::signed(1);1716 let origin2 = Origin::signed(2);17171718 assert_ok!(TemplateModule::set_public_access_mode(1719 origin1.clone(),1720 collection_id,1721 AccessMode::WhiteList1722 ));1723 assert_ok!(TemplateModule::set_mint_permission(1724 origin1.clone(),1725 collection_id,1726 true1727 ));1728 assert_ok!(TemplateModule::add_to_white_list(1729 origin1,1730 collection_id,1731 account(2)1732 ));17331734 assert_ok!(TemplateModule::create_item(1735 origin2,1736 1,1737 account(2),1738 default_nft_data().into()1739 ));1740 });1741}17421743// Total number of collections. Positive test1744#[test]1745fn total_number_collections_bound() {1746 new_test_ext().execute_with(|| {1747 create_test_collection(&CollectionMode::NFT, 1);1748 });1749}17501751// Total number of collections. Negotive test1752#[test]1753fn total_number_collections_bound_neg() {1754 new_test_ext().execute_with(|| {1755 let origin1 = Origin::signed(1);17561757 for i in 0..COLLECTION_NUMBER_LIMIT {1758 create_test_collection(&CollectionMode::NFT, i + 1);1759 }17601761 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();1762 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();1763 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();17641765 // 11-th collection in chain. Expects error1766 assert_noop!(1767 TemplateModule::create_collection(1768 origin1,1769 col_name1,1770 col_desc1,1771 token_prefix1,1772 CollectionMode::NFT1773 ),1774 Error::<Test>::TotalCollectionsLimitExceeded1775 );1776 });1777}17781779// Owned tokens by a single address. Positive test1780#[test]1781fn owned_tokens_bound() {1782 new_test_ext().execute_with(|| {1783 let collection_id = create_test_collection(&CollectionMode::NFT, 1);17841785 let data = default_nft_data();1786 create_test_item(collection_id, &data.clone().into());1787 create_test_item(collection_id, &data.into());1788 });1789}17901791// Owned tokens by a single address. Negotive test1792#[test]1793fn owned_tokens_bound_neg() {1794 new_test_ext().execute_with(|| {1795 let collection_id = create_test_collection(&CollectionMode::NFT, 1);17961797 let origin1 = Origin::signed(1);17981799 for _ in 0..ACCOUNT_TOKEN_OWNERSHIP_LIMIT {1800 let data = default_nft_data();1801 create_test_item(collection_id, &data.clone().into());1802 }18031804 let data = default_nft_data();1805 assert_noop!(1806 TemplateModule::create_item(origin1, 1, account(1), data.into()),1807 Error::<Test>::AccountTokenLimitExceeded1808 );1809 });1810}18111812// Number of collection admins. Positive test1813#[test]1814fn collection_admins_bound() {1815 new_test_ext().execute_with(|| {1816 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18171818 let origin1 = Origin::signed(1);18191820 assert_ok!(TemplateModule::add_collection_admin(1821 origin1.clone(),1822 collection_id,1823 account(2)1824 ));1825 assert_ok!(TemplateModule::add_collection_admin(1826 origin1,1827 collection_id,1828 account(3)1829 ));1830 });1831}18321833// Number of collection admins. Negotive test1834#[test]1835fn collection_admins_bound_neg() {1836 new_test_ext().execute_with(|| {1837 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18381839 let origin1 = Origin::signed(1);18401841 for i in 0..COLLECTION_ADMINS_LIMIT {1842 assert_ok!(TemplateModule::add_collection_admin(1843 origin1.clone(),1844 collection_id,1845 account(2 + i)1846 ));1847 }1848 assert_noop!(1849 TemplateModule::add_collection_admin(1850 origin1,1851 collection_id,1852 account(3 + COLLECTION_ADMINS_LIMIT)1853 ),1854 Error::<Test>::CollectionAdminsLimitExceeded1855 );1856 });1857}1858// #endregion18591860#[test]1861fn set_const_on_chain_schema() {1862 new_test_ext().execute_with(|| {1863 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18641865 let origin1 = Origin::signed(1);1866 assert_ok!(TemplateModule::set_const_on_chain_schema(1867 origin1,1868 collection_id,1869 b"test const on chain schema".to_vec()1870 ));18711872 assert_eq!(1873 TemplateModule::collection_id(collection_id)1874 .unwrap()1875 .const_on_chain_schema,1876 b"test const on chain schema".to_vec()1877 );1878 assert_eq!(1879 TemplateModule::collection_id(collection_id)1880 .unwrap()1881 .variable_on_chain_schema,1882 b"".to_vec()1883 );1884 });1885}18861887#[test]1888fn set_variable_on_chain_schema() {1889 new_test_ext().execute_with(|| {1890 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18911892 let origin1 = Origin::signed(1);1893 assert_ok!(TemplateModule::set_variable_on_chain_schema(1894 origin1,1895 collection_id,1896 b"test variable on chain schema".to_vec()1897 ));18981899 assert_eq!(1900 TemplateModule::collection_id(collection_id)1901 .unwrap()1902 .const_on_chain_schema,1903 b"".to_vec()1904 );1905 assert_eq!(1906 TemplateModule::collection_id(collection_id)1907 .unwrap()1908 .variable_on_chain_schema,1909 b"test variable on chain schema".to_vec()1910 );1911 });1912}19131914#[test]1915fn set_variable_meta_data_on_nft_token_stores_variable_meta_data() {1916 new_test_ext().execute_with(|| {1917 let collection_id = create_test_collection(&CollectionMode::NFT, 1);19181919 let origin1 = Origin::signed(1);19201921 let data = default_nft_data();1922 create_test_item(1, &data.into());19231924 let variable_data = b"test data".to_vec();1925 assert_ok!(TemplateModule::set_variable_meta_data(1926 origin1,1927 collection_id,1928 1,1929 variable_data.clone()1930 ));19311932 assert_eq!(1933 TemplateModule::nft_item_id(collection_id, 1)1934 .unwrap()1935 .variable_data,1936 variable_data1937 );1938 });1939}19401941#[test]1942fn set_variable_meta_data_on_re_fungible_token_stores_variable_meta_data() {1943 new_test_ext().execute_with(|| {1944 let collection_id = create_test_collection(&CollectionMode::ReFungible, 1);19451946 let origin1 = Origin::signed(1);19471948 let data = default_re_fungible_data();1949 create_test_item(1, &data.into());19501951 let variable_data = b"test data".to_vec();1952 assert_ok!(TemplateModule::set_variable_meta_data(1953 origin1,1954 collection_id,1955 1,1956 variable_data.clone()1957 ));19581959 assert_eq!(1960 TemplateModule::refungible_item_id(collection_id, 1)1961 .unwrap()1962 .variable_data,1963 variable_data1964 );1965 });1966}19671968#[test]1969fn set_variable_meta_data_on_fungible_token_fails() {1970 new_test_ext().execute_with(|| {1971 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);19721973 let origin1 = Origin::signed(1);19741975 let data = default_fungible_data();1976 create_test_item(1, &data.into());19771978 let variable_data = b"test data".to_vec();1979 assert_noop!(1980 TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data),1981 Error::<Test>::CantStoreMetadataInFungibleTokens1982 );1983 });1984}19851986#[test]1987fn set_variable_meta_data_on_nft_token_fails_for_big_data() {1988 new_test_ext().execute_with(|| {1989 let collection_id = create_test_collection(&CollectionMode::NFT, 1);19901991 let origin1 = Origin::signed(1);19921993 let data = default_nft_data();1994 create_test_item(1, &data.into());19951996 let variable_data = b"test set_variable_meta_data method, bigger than limits.".to_vec();1997 assert_noop!(1998 TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data),1999 Error::<Test>::TokenVariableDataLimitExceeded2000 );2001 });2002}20032004#[test]2005fn set_variable_meta_data_on_re_fungible_token_fails_for_big_data() {2006 new_test_ext().execute_with(|| {2007 let collection_id = create_test_collection(&CollectionMode::ReFungible, 1);20082009 let origin1 = Origin::signed(1);20102011 let data = default_re_fungible_data();2012 create_test_item(1, &data.into());20132014 let variable_data = b"test set_variable_meta_data method, bigger than limits.".to_vec();2015 assert_noop!(2016 TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data),2017 Error::<Test>::TokenVariableDataLimitExceeded2018 );2019 });2020}20212022#[test]2023fn set_variable_meta_data_on_nft_with_item_owner_permission_flag() {2024 new_test_ext().execute_with(|| {2025 //default_limits();20262027 let collection_id = create_test_collection(&CollectionMode::NFT, 1);20282029 let origin1 = Origin::signed(1);20302031 let data = default_nft_data();2032 create_test_item(1, &data.into());20332034 assert_ok!(TemplateModule::set_meta_update_permission_flag(2035 origin1.clone(),2036 collection_id,2037 MetaUpdatePermission::ItemOwner,2038 ));20392040 let variable_data = b"ten chars.".to_vec();2041 assert_ok!(TemplateModule::set_variable_meta_data(2042 origin1,2043 collection_id,2044 1,2045 variable_data.clone()2046 ));20472048 assert_eq!(2049 TemplateModule::nft_item_id(collection_id, 1)2050 .unwrap()2051 .variable_data,2052 variable_data2053 );2054 });2055}20562057#[test]2058fn set_variable_meta_data_on_nft_with_item_owner_permission_flag_neg() {2059 new_test_ext().execute_with(|| {2060 let collection_id = create_test_collection_for_owner(&CollectionMode::NFT, 1, 1);20612062 let origin1 = Origin::signed(1);20632064 assert_ok!(TemplateModule::set_mint_permission(2065 origin1.clone(),2066 collection_id,2067 true2068 ));2069 assert_ok!(TemplateModule::add_to_white_list(2070 origin1.clone(),2071 collection_id,2072 account(1)2073 ));20742075 let data = default_nft_data();2076 create_test_item(1, &data.into());20772078 assert_ok!(TemplateModule::set_meta_update_permission_flag(2079 origin1.clone(),2080 collection_id,2081 MetaUpdatePermission::ItemOwner,2082 ));20832084 let variable_data = b"1234567890123".to_vec();2085 assert_noop!(2086 TemplateModule::set_variable_meta_data(2087 origin1,2088 collection_id,2089 1,2090 variable_data.clone()2091 ),2092 Error::<Test>::TokenVariableDataLimitExceeded2093 );2094 })2095}20962097#[test]2098fn collection_transfer_flag_works() {2099 new_test_ext().execute_with(|| {2100 let origin1 = Origin::signed(1);21012102 let collection_id = create_test_collection(&CollectionMode::NFT, 1);2103 assert_ok!(TemplateModule::set_transfers_enabled_flag(origin1, 1, true));21042105 let data = default_nft_data();2106 create_test_item(collection_id, &data.into());2107 assert_eq!(TemplateModule::balance_count(1, 1), 1);2108 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);21092110 let origin1 = Origin::signed(1);21112112 // default scenario2113 assert_ok!(TemplateModule::transfer(origin1, account(2), 1, 1, 1000));2114 assert_eq!(TemplateModule::nft_item_id(1, 1).unwrap().owner, account(2));2115 assert_eq!(TemplateModule::balance_count(1, 1), 0);2116 assert_eq!(TemplateModule::balance_count(1, 2), 1);21172118 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);2119 });2120}21212122#[test]2123fn set_variable_meta_data_on_nft_with_admin_flag() {2124 new_test_ext().execute_with(|| {2125 // default_limits();21262127 let collection_id = create_test_collection_for_owner(&CollectionMode::NFT, 2, 1);21282129 let origin1 = Origin::signed(1);2130 let origin2 = Origin::signed(2);21312132 assert_ok!(TemplateModule::set_mint_permission(2133 origin2.clone(),2134 collection_id,2135 true2136 ));2137 assert_ok!(TemplateModule::add_to_white_list(2138 origin2.clone(),2139 collection_id,2140 account(1)2141 ));21422143 assert_ok!(TemplateModule::add_collection_admin(2144 origin2.clone(),2145 collection_id,2146 account(1)2147 ));21482149 let data = default_nft_data();2150 create_test_item(1, &data.into());21512152 assert_ok!(TemplateModule::set_meta_update_permission_flag(2153 origin2.clone(),2154 collection_id,2155 MetaUpdatePermission::Admin,2156 ));21572158 let variable_data = b"test.".to_vec();2159 assert_ok!(TemplateModule::set_variable_meta_data(2160 origin1,2161 collection_id,2162 1,2163 variable_data.clone()2164 ));21652166 assert_eq!(2167 TemplateModule::nft_item_id(collection_id, 1)2168 .unwrap()2169 .variable_data,2170 variable_data2171 );2172 });2173}21742175#[test]2176fn set_variable_meta_data_on_nft_with_admin_flag_neg() {2177 new_test_ext().execute_with(|| {2178 // default_limits();21792180 let collection_id = create_test_collection_for_owner(&CollectionMode::NFT, 2, 1);21812182 let origin1 = Origin::signed(1);2183 let origin2 = Origin::signed(2);21842185 assert_ok!(TemplateModule::set_mint_permission(2186 origin2.clone(),2187 collection_id,2188 true2189 ));2190 assert_ok!(TemplateModule::add_to_white_list(2191 origin2.clone(),2192 collection_id,2193 account(1)2194 ));21952196 let data = default_nft_data();2197 create_test_item(1, &data.into());21982199 assert_ok!(TemplateModule::set_meta_update_permission_flag(2200 origin2.clone(),2201 collection_id,2202 MetaUpdatePermission::Admin,2203 ));22042205 let variable_data = b"test.".to_vec();2206 assert_noop!(2207 TemplateModule::set_variable_meta_data(2208 origin1,2209 collection_id,2210 1,2211 variable_data.clone()2212 ),2213 Error::<Test>::NoPermission2214 );2215 });2216}22172218#[test]2219fn set_variable_meta_flag_after_freeze() {2220 new_test_ext().execute_with(|| {2221 // default_limits();22222223 let collection_id = create_test_collection_for_owner(&CollectionMode::NFT, 2, 1);22242225 let origin2 = Origin::signed(2);22262227 assert_ok!(TemplateModule::set_meta_update_permission_flag(2228 origin2.clone(),2229 collection_id,2230 MetaUpdatePermission::None,2231 ));2232 assert_noop!(2233 TemplateModule::set_meta_update_permission_flag(2234 origin2.clone(),2235 collection_id,2236 MetaUpdatePermission::Admin2237 ),2238 Error::<Test>::MetadataFlagFrozen2239 );2240 });2241}22422243#[test]2244fn set_variable_meta_data_on_nft_with_none_flag_neg() {2245 new_test_ext().execute_with(|| {2246 // default_limits();22472248 let collection_id = create_test_collection_for_owner(&CollectionMode::NFT, 1, 1);2249 let origin1 = Origin::signed(1);22502251 let data = default_nft_data();2252 create_test_item(1, &data.into());22532254 assert_ok!(TemplateModule::set_meta_update_permission_flag(2255 origin1.clone(),2256 collection_id,2257 MetaUpdatePermission::None,2258 ));22592260 let variable_data = b"test.".to_vec();2261 assert_noop!(2262 TemplateModule::set_variable_meta_data(2263 origin1.clone(),2264 collection_id,2265 1,2266 variable_data.clone()2267 ),2268 Error::<Test>::MetadataUpdateDenied2269 );2270 });2271}22722273#[test]2274fn collection_transfer_flag_works_neg() {2275 new_test_ext().execute_with(|| {2276 let origin1 = Origin::signed(1);22772278 let collection_id = create_test_collection(&CollectionMode::NFT, 1);2279 assert_ok!(TemplateModule::set_transfers_enabled_flag(2280 origin1, 1, false2281 ));22822283 let data = default_nft_data();2284 create_test_item(collection_id, &data.into());2285 assert_eq!(TemplateModule::balance_count(1, 1), 1);2286 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);22872288 let origin1 = Origin::signed(1);22892290 // default scenario2291 assert_noop!(2292 TemplateModule::transfer(origin1, account(2), 1, 1, 1000),2293 Error::<Test>::TransferNotAllowed2294 );2295 assert_eq!(TemplateModule::nft_item_id(1, 1).unwrap().owner, account(1));2296 assert_eq!(TemplateModule::balance_count(1, 1), 1);2297 assert_eq!(TemplateModule::balance_count(1, 2), 0);22982299 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);2300 });2301}