AuthorityUtils.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const [user, other] = await ethers.getSigners();
  6. const mock = await ethers.deployContract('$AuthorityUtils');
  7. const notAuthorityMock = await ethers.deployContract('NotAuthorityMock');
  8. const authorityNoDelayMock = await ethers.deployContract('AuthorityNoDelayMock');
  9. const authorityDelayMock = await ethers.deployContract('AuthorityDelayMock');
  10. const authorityNoResponse = await ethers.deployContract('AuthorityNoResponse');
  11. return {
  12. user,
  13. other,
  14. mock,
  15. notAuthorityMock,
  16. authorityNoDelayMock,
  17. authorityDelayMock,
  18. authorityNoResponse,
  19. };
  20. }
  21. describe('AuthorityUtils', function () {
  22. beforeEach(async function () {
  23. Object.assign(this, await loadFixture(fixture));
  24. });
  25. describe('canCallWithDelay', function () {
  26. describe('when authority does not have a canCall function', function () {
  27. beforeEach(async function () {
  28. this.authority = this.notAuthorityMock;
  29. });
  30. it('returns (immediate = 0, delay = 0)', async function () {
  31. const { immediate, delay } = await this.mock.$canCallWithDelay(
  32. this.authority,
  33. this.user,
  34. this.other,
  35. '0x12345678',
  36. );
  37. expect(immediate).to.be.false;
  38. expect(delay).to.equal(0n);
  39. });
  40. });
  41. describe('when authority has no delay', function () {
  42. beforeEach(async function () {
  43. this.authority = this.authorityNoDelayMock;
  44. this.immediate = true;
  45. await this.authority._setImmediate(this.immediate);
  46. });
  47. it('returns (immediate, delay = 0)', async function () {
  48. const { immediate, delay } = await this.mock.$canCallWithDelay(
  49. this.authority,
  50. this.user,
  51. this.other,
  52. '0x12345678',
  53. );
  54. expect(immediate).to.equal(this.immediate);
  55. expect(delay).to.equal(0n);
  56. });
  57. });
  58. describe('when authority replies with a delay', function () {
  59. beforeEach(async function () {
  60. this.authority = this.authorityDelayMock;
  61. });
  62. for (const immediate of [true, false]) {
  63. for (const delay of [0n, 42n]) {
  64. it(`returns (immediate=${immediate}, delay=${delay})`, async function () {
  65. await this.authority._setImmediate(immediate);
  66. await this.authority._setDelay(delay);
  67. const result = await this.mock.$canCallWithDelay(this.authority, this.user, this.other, '0x12345678');
  68. expect(result.immediate).to.equal(immediate);
  69. expect(result.delay).to.equal(delay);
  70. });
  71. }
  72. }
  73. });
  74. describe('when authority replies with empty data', function () {
  75. beforeEach(async function () {
  76. this.authority = this.authorityNoResponse;
  77. });
  78. it('returns (immediate = 0, delay = 0)', async function () {
  79. const { immediate, delay } = await this.mock.$canCallWithDelay(
  80. this.authority,
  81. this.user,
  82. this.other,
  83. '0x12345678',
  84. );
  85. expect(immediate).to.be.false;
  86. expect(delay).to.equal(0n);
  87. });
  88. });
  89. });
  90. });