UpgradeableBeacon.test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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('emits Upgraded event to the first implementation', async function () {
  18. const beacon = await UpgradeableBeacon.new(this.v1.address, owner);
  19. await expectEvent.inTransaction(beacon.contract.transactionHash, beacon, 'Upgraded', {
  20. implementation: this.v1.address,
  21. });
  22. });
  23. it('returns implementation', async function () {
  24. expect(await this.beacon.implementation()).to.equal(this.v1.address);
  25. });
  26. it('can be upgraded by the owner', async function () {
  27. const v2 = await Implementation2.new();
  28. const receipt = await this.beacon.upgradeTo(v2.address, { from: owner });
  29. expectEvent(receipt, 'Upgraded', { implementation: v2.address });
  30. expect(await this.beacon.implementation()).to.equal(v2.address);
  31. });
  32. it('cannot be upgraded to a non-contract', async function () {
  33. await expectRevertCustomError(this.beacon.upgradeTo(other, { from: owner }), 'BeaconInvalidImplementation', [
  34. other,
  35. ]);
  36. });
  37. it('cannot be upgraded by other account', async function () {
  38. const v2 = await Implementation2.new();
  39. await expectRevertCustomError(this.beacon.upgradeTo(v2.address, { from: other }), 'OwnableUnauthorizedAccount', [
  40. other,
  41. ]);
  42. });
  43. });
  44. });