/
Initializable.test.js
145 lines (119 loc) · 5.57 KB
/
Initializable.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const InitializableMock = artifacts.require('InitializableMock');
const ConstructorInitializableMock = artifacts.require('ConstructorInitializableMock');
const ReinitializerMock = artifacts.require('ReinitializerMock');
const SampleChild = artifacts.require('SampleChild');
contract('Initializable', function (accounts) {
describe('basic testing without inheritance', function () {
beforeEach('deploying', async function () {
this.contract = await InitializableMock.new();
});
context('before initialize', function () {
it('initializer has not run', async function () {
expect(await this.contract.initializerRan()).to.equal(false);
});
});
context('after initialize', function () {
beforeEach('initializing', async function () {
await this.contract.initialize();
});
it('initializer has run', async function () {
expect(await this.contract.initializerRan()).to.equal(true);
});
it('initializer does not run again', async function () {
await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
});
});
context('nested under an initializer', function () {
it('initializer modifier reverts', async function () {
await expectRevert(this.contract.initializerNested(), 'Initializable: contract is already initialized');
});
it('onlyInitializing modifier succeeds', async function () {
await this.contract.onlyInitializingNested();
expect(await this.contract.onlyInitializingRan()).to.equal(true);
});
});
it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
await expectRevert(this.contract.initializeOnlyInitializing(), 'Initializable: contract is not initializing');
});
});
it('nested initializer can run during construction', async function () {
const contract2 = await ConstructorInitializableMock.new();
expect(await contract2.initializerRan()).to.equal(true);
expect(await contract2.onlyInitializingRan()).to.equal(true);
});
describe('reinitialization', function () {
beforeEach('deploying', async function () {
this.contract = await ReinitializerMock.new();
});
it('can reinitialize', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await this.contract.initialize();
expect(await this.contract.counter()).to.be.bignumber.equal('1');
await this.contract.reinitialize(2);
expect(await this.contract.counter()).to.be.bignumber.equal('2');
await this.contract.reinitialize(3);
expect(await this.contract.counter()).to.be.bignumber.equal('3');
});
it('can jump multiple steps', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await this.contract.initialize();
expect(await this.contract.counter()).to.be.bignumber.equal('1');
await this.contract.reinitialize(128);
expect(await this.contract.counter()).to.be.bignumber.equal('2');
});
it('cannot nest reinitializers', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await expectRevert(this.contract.nestedReinitialize(2, 3), 'Initializable: contract is already initialized');
await expectRevert(this.contract.nestedReinitialize(3, 2), 'Initializable: contract is already initialized');
});
it('can chain reinitializers', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0');
await this.contract.chainReinitialize(2, 3);
expect(await this.contract.counter()).to.be.bignumber.equal('2');
});
describe('contract locking', function () {
it('prevents initialization', async function () {
await this.contract.disableInitializers();
await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
});
it('prevents re-initialization', async function () {
await this.contract.disableInitializers();
await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
});
it('can lock contract after initialization', async function () {
await this.contract.initialize();
await this.contract.disableInitializers();
await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
});
});
});
describe('complex testing with inheritance', function () {
const mother = '12';
const gramps = '56';
const father = '34';
const child = '78';
beforeEach('deploying', async function () {
this.contract = await SampleChild.new();
});
beforeEach('initializing', async function () {
await this.contract.initialize(mother, gramps, father, child);
});
it('initializes human', async function () {
expect(await this.contract.isHuman()).to.be.equal(true);
});
it('initializes mother', async function () {
expect(await this.contract.mother()).to.be.bignumber.equal(mother);
});
it('initializes gramps', async function () {
expect(await this.contract.gramps()).to.be.bignumber.equal(gramps);
});
it('initializes father', async function () {
expect(await this.contract.father()).to.be.bignumber.equal(father);
});
it('initializes child', async function () {
expect(await this.contract.child()).to.be.bignumber.equal(child);
});
});
});