|
| 1 | +// Using `pub` to avoid invalid `dead_code` warnings, see |
| 2 | +// https://users.rust-lang.org/t/invalid-dead-code-warning-for-submodule-in-integration-test/80259 |
| 3 | +pub mod common; |
| 4 | + |
| 5 | +use anyhow::Ok; |
| 6 | +use common::full_access_key_fallback_contract::FullAccessKeyFallbackContract; |
| 7 | +use common::utils::{assert_only_owner_permission_failure, assert_success_with_unit_return}; |
| 8 | +use near_sdk::serde::Deserialize; |
| 9 | +use near_sdk::serde_json::{from_value, json}; |
| 10 | +use std::iter; |
| 11 | +use std::path::Path; |
| 12 | +use workspaces::network::Sandbox; |
| 13 | +use workspaces::types::{AccessKeyPermission, PublicKey}; |
| 14 | +use workspaces::{Account, AccountId, Contract, Worker}; |
| 15 | + |
| 16 | +const PROJECT_PATH: &str = "./tests/contracts/full_access_key_fallback"; |
| 17 | + |
| 18 | +/// Returns a new PublicKey that can be used in tests. |
| 19 | +/// |
| 20 | +/// It returns a `near_sdk::PublicKey` since that's the type required for |
| 21 | +/// `FullAccessKeyFallback::attach_full_access_key`. |
| 22 | +fn new_public_key() -> near_sdk::PublicKey { |
| 23 | + "ed25519:6E8sCci9badyRkXb3JoRpBj5p8C6Tw41ELDZoiihKEtp" |
| 24 | + .parse() |
| 25 | + .unwrap() |
| 26 | +} |
| 27 | + |
| 28 | +/// Converts a `near_sdk::PublicKey` to a `workspaces::types::PublicKey`. |
| 29 | +fn pk_sdk_to_workspaces(public_key: near_sdk::PublicKey) -> PublicKey { |
| 30 | + // Going via json since there seems to be no direct conversion, see this issue: |
| 31 | + // https://github.com/near/workspaces-rs/issues/262 |
| 32 | + #[derive(Deserialize)] |
| 33 | + struct Wrapper { |
| 34 | + public_key: PublicKey, |
| 35 | + } |
| 36 | + |
| 37 | + let ser = json!({ "public_key": public_key }); |
| 38 | + from_value::<Wrapper>(ser).unwrap().public_key |
| 39 | +} |
| 40 | + |
| 41 | +/// Allows spinning up a setup for testing the contract in [`PROJECT_PATH`] and bundles related |
| 42 | +/// resources. |
| 43 | +struct Setup { |
| 44 | + /// Instance of the deployed contract. |
| 45 | + contract: Contract, |
| 46 | + /// Wrapper around the deployed contract that facilitates interacting with methods provided by |
| 47 | + /// the `FullAccessKeyFallback` plugin. |
| 48 | + fa_key_fallback_contract: FullAccessKeyFallbackContract, |
| 49 | + /// A newly created account without any `Ownable` permissions. |
| 50 | + unauth_account: Account, |
| 51 | +} |
| 52 | + |
| 53 | +impl Setup { |
| 54 | + /// Deploys and initializes the contract in [`PROJECT_PATH`] and returns a new `Setup`. |
| 55 | + /// |
| 56 | + /// The `owner` parameter is passed on to the contract's constructor, allowing to optionally set |
| 57 | + /// the owner during initialization. |
| 58 | + async fn new(worker: Worker<Sandbox>, owner: Option<AccountId>) -> anyhow::Result<Self> { |
| 59 | + // Compile and deploy the contract. |
| 60 | + let wasm = |
| 61 | + common::repo::compile_project(Path::new(PROJECT_PATH), "full_access_key_fallback") |
| 62 | + .await?; |
| 63 | + let contract = worker.dev_deploy(&wasm).await?; |
| 64 | + let fa_key_fallback_contract = FullAccessKeyFallbackContract::new(contract.clone()); |
| 65 | + |
| 66 | + // Call the contract's constructor. |
| 67 | + contract |
| 68 | + .call("new") |
| 69 | + .args_json(json!({ |
| 70 | + "owner": owner, |
| 71 | + })) |
| 72 | + .max_gas() |
| 73 | + .transact() |
| 74 | + .await? |
| 75 | + .into_result()?; |
| 76 | + |
| 77 | + let unauth_account = worker.dev_create_account().await?; |
| 78 | + Ok(Self { |
| 79 | + contract, |
| 80 | + fa_key_fallback_contract, |
| 81 | + unauth_account, |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + /// Asserts the contract's access keys are: |
| 86 | + /// |
| 87 | + /// - the contracts own key plus |
| 88 | + /// - the keys specified in `keys` |
| 89 | + /// |
| 90 | + /// with the order of keys being irrelevant. |
| 91 | + /// |
| 92 | + /// Moreover, it asserts that all access keys have `FullAccess` permission. |
| 93 | + /// |
| 94 | + /// Input parameter `keys` is expected to not contain duplicates. |
| 95 | + async fn assert_full_access_keys(&self, keys: &[PublicKey]) { |
| 96 | + // Assert the number of keys. |
| 97 | + let access_key_infos = self |
| 98 | + .contract |
| 99 | + .view_access_keys() |
| 100 | + .await |
| 101 | + .expect("Should view access keys"); |
| 102 | + assert_eq!( |
| 103 | + access_key_infos.len(), |
| 104 | + keys.len() + 1, // + 1 for the contract's key |
| 105 | + ); |
| 106 | + |
| 107 | + // Assert the attached access keys are the ones we expected and all have `FullAccess`. |
| 108 | + // |
| 109 | + // Since `workspaces::types::PublicKey` doesn't implement `Hash`, it cannot be stored in |
| 110 | + // `std::collections::HashSet`. Hence the search in `access_key_infos` with |
| 111 | + // `find()`. |
| 112 | + let contract_key = self.contract.as_account().secret_key().public_key(); |
| 113 | + let expected_keys = iter::once(&contract_key).chain(keys.iter()); |
| 114 | + for expected_key in expected_keys { |
| 115 | + let attached_key = access_key_infos |
| 116 | + .iter() |
| 117 | + .find(|info| &info.public_key == expected_key) |
| 118 | + .unwrap_or_else(|| panic!("PublicKey {:?} is not attached", expected_key)); |
| 119 | + |
| 120 | + assert!( |
| 121 | + matches!( |
| 122 | + attached_key.access_key.permission, |
| 123 | + AccessKeyPermission::FullAccess, |
| 124 | + ), |
| 125 | + "Unexpected permission of access key {:?}: {:?}", |
| 126 | + attached_key, |
| 127 | + attached_key.access_key.permission, |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/// Smoke test of contract setup. |
| 134 | +#[tokio::test] |
| 135 | +async fn test_setup() -> anyhow::Result<()> { |
| 136 | + let worker = workspaces::sandbox().await?; |
| 137 | + let _ = Setup::new(worker, None).await?; |
| 138 | + |
| 139 | + Ok(()) |
| 140 | +} |
| 141 | + |
| 142 | +#[tokio::test] |
| 143 | +async fn test_non_owner_cannot_attach_full_access_key() -> anyhow::Result<()> { |
| 144 | + let worker = workspaces::sandbox().await?; |
| 145 | + let owner = worker.dev_create_account().await?; |
| 146 | + let setup = Setup::new(worker, Some(owner.id().clone())).await?; |
| 147 | + |
| 148 | + let new_fak = new_public_key(); |
| 149 | + let res = setup |
| 150 | + .fa_key_fallback_contract |
| 151 | + .attach_full_access_key(&setup.unauth_account, new_fak) |
| 152 | + .await?; |
| 153 | + assert_only_owner_permission_failure(res); |
| 154 | + |
| 155 | + Ok(()) |
| 156 | +} |
| 157 | + |
| 158 | +#[tokio::test] |
| 159 | +async fn test_attach_full_access_key() -> anyhow::Result<()> { |
| 160 | + let worker = workspaces::sandbox().await?; |
| 161 | + let owner = worker.dev_create_account().await?; |
| 162 | + let setup = Setup::new(worker, Some(owner.id().clone())).await?; |
| 163 | + |
| 164 | + // Initially there's just the contract's access key. |
| 165 | + setup.assert_full_access_keys(&[]).await; |
| 166 | + |
| 167 | + // Owner may attach a full access key. |
| 168 | + let new_fak = new_public_key(); |
| 169 | + let res = setup |
| 170 | + .fa_key_fallback_contract |
| 171 | + .attach_full_access_key(&owner, new_fak.clone()) |
| 172 | + .await?; |
| 173 | + assert_success_with_unit_return(res); |
| 174 | + setup |
| 175 | + .assert_full_access_keys(&[pk_sdk_to_workspaces(new_fak)]) |
| 176 | + .await; |
| 177 | + |
| 178 | + Ok(()) |
| 179 | +} |
0 commit comments