Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit b079bf1

Browse files
committed
Async/await in access control tests
1 parent bbf52be commit b079bf1

File tree

1 file changed

+90
-89
lines changed

1 file changed

+90
-89
lines changed

test/sale.js

Lines changed: 90 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ contract(`Sale`, (accounts) => {
1919

2020
let tokensForSale;
2121

22-
const wrongTokenBalance = `has an incorrect token balance.`
23-
const badInitialization = `was not initialized properly`
2422
/*
2523
* Utility Functions
2624
*/
@@ -94,6 +92,26 @@ contract(`Sale`, (accounts) => {
9492
});
9593
}
9694

95+
function detectSendObject(...args) {
96+
if(typeof(args[args.length - 1]) === Object) {
97+
return args[args.length - 1]
98+
} else {
99+
return {}
100+
}
101+
}
102+
103+
function asOwner(fn, ...args) {
104+
let sendObject = detectSendObject(args)
105+
sendObject.from = owner
106+
return fn(...args, sendObject)
107+
}
108+
109+
function notAsOwner(fn, ...args) {
110+
let sendObject = detectSendObject(args)
111+
sendObject.from = james
112+
return fn(...args, sendObject)
113+
}
114+
97115
before(() => {
98116
const tokensPreAllocated = totalPreSoldTokens().add(totalFoundersTokens());
99117
saleConf.price = new BN(saleConf.price, 10);
@@ -103,6 +121,7 @@ contract(`Sale`, (accounts) => {
103121
});
104122

105123
describe(`Initial token issuance`, () => {
124+
const wrongTokenBalance = `has an incorrect token balance.`
106125
it(`should instantiate preBuyers with the proper number of tokens`, () => {
107126
Promise.all(
108127
Object.keys(preBuyersConf).map(async (curr, i, arr) => {
@@ -143,6 +162,7 @@ contract(`Sale`, (accounts) => {
143162
});
144163
});
145164
describe(`Instantiation`, () => {
165+
const badInitialization = `was not initialized properly`
146166
it(`should instantiate with the price set to ${saleConf.price} Wei.`, async () => {
147167
const sale = await Sale.deployed()
148168
const price = await sale.price.call()
@@ -176,93 +196,74 @@ contract(`Sale`, (accounts) => {
176196
});
177197

178198
describe(`Owner-only functions`, () => {
179-
it(`should not allow James to change the price.`, () =>
180-
new Promise((resolve, reject) =>
181-
Sale.deployed()
182-
.then((sale) => sale.changePrice(saleConf.price + 1, {from: james}))
183-
.then(() => {
184-
reject(`A non-owner was able to change the sale price`);
185-
})
186-
.catch((err) => Sale.deployed())
187-
.then((sale) => sale.price.call())
188-
.then((price) =>
189-
resolve(
190-
assert.equal(price.toString(10), saleConf.price.toString(10),
191-
`A non-owner was able to change the sale price`)
192-
)
193-
)
194-
.catch((err) => reject(err))
195-
)
196-
);
197-
it(`should not allow James to change the startBlock.`, () =>
198-
new Promise((resolve, reject) =>
199-
Sale.deployed()
200-
.then((sale) =>
201-
sale.changeStartBlock(saleConf.startBlock.add(1), {from: james})
202-
)
203-
.then(() => {
204-
reject(`A non-owner was able to change the sale startBlock`);
205-
})
206-
.catch((err) => Sale.deployed())
207-
.then((sale) => sale.startBlock.call())
208-
.then((startBlock) =>
209-
resolve(
210-
assert.equal(startBlock.toString(10),
211-
saleConf.startBlock.toString(10),
212-
`A non-owner was able to change the sale startBlock`)
213-
)
214-
)
215-
.catch((err) => reject(err))
216-
)
217-
);
218-
it(`should not allow James to change the owner.`, () =>
219-
new Promise((resolve, reject) =>
220-
Sale.deployed()
221-
.then((sale) => sale.changeOwner(james, {from: james}))
222-
.then(() => reject(`A non-owner was able to change the sale owner`))
223-
.catch((err) => Sale.deployed())
224-
.then((sale) => sale.owner.call())
225-
.then((owner) =>
226-
resolve(
227-
assert.equal(owner.valueOf(), saleConf.owner,
228-
`A non-owner was able to change the sale owner`)
229-
)
230-
)
231-
.catch((err) => reject(err))
232-
)
233-
);
234-
it(`should not allow James to change the wallet.`, () =>
235-
new Promise((resolve, reject) =>
236-
Sale.deployed()
237-
.then((sale) => sale.changeWallet(james, {from: james}))
238-
.then(() => reject(`A non-owner was able to change the wallet`))
239-
.catch((err) => Sale.deployed())
240-
.then((sale) => sale.wallet.call())
241-
.then((wallet) =>
242-
resolve(
243-
assert.equal(wallet.valueOf(), saleConf.wallet.toLowerCase(),
244-
`A non-owner was able to change the sale wallet`)
245-
)
246-
)
247-
.catch((err) => reject(err))
248-
)
249-
);
250-
it(`should not allow James to activate the emergencyToggle.`, () =>
251-
new Promise((resolve, reject) =>
252-
Sale.deployed()
253-
.then((sale) => sale.emergencyToggle({from: james}))
254-
.then(() => reject(`A non-owner was able to activate the emergencyToggle`))
255-
.catch((err) => Sale.deployed())
256-
.then((sale) => sale.emergencyFlag.call())
257-
.then((res) =>
258-
resolve(
259-
assert.equal(res.valueOf(), false,
260-
`A non-owner was able to activate the emergencyToggle`)
261-
)
262-
)
263-
.catch((err) => reject(err))
264-
)
265-
);
199+
const nonOwnerAccessError = `A non-owner was able to`
200+
const ownerAccessError = `The owner was unable to`
201+
const unexpectedError = `An unexpected error occurred`
202+
it(`should not allow a non-owner to change the price.`, async () => {
203+
const sale = await Sale.deployed()
204+
try {
205+
await notAsOwner(sale.changePrice, saleConf.price + 1)
206+
} catch(err) {
207+
const errMsg = unexpectedError
208+
assert(isEVMException(err), errMsg)
209+
}
210+
const price = await sale.price.call()
211+
const expected = saleConf.price
212+
const errMsg = nonOwnerAccessError + ` change the price`
213+
assert.strictEqual(price.toString(10), expected.toString(10), errMsg)
214+
})
215+
it(`should not allow a non-owner to change the startBlock.`, async () => {
216+
const sale = await Sale.deployed()
217+
try {
218+
await notAsOwner(sale.startBlock, saleConf.startBlock + 1)
219+
} catch(err) {
220+
const errMsg = unexpectedError
221+
assert(isEVMException(err), errMsg)
222+
}
223+
const startBlock = await sale.startBlock.call()
224+
const expected = saleConf.startBlock
225+
const errMsg = nonOwnerAccessError + ` change the start block`
226+
assert.strictEqual(startBlock.toString(10), expected.toString(10), errMsg)
227+
})
228+
it(`should not allow a non-owner to change the owner.`, async () => {
229+
const sale = await Sale.deployed()
230+
try {
231+
await notAsOwner(sale.owner, james)
232+
} catch(err) {
233+
const errMsg = unexpectedError
234+
assert(isEVMException(err), errMsg)
235+
}
236+
const owner = await sale.owner.call()
237+
const expected = saleConf.owner
238+
const errMsg = nonOwnerAccessError + ` change the owner`
239+
assert.strictEqual(owner.toString(), expected.toString(), errMsg)
240+
});
241+
it(`should not allow a non-owner to change the wallet.`, async () => {
242+
const sale = await Sale.deployed()
243+
try {
244+
await notAsOwner(sale.wallet, james)
245+
} catch(err) {
246+
const errMsg = unexpectedError
247+
assert(isEVMException(err), errMsg)
248+
}
249+
const wallet = await sale.wallet.call()
250+
const expected = saleConf.wallet
251+
const errMsg = nonOwnerAccessError + ` change the wallet`
252+
assert.strictEqual(wallet.toString(), expected.toLowerCase(), errMsg)
253+
})
254+
it(`should not allow a non-owner to activate the emergencyToggle.`, async () => {
255+
const sale = await Sale.deployed()
256+
try {
257+
await notAsOwner(sale.emergencyToggle)
258+
} catch(err) {
259+
const errMsg = unexpectedError
260+
assert(isEVMException(err), errMsg)
261+
}
262+
const emergencyFlag = await sale.emergencyFlag.call()
263+
const expected = false
264+
const errMsg = nonOwnerAccessError + ` change the emergencyToggle`
265+
assert.strictEqual(emergencyFlag, expected, errMsg)
266+
})
266267
it(`should change the owner to miguel.`, () =>
267268
new Promise((resolve, reject) =>
268269
Sale.deployed()

0 commit comments

Comments
 (0)