Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
chore(test): test scenarios for transferring storage and cdn node own…
Browse files Browse the repository at this point in the history
…ership
  • Loading branch information
yahortsaryk committed Jun 29, 2023
1 parent 26af454 commit 4f211a2
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 10 deletions.
8 changes: 0 additions & 8 deletions bucket/ddc_bucket/tests/env_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ pub fn get_events<Event: Decode>() -> Vec<Event> {
raw_events.iter().map(decode_event).collect()
}

pub fn admin_id() -> AccountId {
get_accounts().alice
}

pub fn contract_id() -> AccountId {
AccountId::from([0x09; 32])
}

pub fn print_events(events: &[Event]) {
for ev in events.iter() {
match ev {
Expand Down
8 changes: 7 additions & 1 deletion bucket/ddc_bucket/tests/setup_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
use crate::ddc_bucket::*;
use super::env_utils::*;

pub fn admin_id() -> AccountId {
get_accounts().alice
}

pub fn contract_id() -> AccountId {
AccountId::from([0x09; 32])
}

pub fn setup_contract() -> DdcBucket {
set_caller(admin_id());
Expand All @@ -11,7 +18,6 @@ pub fn setup_contract() -> DdcBucket {
contract
}


pub struct TestCluster {
pub contract: DdcBucket,

Expand Down
211 changes: 210 additions & 1 deletion bucket/ddc_bucket/tests/test_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,213 @@ fn admin_revoke_err_if_not_admin() {
),
Err(OnlySuperAdmin)
);
}
}


#[ink::test]
fn admin_transfer_node_ownership_err_if_not_admin() {
let mut contract = setup_contract();

let new_node_key = AccountId::from([0xc4, 0xcd, 0xaa, 0xfa, 0xf1, 0x30, 0x7d, 0x23, 0xf4, 0x99, 0x84, 0x71, 0xdf, 0x78, 0x59, 0xce, 0x06, 0x3d, 0xce, 0x78, 0x59, 0xc4, 0x3a, 0xe8, 0xef, 0x12, 0x0a, 0xbc, 0x43, 0xc4, 0x84, 0x31]);
let not_admin_id = AccountId::from([0x00, 0xc9, 0x91, 0xf1, 0x63, 0x0f, 0xb4, 0x51, 0xf6, 0x6c, 0x9e, 0xa5, 0xc6, 0xdd, 0xf3, 0x33, 0xd8, 0x48, 0x75, 0xc6, 0x22, 0xf5, 0xd3, 0xde, 0x4a, 0x39, 0xe7, 0x71, 0x6f, 0x74,0xf0, 0x49]);
set_balance(not_admin_id, 1000 * TOKEN);

set_caller_value(not_admin_id, CONTRACT_FEE_LIMIT);
contract.node_create(
new_node_key,
NodeParams::from("{\"url\":\"https://ddc-1.cere.network/storage/new\"}"),
100,
10 * TOKEN,
)?;

let node_info = contract.node_get(new_node_key)?;
assert_eq!(node_info.node.provider_id, not_admin_id);

let new_owner_id = AccountId::from([0xf8, 0x9e, 0xfb, 0x5c, 0x80, 0x72, 0x8e, 0x2a, 0x69, 0x54, 0x73, 0x32, 0x52, 0x8b, 0x03, 0xb7, 0x9d, 0x2c, 0xd5, 0x06, 0xed, 0x38, 0x72, 0x95, 0x19, 0x9c, 0x6b, 0x8f, 0x7e, 0xa3, 0x47, 0x16]);
set_balance(new_owner_id, 1000 * TOKEN);

set_caller(not_admin_id);

assert_eq!(
contract.admin_transfer_node_ownership(
new_node_key,
new_owner_id
),
Err(OnlySuperAdmin)
);
}


#[ink::test]
fn admin_transfer_node_ownership_err_if_provider_is_not_admin() {
let mut contract = setup_contract();

let new_node_key = AccountId::from([0xc4, 0xcd, 0xaa, 0xfa, 0xf1, 0x30, 0x7d, 0x23, 0xf4, 0x99, 0x84, 0x71, 0xdf, 0x78, 0x59, 0xce, 0x06, 0x3d, 0xce, 0x78, 0x59, 0xc4, 0x3a, 0xe8, 0xef, 0x12, 0x0a, 0xbc, 0x43, 0xc4, 0x84, 0x31]);
let not_admin_id = AccountId::from([0x00, 0xc9, 0x91, 0xf1, 0x63, 0x0f, 0xb4, 0x51, 0xf6, 0x6c, 0x9e, 0xa5, 0xc6, 0xdd, 0xf3, 0x33, 0xd8, 0x48, 0x75, 0xc6, 0x22, 0xf5, 0xd3, 0xde, 0x4a, 0x39, 0xe7, 0x71, 0x6f, 0x74,0xf0, 0x49]);
set_balance(not_admin_id, 1000 * TOKEN);

set_caller_value(not_admin_id, CONTRACT_FEE_LIMIT);
contract.node_create(
new_node_key,
NodeParams::from("{\"url\":\"https://ddc-1.cere.network/storage/new\"}"),
100,
10 * TOKEN,
)?;

let node_info = contract.node_get(new_node_key)?;
assert_eq!(node_info.node.provider_id, not_admin_id);

let new_owner_id = AccountId::from([0xf8, 0x9e, 0xfb, 0x5c, 0x80, 0x72, 0x8e, 0x2a, 0x69, 0x54, 0x73, 0x32, 0x52, 0x8b, 0x03, 0xb7, 0x9d, 0x2c, 0xd5, 0x06, 0xed, 0x38, 0x72, 0x95, 0x19, 0x9c, 0x6b, 0x8f, 0x7e, 0xa3, 0x47, 0x16]);
set_balance(new_owner_id, 1000 * TOKEN);

set_caller(admin_id());

assert_eq!(
contract.admin_transfer_node_ownership(
new_node_key,
new_owner_id
),
Err(NodeOwnerIsNotSuperAdmin)
);
}


#[ink::test]
fn admin_transfer_node_ownership_ok() {
let mut contract = setup_contract();

let new_node_key = AccountId::from([0xc4, 0xcd, 0xaa, 0xfa, 0xf1, 0x30, 0x7d, 0x23, 0xf4, 0x99, 0x84, 0x71, 0xdf, 0x78, 0x59, 0xce, 0x06, 0x3d, 0xce, 0x78, 0x59, 0xc4, 0x3a, 0xe8, 0xef, 0x12, 0x0a, 0xbc, 0x43, 0xc4, 0x84, 0x31]);
set_caller_value(admin_id(), CONTRACT_FEE_LIMIT);
contract.node_create(
new_node_key,
NodeParams::from("{\"url\":\"https://ddc-1.cere.network/storage/new\"}"),
100,
10 * TOKEN,
)?;

let node_info1 = contract.node_get(new_node_key)?;
assert_eq!(node_info1.node.provider_id, admin_id());

let new_owner_id = AccountId::from([0xf8, 0x9e, 0xfb, 0x5c, 0x80, 0x72, 0x8e, 0x2a, 0x69, 0x54, 0x73, 0x32, 0x52, 0x8b, 0x03, 0xb7, 0x9d, 0x2c, 0xd5, 0x06, 0xed, 0x38, 0x72, 0x95, 0x19, 0x9c, 0x6b, 0x8f, 0x7e, 0xa3, 0x47, 0x16]);
set_balance(new_owner_id, 1000 * TOKEN);

set_caller(admin_id());
contract.admin_transfer_node_ownership(
new_node_key,
new_owner_id
)?;

let node_info2 = contract.node_get(new_node_key)?;
assert_eq!(node_info2.node.provider_id, new_owner_id);

assert!(
matches!(get_events().pop().unwrap(), Event::NodeOwnershipTransferred(ev) if ev ==
NodeOwnershipTransferred {
account_id: new_owner_id,
node_key: new_node_key
}
)
);
}


#[ink::test]
fn admin_transfer_cdn_node_ownership_err_if_not_admin() {
let mut contract = setup_contract();

let new_cdn_node_key = AccountId::from([0xc4, 0xcd, 0xaa, 0xfa, 0xf1, 0x30, 0x7d, 0x23, 0xf4, 0x99, 0x84, 0x71, 0xdf, 0x78, 0x59, 0xce, 0x06, 0x3d, 0xce, 0x78, 0x59, 0xc4, 0x3a, 0xe8, 0xef, 0x12, 0x0a, 0xbc, 0x43, 0xc4, 0x84, 0x31]);
let not_admin_id = AccountId::from([0x00, 0xc9, 0x91, 0xf1, 0x63, 0x0f, 0xb4, 0x51, 0xf6, 0x6c, 0x9e, 0xa5, 0xc6, 0xdd, 0xf3, 0x33, 0xd8, 0x48, 0x75, 0xc6, 0x22, 0xf5, 0xd3, 0xde, 0x4a, 0x39, 0xe7, 0x71, 0x6f, 0x74,0xf0, 0x49]);
set_balance(not_admin_id, 1000 * TOKEN);

set_caller_value(not_admin_id, CONTRACT_FEE_LIMIT);
contract.cdn_node_create(
new_cdn_node_key,
CdnNodeParams::from("{\"url\":\"https://ddc-1.cere.network/cdn/new\"}"),
)?;

let cdn_node_info = contract.cdn_node_get(new_cdn_node_key)?;
assert_eq!(cdn_node_info.cdn_node.provider_id, not_admin_id);

let new_owner_id = AccountId::from([0xf8, 0x9e, 0xfb, 0x5c, 0x80, 0x72, 0x8e, 0x2a, 0x69, 0x54, 0x73, 0x32, 0x52, 0x8b, 0x03, 0xb7, 0x9d, 0x2c, 0xd5, 0x06, 0xed, 0x38, 0x72, 0x95, 0x19, 0x9c, 0x6b, 0x8f, 0x7e, 0xa3, 0x47, 0x16]);
set_balance(new_owner_id, 1000 * TOKEN);

set_caller(not_admin_id);

assert_eq!(
contract.admin_transfer_cdn_node_ownership(
new_cdn_node_key,
new_owner_id
),

Err(OnlySuperAdmin)
);
}


#[ink::test]
fn admin_transfer_cdn_node_ownership_err_if_provider_is_not_admin() {
let mut contract = setup_contract();

let new_cdn_node_key = AccountId::from([0xc4, 0xcd, 0xaa, 0xfa, 0xf1, 0x30, 0x7d, 0x23, 0xf4, 0x99, 0x84, 0x71, 0xdf, 0x78, 0x59, 0xce, 0x06, 0x3d, 0xce, 0x78, 0x59, 0xc4, 0x3a, 0xe8, 0xef, 0x12, 0x0a, 0xbc, 0x43, 0xc4, 0x84, 0x31]);
let not_admin_id = AccountId::from([0x00, 0xc9, 0x91, 0xf1, 0x63, 0x0f, 0xb4, 0x51, 0xf6, 0x6c, 0x9e, 0xa5, 0xc6, 0xdd, 0xf3, 0x33, 0xd8, 0x48, 0x75, 0xc6, 0x22, 0xf5, 0xd3, 0xde, 0x4a, 0x39, 0xe7, 0x71, 0x6f, 0x74,0xf0, 0x49]);
set_balance(not_admin_id, 1000 * TOKEN);

set_caller_value(not_admin_id, CONTRACT_FEE_LIMIT);
contract.cdn_node_create(
new_cdn_node_key,
CdnNodeParams::from("{\"url\":\"https://ddc-1.cere.network/cdn/new\"}"),
)?;

let cdn_node_info = contract.cdn_node_get(new_cdn_node_key)?;
assert_eq!(cdn_node_info.cdn_node.provider_id, not_admin_id);

let new_owner_id = AccountId::from([0xf8, 0x9e, 0xfb, 0x5c, 0x80, 0x72, 0x8e, 0x2a, 0x69, 0x54, 0x73, 0x32, 0x52, 0x8b, 0x03, 0xb7, 0x9d, 0x2c, 0xd5, 0x06, 0xed, 0x38, 0x72, 0x95, 0x19, 0x9c, 0x6b, 0x8f, 0x7e, 0xa3, 0x47, 0x16]);
set_balance(new_owner_id, 1000 * TOKEN);

set_caller(admin_id());

assert_eq!(
contract.admin_transfer_cdn_node_ownership(
new_cdn_node_key,
new_owner_id
),
Err(CdnNodeOwnerIsNotSuperAdmin)
);
}


#[ink::test]
fn admin_transfer_cdn_node_ownership_ok() {
let mut contract = setup_contract();

let new_cdn_node_key = AccountId::from([0xc4, 0xcd, 0xaa, 0xfa, 0xf1, 0x30, 0x7d, 0x23, 0xf4, 0x99, 0x84, 0x71, 0xdf, 0x78, 0x59, 0xce, 0x06, 0x3d, 0xce, 0x78, 0x59, 0xc4, 0x3a, 0xe8, 0xef, 0x12, 0x0a, 0xbc, 0x43, 0xc4, 0x84, 0x31]);
set_caller_value(admin_id(), CONTRACT_FEE_LIMIT);
contract.cdn_node_create(
new_cdn_node_key,
CdnNodeParams::from("{\"url\":\"https://ddc-1.cere.network/cdn/new\"}"),
)?;

let cdn_node_info1 = contract.cdn_node_get(new_cdn_node_key)?;
assert_eq!(cdn_node_info1.cdn_node.provider_id, admin_id());

let new_owner_id = AccountId::from([0xf8, 0x9e, 0xfb, 0x5c, 0x80, 0x72, 0x8e, 0x2a, 0x69, 0x54, 0x73, 0x32, 0x52, 0x8b, 0x03, 0xb7, 0x9d, 0x2c, 0xd5, 0x06, 0xed, 0x38, 0x72, 0x95, 0x19, 0x9c, 0x6b, 0x8f, 0x7e, 0xa3, 0x47, 0x16]);
set_balance(new_owner_id, 1000 * TOKEN);

set_caller(admin_id());
contract.admin_transfer_cdn_node_ownership(
new_cdn_node_key,
new_owner_id
)?;

let cdn_node_info2 = contract.cdn_node_get(new_cdn_node_key)?;
assert_eq!(cdn_node_info2.cdn_node.provider_id, new_owner_id);

assert!(
matches!(get_events().pop().unwrap(), Event::CdnNodeOwnershipTransferred(ev) if ev ==
CdnNodeOwnershipTransferred {
account_id: new_owner_id,
cdn_node_key: new_cdn_node_key
}
)
);
}

0 comments on commit 4f211a2

Please sign in to comment.