Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto-11214: migrate more tests to foundry #13934

Merged
merged 13 commits into from
Aug 7, 2024
Prev Previous commit
Next Next commit
more tests
  • Loading branch information
FelixFan1992 committed Jul 26, 2024
commit ab74f94bc4ddd2a8bbf0d52472d8534e7e67104e
Original file line number Diff line number Diff line change
Expand Up @@ -1998,7 +1998,7 @@ contract Pause is SetUp {
registry.pause();

vm.expectRevert(Registry.RegistryPaused.selector);
linkUpkeepID = registry.registerUpkeep(
uint256 id = registry.registerUpkeep(
address(TARGET1),
config.maxPerformGas,
UPKEEP_ADMIN,
Expand All @@ -2009,4 +2009,132 @@ contract Pause is SetUp {
""
);
}

// function test_revertsWhen_transmitInPausedRegistry() external {
// vm.startPrank(registry.owner());
// registry.pause();
//
// vm.expectRevert(Registry.RegistryPaused.selector);
// _transmit(usdUpkeepID18, registry);
// }
}

contract Unpause is SetUp {
function test_RevertsWhen_CalledByNonOwner() external {
vm.startPrank(registry.owner());
registry.pause();

vm.expectRevert(bytes("Only callable by owner"));
vm.startPrank(STRANGER);
registry.unpause();
}

function test_CalledByOwner_success() external {
vm.startPrank(registry.owner());
registry.pause();
(IAutomationV21PlusCommon.StateLegacy memory state1, , , , ) = registry.getState();
assertTrue(state1.paused);

registry.unpause();
(IAutomationV21PlusCommon.StateLegacy memory state2, , , , ) = registry.getState();
assertFalse(state2.paused);
}
}

contract CancelUpkeep is SetUp {
event UpkeepCanceled(uint256 indexed id, uint64 indexed atBlockHeight);

function test_RevertsWhen_IdIsInvalid_CalledByAdmin() external {
vm.startPrank(UPKEEP_ADMIN);
vm.expectRevert(Registry.CannotCancel.selector);
registry.cancelUpkeep(1111111);
}

function test_RevertsWhen_IdIsInvalid_CalledByOwner() external {
vm.startPrank(registry.owner());
vm.expectRevert(Registry.CannotCancel.selector);
registry.cancelUpkeep(1111111);
}

function test_RevertsWhen_NotCalledByOwnerOrAdmin() external {
vm.startPrank(STRANGER);
vm.expectRevert(Registry.OnlyCallableByOwnerOrAdmin.selector);
registry.cancelUpkeep(linkUpkeepID);
}

function test_RevertsWhen_UpkeepAlreadyCanceledByAdmin_CalledByOwner() external {
uint256 bn = block.number;
vm.startPrank(UPKEEP_ADMIN);
registry.cancelUpkeep(linkUpkeepID);

vm.roll(50 + bn);

vm.startPrank(registry.owner());
vm.expectRevert(Registry.UpkeepCancelled.selector);
registry.cancelUpkeep(linkUpkeepID);
}

function test_RevertsWhen_UpkeepAlreadyCanceled_CalledByAdmin() external {
uint256 bn = block.number;
vm.startPrank(UPKEEP_ADMIN);
registry.cancelUpkeep(linkUpkeepID);

vm.roll(10 + bn);

vm.expectRevert(Registry.UpkeepCancelled.selector);
registry.cancelUpkeep(linkUpkeepID);
}

function test_RevertsWhen_UpkeepAlreadyCanceled_CalledByOwner() external {
uint256 bn = block.number;
vm.startPrank(registry.owner());
registry.cancelUpkeep(linkUpkeepID);

vm.roll(10 + bn);

vm.expectRevert(Registry.UpkeepCancelled.selector);
registry.cancelUpkeep(linkUpkeepID);
}

function test_CancelUpkeep_SetMaxValidBlockNumber_CalledByAdmin() external {
uint256 bn = block.number;
vm.startPrank(UPKEEP_ADMIN);
registry.cancelUpkeep(linkUpkeepID);

vm.roll(10 + bn);
uint256 maxValidBlocknumber = uint256(registry.getUpkeep(linkUpkeepID).maxValidBlocknumber);

// 50 is the cancellation delay
assertEq(bn + 50, maxValidBlocknumber);
}

function test_CancelUpkeep_SetMaxValidBlockNumber_CalledByOwner() external {
uint256 bn = block.number;
vm.startPrank(registry.owner());
registry.cancelUpkeep(linkUpkeepID);

vm.roll(10 + bn);
uint256 maxValidBlocknumber = uint256(registry.getUpkeep(linkUpkeepID).maxValidBlocknumber);

// cancellation by registry owner is immediate and no cancellation delay is applied
assertEq(bn, maxValidBlocknumber);
}

function test_cancelUpkeep_emitEvent_CalledByAdmin() external {
uint256 bn = block.number;
vm.startPrank(UPKEEP_ADMIN);

vm.expectEmit();
emit UpkeepCanceled(linkUpkeepID, uint64(bn + 50));
registry.cancelUpkeep(linkUpkeepID);
}

function test_cancelUpkeep_emitEvent_CalledByOwner() external {
uint256 bn = block.number;
vm.startPrank(registry.owner());

vm.expectEmit();
emit UpkeepCanceled(linkUpkeepID, uint64(bn));
registry.cancelUpkeep(linkUpkeepID);
}
}
145 changes: 0 additions & 145 deletions contracts/test/v0.8/automation/AutomationRegistry2_3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { ArbitrumModule__factory as ArbitrumModuleFactory } from '../../../typechain/factories/ArbitrumModule__factory'
import { OptimismModule__factory as OptimismModuleFactory } from '../../../typechain/factories/OptimismModule__factory'
import { ILogAutomation__factory as ILogAutomationactory } from '../../../typechain/factories/ILogAutomation__factory'
import { IAutomationForwarder__factory as IAutomationForwarderFactory } from '../../../typechain/factories/IAutomationForwarder__factory'

Check warning on line 28 in contracts/test/v0.8/automation/AutomationRegistry2_3.test.ts

View workflow job for this annotation

GitHub Actions / Solidity Lint

'IAutomationForwarderFactory' is defined but never used. Allowed unused vars must match /^_/u
import { MockArbSys__factory as MockArbSysFactory } from '../../../typechain/factories/MockArbSys__factory'
import { AutomationCompatibleUtils } from '../../../typechain/AutomationCompatibleUtils'
import { MockArbGasInfo } from '../../../typechain/MockArbGasInfo'
Expand Down Expand Up @@ -4890,21 +4890,6 @@
})

