-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathownable.rs
357 lines (298 loc) · 11.1 KB
/
ownable.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Using `pub` to avoid invalid `dead_code` warnings, see
// https://users.rust-lang.org/t/invalid-dead-code-warning-for-submodule-in-integration-test/80259
pub mod common;
use anyhow::Ok;
use common::key::{delete_access_key, get_access_key_infos};
use common::ownable_contract::OwnableContract;
use common::utils::{
assert_access_key_not_found_error, assert_only_owner_permission_failure,
assert_ownable_permission_failure, assert_owner_update_failure, assert_success_with,
};
use near_sdk::serde_json::json;
use near_workspaces::network::Sandbox;
use near_workspaces::result::ExecutionFinalResult;
use near_workspaces::{Account, AccountId, Contract, Worker};
use std::path::Path;
const PROJECT_PATH: &str = "./tests/contracts/ownable";
/// Allows spinning up a setup for testing the contract in [`PROJECT_PATH`] and bundles related
/// resources.
struct Setup {
/// Instance of the deployed contract.
contract: Contract,
/// Wrapper around the deployed contract that facilitates interacting with methods provided by
/// the `Ownable` plugin.
ownable_contract: OwnableContract,
/// A newly created account without any `Ownable` permissions.
unauth_account: Account,
}
impl Setup {
/// Deploys and initializes the contract in [`PROJECT_PATH`] and returns a new `Setup`.
///
/// The `owner` parameter is passed on to the contract's constructor, allowing to optionally set
/// the owner during initialization.
async fn new(worker: Worker<Sandbox>, owner: Option<AccountId>) -> anyhow::Result<Self> {
// Compile and deploy the contract.
let wasm = common::repo::compile_project(Path::new(PROJECT_PATH), "ownable").await?;
let contract = worker.dev_deploy(&wasm).await?;
let ownable_contract = OwnableContract::new(contract.clone());
// Call the contract's constructor.
contract
.call("new")
.args_json(json!({
"owner": owner,
}))
.max_gas()
.transact()
.await?
.into_result()?;
let unauth_account = worker.dev_create_account().await?;
Ok(Self {
contract,
ownable_contract,
unauth_account,
})
}
/// Calls the contract's `get_counter` method from an account without any `Ownable` permissions.
async fn get_counter(&self) -> anyhow::Result<u64> {
let res = self
.unauth_account
.call(self.contract.id(), "get_counter")
.view()
.await?;
Ok(res.json::<u64>()?)
}
/// Calls one of the methods that increases the counter with signature:
///
/// ```ignore
/// method_name(&mut self) -> u64
/// ```
async fn call_counter_increaser(
&self,
caller: &Account,
method_name: &str,
) -> near_workspaces::Result<ExecutionFinalResult> {
caller
.call(self.contract.id(), method_name)
.max_gas()
.transact()
.await
}
/// Asserts the contract's `owner_get` method returns the expected value.
async fn assert_owner_is(&self, expected: Option<&AccountId>) {
// Call from an account without any permissions since `owner_get` is unrestricted.
let owner = self
.ownable_contract
.owner_get(&self.unauth_account)
.await
.unwrap();
assert_eq!(owner.as_ref(), expected);
}
}
/// Smoke test of contract setup and basic functionality.
#[tokio::test]
async fn test_setup() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let setup = Setup::new(worker, None).await?;
assert_eq!(setup.get_counter().await?, 0);
let res = setup
.call_counter_increaser(&setup.unauth_account, "increase")
.await?;
assert_success_with(res, 1);
assert_eq!(setup.get_counter().await?, 1);
Ok(())
}
#[tokio::test]
async fn test_owner_is() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let owner = worker.dev_create_account().await?;
let setup = Setup::new(worker, Some(owner.id().clone())).await?;
// Returns false for an account that isn't owner.
assert!(
!setup
.ownable_contract
.owner_is(&setup.unauth_account)
.await?
);
// Returns true for the owner.
assert!(setup.ownable_contract.owner_is(&owner).await?);
Ok(())
}
#[tokio::test]
async fn test_set_owner_ok() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let setup = Setup::new(worker, None).await?;
setup.assert_owner_is(None).await;
let owner_id = setup.unauth_account.id();
setup
.ownable_contract
.owner_set(setup.contract.as_account(), Some(owner_id.clone()))
.await?
.into_result()?;
setup.assert_owner_is(Some(owner_id)).await;
Ok(())
}
#[tokio::test]
async fn test_set_owner_fail() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let owner = worker.dev_create_account().await?;
let setup = Setup::new(worker, Some(owner.id().clone())).await?;
setup.assert_owner_is(Some(owner.id())).await;
let res = setup
.ownable_contract
.owner_set(
&setup.unauth_account,
Some(setup.unauth_account.id().clone()),
)
.await?;
assert_owner_update_failure(res);
Ok(())
}
#[tokio::test]
async fn test_remove_owner() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let owner = worker.dev_create_account().await?;
let setup = Setup::new(worker, Some(owner.id().clone())).await?;
setup.assert_owner_is(Some(owner.id())).await;
setup
.ownable_contract
.owner_set(&owner, None)
.await?
.into_result()?;
setup.assert_owner_is(None).await;
Ok(())
}
/// Contract itself may successfully call a method protected by `#[only(self)]`.
#[tokio::test]
async fn test_only_self_ok() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let setup = Setup::new(worker, None).await?;
assert_eq!(setup.get_counter().await?, 0);
let res = setup
.call_counter_increaser(setup.contract.as_account(), "increase_4")
.await?;
assert_success_with(res, 4);
assert_eq!(setup.get_counter().await?, 4);
Ok(())
}
/// A method protected by `#[only(self)]` fails if called from another account.
#[tokio::test]
async fn test_only_self_fail_unauth() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let setup = Setup::new(worker, None).await?;
let res = setup
.call_counter_increaser(&setup.unauth_account, "increase_4")
.await?;
assert_ownable_permission_failure(res);
Ok(())
}
/// A method protected by `#[only(self)]` fails if called by the owner.
#[tokio::test]
async fn test_only_self_fail_owner() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let owner = worker.dev_create_account().await?;
let setup = Setup::new(worker, Some(owner.id().clone())).await?;
let res = setup.call_counter_increaser(&owner, "increase_4").await?;
assert_ownable_permission_failure(res);
Ok(())
}
/// Calling a method protected by `#[only(owner)]` from the owner succeeds.
#[tokio::test]
async fn test_only_owner_ok() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let owner = worker.dev_create_account().await?;
let setup = Setup::new(worker, Some(owner.id().clone())).await?;
assert_eq!(setup.get_counter().await?, 0);
let res = setup.call_counter_increaser(&owner, "increase_3").await?;
assert_success_with(res, 3);
assert_eq!(setup.get_counter().await?, 3);
Ok(())
}
/// A method protected by `#[only(owner)]` fails if called by the contract itself.
#[tokio::test]
async fn test_only_owner_fail_self() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let setup = Setup::new(worker, None).await?;
let res = setup
.call_counter_increaser(setup.contract.as_account(), "increase_3")
.await?;
assert_only_owner_permission_failure(res);
Ok(())
}
/// A method protected by `#[only(owner)]` fails if called by another account.
#[tokio::test]
async fn test_only_owner_fail() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let setup = Setup::new(worker, None).await?;
let res = setup
.call_counter_increaser(&setup.unauth_account, "increase_3")
.await?;
assert_only_owner_permission_failure(res);
Ok(())
}
/// Calling a method protected by `#[only(self, owner)]` succeeds if called by the contract itself
/// or by the owner.
#[tokio::test]
async fn test_only_self_owner_ok() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let owner = worker.dev_create_account().await?;
let setup = Setup::new(worker, Some(owner.id().clone())).await?;
assert_eq!(setup.get_counter().await?, 0);
let res = setup
.call_counter_increaser(setup.contract.as_account(), "increase_2")
.await?;
assert_success_with(res, 2);
assert_eq!(setup.get_counter().await?, 2);
let res = setup.call_counter_increaser(&owner, "increase_2").await?;
assert_success_with(res, 4);
assert_eq!(setup.get_counter().await?, 4);
Ok(())
}
/// Calling a method protected by `#[only(self, owner)]` fails if called by another account.
#[tokio::test]
async fn test_only_self_owner_fail() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let setup = Setup::new(worker, None).await?;
let res = setup
.call_counter_increaser(&setup.unauth_account, "increase_2")
.await?;
assert_ownable_permission_failure(res);
Ok(())
}
/// Verifies that the contract cannot set a new owner after its access keys are removed.
#[tokio::test]
async fn test_removing_contract_keys_freezes_owner() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let owner = worker.dev_create_account().await?;
let setup = Setup::new(worker, Some(owner.id().clone())).await?;
setup.assert_owner_is(Some(owner.id())).await;
// Remove the contract's access key.
let contract_key = setup.contract.as_account().secret_key().public_key();
delete_access_key(
setup.contract.as_account(),
setup.contract.id(),
contract_key,
)
.await?
.into_result()?;
// Assert the contract has no access keys anymore.
let access_key_infos = get_access_key_infos(&setup.contract).await;
assert_eq!(access_key_infos.len(), 0, "There should be no access keys");
// Remove the current owner.
setup
.ownable_contract
.owner_set(&owner, None)
.await?
.into_result()?;
setup.assert_owner_is(None).await;
// Verify setting a new owner fails since the contract has no access keys.
let res = setup
.ownable_contract
.owner_set(
setup.contract.as_account(),
Some(setup.unauth_account.id().clone()),
)
.await;
assert_access_key_not_found_error(res);
setup.assert_owner_is(None).await;
Ok(())
}