Initializable.test.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const InitializableMock = artifacts.require('InitializableMock');
  4. const ConstructorInitializableMock = artifacts.require('ConstructorInitializableMock');
  5. const ChildConstructorInitializableMock = artifacts.require('ChildConstructorInitializableMock');
  6. const ReinitializerMock = artifacts.require('ReinitializerMock');
  7. const SampleChild = artifacts.require('SampleChild');
  8. const DisableBad1 = artifacts.require('DisableBad1');
  9. const DisableBad2 = artifacts.require('DisableBad2');
  10. const DisableOk = artifacts.require('DisableOk');
  11. contract('Initializable', function () {
  12. describe('basic testing without inheritance', function () {
  13. beforeEach('deploying', async function () {
  14. this.contract = await InitializableMock.new();
  15. });
  16. describe('before initialize', function () {
  17. it('initializer has not run', async function () {
  18. expect(await this.contract.initializerRan()).to.equal(false);
  19. });
  20. it('_initializing returns false before initialization', async function () {
  21. expect(await this.contract.isInitializing()).to.equal(false);
  22. });
  23. });
  24. describe('after initialize', function () {
  25. beforeEach('initializing', async function () {
  26. await this.contract.initialize();
  27. });
  28. it('initializer has run', async function () {
  29. expect(await this.contract.initializerRan()).to.equal(true);
  30. });
  31. it('_initializing returns false after initialization', async function () {
  32. expect(await this.contract.isInitializing()).to.equal(false);
  33. });
  34. it('initializer does not run again', async function () {
  35. await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
  36. });
  37. });
  38. describe('nested under an initializer', function () {
  39. it('initializer modifier reverts', async function () {
  40. await expectRevert(this.contract.initializerNested(), 'Initializable: contract is already initialized');
  41. });
  42. it('onlyInitializing modifier succeeds', async function () {
  43. await this.contract.onlyInitializingNested();
  44. expect(await this.contract.onlyInitializingRan()).to.equal(true);
  45. });
  46. });
  47. it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
  48. await expectRevert(this.contract.initializeOnlyInitializing(), 'Initializable: contract is not initializing');
  49. });
  50. });
  51. it('nested initializer can run during construction', async function () {
  52. const contract2 = await ConstructorInitializableMock.new();
  53. expect(await contract2.initializerRan()).to.equal(true);
  54. expect(await contract2.onlyInitializingRan()).to.equal(true);
  55. });
  56. it('multiple constructor levels can be initializers', async function () {
  57. const contract2 = await ChildConstructorInitializableMock.new();
  58. expect(await contract2.initializerRan()).to.equal(true);
  59. expect(await contract2.childInitializerRan()).to.equal(true);
  60. expect(await contract2.onlyInitializingRan()).to.equal(true);
  61. });
  62. describe('reinitialization', function () {
  63. beforeEach('deploying', async function () {
  64. this.contract = await ReinitializerMock.new();
  65. });
  66. it('can reinitialize', async function () {
  67. expect(await this.contract.counter()).to.be.bignumber.equal('0');
  68. await this.contract.initialize();
  69. expect(await this.contract.counter()).to.be.bignumber.equal('1');
  70. await this.contract.reinitialize(2);
  71. expect(await this.contract.counter()).to.be.bignumber.equal('2');
  72. await this.contract.reinitialize(3);
  73. expect(await this.contract.counter()).to.be.bignumber.equal('3');
  74. });
  75. it('can jump multiple steps', async function () {
  76. expect(await this.contract.counter()).to.be.bignumber.equal('0');
  77. await this.contract.initialize();
  78. expect(await this.contract.counter()).to.be.bignumber.equal('1');
  79. await this.contract.reinitialize(128);
  80. expect(await this.contract.counter()).to.be.bignumber.equal('2');
  81. });
  82. it('cannot nest reinitializers', async function () {
  83. expect(await this.contract.counter()).to.be.bignumber.equal('0');
  84. await expectRevert(this.contract.nestedReinitialize(2, 2), 'Initializable: contract is already initialized');
  85. await expectRevert(this.contract.nestedReinitialize(2, 3), 'Initializable: contract is already initialized');
  86. await expectRevert(this.contract.nestedReinitialize(3, 2), 'Initializable: contract is already initialized');
  87. });
  88. it('can chain reinitializers', async function () {
  89. expect(await this.contract.counter()).to.be.bignumber.equal('0');
  90. await this.contract.chainReinitialize(2, 3);
  91. expect(await this.contract.counter()).to.be.bignumber.equal('2');
  92. });
  93. it('_getInitializedVersion returns right version', async function () {
  94. await this.contract.initialize();
  95. expect(await this.contract.getInitializedVersion()).to.be.bignumber.equal('1');
  96. await this.contract.reinitialize(12);
  97. expect(await this.contract.getInitializedVersion()).to.be.bignumber.equal('12');
  98. });
  99. describe('contract locking', function () {
  100. it('prevents initialization', async function () {
  101. await this.contract.disableInitializers();
  102. await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
  103. });
  104. it('prevents re-initialization', async function () {
  105. await this.contract.disableInitializers();
  106. await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
  107. });
  108. it('can lock contract after initialization', async function () {
  109. await this.contract.initialize();
  110. await this.contract.disableInitializers();
  111. await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
  112. });
  113. });
  114. });
  115. describe('events', function () {
  116. it('constructor initialization emits event', async function () {
  117. const contract = await ConstructorInitializableMock.new();
  118. await expectEvent.inTransaction(contract.transactionHash, contract, 'Initialized', { version: '1' });
  119. });
  120. it('initialization emits event', async function () {
  121. const contract = await ReinitializerMock.new();
  122. const { receipt } = await contract.initialize();
  123. expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
  124. expectEvent(receipt, 'Initialized', { version: '1' });
  125. });
  126. it('reinitialization emits event', async function () {
  127. const contract = await ReinitializerMock.new();
  128. const { receipt } = await contract.reinitialize(128);
  129. expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
  130. expectEvent(receipt, 'Initialized', { version: '128' });
  131. });
  132. it('chained reinitialization emits multiple events', async function () {
  133. const contract = await ReinitializerMock.new();
  134. const { receipt } = await contract.chainReinitialize(2, 3);
  135. expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(2);
  136. expectEvent(receipt, 'Initialized', { version: '2' });
  137. expectEvent(receipt, 'Initialized', { version: '3' });
  138. });
  139. });
  140. describe('complex testing with inheritance', function () {
  141. const mother = '12';
  142. const gramps = '56';
  143. const father = '34';
  144. const child = '78';
  145. beforeEach('deploying', async function () {
  146. this.contract = await SampleChild.new();
  147. });
  148. beforeEach('initializing', async function () {
  149. await this.contract.initialize(mother, gramps, father, child);
  150. });
  151. it('initializes human', async function () {
  152. expect(await this.contract.isHuman()).to.be.equal(true);
  153. });
  154. it('initializes mother', async function () {
  155. expect(await this.contract.mother()).to.be.bignumber.equal(mother);
  156. });
  157. it('initializes gramps', async function () {
  158. expect(await this.contract.gramps()).to.be.bignumber.equal(gramps);
  159. });
  160. it('initializes father', async function () {
  161. expect(await this.contract.father()).to.be.bignumber.equal(father);
  162. });
  163. it('initializes child', async function () {
  164. expect(await this.contract.child()).to.be.bignumber.equal(child);
  165. });
  166. });
  167. describe('disabling initialization', function () {
  168. it('old and new patterns in bad sequence', async function () {
  169. await expectRevert(DisableBad1.new(), 'Initializable: contract is already initialized');
  170. await expectRevert(DisableBad2.new(), 'Initializable: contract is initializing');
  171. });
  172. it('old and new patterns in good sequence', async function () {
  173. const ok = await DisableOk.new();
  174. await expectEvent.inConstruction(ok, 'Initialized', { version: '1' });
  175. await expectEvent.inConstruction(ok, 'Initialized', { version: '255' });
  176. });
  177. });
  178. });