ProxyAdmin.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ImplV1 = artifacts.require('DummyImplementation');
  4. const ImplV2 = artifacts.require('DummyImplementationV2');
  5. const ProxyAdmin = artifacts.require('ProxyAdmin');
  6. const TransparentUpgradeableProxy = artifacts.require('TransparentUpgradeableProxy');
  7. contract('ProxyAdmin', function (accounts) {
  8. const [proxyAdminOwner, newAdmin, anotherAccount] = accounts;
  9. before('set implementations', async function () {
  10. this.implementationV1 = await ImplV1.new();
  11. this.implementationV2 = await ImplV2.new();
  12. });
  13. beforeEach(async function () {
  14. const initializeData = Buffer.from('');
  15. this.proxyAdmin = await ProxyAdmin.new({ from: proxyAdminOwner });
  16. this.proxy = await TransparentUpgradeableProxy.new(
  17. this.implementationV1.address,
  18. this.proxyAdmin.address,
  19. initializeData,
  20. { from: proxyAdminOwner },
  21. );
  22. });
  23. it('has an owner', async function () {
  24. expect(await this.proxyAdmin.owner()).to.equal(proxyAdminOwner);
  25. });
  26. describe('#getProxyAdmin', function () {
  27. it('returns proxyAdmin as admin of the proxy', async function () {
  28. const admin = await this.proxyAdmin.getProxyAdmin(this.proxy.address);
  29. expect(admin).to.be.equal(this.proxyAdmin.address);
  30. });
  31. });
  32. describe('#changeProxyAdmin', function () {
  33. it('fails to change proxy admin if its not the proxy owner', async function () {
  34. await expectRevert(
  35. this.proxyAdmin.changeProxyAdmin(this.proxy.address, newAdmin, { from: anotherAccount }),
  36. 'caller is not the owner',
  37. );
  38. });
  39. it('changes proxy admin', async function () {
  40. await this.proxyAdmin.changeProxyAdmin(this.proxy.address, newAdmin, { from: proxyAdminOwner });
  41. expect(await this.proxy.admin.call({ from: newAdmin })).to.eq(newAdmin);
  42. });
  43. });
  44. describe('#getProxyImplementation', function () {
  45. it('returns proxy implementation address', async function () {
  46. const implementationAddress = await this.proxyAdmin.getProxyImplementation(this.proxy.address);
  47. expect(implementationAddress).to.be.equal(this.implementationV1.address);
  48. });
  49. });
  50. describe('#upgrade', function () {
  51. context('with unauthorized account', function () {
  52. it('fails to upgrade', async function () {
  53. await expectRevert(
  54. this.proxyAdmin.upgrade(this.proxy.address, this.implementationV2.address, { from: anotherAccount }),
  55. 'caller is not the owner',
  56. );
  57. });
  58. });
  59. context('with authorized account', function () {
  60. it('upgrades implementation', async function () {
  61. await this.proxyAdmin.upgrade(this.proxy.address, this.implementationV2.address, { from: proxyAdminOwner });
  62. const implementationAddress = await this.proxyAdmin.getProxyImplementation(this.proxy.address);
  63. expect(implementationAddress).to.be.equal(this.implementationV2.address);
  64. });
  65. });
  66. });
  67. describe('#upgradeAndCall', function () {
  68. context('with unauthorized account', function () {
  69. it('fails to upgrade', async function () {
  70. const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI();
  71. await expectRevert(
  72. this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData,
  73. { from: anotherAccount },
  74. ),
  75. 'caller is not the owner',
  76. );
  77. });
  78. });
  79. context('with authorized account', function () {
  80. context('with invalid callData', function () {
  81. it('fails to upgrade', async function () {
  82. const callData = '0x12345678';
  83. await expectRevert.unspecified(
  84. this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData,
  85. { from: proxyAdminOwner },
  86. ),
  87. );
  88. });
  89. });
  90. context('with valid callData', function () {
  91. it('upgrades implementation', async function () {
  92. const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI();
  93. await this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData,
  94. { from: proxyAdminOwner },
  95. );
  96. const implementationAddress = await this.proxyAdmin.getProxyImplementation(this.proxy.address);
  97. expect(implementationAddress).to.be.equal(this.implementationV2.address);
  98. });
  99. });
  100. });
  101. });
  102. });