Ownable behavior (#929)
* added function to renounce ownership * Provide an Ownable behavior for testing (#905) * Fix indentation * Convert to use should assertions
This commit is contained in:
committed by
Matt Condon
parent
4223c9d50e
commit
20b85be6aa
50
test/ownership/Ownable.behaviour.js
Normal file
50
test/ownership/Ownable.behaviour.js
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import EVMRevert from '../helpers/EVMRevert';
|
||||||
|
|
||||||
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
||||||
|
|
||||||
|
require('chai')
|
||||||
|
.use(require('chai-as-promised'))
|
||||||
|
.should();
|
||||||
|
|
||||||
|
export default function (accounts) {
|
||||||
|
describe('as an ownable', function () {
|
||||||
|
it('should have an owner', async function () {
|
||||||
|
let owner = await this.ownable.owner();
|
||||||
|
owner.should.not.eq(ZERO_ADDRESS);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('changes owner after transfer', async function () {
|
||||||
|
let other = accounts[1];
|
||||||
|
await this.ownable.transferOwnership(other);
|
||||||
|
let owner = await this.ownable.owner();
|
||||||
|
|
||||||
|
owner.should.eq(other);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should prevent non-owners from transfering', async function () {
|
||||||
|
const other = accounts[2];
|
||||||
|
const owner = await this.ownable.owner.call();
|
||||||
|
owner.should.not.eq(other);
|
||||||
|
await this.ownable.transferOwnership(other, { from: other }).should.be.rejectedWith(EVMRevert);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should guard ownership against stuck state', async function () {
|
||||||
|
let originalOwner = await this.ownable.owner();
|
||||||
|
await this.ownable.transferOwnership(null, { from: originalOwner }).should.be.rejectedWith(EVMRevert);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loses owner after renouncement', async function () {
|
||||||
|
await this.ownable.renounceOwnership();
|
||||||
|
let owner = await this.ownable.owner();
|
||||||
|
|
||||||
|
owner.should.eq(ZERO_ADDRESS);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should prevent non-owners from renouncement', async function () {
|
||||||
|
const other = accounts[2];
|
||||||
|
const owner = await this.ownable.owner.call();
|
||||||
|
owner.should.not.eq(other);
|
||||||
|
await this.ownable.renounceOwnership({ from: other }).should.be.rejectedWith(EVMRevert);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -1,52 +1,11 @@
|
|||||||
|
import shouldBehaveLikeOwnable from './Ownable.behaviour';
|
||||||
|
|
||||||
import assertRevert from '../helpers/assertRevert';
|
const Ownable = artifacts.require('Ownable');
|
||||||
|
|
||||||
var Ownable = artifacts.require('Ownable');
|
|
||||||
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
||||||
|
|
||||||
contract('Ownable', function (accounts) {
|
contract('Ownable', function (accounts) {
|
||||||
let ownable;
|
|
||||||
|
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
ownable = await Ownable.new();
|
this.ownable = await Ownable.new();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have an owner', async function () {
|
shouldBehaveLikeOwnable(accounts);
|
||||||
let owner = await ownable.owner();
|
|
||||||
assert.isTrue(owner !== 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('changes owner after transfer', async function () {
|
|
||||||
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 () {
|
|
||||||
const other = accounts[2];
|
|
||||||
const owner = await ownable.owner.call();
|
|
||||||
assert.isTrue(owner !== other);
|
|
||||||
await assertRevert(ownable.transferOwnership(other, { from: other }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should guard ownership against stuck state', async function () {
|
|
||||||
let originalOwner = await ownable.owner();
|
|
||||||
await assertRevert(ownable.transferOwnership(null, { from: originalOwner }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('loses owner after renouncement', async function () {
|
|
||||||
await ownable.renounceOwnership();
|
|
||||||
let owner = await ownable.owner();
|
|
||||||
|
|
||||||
assert.isTrue(owner === ZERO_ADDRESS);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should prevent non-owners from renouncement', async function () {
|
|
||||||
const other = accounts[2];
|
|
||||||
const owner = await ownable.owner.call();
|
|
||||||
assert.isTrue(owner !== other);
|
|
||||||
await assertRevert(ownable.renounceOwnership({ from: other }));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user