diff --git a/near-plugins-derive/tests/access_controllable.rs b/near-plugins-derive/tests/access_controllable.rs index 056b35a..9224843 100644 --- a/near-plugins-derive/tests/access_controllable.rs +++ b/near-plugins-derive/tests/access_controllable.rs @@ -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}; @@ -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) -> anyhow::Result { + Self::deploy_contract( + wasm, + json!({ + "admins": HashMap::::new(), + "grantees": HashMap::::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, grantees: HashMap, ) -> anyhow::Result { - 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, args: serde_json::Value) -> anyhow::Result { + 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? @@ -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(()) +} diff --git a/near-plugins-derive/tests/data/access_controllable_v_0_2_0.wasm b/near-plugins-derive/tests/data/access_controllable_v_0_2_0.wasm new file mode 100755 index 0000000..e98b89e Binary files /dev/null and b/near-plugins-derive/tests/data/access_controllable_v_0_2_0.wasm differ