From a5f3784f6fb32c76773c997cdc6920f2702e9230 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Wed, 8 Jan 2025 10:53:27 +0200 Subject: [PATCH] test: refactor start_time to use rstest --- Cargo.lock | 37 ++++- packages/std/Cargo.toml | 1 + .../std/src/ado_contract/permissioning.rs | 127 +++++++++++------- 3 files changed, 113 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c83b520b2..42c87551a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -929,6 +929,7 @@ dependencies = [ "osmosis-std-derive 0.15.3", "prost 0.11.9", "regex", + "rstest 0.24.0", "schemars", "semver", "serde", @@ -3605,7 +3606,7 @@ dependencies = [ "cw20 1.1.2", "cw721 0.18.0", "prost-types 0.13.4", - "rstest", + "rstest 0.23.0", "serde", "tokio", ] @@ -4769,7 +4770,19 @@ checksum = "0a2c585be59b6b5dd66a9d2084aa1d8bd52fbdb806eafdeffb52791147862035" dependencies = [ "futures", "futures-timer", - "rstest_macros", + "rstest_macros 0.23.0", + "rustc_version", +] + +[[package]] +name = "rstest" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03e905296805ab93e13c1ec3a03f4b6c4f35e9498a3d5fa96dc626d22c03cd89" +dependencies = [ + "futures-timer", + "futures-util", + "rstest_macros 0.24.0", "rustc_version", ] @@ -4791,6 +4804,24 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "rstest_macros" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef0053bbffce09062bee4bcc499b0fbe7a57b879f1efe088d6d8d4c7adcdef9b" +dependencies = [ + "cfg-if", + "glob", + "proc-macro-crate", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version", + "syn 2.0.90", + "unicode-ident", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -5732,7 +5763,7 @@ dependencies = [ "cw721 0.18.0", "cw721-base 0.18.0", "ibc-relayer-types", - "rstest", + "rstest 0.23.0", "toml 0.8.19", ] diff --git a/packages/std/Cargo.toml b/packages/std/Cargo.toml index 9a267f7b6..fa20097bc 100644 --- a/packages/std/Cargo.toml +++ b/packages/std/Cargo.toml @@ -47,6 +47,7 @@ prost = { version = "0.11.2", default-features = false, features = [ [dev-dependencies] cw-multi-test = { workspace = true } +rstest = "0.24.0" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] cw-orch = { workspace = true } diff --git a/packages/std/src/ado_contract/permissioning.rs b/packages/std/src/ado_contract/permissioning.rs index 97a4ccc18..ec362f490 100644 --- a/packages/std/src/ado_contract/permissioning.rs +++ b/packages/std/src/ado_contract/permissioning.rs @@ -569,6 +569,7 @@ mod tests { }; use super::*; + use rstest::*; #[test] fn test_permissioned_action() { @@ -862,20 +863,63 @@ mod tests { assert!(res.is_ok()); } - #[test] - fn test_permission_start_time() { + #[fixture] + fn contract<'a>() -> ADOContract<'a> { + ADOContract::default() + } + + #[fixture] + fn action() -> &'static str { + "action" + } + + #[fixture] + fn actor() -> &'static str { + "actor" + } + + #[fixture] + fn start_time() -> u64 { + 2 + } + + #[fixture] + fn start(start_time: u64) -> Option { + Some(Expiry::AtTime(MillisecondsExpiration::from_seconds( + start_time, + ))) + } + + #[rstest] + #[case(true, true, false, true)] // Whitelist, at start time, should succeed + #[case(true, false, false, false)] // Whitelist, before start time, should error + #[case(true, false, true, true)] // Whitelist, after start time, should succeed + #[case(false, false, false, true)] // Blacklist, before start time, should succeed + #[case(false, true, false, false)] // Blacklist, at start time, should error + #[case(false, false, true, false)] // Blacklist, after start time, should error + fn test_permission_start_time( + contract: ADOContract, + action: &str, + actor: &str, + start_time: u64, + start: Option, + #[case] is_whitelisted: bool, + #[case] is_at_start_time: bool, + #[case] is_after_start_time: bool, + #[case] expected_success: bool, + ) { let mut deps = mock_dependencies(); let mut env = mock_env(); - env.block.height = 0; - let action = "action"; - let actor = "actor"; - let contract = ADOContract::default(); - let start_time = 2; - let start = Some(Expiry::AtTime(MillisecondsExpiration::from_seconds( - start_time, - ))); - env.block.time = MillisecondsExpiration::from_seconds(0).into(); + env.block.time = MillisecondsExpiration::from_seconds(if is_at_start_time { + start_time + } else if is_after_start_time { + start_time + 1 + } else { + 0 + }) + .into(); + contract .owner .save(deps.as_mut().storage, &Addr::unchecked("owner")) @@ -885,56 +929,41 @@ mod tests { .permission_action(action, deps.as_mut().storage) .unwrap(); - // Test Whitelist with start time - let permission = Permission::Local(LocalPermission::Whitelisted { - start: start.clone(), - expiration: None, - }); - ADOContract::set_permission(deps.as_mut().storage, action, actor, permission).unwrap(); - - // Before start time - should error - let res = contract.is_permissioned(deps.as_mut(), env.clone(), action, actor); - assert!(res.is_err()); - - // At start time - should succeed - env.block.time = MillisecondsExpiration::from_seconds(start_time).into(); - let res = contract.is_permissioned(deps.as_mut(), env.clone(), action, actor); - assert!(res.is_ok()); - - // After start time - should succeed - env.block.time = MillisecondsExpiration::from_seconds(start_time + 1).into(); - let res = contract.is_permissioned(deps.as_mut(), env.clone(), action, actor); - assert!(res.is_ok()); + let permission = if is_whitelisted { + Permission::Local(LocalPermission::Whitelisted { + start, + expiration: None, + }) + } else { + Permission::Local(LocalPermission::Blacklisted { + start, + expiration: None, + }) + }; - // Test Blacklist with start time - env.block.time = MillisecondsExpiration::from_seconds(0).into(); - let permission = Permission::Local(LocalPermission::Blacklisted { - start, - expiration: None, - }); ADOContract::set_permission(deps.as_mut().storage, action, actor, permission).unwrap(); - // Before start time - should succeed (blacklist not active yet) let res = contract.is_permissioned(deps.as_mut(), env.clone(), action, actor); - assert!(res.is_ok()); - // At start time - should error (blacklist becomes active) - env.block.time = MillisecondsExpiration::from_seconds(start_time).into(); - let res = contract.is_permissioned(deps.as_mut(), env.clone(), action, actor); - assert!(res.is_err()); + if expected_success { + assert!(res.is_ok()); + } else { + assert!(res.is_err()); + } + } - // After start time - should error (blacklist remains active) - env.block.time = MillisecondsExpiration::from_seconds(start_time + 1).into(); - let res = contract.is_permissioned(deps.as_mut(), env.clone(), action, actor); - assert!(res.is_err()); + #[rstest] + fn test_permission_start_time_disabled_action(action: &str, actor: &str) { + let mut deps = mock_dependencies(); + let mut env = mock_env(); + let contract = ADOContract::default(); env.block.time = MillisecondsExpiration::from_seconds(0).into(); - // Test interaction with disabled action permissioning ADOContract::default().disable_action_permission(action, deps.as_mut().storage); let res = contract.is_permissioned(deps.as_mut(), env, action, actor); - assert!(res.is_ok()); + assert!(res.is_err()); } #[test]