Added unit tests for Ownable

This commit is contained in:
Alex Towle 2019-07-26 13:14:39 -07:00
parent 4a4d2e7079
commit f4f922acb5
7 changed files with 84 additions and 1 deletions

View File

@ -36,6 +36,7 @@
"test/TestConstants.sol",
"test/TestLibAddressArray.sol",
"test/TestLibBytes.sol",
"test/TestOwnable.sol",
"test/TestSafeMath.sol"
]
}

View File

@ -0,0 +1,13 @@
pragma solidity ^0.5.9;
import "../src/Ownable.sol";
contract TestOwnable is
Ownable
{
function externalOnlyOwner()
external
onlyOwner
{} // solhint-disable-line no-empty-blocks
}

View File

@ -34,7 +34,7 @@
"lint-contracts": "solhint -c ../.solhint.json contracts/**/**/**/**/*.sol"
},
"config": {
"abis": "./generated-artifacts/@(IOwnable|LibAddress|LibBytes|LibEIP1271|LibEIP712|Ownable|ReentrancyGuard|RichErrors|SafeMath|TestConstants|TestLibAddressArray|TestLibBytes|TestSafeMath).json",
"abis": "./generated-artifacts/@(IOwnable|LibAddress|LibBytes|LibEIP1271|LibEIP712|Ownable|ReentrancyGuard|RichErrors|SafeMath|TestConstants|TestLibAddressArray|TestLibBytes|TestOwnable|TestSafeMath).json",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually."
},
"repository": {

View File

@ -17,6 +17,7 @@ import * as SafeMath from '../generated-artifacts/SafeMath.json';
import * as TestConstants from '../generated-artifacts/TestConstants.json';
import * as TestLibAddressArray from '../generated-artifacts/TestLibAddressArray.json';
import * as TestLibBytes from '../generated-artifacts/TestLibBytes.json';
import * as TestOwnable from '../generated-artifacts/TestOwnable.json';
import * as TestSafeMath from '../generated-artifacts/TestSafeMath.json';
export const artifacts = {
LibAddress: LibAddress as ContractArtifact,
@ -31,5 +32,6 @@ export const artifacts = {
TestConstants: TestConstants as ContractArtifact,
TestLibAddressArray: TestLibAddressArray as ContractArtifact,
TestLibBytes: TestLibBytes as ContractArtifact,
TestOwnable: TestOwnable as ContractArtifact,
TestSafeMath: TestSafeMath as ContractArtifact,
};

View File

@ -15,4 +15,5 @@ export * from '../generated-wrappers/safe_math';
export * from '../generated-wrappers/test_constants';
export * from '../generated-wrappers/test_lib_address_array';
export * from '../generated-wrappers/test_lib_bytes';
export * from '../generated-wrappers/test_ownable';
export * from '../generated-wrappers/test_safe_math';

View File

@ -0,0 +1,65 @@
import { chaiSetup, constants, provider, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { OwnableRevertErrors } from '@0x/utils';
import * as chai from 'chai';
import * as _ from 'lodash';
import { artifacts, TestOwnableContract } from '../src';
chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
describe('Ownable', () => {
let ownable: TestOwnableContract;
let owner: string;
let nonOwner: string;
before(async () => {
const accounts = await web3Wrapper.getAvailableAddressesAsync();
owner = await accounts[0];
nonOwner = await accounts[1];
await blockchainLifecycle.startAsync();
// Deploy SafeMath from the owner address
txDefaults.from = owner;
ownable = await TestOwnableContract.deployFrom0xArtifactAsync(
artifacts.TestOwnable,
provider,
txDefaults,
);
});
after(async () => {
await blockchainLifecycle.revertAsync();
});
// tslint:disable:no-unused-expression
describe('onlyOwner', () => {
it('should throw if sender is not owner', async () => {
const expectedError = new OwnableRevertErrors.OnlyOwnerError(
nonOwner,
owner,
);
return expect(ownable.externalOnlyOwner.callAsync({ from: nonOwner })).to.revertWith(expectedError);
});
it('should succeed if sender is owner', async () => {
expect(ownable.externalOnlyOwner.callAsync({ from: owner })).to.be.fulfilled;
});
});
describe('transferOwnership', () => {
it('should not transfer ownership if the specified new owner is the zero address', async () => {
expect(ownable.transferOwnership.sendTransactionAsync(constants.NULL_ADDRESS, { from: owner })).to.be.fulfilled;
const updatedOwner = await ownable.owner.callAsync();
expect(updatedOwner).to.be.eq(owner);
});
it('should transfer ownership if the specified new owner is not the zero address', async () => {
expect(ownable.transferOwnership.sendTransactionAsync(nonOwner, { from: owner })).to.be.fulfilled;
const updatedOwner = await ownable.owner.callAsync();
expect(updatedOwner).to.be.eq(nonOwner);
});
});
// tslint:enable:no-unused-expression
});

View File

@ -15,6 +15,7 @@
"generated-artifacts/TestConstants.json",
"generated-artifacts/TestLibAddressArray.json",
"generated-artifacts/TestLibBytes.json",
"generated-artifacts/TestOwnable.json",
"generated-artifacts/TestSafeMath.json"
],
"exclude": ["./deploy/solc/solc_bin"]