describe('#pause', () => {
// it('reverts if called by a non-owner', async () => {
// await evmRevert(
// registry.connect(keeper1).pause(),
// 'Only callable by owner',
// )
// })
//
// it('marks the contract as paused', async () => {
// assert.isFalse((await registry.getState()).state.paused)
//
// await registry.connect(owner).pause()
//
// assert.isTrue((await registry.getState()).state.paused)
// })

it('Does not allow transmits when paused', async () => {
await registry.connect(owner).pause()

Expand All @@ -4914,48 +4899,6 @@
'RegistryPaused',
)
})

// it('Does not allow creation of new upkeeps when paused', async () => {
// await registry.connect(owner).pause()
//
// await evmRevertCustomError(
// registry
// .connect(owner)
// .registerUpkeep(
// mock.address,
// performGas,
// await admin.getAddress(),
// Trigger.CONDITION,
// linkToken.address,
// '0x',
// '0x',
// '0x',
// ),
// registry,
// 'RegistryPaused',
// )
// })
})

describe('#unpause', () => {
beforeEach(async () => {
await registry.connect(owner).pause()
})

it('reverts if called by a non-owner', async () => {
await evmRevert(
registry.connect(keeper1).unpause(),
'Only callable by owner',
)
})

it('marks the contract as not paused', async () => {
assert.isTrue((await registry.getState()).state.paused)

await registry.connect(owner).unpause()

assert.isFalse((await registry.getState()).state.paused)
})
})

