UpgradeableBeacon.test.js 1.8 KB

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