Secondary.test.js 2.3 KB

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