UpgradeableBeacon.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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(
  10. UpgradeableBeacon.new(accounts[0], owner),
  11. 'UpgradeableBeacon: implementation is not a contract',
  12. );
  13. });
  14. context('once deployed', async function () {
  15. beforeEach('deploying beacon', async function () {
  16. this.v1 = await Implementation1.new();
  17. this.beacon = await UpgradeableBeacon.new(this.v1.address, owner);
  18. });
  19. it('returns implementation', async function () {
  20. expect(await this.beacon.implementation()).to.equal(this.v1.address);
  21. });
  22. it('can be upgraded by the owner', async function () {
  23. const v2 = await Implementation2.new();
  24. const receipt = await this.beacon.upgradeTo(v2.address, { from: owner });
  25. expectEvent(receipt, 'Upgraded', { implementation: v2.address });
  26. expect(await this.beacon.implementation()).to.equal(v2.address);
  27. });
  28. it('cannot be upgraded to a non-contract', async function () {
  29. await expectRevert(
  30. this.beacon.upgradeTo(other, { from: owner }),
  31. 'UpgradeableBeacon: implementation is not a contract',
  32. );
  33. });
  34. it('cannot be upgraded by other account', async function () {
  35. const v2 = await Implementation2.new();
  36. await expectRevert(this.beacon.upgradeTo(v2.address, { from: other }), 'Ownable: caller is not the owner');
  37. });
  38. });
  39. });