-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test__examples__AccountStorage.js
183 lines (151 loc) · 5.1 KB
/
test__examples__AccountStorage.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
"use strict";
const Account = artifacts.require("Account");
const AccountStorage = artifacts.require("AccountStorage");
//
contract("test/examples/AccountStorage.sol", (accounts) => {
const owner_AccountStorage = accounts[0];
const owner_Account = accounts[1];
const owner_Account__name = "Jain";
//
afterEach(async () => {
const instance = await AccountStorage.deployed();
if (await instance.has(owner_Account)) {
await instance.remove(owner_Account);
}
});
//
it("AccountStorage.get allows for contract reconstitution", async () => {
const instance = await AccountStorage.deployed();
const contract = await Account.deployed();
await instance.set(owner_Account, contract.address, {
from: owner_AccountStorage,
});
const got_address = await instance.get(owner_Account);
const got_contract = await Account.at(got_address);
const got_name = await got_contract.name();
assert.equal(got_name, owner_Account__name, "Wat!?");
});
//
it("AccountStorage.has returns true/false where expected", async () => {
const instance = await AccountStorage.deployed();
const contract = await Account.deployed();
assert.isFalse(await instance.has(owner_Account), "Account was detected");
await instance.set(owner_Account, contract.address, {
from: owner_AccountStorage,
});
return assert.isTrue(
await instance.has(owner_Account),
"Account not detected"
);
});
//
it("AccountStorage.listKeys returns an array of addresses", async () => {
const instance = await AccountStorage.deployed();
const contract = await Account.deployed();
assert.deepEqual([], await instance.listKeys(), "Key list not empty");
await instance.set(owner_Account, contract.address, {
from: owner_AccountStorage,
});
return assert.deepEqual(
[owner_Account],
await instance.listKeys(),
"Key list is empty"
);
});
//
it("AccountStorage.remove allowed from owner", async () => {
const instance = await AccountStorage.deployed();
const contract = await Account.deployed();
await instance.set(owner_Account, contract.address, {
from: owner_AccountStorage,
});
await instance.remove(owner_Account, {
from: owner_AccountStorage,
});
try {
await instance.remove(owner_Account, {
from: owner_AccountStorage,
});
} catch (error) {
if (error.reason === "AccountStorage.remove: value not defined") {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
const {
words: [got_size],
} = await instance.size();
return assert.equal(got_size, 0, "AccountStorage size did not decrease");
});
//
it("AccountStorage.remove disallowed from non-owner", async () => {
const instance = await AccountStorage.deployed();
const contract = await Account.deployed();
await instance.set(owner_Account, contract.address, {
from: owner_AccountStorage,
});
try {
await instance.remove(owner_Account, {
from: accounts[9],
});
} catch (error) {
if (
error.reason === "AccountStorage.remove: Message sender not an owner"
) {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("AccountStorage.selfDestruct allowed from owner", async () => {
const instance = await AccountStorage.new(owner_AccountStorage);
await instance.selfDestruct(owner_AccountStorage, {
from: owner_AccountStorage,
});
return assert.isTrue(true, "Wat!?");
});
//
it("AccountStorage.selfDestruct disallowed from non-owner", async () => {
const instance = await AccountStorage.new(owner_AccountStorage);
return assert.isTrue(true, "Wat!?");
try {
await instance.selfDestruct(owner_AccountStorage, { from: owner_Account });
} catch (error) {
if (error.reason === "AccountStorage.selfDestruct: message sender not an owner") {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("AccountStorage.set allowed from owner", async () => {
const instance = await AccountStorage.deployed();
const contract = await Account.deployed();
await instance.set(owner_Account, contract.address, {
from: owner_AccountStorage,
});
assert.isTrue(
await instance.has(owner_Account),
"Failed to set key/value pair"
);
});
//
it("AccountStorage.set disallowed from non-owner", async () => {
const instance = await AccountStorage.deployed();
const contract = await Account.deployed();
try {
await instance.set(owner_Account, contract.address, {
from: accounts[9],
});
} catch (error) {
if (error.reason === "AccountStorage.set: Message sender not an owner") {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
});