-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadministratable.js
86 lines (64 loc) · 3.78 KB
/
administratable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* global artifacts contract it assert */
const { shouldFail, expectEvent } = require('openzeppelin-test-helpers')
const SukuToken = artifacts.require('SukuToken')
contract('Administratable', (accounts) => {
it('should deploy', async () => {
const tokenInstance = await SukuToken.new(accounts[0])
assert.equal(tokenInstance !== null, true, 'Contract should be deployed')
})
it('should allow adding and removing for owner', async () => {
const tokenInstance = await SukuToken.new(accounts[0])
// Validate acct 1 is not an admin by default
let isAdmin = await tokenInstance.isAdministrator(accounts[1])
assert.equal(isAdmin, false, 'Account should not be admin by default')
// Adding an admin to the list should be successful for the owner (address[0])
await tokenInstance.addAdmin(accounts[1], { from: accounts[0] })
isAdmin = await tokenInstance.isAdministrator.call(accounts[1])
assert.equal(isAdmin, true, 'Owner should be able to add admin')
// Removing the admin should be successful for the owner (address[0])
await tokenInstance.removeAdmin(accounts[1], { from: accounts[0] })
isAdmin = await tokenInstance.isAdministrator.call(accounts[1])
assert.equal(isAdmin, false, 'Owner should be able to remove admin')
})
it('should preventing adding and removing for non-owner', async () => {
const tokenInstance = await SukuToken.new(accounts[0])
// Validate acct 2 is not an admin by default
let isAdmin = await tokenInstance.isAdministrator(accounts[2])
assert.equal(isAdmin, false, 'Account should not be admin by default')
// Adding an address to the list should fail for non-owner (address[1])
await shouldFail.reverting(tokenInstance.addAdmin(accounts[2], { from: accounts[1] }))
// Adding the address to admin list should not impact this - only owner can add other admins
await tokenInstance.addAdmin(accounts[1], { from: accounts[0] })
await shouldFail.reverting(tokenInstance.addAdmin(accounts[2], { from: accounts[1] }))
// Verify a non-owner can't remove an admin (including itself)
await shouldFail.reverting(tokenInstance.removeAdmin(accounts[1], { from: accounts[1] }))
await shouldFail.reverting(tokenInstance.removeAdmin(accounts[1], { from: accounts[2] }))
})
it('should emit events for adding admins', async () => {
const tokenInstance = await SukuToken.new(accounts[0])
let { logs } = await tokenInstance.addAdmin(accounts[3], { from: accounts[0] })
expectEvent.inLogs(logs, 'AdminAdded', { addedAdmin: accounts[3], addedBy: accounts[0] })
})
it('should emit events for removing admins', async () => {
const tokenInstance = await SukuToken.new(accounts[0])
await tokenInstance.addAdmin(accounts[3], { from: accounts[0] })
const { logs } = await tokenInstance.removeAdmin(accounts[3], { from: accounts[0] })
expectEvent.inLogs(logs, 'AdminRemoved', { removedAdmin: accounts[3], removedBy: accounts[0] })
})
it('should preventing adding an admin when already an admin', async () => {
const tokenInstance = await SukuToken.new(accounts[0])
// The first add should succeed
await tokenInstance.addAdmin(accounts[1], { from: accounts[0] })
// The second add should fail
await shouldFail.reverting(tokenInstance.addAdmin(accounts[1], { from: accounts[0] }))
})
it('should preventing removing an admin when it is not an admin', async () => {
const tokenInstance = await SukuToken.new(accounts[0])
// Add an accct to the admin list
await tokenInstance.addAdmin(accounts[1], { from: accounts[0] })
// The first removal should succeed.
await tokenInstance.removeAdmin(accounts[1], { from: accounts[0] })
// The second removal should fail
await shouldFail.reverting(tokenInstance.removeAdmin(accounts[1], { from: accounts[0] }))
})
})