SignatureBouncer.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import assertRevert from '../helpers/assertRevert';
  2. import { signHex } from '../helpers/sign';
  3. const Bouncer = artifacts.require('SignatureBouncerMock');
  4. require('chai')
  5. .use(require('chai-as-promised'))
  6. .should();
  7. export const getSigner = (contract, signer, data = '') => (addr) => {
  8. // via: https://github.com/OpenZeppelin/zeppelin-solidity/pull/812/files
  9. const message = contract.address.substr(2) + addr.substr(2) + data;
  10. // ^ substr to remove `0x` because in solidity the address is a set of byes, not a string `0xabcd`
  11. return signHex(signer, message);
  12. };
  13. contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBouncer]) => {
  14. before(async function () {
  15. this.bouncer = await Bouncer.new({ from: owner });
  16. this.roleBouncer = await this.bouncer.ROLE_BOUNCER();
  17. this.genSig = getSigner(this.bouncer, bouncerAddress);
  18. });
  19. it('should have a default owner of self', async function () {
  20. const theOwner = await this.bouncer.owner();
  21. theOwner.should.eq(owner);
  22. });
  23. it('should allow owner to add a bouncer', async function () {
  24. await this.bouncer.addBouncer(bouncerAddress, { from: owner });
  25. const hasRole = await this.bouncer.hasRole(bouncerAddress, this.roleBouncer);
  26. hasRole.should.eq(true);
  27. });
  28. it('should not allow anyone to add a bouncer', async function () {
  29. await assertRevert(
  30. this.bouncer.addBouncer(bouncerAddress, { from: anyone })
  31. );
  32. });
  33. context('modifiers', () => {
  34. it('should allow valid signature for sender', async function () {
  35. await this.bouncer.onlyWithValidSignature(
  36. this.genSig(authorizedUser),
  37. { from: authorizedUser }
  38. );
  39. });
  40. it('should not allow invalid signature for sender', async function () {
  41. await assertRevert(
  42. this.bouncer.onlyWithValidSignature(
  43. 'abcd',
  44. { from: authorizedUser }
  45. )
  46. );
  47. });
  48. });
  49. context('signatures', () => {
  50. it('should accept valid message for valid user', async function () {
  51. const isValid = await this.bouncer.checkValidSignature(
  52. authorizedUser,
  53. this.genSig(authorizedUser)
  54. );
  55. isValid.should.eq(true);
  56. });
  57. it('should not accept invalid message for valid user', async function () {
  58. const isValid = await this.bouncer.checkValidSignature(
  59. authorizedUser,
  60. this.genSig(anyone)
  61. );
  62. isValid.should.eq(false);
  63. });
  64. it('should not accept invalid message for invalid user', async function () {
  65. const isValid = await this.bouncer.checkValidSignature(
  66. anyone,
  67. 'abcd'
  68. );
  69. isValid.should.eq(false);
  70. });
  71. it('should not accept valid message for invalid user', async function () {
  72. const isValid = await this.bouncer.checkValidSignature(
  73. anyone,
  74. this.genSig(authorizedUser)
  75. );
  76. isValid.should.eq(false);
  77. });
  78. });
  79. context('management', () => {
  80. it('should not allow anyone to add bouncers', async function () {
  81. await assertRevert(
  82. this.bouncer.addBouncer(newBouncer, { from: anyone })
  83. );
  84. });
  85. it('should be able to add bouncers', async function () {
  86. await this.bouncer.addBouncer(newBouncer, { from: owner })
  87. .should.be.fulfilled;
  88. });
  89. it('should not allow adding invalid address', async function () {
  90. await assertRevert(
  91. this.bouncer.addBouncer('0x0', { from: owner })
  92. );
  93. });
  94. it('should not allow anyone to remove bouncer', async function () {
  95. await assertRevert(
  96. this.bouncer.removeBouncer(newBouncer, { from: anyone })
  97. );
  98. });
  99. it('should be able to remove bouncers', async function () {
  100. await this.bouncer.removeBouncer(newBouncer, { from: owner })
  101. .should.be.fulfilled;
  102. });
  103. });
  104. });