diff --git a/tests/conftest.py b/tests/conftest.py index 889e463..52a04c1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,8 @@ import pytest from ape.contracts import ContractContainer from ape.utils import ZERO_ADDRESS +from ape_test import TestAccount +from eip712 import EIP712Message from ethpm_types import ContractType from ape_safe import MultiSend @@ -17,6 +19,21 @@ ape.config.DATA_FOLDER = DATA_FOLDER +@pytest.fixture(autouse=True) +def fix_eip712_signing(monkeypatch): + # TODO: `ape_test.TestAccount.sign_message` doesn't support `EIP712Message` yet + # See: https://github.com/ApeWorX/ape/issues/1961 + original_sign_message = TestAccount.sign_message + + def modified_sign_message(self, msg): + if isinstance(msg, EIP712Message): + msg = msg.signable_message + + return original_sign_message(self, msg) + + monkeypatch.setattr(TestAccount, "sign_message", modified_sign_message) + + @pytest.fixture(scope="session") def config(): return ape.config @@ -37,6 +54,11 @@ def receiver(accounts): return accounts[9] +@pytest.fixture(scope="session") +def delegate(accounts): + return accounts[8] + + @pytest.fixture(scope="session", params=["1.3.0"]) # TODO: Test more versions later? def VERSION(request): return request.param diff --git a/tests/functional/test_delegates.py b/tests/functional/test_delegates.py new file mode 100644 index 0000000..9b1eeec --- /dev/null +++ b/tests/functional/test_delegates.py @@ -0,0 +1,41 @@ +import pytest + +from ape_safe.exceptions import SafeClientException + + +def test_manage_delegates(safe, delegate, OWNERS): + owner = OWNERS[0] + assert owner.address not in safe.client.get_delegates() + + safe.client.add_delegate(delegate, "pepito", owner) + assert delegate.address in safe.client.get_delegates()[owner.address] + assert delegate.address in safe.all_delegates() + # NOTE: Only in MockSafeClient + assert safe.client.delegator_for_delegate(delegate.address) == owner.address + + safe.client.remove_delegate(delegate, owner) + assert owner.address not in safe.client.get_delegates() + + with pytest.raises(SafeClientException): + # Only signers can create a delegate + safe.client.add_delegate(owner, "root privledges", delegate) + + +def test_delegate_can_propose_safe_tx(safe, delegate, OWNERS): + owner = OWNERS[0] + + safe_tx = safe.create_safe_tx( + safe.contract.addOwnerWithThreshold.as_transaction(delegate, safe.confirmations_required) + ) + + with pytest.raises(SafeClientException): + # Not a delegate or signer + safe.propose_safe_tx(safe_tx, submitter=delegate) + + safe.client.add_delegate(delegate, "pepito", owner) + safe.propose_safe_tx(safe_tx, submitter=delegate) + + assert len(safe.get_api_confirmations(safe_tx)) == 0 + assert list( + (safe_tx.signable_message, confs) for safe_tx, confs in safe.pending_transactions() + ) == [(safe_tx.signable_message, [])]