forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAuthorityUtils.test.js
112 lines (98 loc) · 3.67 KB
/
AuthorityUtils.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { MAX_UINT32, MAX_UINT64 } = require('../../helpers/constants');
async function fixture() {
const [user, other] = await ethers.getSigners();
const mock = await ethers.deployContract('$AuthorityUtils');
const notAuthorityMock = await ethers.deployContract('NotAuthorityMock');
const authorityNoDelayMock = await ethers.deployContract('AuthorityNoDelayMock');
const authorityDelayMock = await ethers.deployContract('AuthorityDelayMock');
const authorityNoResponse = await ethers.deployContract('AuthorityNoResponse');
return {
user,
other,
mock,
notAuthorityMock,
authorityNoDelayMock,
authorityDelayMock,
authorityNoResponse,
};
}
describe('AuthorityUtils', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('canCallWithDelay', function () {
describe('when authority does not have a canCall function', function () {
beforeEach(async function () {
this.authority = this.notAuthorityMock;
});
it('returns (immediate = 0, delay = 0)', async function () {
const { immediate, delay } = await this.mock.$canCallWithDelay(
this.authority,
this.user,
this.other,
'0x12345678',
);
expect(immediate).to.be.false;
expect(delay).to.equal(0n);
});
});
describe('when authority has no delay', function () {
beforeEach(async function () {
this.authority = this.authorityNoDelayMock;
this.immediate = true;
await this.authority._setImmediate(this.immediate);
});
it('returns (immediate, delay = 0)', async function () {
const { immediate, delay } = await this.mock.$canCallWithDelay(
this.authority,
this.user,
this.other,
'0x12345678',
);
expect(immediate).to.equal(this.immediate);
expect(delay).to.equal(0n);
});
});
describe('when authority replies with a delay', function () {
beforeEach(async function () {
this.authority = this.authorityDelayMock;
});
for (const immediate of [true, false]) {
for (const delay of [0n, 42n, MAX_UINT32]) {
it(`returns (immediate=${immediate}, delay=${delay})`, async function () {
await this.authority._setImmediate(immediate);
await this.authority._setDelay(delay);
const result = await this.mock.$canCallWithDelay(this.authority, this.user, this.other, '0x12345678');
expect(result.immediate).to.equal(immediate);
expect(result.delay).to.equal(delay);
});
}
}
it('out of bound delay', async function () {
await this.authority._setImmediate(false);
await this.authority._setDelay(MAX_UINT64); // bigger than the expected uint32
const result = await this.mock.$canCallWithDelay(this.authority, this.user, this.other, '0x12345678');
expect(result.immediate).to.equal(false);
expect(result.delay).to.equal(0n);
});
});
describe('when authority replies with empty data', function () {
beforeEach(async function () {
this.authority = this.authorityNoResponse;
});
it('returns (immediate = 0, delay = 0)', async function () {
const { immediate, delay } = await this.mock.$canCallWithDelay(
this.authority,
this.user,
this.other,
'0x12345678',
);
expect(immediate).to.be.false;
expect(delay).to.equal(0n);
});
});
});
});