Secondary.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { ZERO_ADDRESS } = constants;
  4. const { expect } = require('chai');
  5. const SecondaryMock = contract.fromArtifact('SecondaryMock');
  6. describe('Secondary', function () {
  7. const [ primary, newPrimary, other ] = accounts;
  8. beforeEach(async function () {
  9. this.secondary = await SecondaryMock.new({ from: primary });
  10. });
  11. it('stores the primary\'s address', async function () {
  12. expect(await this.secondary.primary()).to.equal(primary);
  13. });
  14. describe('onlyPrimary', function () {
  15. it('allows the primary account to call onlyPrimary functions', async function () {
  16. await this.secondary.onlyPrimaryMock({ from: primary });
  17. });
  18. it('reverts when anyone calls onlyPrimary functions', async function () {
  19. await expectRevert(this.secondary.onlyPrimaryMock({ from: other }),
  20. 'Secondary: caller is not the primary account'
  21. );
  22. });
  23. });
  24. describe('transferPrimary', function () {
  25. it('makes the recipient the new primary', async function () {
  26. const { logs } = await this.secondary.transferPrimary(newPrimary, { from: primary });
  27. expectEvent.inLogs(logs, 'PrimaryTransferred', { recipient: newPrimary });
  28. expect(await this.secondary.primary()).to.equal(newPrimary);
  29. });
  30. it('reverts when transferring to the null address', async function () {
  31. await expectRevert(this.secondary.transferPrimary(ZERO_ADDRESS, { from: primary }),
  32. 'Secondary: new primary is the zero address'
  33. );
  34. });
  35. it('reverts when called by anyone', async function () {
  36. await expectRevert(this.secondary.transferPrimary(newPrimary, { from: other }),
  37. 'Secondary: caller is not the primary account'
  38. );
  39. });
  40. context('with new primary', function () {
  41. beforeEach(async function () {
  42. await this.secondary.transferPrimary(newPrimary, { from: primary });
  43. });
  44. it('allows the new primary account to call onlyPrimary functions', async function () {
  45. await this.secondary.onlyPrimaryMock({ from: newPrimary });
  46. });
  47. it('reverts when the old primary account calls onlyPrimary functions', async function () {
  48. await expectRevert(this.secondary.onlyPrimaryMock({ from: primary }),
  49. 'Secondary: caller is not the primary account'
  50. );
  51. });
  52. });
  53. });
  54. });