Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Pallet-name-service tests (#7676)
Browse files Browse the repository at this point in the history
* set_name and bid tests

* claim and free tests

* assign, unassign, make_permanent tests

* fixes

* whitespace fixes

* remove unused dep

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
  • Loading branch information
stanly-johnson and shawntabrizi authored Dec 9, 2020
1 parent 04b0e8d commit 474c2ea
Showing 1 changed file with 261 additions and 2 deletions.
263 changes: 261 additions & 2 deletions frame/name-service/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use super::mock::*;
use sp_core::blake2_256;
use frame_support::{
assert_ok, assert_noop,
traits::{OnInitialize, OnFinalize}
traits::{OnInitialize, OnFinalize},
error::BadOrigin
};
use pallet_balances::Error as BalancesError;

fn run_to_block(n: u64) {
while System::block_number() < n {
Expand Down Expand Up @@ -92,3 +92,262 @@ fn end_to_end_should_work() {
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 5));
});
}

#[test]
fn set_name_works(){
new_test_ext().execute_with(|| {
// Test data
let name = b"shawntabrizi";
let name_hash = blake2_256(name);

// name setting by manager works correctly
let status = NameStatus::Owned{ who: 1, expiration: None };
assert_ok!(NameService::set_name(Origin::signed(100), name_hash.clone(), status.clone()));

let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, status);

// non manager call should fail
assert_noop!(NameService::set_name(Origin::signed(1), name_hash, status), BadOrigin);
})
}

#[test]
fn bid_works(){
new_test_ext().execute_with(|| {
// Test data
let name = b"shawntabrizi";
let name_hash = blake2_256(name);

// call with less than MinBid should fail
assert_noop!(
NameService::bid(Origin::signed(1), name_hash, <mock::Test as Trait>::MinBid::get().saturating_sub(1)),
Error::<Test>::InvalidBid
);

// create bid works correctly
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 5));
let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::Bidding {
who: 1,
bid_end: <mock::Test as Trait>::BiddingPeriod::get(),
amount: 5
});
assert_eq!(Balances::free_balance(&1), 95);

// another bid at same price should fail
assert_noop!(NameService::bid(Origin::signed(2), name_hash, 5), Error::<Test>::InvalidBid);

// previous bidder should be able to raise bid
run_to_block(2);
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 10));
let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::Bidding {
who: 1,
bid_end: <mock::Test as Trait>::BiddingPeriod::get().saturating_add(2),
amount: 10
});
assert_eq!(Balances::free_balance(&1), 90);

// another user can outbid current bidder
assert_ok!(NameService::bid(Origin::signed(2), name_hash, 20));
let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::Bidding {
who: 2,
bid_end: <mock::Test as Trait>::BiddingPeriod::get().saturating_add(2),
amount: 20
});
assert_eq!(Balances::free_balance(&1), 100);
assert_eq!(Balances::free_balance(&2), 180);

// cannot bid on expired item
run_to_block(12);
assert_noop!(NameService::bid(Origin::signed(2), name_hash, 25), Error::<Test>::InvalidBid);
})
}

#[test]
fn claim_works(){
new_test_ext().execute_with(|| {
// Test data
let name = b"shawntabrizi";
let name_hash = blake2_256(name);

// claim to non existent name should fail
assert_noop!(NameService::claim(Origin::signed(1), name_hash, 1), Error::<Test>::InvalidClaim);

// setup a bid to claim
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 10));

// claim before bid expiry should fail
assert_noop!(NameService::claim(Origin::signed(1), name_hash, 1), Error::<Test>::NotExpired);

run_to_block(<mock::Test as Trait>::BiddingPeriod::get());

// cannot invoke with less than one period
assert_noop!(NameService::claim(Origin::signed(1), name_hash, 0), Error::<Test>::InvalidClaim);

// call by not current bidder should fail
assert_noop!(NameService::claim(Origin::signed(2), name_hash, 1), Error::<Test>::NotBidder);

// claim by successful bidder should pass
assert_ok!(NameService::claim(Origin::signed(1), name_hash, 2));
assert_eq!(Balances::free_balance(&1), 60);
// ensure reserves have been slashed
assert_eq!(Balances::total_balance(&1), 60);

let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::Owned {
who: 1,
expiration: Some(<mock::Test as Trait>::OwnershipPeriod::get()
.saturating_mul(2)
.saturating_add(<mock::Test as Trait>::BiddingPeriod::get())),
});

// call to previously claimed name should fail
assert_noop!(NameService::claim(Origin::signed(1), name_hash, 1), Error::<Test>::InvalidClaim);
});
}

