From 072f854937630ec09e8658b117b7bb6bfd3093f9 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 6 Dec 2023 14:24:55 +0100 Subject: [PATCH] Drafting tests for AcreRouter --- core/test/AcreRouter.test.ts | 148 +++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 core/test/AcreRouter.test.ts diff --git a/core/test/AcreRouter.test.ts b/core/test/AcreRouter.test.ts new file mode 100644 index 000000000..af3e358b9 --- /dev/null +++ b/core/test/AcreRouter.test.ts @@ -0,0 +1,148 @@ +import { ethers, getUnnamedAccounts, getNamedAccounts } from "hardhat" +import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers" +import { Address } from "hardhat-deploy/types" +import { expect } from "chai" +import { + SnapshotRestorer, + takeSnapshot, +} from "@nomicfoundation/hardhat-toolbox/network-helpers" +import type { AcreRouter } from "../typechain" + +describe("AcreRouter", () => { + let snapshot: SnapshotRestorer + + let acreRouter: AcreRouter + let deployerSigner: HardhatEthersSigner + let governanceSigner: HardhatEthersSigner + let thirdPartySigner: HardhatEthersSigner + let vault1: Address + let vault2: Address + let vault3: Address + let vault4: Address + + // TODO: + // Put some of this setup to deployment scripts and/or helpers e.g. ownership + // transfer, acreRouter deployment etc. See: https://github.com/thesis/acre/pull/58 + // and https://github.com/thesis/acre/pull/64 + before(async () => { + const accounts = await getUnnamedAccounts() + ;[vault1, vault2, vault3, vault4] = accounts + const { deployer, governance } = await getNamedAccounts() + deployerSigner = await ethers.getSigner(deployer) + governanceSigner = await ethers.getSigner(governance) + thirdPartySigner = await ethers.getSigner(accounts[0]) + const AcreRouter = await ethers.getContractFactory("AcreRouter") + acreRouter = await AcreRouter.connect(deployerSigner).deploy() + acreRouter.transferOwnership(governance) + }) + + beforeEach(async () => { + snapshot = await takeSnapshot() + }) + + afterEach(async () => { + await snapshot.restore() + }) + + describe("addVault", () => { + context("when caller is not a governance account", () => { + it("should revert when adding a vault", async () => { + await expect( + acreRouter.connect(thirdPartySigner).addVault(vault1), + ).to.be.revertedWithCustomError( + acreRouter, + "OwnableUnauthorizedAccount", + ) + }) + }) + + context("when caller is a governance account", () => { + it("should be able to add vaults", async () => { + await acreRouter.connect(governanceSigner).addVault(vault1) + await acreRouter.connect(governanceSigner).addVault(vault2) + await acreRouter.connect(governanceSigner).addVault(vault3) + + expect(await acreRouter.vaults(0)).to.equal(vault1) + const isVault1Approved = await acreRouter.vaultsInfo(vault1) + expect(isVault1Approved).to.equal(true) + + expect(await acreRouter.vaults(1)).to.equal(vault2) + const isVault2Approved = await acreRouter.vaultsInfo(vault1) + expect(isVault2Approved).to.equal(true) + + expect(await acreRouter.vaults(2)).to.equal(vault3) + const isVault3Approved = await acreRouter.vaultsInfo(vault1) + expect(isVault3Approved).to.equal(true) + }) + + it("should not be able to add the same vault twice", async () => { + await acreRouter.connect(governanceSigner).addVault(vault1) + await expect( + acreRouter.connect(governanceSigner).addVault(vault1), + ).to.be.revertedWith("Vault already approved") + }) + + it("should emit an event when adding a vault", async () => { + await expect(acreRouter.connect(governanceSigner).addVault(vault1)) + .to.emit(acreRouter, "VaultAdded") + .withArgs(vault1) + }) + }) + }) + + describe("removeVault", () => { + beforeEach(async () => { + await acreRouter.connect(governanceSigner).addVault(vault1) + await acreRouter.connect(governanceSigner).addVault(vault2) + await acreRouter.connect(governanceSigner).addVault(vault3) + }) + + context("when caller is not a governance account", () => { + it("should revert when adding a vault", async () => { + await expect( + acreRouter.connect(thirdPartySigner).removeVault(vault1), + ).to.be.revertedWithCustomError( + acreRouter, + "OwnableUnauthorizedAccount", + ) + }) + }) + + context("when caller is a governance account", () => { + it("should be able to remove vaults", async () => { + await acreRouter.connect(governanceSigner).removeVault(vault1) + + // Last vault replaced the first vault in the 'vaults' array + expect(await acreRouter.vaults(0)).to.equal(vault3) + const isVault1Approved = await acreRouter.vaultsInfo(vault1) + expect(isVault1Approved).to.equal(false) + expect(await acreRouter.vaultsLength()).to.equal(2) + + await acreRouter.connect(governanceSigner).removeVault(vault2) + + // Last vault (vault2) was removed from the 'vaults' array + expect(await acreRouter.vaults(0)).to.equal(vault3) + expect(await acreRouter.vaultsLength()).to.equal(1) + const isVault2Approved = await acreRouter.vaultsInfo(vault2) + expect(isVault2Approved).to.equal(false) + + await acreRouter.connect(governanceSigner).removeVault(vault3) + expect(await acreRouter.vaultsLength()).to.equal(0) + const isVault3Approved = await acreRouter.vaultsInfo(vault3) + expect(isVault3Approved).to.equal(false) + }) + + it("should not be able to remove a vault that is not approved", async () => { + await expect( + acreRouter.connect(governanceSigner).removeVault(vault4), + ).to.be.revertedWith("Not a vault") + }) + + it("should emit an event when removing a vault", async () => { + await expect(acreRouter.connect(governanceSigner).removeVault(vault1)) + .to.emit(acreRouter, "VaultRemoved") + .withArgs(vault1) + }) + }) + }) +})