describe('#setPayees', () => {
Expand Down Expand Up @@ -5078,41 +5021,7 @@
})

describe('#cancelUpkeep', () => {
it('reverts if the ID is not valid', async () => {
await evmRevertCustomError(
registry.connect(owner).cancelUpkeep(upkeepId.add(1)),
registry,
'CannotCancel',
)
})

it('reverts if called by a non-owner/non-admin', async () => {
await evmRevertCustomError(
registry.connect(keeper1).cancelUpkeep(upkeepId),
registry,
'OnlyCallableByOwnerOrAdmin',
)
})

describe('when called by the owner', async () => {
it('sets the registration to invalid immediately', async () => {
const tx = await registry.connect(owner).cancelUpkeep(upkeepId)
const receipt = await tx.wait()
const registration = await registry.getUpkeep(upkeepId)
assert.equal(
registration.maxValidBlocknumber.toNumber(),
receipt.blockNumber,
)
})

it('emits an event', async () => {
const tx = await registry.connect(owner).cancelUpkeep(upkeepId)
const receipt = await tx.wait()
await expect(tx)
.to.emit(registry, 'UpkeepCanceled')
.withArgs(upkeepId, BigNumber.from(receipt.blockNumber))
})

it('immediately prevents upkeep', async () => {
await registry.connect(owner).cancelUpkeep(upkeepId)

Expand All @@ -5124,19 +5033,10 @@
assert.equal(cancelledUpkeepReportLogs.length, 1)
})

it('does not revert if reverts if called multiple times', async () => {
await registry.connect(owner).cancelUpkeep(upkeepId)
await evmRevertCustomError(
registry.connect(owner).cancelUpkeep(upkeepId),
registry,
'UpkeepCancelled',
)
})

describe('when called by the owner when the admin has just canceled', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// @ts-ignore

Check warning on line 5038 in contracts/test/v0.8/automation/AutomationRegistry2_3.test.ts

View workflow job for this annotation

GitHub Actions / Solidity Lint

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
let oldExpiration: BigNumber

Check warning on line 5039 in contracts/test/v0.8/automation/AutomationRegistry2_3.test.ts

View workflow job for this annotation

GitHub Actions / Solidity Lint

'oldExpiration' is assigned a value but never used. Allowed unused vars must match /^_/u

beforeEach(async () => {
await registry.connect(admin).cancelUpkeep(upkeepId)
Expand All @@ -5155,51 +5055,6 @@
})

describe('when called by the admin', async () => {
it('reverts if called again by the admin', async () => {
await registry.connect(admin).cancelUpkeep(upkeepId)

await evmRevertCustomError(
registry.connect(admin).cancelUpkeep(upkeepId),
registry,
'UpkeepCancelled',
)
})

it('reverts if called by the owner after the timeout', async () => {
await registry.connect(admin).cancelUpkeep(upkeepId)

for (let i = 0; i < cancellationDelay; i++) {
await ethers.provider.send('evm_mine', [])
}

await evmRevertCustomError(
registry.connect(owner).cancelUpkeep(upkeepId),
registry,
'UpkeepCancelled',
)
})

it('sets the registration to invalid in 50 blocks', async () => {
const tx = await registry.connect(admin).cancelUpkeep(upkeepId)
const receipt = await tx.wait()
const registration = await registry.getUpkeep(upkeepId)
assert.equal(
registration.maxValidBlocknumber.toNumber(),
receipt.blockNumber + 50,
)
})

it('emits an event', async () => {
const tx = await registry.connect(admin).cancelUpkeep(upkeepId)
const receipt = await tx.wait()
await expect(tx)
.to.emit(registry, 'UpkeepCanceled')
.withArgs(
upkeepId,
BigNumber.from(receipt.blockNumber + cancellationDelay),
)
})

it('immediately prevents upkeep', async () => {
await linkToken.connect(owner).approve(registry.address, toWei('100'))
await registry.connect(owner).addFunds(upkeepId, toWei('100'))
Expand Down
Loading