Added unit tests for onlyAuthorized

This commit is contained in:
Alex Towle 2019-11-25 14:02:53 -06:00
parent d5e6b38450
commit 1fd92b6cbd
6 changed files with 80 additions and 41 deletions

View File

@ -0,0 +1,33 @@
/*
Copyright 2019 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.5.9;
import "../src/Authorizable.sol";
// solhint-disable no-empty-blocks
contract TestAuthorizable is
Authorizable
{
function onlyAuthorizedFn()
external
view
onlyAuthorized
{}
}

View File

@ -38,7 +38,7 @@
"config": {
"publicInterfaceContracts": "Authorizable,IAuthorizable,IOwnable,LibAddress,LibAddressArray,LibAddressArrayRichErrors,LibAuthorizableRichErrors,LibBytes,LibBytesRichErrors,LibEIP1271,LibEIP712,LibFractions,LibOwnableRichErrors,LibReentrancyGuardRichErrors,LibRichErrors,LibSafeMath,LibSafeMathRichErrors,Ownable,ReentrancyGuard,Refundable",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./test/generated-artifacts/@(Authorizable|DeploymentConstants|IAuthorizable|IOwnable|LibAddress|LibAddressArray|LibAddressArrayRichErrors|LibAuthorizableRichErrors|LibBytes|LibBytesRichErrors|LibEIP1271|LibEIP712|LibFractions|LibOwnableRichErrors|LibReentrancyGuardRichErrors|LibRichErrors|LibSafeMath|LibSafeMathRichErrors|Ownable|ReentrancyGuard|Refundable|TestLibAddress|TestLibAddressArray|TestLibBytes|TestLibEIP712|TestLibRichErrors|TestLibSafeMath|TestLogDecoding|TestLogDecodingDownstream|TestOwnable|TestReentrancyGuard|TestRefundable|TestRefundableReceiver).json"
"abis": "./test/generated-artifacts/@(Authorizable|DeploymentConstants|IAuthorizable|IOwnable|LibAddress|LibAddressArray|LibAddressArrayRichErrors|LibAuthorizableRichErrors|LibBytes|LibBytesRichErrors|LibEIP1271|LibEIP712|LibFractions|LibOwnableRichErrors|LibReentrancyGuardRichErrors|LibRichErrors|LibSafeMath|LibSafeMathRichErrors|Ownable|ReentrancyGuard|Refundable|TestAuthorizable|TestLibAddress|TestLibAddressArray|TestLibBytes|TestLibEIP712|TestLibRichErrors|TestLibSafeMath|TestLogDecoding|TestLogDecodingDownstream|TestOwnable|TestReentrancyGuard|TestRefundable|TestRefundableReceiver).json"
},
"repository": {
"type": "git",

View File

@ -26,6 +26,7 @@ import * as LibSafeMathRichErrors from '../test/generated-artifacts/LibSafeMathR
import * as Ownable from '../test/generated-artifacts/Ownable.json';
import * as ReentrancyGuard from '../test/generated-artifacts/ReentrancyGuard.json';
import * as Refundable from '../test/generated-artifacts/Refundable.json';
import * as TestAuthorizable from '../test/generated-artifacts/TestAuthorizable.json';
import * as TestLibAddress from '../test/generated-artifacts/TestLibAddress.json';
import * as TestLibAddressArray from '../test/generated-artifacts/TestLibAddressArray.json';
import * as TestLibBytes from '../test/generated-artifacts/TestLibBytes.json';
@ -60,6 +61,7 @@ export const artifacts = {
Refundable: Refundable as ContractArtifact,
IAuthorizable: IAuthorizable as ContractArtifact,
IOwnable: IOwnable as ContractArtifact,
TestAuthorizable: TestAuthorizable as ContractArtifact,
TestLibAddress: TestLibAddress as ContractArtifact,
TestLibAddressArray: TestLibAddressArray as ContractArtifact,
TestLibBytes: TestLibBytes as ContractArtifact,

View File

@ -1,52 +1,30 @@
import { chaiSetup, constants, provider, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { blockchainTests, constants, expect } from '@0x/contracts-test-utils';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import * as _ from 'lodash';
import AuthorizableRevertErrors = require('../src/authorizable_revert_errors');
import OwnableRevertErrors = require('../src/ownable_revert_errors');
import { artifacts } from './artifacts';
import { AuthorizableContract } from './wrappers';
import { TestAuthorizableContract } from './wrappers';
chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
describe('Authorizable', () => {
blockchainTests.resets('Authorizable', env => {
let owner: string;
let notOwner: string;
let address: string;
let authorizable: AuthorizableContract;
let authorizable: TestAuthorizableContract;
before(async () => {
await blockchainLifecycle.startAsync();
});
after(async () => {
await blockchainLifecycle.revertAsync();
});
before(async () => {
const accounts = await web3Wrapper.getAvailableAddressesAsync();
const accounts = await env.getAccountAddressesAsync();
[owner, address, notOwner] = _.slice(accounts, 0, 3);
authorizable = await AuthorizableContract.deployFrom0xArtifactAsync(
artifacts.Authorizable,
provider,
txDefaults,
{},
authorizable = await TestAuthorizableContract.deployFrom0xArtifactAsync(
artifacts.TestAuthorizable,
env.provider,
env.txDefaults,
artifacts,
);
});
beforeEach(async () => {
await blockchainLifecycle.startAsync();
});
afterEach(async () => {
await blockchainLifecycle.revertAsync();
});
describe('addAuthorizedAddress', () => {
it('should revert if not called by owner', async () => {
const expectedError = new OwnableRevertErrors.OnlyOwnerError(notOwner, owner);
@ -74,6 +52,26 @@ describe('Authorizable', () => {
});
});
describe('onlyAuthorized', () => {
before(async () => {
await authorizable.addAuthorizedAddress(owner).awaitTransactionSuccessAsync({ from: owner });
});
after(async () => {
await authorizable.removeAuthorizedAddress(owner).awaitTransactionSuccessAsync({ from: owner });
});
it('should revert if sender is not authorized', async () => {
const tx = authorizable.onlyAuthorizedFn().callAsync({ from: notOwner });
const expectedError = new AuthorizableRevertErrors.SenderNotAuthorizedError(notOwner);
return expect(tx).to.revertWith(expectedError);
});
it('should succeed if sender is authorized', async () => {
await authorizable.onlyAuthorizedFn().callAsync({ from: owner });
});
});
describe('removeAuthorizedAddress', () => {
it('should revert if not called by owner', async () => {
await authorizable.addAuthorizedAddress(address).awaitTransactionSuccessAsync({ from: owner });
@ -151,16 +149,20 @@ describe('Authorizable', () => {
});
describe('getAuthorizedAddresses', () => {
it('should return all authorized addresses', async () => {
const initial = await authorizable.getAuthorizedAddresses().callAsync();
expect(initial).to.have.length(0);
it('should return correct authorized addresses', async () => {
// Initial Authorities
let authorities = await authorizable.getAuthorizedAddresses().callAsync();
expect(authorities).to.be.deep.eq([]);
// Authorities after addition
await authorizable.addAuthorizedAddress(address).awaitTransactionSuccessAsync({ from: owner });
const afterAdd = await authorizable.getAuthorizedAddresses().callAsync();
expect(afterAdd).to.have.length(1);
expect(afterAdd).to.include(address);
authorities = await authorizable.getAuthorizedAddresses().callAsync();
expect(authorities).to.be.deep.eq([address]);
// Authorities after removal
await authorizable.removeAuthorizedAddress(address).awaitTransactionSuccessAsync({ from: owner });
const afterRemove = await authorizable.getAuthorizedAddresses().callAsync();
expect(afterRemove).to.have.length(0);
authorities = await authorizable.getAuthorizedAddresses().callAsync();
expect(authorities).to.be.deep.eq([]);
});
});
});

View File

@ -24,6 +24,7 @@ export * from '../test/generated-wrappers/lib_safe_math_rich_errors';
export * from '../test/generated-wrappers/ownable';
export * from '../test/generated-wrappers/reentrancy_guard';
export * from '../test/generated-wrappers/refundable';
export * from '../test/generated-wrappers/test_authorizable';
export * from '../test/generated-wrappers/test_lib_address';
export * from '../test/generated-wrappers/test_lib_address_array';
export * from '../test/generated-wrappers/test_lib_bytes';

View File

@ -44,6 +44,7 @@
"test/generated-artifacts/Ownable.json",
"test/generated-artifacts/ReentrancyGuard.json",
"test/generated-artifacts/Refundable.json",
"test/generated-artifacts/TestAuthorizable.json",
"test/generated-artifacts/TestLibAddress.json",
"test/generated-artifacts/TestLibAddressArray.json",
"test/generated-artifacts/TestLibBytes.json",