Initializable.test.js 8.8 KB

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