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

@@ -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([]);
});
});
});