Initializable.test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. contract('Initializable', function (accounts) {
  9. describe('basic testing without inheritance', function () {
  10. beforeEach('deploying', async function () {
  11. this.contract = await InitializableMock.new();
  12. });
  13. describe('before initialize', function () {
  14. it('initializer has not run', async function () {
  15. expect(await this.contract.initializerRan()).to.equal(false);
  16. });
  17. });
  18. describe('after initialize', function () {
  19. beforeEach('initializing', async function () {
  20. await this.contract.initialize();
  21. });
  22. it('initializer has run', async function () {
  23. expect(await this.contract.initializerRan()).to.equal(true);
  24. });
  25. it('initializer does not run again', async function () {
  26. await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
  27. });
  28. });
  29. describe('nested under an initializer', function () {
  30. it('initializer modifier reverts', async function () {
  31. await expectRevert(this.contract.initializerNested(), 'Initializable: contract is already initialized');
  32. });
  33. it('onlyInitializing modifier succeeds', async function () {
  34. await this.contract.onlyInitializingNested();
  35. expect(await this.contract.onlyInitializingRan()).to.equal(true);
  36. });
  37. });
  38. it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
  39. await expectRevert(this.contract.initializeOnlyInitializing(), 'Initializable: contract is not initializing');
  40. });
  41. });
  42. it('nested initializer can run during construction', async function () {
  43. const contract2 = await ConstructorInitializableMock.new();
  44. expect(await contract2.initializerRan()).to.equal(true);
  45. expect(await contract2.onlyInitializingRan()).to.equal(true);
  46. });
  47. it('multiple constructor levels can be initializers', async function () {
  48. const contract2 = await ChildConstructorInitializableMock.new();
  49. expect(await contract2.initializerRan()).to.equal(true);
  50. expect(await contract2.childInitializerRan()).to.equal(true);
  51. expect(await contract2.onlyInitializingRan()).to.equal(true);
  52. });
  53. describe('reinitialization', function () {
  54. beforeEach('deploying', async function () {
  55. this.contract = await ReinitializerMock.new();
  56. });
  57. it('can reinitialize', async function () {
  58. expect(await this.contract.counter()).to.be.bignumber.equal('0');
  59. await this.contract.initialize();
  60. expect(await this.contract.counter()).to.be.bignumber.equal('1');
  61. await this.contract.reinitialize(2);
  62. expect(await this.contract.counter()).to.be.bignumber.equal('2');
  63. await this.contract.reinitialize(3);
  64. expect(await this.contract.counter()).to.be.bignumber.equal('3');
  65. });
  66. it('can jump multiple steps', 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(128);
  71. expect(await this.contract.counter()).to.be.bignumber.equal('2');
  72. });
  73. it('cannot nest reinitializers', async function () {
  74. expect(await this.contract.counter()).to.be.bignumber.equal('0');
  75. await expectRevert(this.contract.nestedReinitialize(2, 2), 'Initializable: contract is already initialized');
  76. await expectRevert(this.contract.nestedReinitialize(2, 3), 'Initializable: contract is already initialized');
  77. await expectRevert(this.contract.nestedReinitialize(3, 2), 'Initializable: contract is already initialized');
  78. });
  79. it('can chain reinitializers', async function () {
  80. expect(await this.contract.counter()).to.be.bignumber.equal('0');
  81. await this.contract.chainReinitialize(2, 3);
  82. expect(await this.contract.counter()).to.be.bignumber.equal('2');
  83. });
  84. describe('contract locking', function () {
  85. it('prevents initialization', async function () {
  86. await this.contract.disableInitializers();
  87. await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
  88. });
  89. it('prevents re-initialization', async function () {
  90. await this.contract.disableInitializers();
  91. await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
  92. });
  93. it('can lock contract after initialization', async function () {
  94. await this.contract.initialize();
  95. await this.contract.disableInitializers();
  96. await expectRevert(this.contract.reinitialize(255), 'Initializable: contract is already initialized');
  97. });
  98. });
  99. });
  100. describe('events', function () {
  101. it('constructor initialization emits event', async function () {
  102. const contract = await ConstructorInitializableMock.new();
  103. await expectEvent.inTransaction(contract.transactionHash, contract, 'Initialized', { version: '1' });
  104. });
  105. it('initialization emits event', async function () {
  106. const contract = await ReinitializerMock.new();
  107. const { receipt } = await contract.initialize();
  108. expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
  109. expectEvent(receipt, 'Initialized', { version: '1' });
  110. });
  111. it('reinitialization emits event', async function () {
  112. const contract = await ReinitializerMock.new();
  113. const { receipt } = await contract.reinitialize(128);
  114. expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
  115. expectEvent(receipt, 'Initialized', { version: '128' });
  116. });
  117. it('chained reinitialization emits multiple events', async function () {
  118. const contract = await ReinitializerMock.new();
  119. const { receipt } = await contract.chainReinitialize(2, 3);
  120. expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(2);
  121. expectEvent(receipt, 'Initialized', { version: '2' });
  122. expectEvent(receipt, 'Initialized', { version: '3' });
  123. });
  124. });
  125. describe('complex testing with inheritance', function () {
  126. const mother = '12';
  127. const gramps = '56';
  128. const father = '34';
  129. const child = '78';
  130. beforeEach('deploying', async function () {
  131. this.contract = await SampleChild.new();
  132. });
  133. beforeEach('initializing', async function () {
  134. await this.contract.initialize(mother, gramps, father, child);
  135. });
  136. it('initializes human', async function () {
  137. expect(await this.contract.isHuman()).to.be.equal(true);
  138. });
  139. it('initializes mother', async function () {
  140. expect(await this.contract.mother()).to.be.bignumber.equal(mother);
  141. });
  142. it('initializes gramps', async function () {
  143. expect(await this.contract.gramps()).to.be.bignumber.equal(gramps);
  144. });
  145. it('initializes father', async function () {
  146. expect(await this.contract.father()).to.be.bignumber.equal(father);
  147. });
  148. it('initializes child', async function () {
  149. expect(await this.contract.child()).to.be.bignumber.equal(child);
  150. });
  151. });
  152. });