Secondary.test.js 2.3 KB

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