Skip to content

Commit

Permalink
Test upgrade access_controlable (#141)
Browse files Browse the repository at this point in the history
* Test upgrade

* Add more storage changes

* Reduce test binary size
  • Loading branch information
kiseln authored Aug 16, 2024
1 parent 487836d commit eff928f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
93 changes: 87 additions & 6 deletions near-plugins-derive/tests/access_controllable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use common::utils::{
assert_success_with,
};
use near_plugins::access_controllable::{PermissionedAccounts, PermissionedAccountsPerRole};
use near_sdk::serde_json::json;
use near_sdk::serde_json::{self, json};
use near_workspaces::network::Sandbox;
use near_workspaces::result::ExecutionFinalResult;
use near_workspaces::{Account, AccountId, Contract, Worker};
Expand Down Expand Up @@ -42,25 +42,39 @@ impl Setup {
Self::new_with_admins_and_grantees(Default::default(), Default::default()).await
}

/// Deploys the contract with a specific wasm binary.
async fn new_with_wasm(wasm: Vec<u8>) -> anyhow::Result<Self> {
Self::deploy_contract(
wasm,
json!({
"admins": HashMap::<String, AccountId>::new(),
"grantees": HashMap::<String, AccountId>::new()
}),
)
.await
}

/// Deploys the contract and passes `admins` and `grantees` to the initialization method. Note
/// that accounts corresponding to the ids in `admins` and `grantees` are _not_ created.
async fn new_with_admins_and_grantees(
admins: HashMap<String, AccountId>,
grantees: HashMap<String, AccountId>,
) -> anyhow::Result<Self> {
let worker = near_workspaces::sandbox().await?;
let wasm =
common::repo::compile_project(Path::new(PROJECT_PATH), "access_controllable").await?;

Self::deploy_contract(wasm, json!({ "admins": admins, "grantees": grantees })).await
}

async fn deploy_contract(wasm: Vec<u8>, args: serde_json::Value) -> anyhow::Result<Self> {
let worker = near_workspaces::sandbox().await?;
let contract = AccessControllableContract::new(worker.dev_deploy(&wasm).await?);
let account = worker.dev_create_account().await?;

contract
.contract()
.call("new")
.args_json(json!({
"admins": admins,
"grantees": grantees,
}))
.args_json(args)
.max_gas()
.transact()
.await?
Expand Down Expand Up @@ -1520,3 +1534,70 @@ async fn test_acl_revoke_role_unchecked_is_private() -> anyhow::Result<()> {
assert_private_method_failure(res, "acl_revoke_role_unchecked");
Ok(())
}

const WASM_V_0_2_0_FILEPATH: &str = "tests/data/access_controllable_v_0_2_0.wasm";

#[tokio::test]
async fn test_upgrade_storage() -> anyhow::Result<()> {
let role = "ByMax2Increaser";
let role2 = "Resetter";

let old_wasm = std::fs::read(WASM_V_0_2_0_FILEPATH)?;
let Setup {
contract, account, ..
} = Setup::new_with_wasm(old_wasm).await?;

let contract_account = contract.contract().as_account();

let _ = contract
.acl_init_super_admin(contract_account, account.id())
.await?;

let result = contract
.acl_grant_role(&account, role, account.id())
.await?;
assert!(result.is_some());
assert!(result.unwrap());

let result = contract
.acl_grant_role(&account, role2, account.id())
.await?;
assert!(result.is_some());
assert!(result.unwrap());

let admin_account_id: AccountId = "alice.near".parse().unwrap();
let added = contract
.acl_add_admin(&account, role, &admin_account_id)
.await?;
assert!(added.is_some());
assert!(added.unwrap());

let new_wasm =
common::repo::compile_project(Path::new(PROJECT_PATH), "access_controllable").await?;

// New version
let contract = contract
.contract()
.as_account()
.deploy(&new_wasm)
.await
.unwrap()
.result;
let contract = AccessControllableContract::new(contract);

let has_role = contract.acl_has_role(&account, role, account.id()).await?;
assert!(has_role);

let has_role = contract.acl_has_role(&account, role2, account.id()).await?;
assert!(has_role);

let is_admin = contract
.acl_is_admin(&account, role, &admin_account_id)
.await?;
assert!(is_admin);

let is_super_admin = contract.acl_is_super_admin(&account, account.id()).await?;
assert!(is_super_admin);

Ok(())
}
Binary file not shown.

0 comments on commit eff928f

Please sign in to comment.