Skip to content

Commit ecbb53a

Browse files
committed
test: strengthen as_contract_with_func auth coverage
1 parent 476e622 commit ecbb53a

3 files changed

Lines changed: 98 additions & 9 deletions

File tree

soroban-sdk/src/env.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,17 @@ impl Env {
17881788
///
17891789
/// Used to write or read contract data, or take other actions in tests for
17901790
/// setting up tests or asserting on internal state.
1791-
pub fn as_contract_with_func<T>(&self, id: &Address, func: &crate::Symbol, f: impl FnOnce() -> T) -> T {
1791+
///
1792+
/// Unlike [`as_contract`][Self::as_contract], `func` is recorded as the
1793+
/// current contract function for authorization. This lets
1794+
/// [`mock_auths`][Self::mock_auths] and [`auths`][Self::auths] observe the
1795+
/// same function name that [`Address::require_auth`] uses.
1796+
pub fn as_contract_with_func<T>(
1797+
&self,
1798+
id: &Address,
1799+
func: &crate::Symbol,
1800+
f: impl FnOnce() -> T,
1801+
) -> T {
17921802
let id = id.contract_id();
17931803
let mut t: Option<T> = None;
17941804
self.env_impl
@@ -1886,6 +1896,11 @@ impl Env {
18861896
///
18871897
/// Used to write or read contract data, or take other actions in tests for
18881898
/// setting up tests or asserting on internal state.
1899+
///
1900+
/// Unlike [`try_as_contract`][Self::try_as_contract], `func` is recorded as
1901+
/// the current contract function for authorization. This lets
1902+
/// [`mock_auths`][Self::mock_auths] and [`auths`][Self::auths] observe the
1903+
/// same function name that [`Address::require_auth`] uses.
18891904
pub fn try_as_contract_with_func<T, E>(
18901905
&self,
18911906
id: &Address,
@@ -1898,10 +1913,12 @@ impl Env {
18981913
{
18991914
let id = id.contract_id();
19001915
let mut t: Option<T> = None;
1901-
let result = self.env_impl.try_with_test_contract_frame(id, func.to_symbol_val(), || {
1902-
t = Some(f());
1903-
Ok(().into())
1904-
});
1916+
let result = self
1917+
.env_impl
1918+
.try_with_test_contract_frame(id, func.to_symbol_val(), || {
1919+
t = Some(f());
1920+
Ok(().into())
1921+
});
19051922

19061923
match result {
19071924
Ok(_) => Ok(t.unwrap()),

soroban-sdk/src/tests/env.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,30 @@ fn test_as_contract_with_func() {
386386

387387
#[test]
388388
fn test_try_as_contract_with_func() {
389+
use crate::testutils::{MockAuth, MockAuthInvoke};
390+
use crate::IntoVal;
391+
389392
let env = Env::default();
390393

391394
let addr = Address::generate(&env);
392395
env.register_at(&addr, Contract, ());
393396

394397
let func_name = Symbol::new(&env, "custom_fn");
398+
let user = Address::generate(&env);
399+
400+
env.mock_auths(&[MockAuth {
401+
address: &user,
402+
invoke: &MockAuthInvoke {
403+
contract: &addr,
404+
fn_name: "custom_fn",
405+
args: ().into_val(&env),
406+
sub_invokes: &[],
407+
},
408+
}]);
395409

396410
let result = env.try_as_contract_with_func::<Symbol, Error>(&addr, &func_name, || {
411+
user.require_auth();
397412
Symbol::new(&env, "hello")
398413
});
399414
assert_eq!(result, Ok(Symbol::new(&env, "hello")));
400415
}
401-

soroban-sdk/test_snapshots/tests/env/test_try_as_contract_with_func.1.json

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
{
22
"generators": {
3-
"address": 1,
4-
"nonce": 0,
3+
"address": 2,
4+
"nonce": 1,
55
"mux_id": 0
66
},
77
"auth": [
88
[],
9-
[]
9+
[],
10+
[
11+
[
12+
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
13+
{
14+
"function": {
15+
"contract_fn": {
16+
"contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
17+
"function_name": "custom_fn",
18+
"args": []
19+
}
20+
},
21+
"sub_invocations": []
22+
}
23+
]
24+
]
1025
],
1126
"ledger": {
1227
"protocol_version": 27,
@@ -41,6 +56,49 @@
4156
},
4257
"live_until": 4095
4358
},
59+
{
60+
"entry": {
61+
"last_modified_ledger_seq": 0,
62+
"data": {
63+
"contract_data": {
64+
"ext": "v0",
65+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
66+
"key": "ledger_key_contract_instance",
67+
"durability": "persistent",
68+
"val": {
69+
"contract_instance": {
70+
"executable": {
71+
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
72+
},
73+
"storage": null
74+
}
75+
}
76+
}
77+
},
78+
"ext": "v0"
79+
},
80+
"live_until": 4095
81+
},
82+
{
83+
"entry": {
84+
"last_modified_ledger_seq": 0,
85+
"data": {
86+
"contract_data": {
87+
"ext": "v0",
88+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
89+
"key": {
90+
"ledger_key_nonce": {
91+
"nonce": "1"
92+
}
93+
},
94+
"durability": "temporary",
95+
"val": "void"
96+
}
97+
},
98+
"ext": "v0"
99+
},
100+
"live_until": 6311999
101+
},
44102
{
45103
"entry": {
46104
"last_modified_ledger_seq": 0,

0 commit comments

Comments
 (0)