Skip to content

Commit

Permalink
Migrate Ownable tests (#1033)
Browse files Browse the repository at this point in the history
* Update ERC20Votes tests

* Run linter

* Add test helpers for Ownable tests

* Migrate Ownable tests

* Migrate OwnableTwoStep tests

* Migrate Ownable Dual Dispatcher tests

* Support event changes in snforge 0.26

* Resolve review issues

* Address review comments
  • Loading branch information
immrsd authored Jul 17, 2024
1 parent fbfb819 commit 8fe4f80
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 129 deletions.
8 changes: 4 additions & 4 deletions src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// #[cfg(test)]
// mod access;
#[cfg(test)]
mod access;
// #[cfg(test)]
// mod account;
// #[cfg(test)]
// mod cryptography;
// #[cfg(test)]
// mod introspection;
#[cfg(test)]
mod introspection;
mod mocks;
// #[cfg(test)]
// mod presets;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/access.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod common;

mod test_accesscontrol;
mod test_dual_accesscontrol;
// mod test_accesscontrol;
// mod test_dual_accesscontrol;
mod test_dual_ownable;
mod test_ownable;
mod test_ownable_twostep;
44 changes: 24 additions & 20 deletions src/tests/access/common.cairo
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
use openzeppelin::access::ownable::OwnableComponent::OwnershipTransferred;
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::tests::utils::EventSpyExt;
use openzeppelin::tests::utils;
use openzeppelin::utils::serde::SerializedAppend;
use snforge_std::EventSpy;
use starknet::ContractAddress;

pub(crate) fn assert_only_event_ownership_transferred(
contract: ContractAddress, previous_owner: ContractAddress, new_owner: ContractAddress
) {
assert_event_ownership_transferred(contract, previous_owner, new_owner);
utils::assert_no_events_left(contract);
}

pub(crate) fn assert_event_ownership_transferred(
contract: ContractAddress, previous_owner: ContractAddress, new_owner: ContractAddress
) {
let event = utils::pop_log::<OwnableComponent::Event>(contract).unwrap();
let expected = OwnableComponent::Event::OwnershipTransferred(
OwnershipTransferred { previous_owner, new_owner }
);
assert!(event == expected);
#[generate_trait]
pub(crate) impl OwnableSpyHelpersImpl of OwnableSpyHelpers {
fn assert_only_event_ownership_transferred(
ref self: EventSpy,
contract: ContractAddress,
previous_owner: ContractAddress,
new_owner: ContractAddress
) {
self.assert_event_ownership_transferred(contract, previous_owner, new_owner);
self.assert_no_events_left_from(contract);
}

let mut indexed_keys = array![];
indexed_keys.append_serde(selector!("OwnershipTransferred"));
indexed_keys.append_serde(previous_owner);
indexed_keys.append_serde(new_owner);
utils::assert_indexed_keys(event, indexed_keys.span());
fn assert_event_ownership_transferred(
ref self: EventSpy,
contract: ContractAddress,
previous_owner: ContractAddress,
new_owner: ContractAddress
) {
let expected = OwnableComponent::Event::OwnershipTransferred(
OwnershipTransferred { previous_owner, new_owner }
);
self.assert_emitted_single(contract, expected);
}
}
54 changes: 30 additions & 24 deletions src/tests/access/test_dual_ownable.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use core::num::traits::Zero;
use openzeppelin::access::ownable::dual_ownable::DualCaseOwnable;
use openzeppelin::access::ownable::dual_ownable::DualCaseOwnableTrait;
use openzeppelin::access::ownable::interface::IOwnableCamelOnlyDispatcher;
use openzeppelin::access::ownable::interface::IOwnableDispatcher;
use openzeppelin::access::ownable::interface::IOwnableDispatcherTrait;
use openzeppelin::access::ownable::dual_ownable::{DualCaseOwnable, DualCaseOwnableTrait};
use openzeppelin::access::ownable::interface::{
IOwnableDispatcher, IOwnableCamelOnlyDispatcher, IOwnableDispatcherTrait
};
use openzeppelin::tests::mocks::non_implementing_mock::NonImplementingMock;
use openzeppelin::tests::mocks::ownable_mocks::{
CamelOwnableMock, CamelOwnablePanicMock, SnakeOwnableMock, SnakeOwnablePanicMock
};
use openzeppelin::tests::utils::constants::{OWNER, NEW_OWNER};
use openzeppelin::tests::utils;
use openzeppelin::utils::serde::SerializedAppend;
use starknet::testing::set_contract_address;
use snforge_std::start_cheat_caller_address;

//
// Setup
Expand All @@ -20,14 +19,14 @@ use starknet::testing::set_contract_address;
fn setup_snake() -> (DualCaseOwnable, IOwnableDispatcher) {
let mut calldata = array![];
calldata.append_serde(OWNER());
let target = utils::deploy(SnakeOwnableMock::TEST_CLASS_HASH, calldata);
let target = utils::declare_and_deploy("SnakeOwnableMock", calldata);
(DualCaseOwnable { contract_address: target }, IOwnableDispatcher { contract_address: target })
}

fn setup_camel() -> (DualCaseOwnable, IOwnableCamelOnlyDispatcher) {
let mut calldata = array![];
calldata.append_serde(OWNER());
let target = utils::deploy(CamelOwnableMock::TEST_CLASS_HASH, calldata);
let target = utils::declare_and_deploy("CamelOwnableMock", calldata);
(
DualCaseOwnable { contract_address: target },
IOwnableCamelOnlyDispatcher { contract_address: target }
Expand All @@ -36,13 +35,13 @@ fn setup_camel() -> (DualCaseOwnable, IOwnableCamelOnlyDispatcher) {

fn setup_non_ownable() -> DualCaseOwnable {
let calldata = array![];
let target = utils::deploy(NonImplementingMock::TEST_CLASS_HASH, calldata);
let target = utils::declare_and_deploy("NonImplementingMock", calldata);
DualCaseOwnable { contract_address: target }
}

fn setup_ownable_panic() -> (DualCaseOwnable, DualCaseOwnable) {
let snake_target = utils::deploy(SnakeOwnablePanicMock::TEST_CLASS_HASH, array![]);
let camel_target = utils::deploy(CamelOwnablePanicMock::TEST_CLASS_HASH, array![]);
let snake_target = utils::declare_and_deploy("SnakeOwnablePanicMock", array![]);
let camel_target = utils::declare_and_deploy("CamelOwnablePanicMock", array![]);
(
DualCaseOwnable { contract_address: snake_target },
DualCaseOwnable { contract_address: camel_target }
Expand All @@ -66,14 +65,15 @@ fn test_dual_owner() {
}

#[test]
#[ignore] // REASON: inconsistent ENTRYPOINT_NOT_FOUND panic message
#[should_panic(expected: ('ENTRYPOINT_NOT_FOUND',))]
fn test_dual_no_owner() {
let dispatcher = setup_non_ownable();
dispatcher.owner();
}

#[test]
#[should_panic(expected: ("Some error", 'ENTRYPOINT_FAILED',))]
#[should_panic(expected: ("Some error",))]
fn test_dual_owner_exists_and_panics() {
let (dispatcher, _) = setup_ownable_panic();
dispatcher.owner();
Expand All @@ -86,22 +86,23 @@ fn test_dual_owner_exists_and_panics() {
#[test]
fn test_dual_transfer_ownership() {
let (dispatcher, target) = setup_snake();
set_contract_address(OWNER());
start_cheat_caller_address(dispatcher.contract_address, OWNER());
dispatcher.transfer_ownership(NEW_OWNER());

let current_owner = target.owner();
assert_eq!(current_owner, NEW_OWNER());
}

#[test]
#[ignore] // REASON: inconsistent ENTRYPOINT_NOT_FOUND panic message
#[should_panic(expected: ('ENTRYPOINT_NOT_FOUND',))]
fn test_dual_no_transfer_ownership() {
let dispatcher = setup_non_ownable();
dispatcher.transfer_ownership(NEW_OWNER());
}

#[test]
#[should_panic(expected: ("Some error", 'ENTRYPOINT_FAILED',))]
#[should_panic(expected: ("Some error",))]
fn test_dual_transfer_ownership_exists_and_panics() {
let (dispatcher, _) = setup_ownable_panic();
dispatcher.transfer_ownership(NEW_OWNER());
Expand All @@ -110,22 +111,23 @@ fn test_dual_transfer_ownership_exists_and_panics() {
#[test]
fn test_dual_renounce_ownership() {
let (dispatcher, target) = setup_snake();
set_contract_address(OWNER());
start_cheat_caller_address(dispatcher.contract_address, OWNER());
dispatcher.renounce_ownership();

let current_owner = target.owner();
assert!(current_owner.is_zero());
}

#[test]
#[ignore] // REASON: inconsistent ENTRYPOINT_NOT_FOUND panic message
#[should_panic(expected: ('ENTRYPOINT_NOT_FOUND',))]
fn test_dual_no_renounce_ownership() {
let dispatcher = setup_non_ownable();
dispatcher.renounce_ownership();
}

#[test]
#[should_panic(expected: ("Some error", 'ENTRYPOINT_FAILED',))]
#[should_panic(expected: ("Some error",))]
fn test_dual_renounce_ownership_exists_and_panics() {
let (dispatcher, _) = setup_ownable_panic();
dispatcher.renounce_ownership();
Expand All @@ -136,36 +138,40 @@ fn test_dual_renounce_ownership_exists_and_panics() {
//

#[test]
#[ignore]
fn test_dual_transferOwnership() {
let (dispatcher, _) = setup_camel();
set_contract_address(OWNER());
start_cheat_caller_address(dispatcher.contract_address, OWNER());
dispatcher.transfer_ownership(NEW_OWNER());

let current_owner = dispatcher.owner();
assert_eq!(current_owner, NEW_OWNER());
}

#[test]
#[should_panic(expected: ("Some error", 'ENTRYPOINT_FAILED',))]
#[ignore]
#[should_panic(expected: ("Some error",))]
fn test_dual_transferOwnership_exists_and_panics() {
let (_, dispatcher) = setup_ownable_panic();
dispatcher.transfer_ownership(NEW_OWNER());
let (_, camel_dispatcher) = setup_ownable_panic();
camel_dispatcher.transfer_ownership(NEW_OWNER());
}

#[test]
#[ignore]
fn test_dual_renounceOwnership() {
let (dispatcher, _) = setup_camel();
set_contract_address(OWNER());
start_cheat_caller_address(dispatcher.contract_address, OWNER());
dispatcher.renounce_ownership();

let current_owner = dispatcher.owner();
assert!(current_owner.is_zero());
}

#[test]
#[should_panic(expected: ("Some error", 'ENTRYPOINT_FAILED',))]
#[ignore]
#[should_panic(expected: ("Some error",))]
fn test_dual_renounceOwnership_exists_and_panics() {
let (_, dispatcher) = setup_ownable_panic();
dispatcher.renounce_ownership();
let (_, camel_dispatcher) = setup_ownable_panic();
camel_dispatcher.renounce_ownership();
}

Loading

0 comments on commit 8fe4f80

Please sign in to comment.