UpgradeableBeacon.test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { expectRevert, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const UpgradeableBeacon = artifacts.require('UpgradeableBeacon');
  4. const Implementation1 = artifacts.require('Implementation1');
  5. const Implementation2 = artifacts.require('Implementation2');
  6. contract('UpgradeableBeacon', function (accounts) {
  7. const [owner, other] = accounts;
  8. it('cannot be created with non-contract implementation', async function () {
  9. await expectRevert(UpgradeableBeacon.new(accounts[0]), 'UpgradeableBeacon: implementation is not a contract');
  10. });
  11. context('once deployed', async function () {
  12. beforeEach('deploying beacon', async function () {
  13. this.v1 = await Implementation1.new();
  14. this.beacon = await UpgradeableBeacon.new(this.v1.address, { from: owner });
  15. });
  16. it('returns implementation', async function () {
  17. expect(await this.beacon.implementation()).to.equal(this.v1.address);
  18. });
  19. it('can be upgraded by the owner', async function () {
  20. const v2 = await Implementation2.new();
  21. const receipt = await this.beacon.upgradeTo(v2.address, { from: owner });
  22. expectEvent(receipt, 'Upgraded', { implementation: v2.address });
  23. expect(await this.beacon.implementation()).to.equal(v2.address);
  24. });
  25. it('cannot be upgraded to a non-contract', async function () {
  26. await expectRevert(
  27. this.beacon.upgradeTo(other, { from: owner }),
  28. 'UpgradeableBeacon: implementation is not a contract',
  29. );
  30. });
  31. it('cannot be upgraded by other account', async function () {
  32. const v2 = await Implementation2.new();
  33. await expectRevert(this.beacon.upgradeTo(v2.address, { from: other }), 'Ownable: caller is not the owner');
  34. });
  35. });
  36. });