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