Skip to content

Commit

Permalink
added more tests for dao-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Mar 18, 2022
1 parent 6305790 commit 4089a18
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 38 deletions.
3 changes: 1 addition & 2 deletions contracts/core/DaoFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ contract DaoFactory is CloneFactory {
addresses[hashedName] == address(0x0),
string(abi.encodePacked("name ", daoName, " already taken"))
);
DaoRegistry dao =
DaoRegistry(_createClone(identityAddress));
DaoRegistry dao = DaoRegistry(_createClone(identityAddress));

address daoAddr = address(dao);
addresses[hashedName] = daoAddr;
Expand Down
4 changes: 2 additions & 2 deletions contracts/core/DaoRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,14 @@ contract DaoRegistry is MemberGuard, AdapterGuard {
* @dev Block number must be a finalized block or else this function will revert to prevent misinformation.
* @param memberAddr The address of the account to check
* @param blockNumber The block number to get the vote balance at
* @return The number of votes the account had as of the given block
* @return The delegate key of the member
*/
function getPriorDelegateKey(address memberAddr, uint256 blockNumber)
external
view
returns (address)
{
require(blockNumber < block.number, "Uni::getPriorDelegateKey: NYD");
require(blockNumber < block.number, "getPriorDelegateKey: NYD");

uint32 nCheckpoints = _numCheckpoints[memberAddr];
if (nCheckpoints == 0) {
Expand Down
312 changes: 278 additions & 34 deletions test/core/dao-registry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
sha3,
ZERO_ADDRESS,
MEMBER_COUNT,
DAI_TOKEN,
} = require("../../utils/contract-util");

const {
Expand All @@ -40,14 +41,19 @@ const {
BankExtension,
web3,
getAccounts,
getSigners,
getCurrentBlockNumber,
expectEvent,
advanceTime,
txSigner,
} = require("../../utils/hardhat-test-util");

describe("Core - DaoRegistry", () => {
let accounts, owner, creator;
let accounts, owner, creator, signers;

before("deploy dao", async () => {
accounts = await getAccounts();
signers = await getSigners();
owner = accounts[0];
creator = accounts[1];

Expand Down Expand Up @@ -306,29 +312,6 @@ describe("Core - DaoRegistry", () => {
).to.be.revertedWith("extensionId already in use");
});

it("should not be possible to add an extension without the correct ACL flag", async () => {
const extensionId = sha3("bank");
const registry = this.dao;
await this.factories.bankExtFactory.create(registry.address, 10);
const bankAddress =
await this.factories.bankExtFactory.getExtensionAddress(
registry.address
);

await expect(
registry.addExtension(extensionId, bankAddress)
).to.be.revertedWith("accessDenied");
});

it("should not be possible to remove an extension without the correct ACL flag", async () => {
const extensionId = sha3("bank");
const registry = this.dao;

await expect(registry.removeExtension(extensionId)).to.be.revertedWith(
"accessDenied"
);
});

it("should not be possible to remove an extension with an empty id", async () => {
const extensionId = fromUtf8("");
const registry = await DaoRegistry.new();
Expand Down Expand Up @@ -418,8 +401,197 @@ describe("Core - DaoRegistry", () => {
});
});

describe.skip("Delegate", async () => {
//TODO delegate
describe("Delegate", async () => {
it("should be possible to add a delegate key", async () => {
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
const delegate = accounts[2];
await expectEvent(
registry.updateDelegateKey(owner, delegate),
"UpdateDelegateKey",
owner,
delegate
);
});

it("should not be possible to add a delegate key if the member does not exist", async () => {
const registry = await DaoRegistry.new();
const delegate = accounts[2];
await expect(
registry.updateDelegateKey(owner, delegate)
).to.be.revertedWith("member does not exist");
});

it("should not be possible to use zero address as delegate", async () => {
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await expect(
registry.updateDelegateKey(owner, ZERO_ADDRESS)
).to.be.revertedWith("newDelegateKey cannot be 0");
});

it("should not be possible to overwrite a delegate", async () => {
const delegate = accounts[1];
const memberA = accounts[2];
const memberB = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);
await registry.potentialNewMember(memberB);

await registry.updateDelegateKey(memberA, delegate);

await expect(
registry.updateDelegateKey(memberB, delegate)
).to.be.revertedWith("cannot overwrite existing delegated keys");
});

it("should not be possible to use a delegate to update a delegate", async () => {
const delegateA = accounts[1];
const delegateB = accounts[2];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

await registry.updateDelegateKey(memberA, delegateA);

await expect(
registry.updateDelegateKey(delegateA, delegateA)
).to.be.revertedWith("address already taken as delegated key");
});

it("should be possible to get the member address by delegate", async () => {
const delegateA = accounts[1];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

await registry.updateDelegateKey(memberA, delegateA);

expect(await registry.getAddressIfDelegated(delegateA)).to.be.equal(
memberA
);
});

it("should be return the provided address if it is not a delegate", async () => {
const delegateA = accounts[1];
const delegateB = accounts[2];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

await registry.updateDelegateKey(memberA, delegateA);

expect(await registry.getAddressIfDelegated(delegateA)).to.be.equal(
memberA
);

expect(await registry.getAddressIfDelegated(delegateB)).to.be.equal(
delegateB
);
});

it("should be possible to get the current delegate by member", async () => {
const delegateA = accounts[1];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

await registry.updateDelegateKey(memberA, delegateA);

expect(await registry.getCurrentDelegateKey(memberA)).to.be.equal(
delegateA
);
});

it("should return the member address if there is no previous delegate by member", async () => {
const delegateA = accounts[1];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

await registry.updateDelegateKey(memberA, delegateA);
expect(await registry.getPreviousDelegateKey(memberA)).to.be.equal(
memberA
);
});

it("should be possible to get the previous delegate by member", async () => {
const delegateA = accounts[1];
const delegateB = accounts[2];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

await registry.updateDelegateKey(memberA, delegateA);
expect(await registry.getPreviousDelegateKey(memberA)).to.be.equal(
memberA
);

await registry.updateDelegateKey(memberA, delegateB);
expect(await registry.getPreviousDelegateKey(memberA)).to.be.equal(
delegateA
);
expect(await registry.getCurrentDelegateKey(memberA)).to.be.equal(
delegateB
);
});
});

it("should return the member address if the prior delegate has no checkpoints in a valid block", async () => {
const delegateA = accounts[1];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

const blockNumber = await getCurrentBlockNumber();
await registry.updateDelegateKey(memberA, delegateA);

expect(
await registry.getPriorDelegateKey(memberA, blockNumber)
).to.be.equal(memberA);
});

it("should return the member delegate prior to the valid block", async () => {
const delegateA = accounts[1];
const delegateB = accounts[2];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);

await registry.updateDelegateKey(memberA, delegateA);
await advanceTime(2000);

const blockNumber = await getCurrentBlockNumber();
await registry.updateDelegateKey(memberA, delegateB);
await advanceTime(2000);

expect(
await registry.getPriorDelegateKey(memberA, blockNumber)
).to.be.equal(delegateA);
});

it("should not be possible to get the prior delegate by member with an invalid block", async () => {
const delegateA = accounts[1];
const memberA = accounts[3];
const registry = await DaoRegistry.new();
await registry.potentialNewMember(owner);
await registry.potentialNewMember(memberA);
const blockNumber = await getCurrentBlockNumber();

await registry.updateDelegateKey(memberA, delegateA);

await expect(
registry.getPriorDelegateKey(memberA, blockNumber + 1)
).to.be.revertedWith("getPriorDelegateKey: NYD");
});
});

Expand All @@ -445,12 +617,6 @@ describe("Core - DaoRegistry", () => {
expect(await dao.getProposalFlag(proposalId, 2)).to.be.false;
});

it("should not be possible to submit a proposal without the right ACL flag SUBMIT_PROPOSAL", async () => {
await expect(
this.dao.submitProposal(sha3(fromUtf8("proposal1")))
).to.be.revertedWith("accessDenied");
});

it("should return false if the proposal does not exist", async () => {
expect(await this.dao.getProposalFlag(sha3(fromUtf8("proposal1")), 0))
.to.be.false;
Expand Down Expand Up @@ -593,8 +759,86 @@ describe("Core - DaoRegistry", () => {
//TODO
});

describe.skip("Access Control", async () => {
//TODO ACLs, initialize, finalize, lock, unlock
describe("Access Control", async () => {
it("should not be possible to call initialize more than once", async () => {
await expect(this.dao.initialize(creator, owner)).to.be.revertedWith(
"dao already initialized"
);
});

it.skip("should not be possible to call finalizeDao if the sender is not an active member or an adapter", async () => {
await expect(
txSigner(signers[2], this.dao).finalizeDao({ from: creator })
).to.be.revertedWith("not allowed to finalize");
});

it("should not be possible to call setConfiguration without the SET_CONFIGURATION permission", async () => {
await expect(
this.dao.setConfiguration(sha3("config1"), 1)
).to.be.revertedWith("accessDenied");
});

it("should not be possible to call setAddressConfiguration without the SET_CONFIGURATION permission", async () => {
await expect(
this.dao.setAddressConfiguration(sha3("config1"), DAI_TOKEN)
).to.be.revertedWith("accessDenied");
});

it("should not be possible to call replaceAdapter without the REPLACE_ADAPTER permission", async () => {
await expect(
this.dao.replaceAdapter(sha3("adapter1"), accounts[2], 1, [], [])
).to.be.revertedWith("accessDenied");
});

it("should not be possible to call addExtension without the ADD_EXTENSION permission", async () => {
await expect(
this.dao.addExtension(sha3("extension1"), accounts[2])
).to.be.revertedWith("accessDenied");
});

it("should not be possible to call removeExtension without the REMOVE_EXTENSION permission", async () => {
await expect(
this.dao.removeExtension(sha3("extension1"))
).to.be.revertedWith("accessDenied");
});

it("should not be possible to call setAclToExtensionForAdapter without the ADD_EXTENSION permission", async () => {
await expect(
this.dao.setAclToExtensionForAdapter(accounts[2], accounts[3], 1)
).to.be.revertedWith("accessDenied");
});

it("should not be possible to call submitProposal without the SUBMIT_PROPOSAL permission", async () => {
await expect(
this.dao.submitProposal(sha3(fromUtf8("proposal1")))
).to.be.revertedWith("accessDenied");
});

it("should not be possible to call jailMember without the JAIL_MEMBER permission", async () => {
await expect(this.dao.jailMember(owner)).to.be.revertedWith(
"accessDenied"
);
});

it("should not be possible to call unjailMember without the JAIL_MEMBER permission", async () => {
await expect(this.dao.unjailMember(owner)).to.be.revertedWith(
"accessDenied"
);
});

it("should not be possible to call potentialNewMember without the NEW_MEMBER permission", async () => {
await expect(this.dao.potentialNewMember(owner)).to.be.revertedWith(
"accessDenied"
);
});

it("should not be possible to call updateDelegateKey without the UPDATE_DELEGATE_KEY permission", async () => {
await expect(
this.dao.updateDelegateKey(owner, accounts[2])
).to.be.revertedWith("accessDenied");
});

//TODO lock, unlock
});

describe("General", async () => {
Expand Down
Loading

0 comments on commit 4089a18

Please sign in to comment.