Skip to content
This repository has been archived by the owner on Jan 31, 2021. It is now read-only.

Commit

Permalink
Update Ownable contracts
Browse files Browse the repository at this point in the history
Update ownable contract to allow for renouncement of ownership. Also
added testing files for the contract so that we can ensure they are
working as expected. Added assertThrow testing helpers for ownable
testing files.
  • Loading branch information
Chris Purta committed May 4, 2018
1 parent 1ea31fa commit 875c696
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
9 changes: 9 additions & 0 deletions contracts/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract Ownable {
address public owner;


event OwnershipRenounced(address indexed previousOwner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);


Expand Down Expand Up @@ -39,4 +40,12 @@ contract Ownable {
owner = newOwner;
}

/**
* @dev Allows the current owner to relinquish control of the contract.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipRenounced(owner);
owner = address(0);
}

}
21 changes: 21 additions & 0 deletions test/Bucket.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { assertRevert } = require("./helpers/assertThrow")
const Bucket = artifacts.require("Bucket")

contract("Bucket", accounts => {
Expand Down Expand Up @@ -61,5 +62,25 @@ contract("Bucket", accounts => {
let file = await bucket.totalFiles()
assert.equal(file.toNumber(), 1, "bucket should only contain 1 file")
})

context("non-owners", async () => {
it("non-owner cannot add file", async () => {
return assertRevert( async () => {
await bucket.addFile("foo.txt", "fakehash123abc", {from: accounts[1]})
})
})

it("non-owner cannot remove file", async () => {
return assertRevert( async () => {
await bucket.removeFile("foo.txt", {from: accounts[1]})
})
})

it("non-owner cannot set file hash", async () => {
return assertRevert( async () => {
await bucket.setIPFSHash("foo.txt", "321hashfake", {from: accounts[1]})
})
})
})
})
})
30 changes: 18 additions & 12 deletions test/ownable/Ownable.test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
const { assertRevert } = require('../helpers/assertThrow');

var Ownable = artifacts.require('Ownable');
const Ownable = artifacts.require('Ownable');
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

contract('Ownable', function (accounts) {
contract('Ownable', accounts => {
let ownable;

beforeEach(async function () {
beforeEach(async () => {
ownable = await Ownable.new();
});

it('should have an owner', async function () {
it('should have an owner', async () => {
let owner = await ownable.owner();
assert.isTrue(owner !== 0);
});

it('changes owner after transfer', async function () {
it('changes owner after transfer', async () => {
let other = accounts[1];
await ownable.transferOwnership(other);
let owner = await ownable.owner();

assert.isTrue(owner === other);
});

it('should prevent non-owners from transfering', async function () {
it('should prevent non-owners from transfering', async () => {
const other = accounts[2];
const owner = await ownable.owner.call();
assert.isTrue(owner !== other);
await assertRevert(ownable.transferOwnership(other, { from: other }));
return assertRevert( async () => {
await ownable.transferOwnership(other, { from: other })
});
});

it('should guard ownership against stuck state', async function () {
it('should guard ownership against stuck state', async () => {
let originalOwner = await ownable.owner();
await assertRevert(ownable.transferOwnership(null, { from: originalOwner }));
return assertRevert( async () => {
await ownable.transferOwnership(null, { from: originalOwner })
});
});

it('loses owner after renouncement', async function () {
it('loses owner after renouncement', async () => {
await ownable.renounceOwnership();
let owner = await ownable.owner();

assert.isTrue(owner === ZERO_ADDRESS);
});

it('should prevent non-owners from renouncement', async function () {
it('should prevent non-owners from renouncement', async () => {
const other = accounts[2];
const owner = await ownable.owner.call();
assert.isTrue(owner !== other);
await assertRevert(ownable.renounceOwnership({ from: other }));
return assertRevert( async () => {
await ownable.renounceOwnership({ from: other })
});
});
});

0 comments on commit 875c696

Please sign in to comment.