#[test]
fn free_works(){
new_test_ext().execute_with(|| {
// Test data
let name = b"shawntabrizi";
let name_hash = blake2_256(name);

// free non existent name should fail
assert_noop!(NameService::free(Origin::signed(1), name_hash), Error::<Test>::AlreadyAvailable);

// setup a bid to free
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 10));

// free a name in bidding period should fail
assert_noop!(NameService::free(Origin::signed(2), name_hash), Error::<Test>::NotExpired);

// free should wait for claim period
run_to_block(<mock::Test as Trait>::BiddingPeriod::get());
assert_noop!(NameService::free(Origin::signed(2), name_hash), Error::<Test>::NotExpired);

// call after (bid_end+bidding+claim+1) period should pass
let ideal_block = <mock::Test as Trait>::BiddingPeriod::get().saturating_add(<mock::Test as Trait>::ClaimPeriod::get());
run_to_block(ideal_block.saturating_add(11));
assert_ok!(NameService::free(Origin::signed(1), name_hash));

let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::default());

// original bidder reserve balance is slashed
assert_eq!(Balances::free_balance(&1), 90);
assert_eq!(Balances::reserved_balance(&1), 0);

// setup a claimed name to free
assert_ok!(NameService::bid(Origin::signed(2), name_hash, 10));
run_to_block(100);
assert_ok!(NameService::claim(Origin::signed(2), name_hash, 1));

// only current owner should be able to free non expired name
assert_noop!(NameService::free(Origin::signed(1), name_hash), Error::<Test>::NotExpired);
assert_ok!(NameService::free(Origin::signed(2), name_hash));
let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::default());
});

}

#[test]
fn assign_works(){
new_test_ext().execute_with(|| {
// Test data
let name = b"shawntabrizi";
let name_hash = blake2_256(name);

// setup a claimed name to assign
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 10));
run_to_block(<mock::Test as Trait>::BiddingPeriod::get());
assert_ok!(NameService::claim(Origin::signed(1), name_hash, 1));

// non owner calls should fail
assert_noop!(NameService::assign(Origin::signed(2), name_hash, Some(4)), Error::<Test>::NotOwner);

// owner can assign accountID
assert_ok!(NameService::assign(Origin::signed(1), name_hash, Some(4)));
assert_eq!(Lookup::<Test>::get(&name_hash), Some(4));

// owner can unassign accountId
assert_ok!(NameService::assign(Origin::signed(1), name_hash, None));
assert_eq!(Lookup::<Test>::get(&name_hash), None);
});
}

#[test]
fn unassign_works(){
new_test_ext().execute_with(|| {
// Test data
let name_hash = blake2_256(b"shawntabrizi");

// setup an assigned name to test
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 10));
run_to_block(<mock::Test as Trait>::BiddingPeriod::get());
assert_ok!(NameService::claim(Origin::signed(1), name_hash, 1));
assert_ok!(NameService::assign(Origin::signed(1), name_hash, Some(1)));

// non assigned account call should fail
assert_noop!(NameService::unassign(Origin::signed(2), name_hash), Error::<Test>::NotAssigned);

// assigned account call should pass
assert_ok!(NameService::unassign(Origin::signed(1), name_hash));
assert_eq!(Lookup::<Test>::get(&name_hash), None);
});
}

#[test]
fn make_permanent_works(){
new_test_ext().execute_with(|| {
// Test data
let name_hash = blake2_256(b"shawntabrizi");

// setup an assigned name to test
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 10));
run_to_block(<mock::Test as Trait>::BiddingPeriod::get());
assert_ok!(NameService::claim(Origin::signed(1), name_hash, 1));

// call from non permeance account should fail
assert_noop!(NameService::make_permanent(Origin::signed(1), name_hash), BadOrigin);

// call from permeance accout should pass
assert_ok!(NameService::make_permanent(Origin::signed(200), name_hash));
let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::Owned {
who: 1,
expiration: None,
});
});
}

#[test]
fn extend_ownership_works(){
new_test_ext().execute_with(|| {
// Test data
let name_hash = blake2_256(b"shawntabrizi");

// call with non claimed name should fail
assert_noop!(NameService::extend_ownership(Origin::signed(1), name_hash), Error::<Test>::UnexpectedState);

// setup an assigned name to test
assert_ok!(NameService::bid(Origin::signed(1), name_hash, 10));
run_to_block(<mock::Test as Trait>::BiddingPeriod::get());
assert_ok!(NameService::claim(Origin::signed(1), name_hash, 1));

// call to extend ownership should pass
assert_ok!(NameService::extend_ownership(Origin::signed(2), name_hash));
// balance of caller should reduce by the extension fee
assert_eq!(Balances::free_balance(&2), 200.saturating_sub(5));

let stored_data = Registration::<Test>::get(&name_hash);
assert_eq!(stored_data, NameStatus::Owned {
who: 1,
expiration: Some(210)
});
});
}

0 comments on commit 474c2ea

Please sign in to comment.