/
test__examples__Host.js
219 lines (188 loc) · 6.33 KB
/
test__examples__Host.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"use strict";
const Host = artifacts.require("Host");
const Account = artifacts.require("Account");
const AddressStorage = artifacts.require("AddressStorage");
//
contract("test/examples/Host.sol", (accounts) => {
const owner_Host = accounts[1];
const owner_Account = accounts[2];
const owner_Account__name = "Jain";
const owner_Account__new_owner = accounts[9];
//
afterEach(async () => {
const host = await Host.deployed();
const active_accounts_reference = await host.active_accounts.call();
const active_accounts_instance = await AddressStorage.at(
active_accounts_reference
);
const banned_accounts_reference = await host.banned_accounts.call();
const banned_accounts_instance = await AddressStorage.at(
banned_accounts_reference
);
for (const account_owner of [owner_Account, owner_Account__new_owner]) {
if (await active_accounts_instance.has(account_owner)) {
await host.removeAccount(account_owner, { from: owner_Host });
}
if (await banned_accounts_instance.has(account_owner)) {
await host.removeAccount(account_owner, { from: owner_Host });
}
}
});
//
it("Host.banAccount allowed for owner on active account", async () => {
const host = await Host.deployed();
await host.registerAccount(owner_Account, owner_Account__name);
const {
logs: [log = {}],
} = await host.banAccount(owner_Account, { from: owner_Host });
assert.equal(log.event, "BannedAccount", "Failed to parse expected event");
const {
args: { owner, account_reference },
} = log;
return assert.equal(
owner,
owner_Account,
"Failed to parse expected account owner"
);
});
//
it("Host.importAccount allowed for new contract", async () => {
const host = await Host.deployed();
const account = await Account.deployed();
const {
logs: [log = {}],
} = await host.importAccount(account.address);
assert.equal(
log.event,
"ActivatedAccount",
"Failed to parse expected event"
);
const {
args: { owner, account_reference },
} = log;
return assert.equal(
owner,
owner_Account,
"Failed to parse expected account owner"
);
});
//
it("Host.importAccount disallows duplicate accounts", async () => {
const host = await Host.deployed();
const account = await Account.deployed();
await host.importAccount(account.address);
try {
await host.importAccount(account.address);
} catch (error) {
if (error.reason === "Host.importAccount: account already active") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("Host.importAccount disallows banned accounts", async () => {
const host = await Host.deployed();
const account = await Account.deployed();
await host.importAccount(account.address);
await host.banAccount(await account.owner(), { from: owner_Host });
try {
await host.importAccount(account.address);
} catch (error) {
if (error.reason === "Host.importAccount: account was banned") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("Host.registerAccount allows new account", async () => {
const host = await Host.deployed();
const {
logs: [log = {}],
} = await host.registerAccount(owner_Account, owner_Account__name);
assert.equal(
log.event,
"ActivatedAccount",
"Failed to parse expected event"
);
const {
args: { owner, account_reference },
} = log;
return assert.equal(
owner,
owner_Account,
"Failed to parse expected account owner"
);
});
//
it("Host.registerAccount disallows duplicate accounts", async () => {
const host = await Host.deployed();
await host.registerAccount(owner_Account, owner_Account__name);
try {
await host.registerAccount(owner_Account, owner_Account__name);
} catch (error) {
if (error.reason === "Host.registerAccount: account already active") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("Host.registerAccount disallows banned accounts", async () => {
const host = await Host.deployed();
await host.registerAccount(owner_Account, owner_Account__name);
await host.banAccount(owner_Account, { from: owner_Host });
try {
await host.registerAccount(owner_Account, owner_Account__name);
} catch (error) {
if (error.reason === "Host.registerAccount: account was banned") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("Host.updateKey allowed by Account owner", async () => {
const host = await Host.deployed();
const account = await Account.new(owner_Account, owner_Account__name);
await host.importAccount(account.address);
await account.changeOwner(owner_Account__new_owner, {
from: owner_Account,
});
await host.updateKey(owner_Account, { from: owner_Account__new_owner });
});
//
it("Host.updateKey disallowed from non-owner of Account", async () => {
const host = await Host.deployed();
const account = await Account.new(owner_Account, owner_Account__name);
await host.importAccount(account.address);
await account.changeOwner(owner_Account__new_owner, {
from: owner_Account,
});
try {
await host.updateKey(owner_Account, { from: owner_Host });
} catch (error) {
if (error.reason === "Host.updateKey: message sender not Account owner") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("Host.whoIs returns expected account name", async () => {
const host = await Host.deployed();
await host.registerAccount(owner_Account, owner_Account__name);
const name = await host.whoIs(owner_Account);
return assert.equal(
name,
owner_Account__name,
"Failed to parse expected name from contract"
);
});
});