Files
openzeppelin-contracts/test/ownership/Whitelist.test.js
Matt Condon 4223c9d50e feat: refactor whitelist.sol to use RBAC (#893)
* feat: refactor whitelist.sol to use RBAC

* fix: remove poor backwards compat attempt
2018-05-08 17:20:28 -07:00

94 lines
2.9 KiB
JavaScript

import expectThrow from '../helpers/expectThrow';
import expectEvent from '../helpers/expectEvent';
const WhitelistMock = artifacts.require('WhitelistMock');
require('chai')
.use(require('chai-as-promised'))
.should();
contract('Whitelist', function (accounts) {
let mock;
const [
owner,
whitelistedAddress1,
whitelistedAddress2,
anyone,
] = accounts;
const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2];
before(async function () {
mock = await WhitelistMock.new();
});
context('in normal conditions', () => {
it('should add address to the whitelist', async function () {
await expectEvent.inTransaction(
mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }),
'WhitelistedAddressAdded'
);
const isWhitelisted = await mock.whitelist(whitelistedAddress1);
isWhitelisted.should.be.equal(true);
});
it('should add addresses to the whitelist', async function () {
await expectEvent.inTransaction(
mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }),
'WhitelistedAddressAdded'
);
for (let addr of whitelistedAddresses) {
const isWhitelisted = await mock.whitelist(addr);
isWhitelisted.should.be.equal(true);
}
});
it('should remove address from the whitelist', async function () {
await expectEvent.inTransaction(
mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
'WhitelistedAddressRemoved'
);
let isWhitelisted = await mock.whitelist(whitelistedAddress1);
isWhitelisted.should.be.equal(false);
});
it('should remove addresses from the the whitelist', async function () {
await expectEvent.inTransaction(
mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }),
'WhitelistedAddressRemoved'
);
for (let addr of whitelistedAddresses) {
const isWhitelisted = await mock.whitelist(addr);
isWhitelisted.should.be.equal(false);
}
});
it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async () => {
await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
await mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 })
.should.be.fulfilled;
});
});
context('in adversarial conditions', () => {
it('should not allow "anyone" to add to the whitelist', async () => {
await expectThrow(
mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
);
});
it('should not allow "anyone" to remove from the whitelist', async () => {
await expectThrow(
mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
);
});
it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async () => {
await expectThrow(
mock.onlyWhitelistedCanDoThis({ from: anyone })
);
});
});
});