From 6fce84b4dd6281abf355c8da3e27d4ea621c0cfb Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Fri, 19 Apr 2024 16:33:21 +0200
Subject: [PATCH] wip: add subscription tests draft
---
.../contracts/coordination/Subscription.sol | 2 +
tests/test_subscription.py | 74 +++++++++++++++++++
2 files changed, 76 insertions(+)
create mode 100644 tests/test_subscription.py
diff --git a/contracts/contracts/coordination/Subscription.sol b/contracts/contracts/coordination/Subscription.sol
index 03ba5ca1..ff73d2d7 100644
--- a/contracts/contracts/coordination/Subscription.sol
+++ b/contracts/contracts/coordination/Subscription.sol
@@ -166,6 +166,8 @@ abstract contract Subscription {
feeToken.safeTransfer(msg.sender, refundAmount);
delete subscriptions[subscriptionId];
delete subscribers[lookupKey(ritualId, msg.sender)];
+
+ // TODO: Emit event?
}
}
diff --git a/tests/test_subscription.py b/tests/test_subscription.py
new file mode 100644
index 00000000..04a5d1e5
--- /dev/null
+++ b/tests/test_subscription.py
@@ -0,0 +1,74 @@
+import pytest
+
+RITUAL_ID = 0
+ERC20_SUPPLY = 10 ** 24
+
+
+@pytest.fixture(scope="module")
+def deployer(accounts):
+ return accounts[0]
+
+
+@pytest.fixture(scope="module")
+def authority(accounts):
+ return accounts[1]
+
+
+@pytest.fixture(scope="module")
+def subscriber(accounts):
+ return accounts[2]
+
+
+@pytest.fixture(scope="module")
+def beneficiary(accounts):
+ return accounts[3]
+
+
+@pytest.fixture()
+def coordinator(project, deployer):
+ contract = project.CoordinatorForEncryptionAuthorizerMock.deploy(
+ sender=deployer,
+ )
+ return contract
+
+
+@pytest.fixture()
+def fee_token(project, deployer):
+ return project.TestToken.deploy(ERC20_SUPPLY, sender=deployer)
+
+
+@pytest.fixture()
+def subscription(project, coordinator, fee_token, beneficiary, authority):
+ return project.UpfrontSubscriptionWithEncryptorsCap.deploy(
+ coordinator.address, fee_token.address, beneficiary, sender=authority
+ )
+
+
+def test_new_subscription(subscription, fee_token, deployer):
+ cost = subscription.subscriptionFee()
+ fee_token.approve(subscription.address, cost, sender=deployer)
+ tx = subscription.newSubscription(RITUAL_ID, sender=deployer)
+ assert subscription.numberOfSubscriptions() == 1
+ # TODO: Fix this - Currently fails because fee_token is a mock contract
+ # assert tx.events == [
+ # fee_token.Transfer(admin, subscription.address, cost),
+ # managed_allow_list.AddressAuthorizationSet(RITUAL_ID, admin, True)
+ # ]
+ assert len(tx.events) == 1
+ assert subscription.authorizationActionsCap(RITUAL_ID, deployer) == 1000
+
+
+def test_cancel_subscription(subscription, fee_token, deployer):
+ cost = subscription.subscriptionFee()
+ fee_token.approve(subscription.address, cost, sender=deployer)
+ subscription.newSubscription(RITUAL_ID, sender=deployer)
+ assert subscription.numberOfSubscriptions() == 1
+ assert subscription.authorizationActionsCap(RITUAL_ID, deployer) == 1000
+
+ subscription.cancelSubscription(RITUAL_ID, 0, sender=deployer)
+ # TODO: Fix assertions
+ # assert not subscription.subscriptions()[0]
+ # assert not subscription.authorizationActionsCap(RITUAL_ID, deployer)
+
+
+# TODO: Add more tests