Heritable.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import increaseTime from './helpers/increaseTime';
  2. import expectThrow from './helpers/expectThrow';
  3. const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
  4. const Heritable = artifacts.require('Heritable');
  5. contract('Heritable', function (accounts) {
  6. let heritable;
  7. let owner;
  8. beforeEach(async function () {
  9. heritable = await Heritable.new(4141);
  10. owner = await heritable.owner();
  11. });
  12. it('should start off with an owner, but without heir', async function () {
  13. const heir = await heritable.heir();
  14. assert.equal(typeof (owner), 'string');
  15. assert.equal(typeof (heir), 'string');
  16. assert.notStrictEqual(
  17. owner, NULL_ADDRESS,
  18. 'Owner shouldn\'t be the null address'
  19. );
  20. assert.isTrue(
  21. heir === NULL_ADDRESS,
  22. 'Heir should be the null address'
  23. );
  24. });
  25. it('only owner should set heir', async function () {
  26. const newHeir = accounts[1];
  27. const someRandomAddress = accounts[2];
  28. assert.isTrue(owner !== someRandomAddress);
  29. await heritable.setHeir(newHeir, { from: owner });
  30. await expectThrow(heritable.setHeir(newHeir, { from: someRandomAddress }));
  31. });
  32. it('owner can remove heir', async function () {
  33. const newHeir = accounts[1];
  34. await heritable.setHeir(newHeir, { from: owner });
  35. let heir = await heritable.heir();
  36. assert.notStrictEqual(heir, NULL_ADDRESS);
  37. await heritable.removeHeir();
  38. heir = await heritable.heir();
  39. assert.isTrue(heir === NULL_ADDRESS);
  40. });
  41. it('heir can claim ownership only if owner is dead and timeout was reached', async function () {
  42. const heir = accounts[1];
  43. await heritable.setHeir(heir, { from: owner });
  44. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  45. await heritable.proclaimDeath({ from: heir });
  46. await increaseTime(1);
  47. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  48. await increaseTime(4141);
  49. await heritable.claimHeirOwnership({ from: heir });
  50. assert.isTrue(await heritable.heir() === heir);
  51. });
  52. it('heir can\'t claim ownership if owner heartbeats', async function () {
  53. const heir = accounts[1];
  54. await heritable.setHeir(heir, { from: owner });
  55. await heritable.proclaimDeath({ from: heir });
  56. await heritable.heartbeat({ from: owner });
  57. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  58. await heritable.proclaimDeath({ from: heir });
  59. await increaseTime(4141);
  60. await heritable.heartbeat({ from: owner });
  61. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  62. });
  63. it('should log events appropriately', async function () {
  64. const heir = accounts[1];
  65. const setHeirLogs = (await heritable.setHeir(heir, { from: owner })).logs;
  66. const setHeirEvent = setHeirLogs.find(e => e.event === 'HeirChanged');
  67. assert.isTrue(setHeirEvent.args.owner === owner);
  68. assert.isTrue(setHeirEvent.args.newHeir === heir);
  69. const heartbeatLogs = (await heritable.heartbeat({ from: owner })).logs;
  70. const heartbeatEvent = heartbeatLogs.find(e => e.event === 'OwnerHeartbeated');
  71. assert.isTrue(heartbeatEvent.args.owner === owner);
  72. const proclaimDeathLogs = (await heritable.proclaimDeath({ from: heir })).logs;
  73. const ownerDeadEvent = proclaimDeathLogs.find(e => e.event === 'OwnerProclaimedDead');
  74. assert.isTrue(ownerDeadEvent.args.owner === owner);
  75. assert.isTrue(ownerDeadEvent.args.heir === heir);
  76. await increaseTime(4141);
  77. const claimHeirOwnershipLogs = (await heritable.claimHeirOwnership({ from: heir })).logs;
  78. const ownershipTransferredEvent = claimHeirOwnershipLogs.find(e => e.event === 'OwnershipTransferred');
  79. const heirOwnershipClaimedEvent = claimHeirOwnershipLogs.find(e => e.event === 'HeirOwnershipClaimed');
  80. assert.isTrue(ownershipTransferredEvent.args.previousOwner === owner);
  81. assert.isTrue(ownershipTransferredEvent.args.newOwner === heir);
  82. assert.isTrue(heirOwnershipClaimedEvent.args.previousOwner === owner);
  83. assert.isTrue(heirOwnershipClaimedEvent.args.newOwner === heir);
  84. });
  85. });