Skip to content

Commit

Permalink
test: add tests for delegate feature (using MockClient)
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Jun 5, 2024
1 parent 2b4c695 commit 7439a4b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +17,21 @@
TESTS_DIR = Path(__file__).parent.absolute()


@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", autouse=True)
def config():
cfg = ape.config
Expand All @@ -40,6 +57,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
Expand Down
41 changes: 41 additions & 0 deletions tests/functional/test_delegates.py
Original file line number Diff line number Diff line change
@@ -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, [])]

0 comments on commit 7439a4b

Please sign in to comment.