-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test__examples__Account.js
162 lines (148 loc) · 4.6 KB
/
test__examples__Account.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
"use strict";
const Account = artifacts.require("Account");
const Host = artifacts.require("Host");
const AddressStorage = artifacts.require("AddressStorage");
const StringStorage = artifacts.require("StringStorage");
const {
revertToSnapShot,
takeSnapShot,
} = require("./lib/web3-ganache-helpers.js");
//
contract("test/examples/Account.sol", (accounts) => {
const Account__owner = accounts[1];
const Account__new_owner = accounts[9];
const Account__data__key = "name";
const Account__data__value = "Jain";
const Host__fee = 100;
let snapshot_id;
//
beforeEach(async () => {
snapshot_id = (await takeSnapShot()).result;
});
//
afterEach(async () => {
await revertToSnapShot(snapshot_id);
});
//
it("Account.changeOwner allowed by owner", async () => {
const account = await Account.deployed();
await account.changeOwner(Account__new_owner, {
from: Account__owner,
});
return assert.equal(
Account__new_owner,
await account.owner(),
"Failed to change Account owner"
);
});
//
it("Account.changeOwner disallowed from non-owner", async () => {
const account = await Account.deployed();
try {
await account.changeOwner(Account__new_owner, {
from: Account__new_owner,
});
} catch (error) {
if (error.reason === "Account.changeOwner: message sender not an owner") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("Account.hostRegister allowed by owner", async () => {
const account = await Account.deployed();
const host = await Host.deployed();
await account.hostRegister(host.address, {
value: Host__fee,
from: Account__owner,
});
const account__registered = await AddressStorage.at(
await account.registered()
);
assert.isTrue(
await account__registered.has(host.address),
"Failed to register host to account"
);
const host__registered = await AddressStorage.at(await host.registered());
assert.isTrue(
await host__registered.has(account.address),
"Faled to register account to host"
);
});
//
it("Account.hostRegister disallowed from non-owner", async () => {
const account = await Account.deployed();
const host = await Host.deployed();
try {
await account.hostRegister(host.address, {
from: Account__new_owner,
});
} catch (error) {
if (error.reason === "Account.hostRegister: message sender not an owner") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("Account.hostRemove allowed by owner", async () => {
const account = await Account.deployed();
const host = await Host.deployed();
await account.hostRegister(host.address, {
value: Host__fee,
from: Account__owner,
});
await account.hostRemove(host.address, {
from: Account__owner,
});
const account__registered = await AddressStorage.at(
await account.registered()
);
const account__removed = await AddressStorage.at(await account.removed());
assert.isFalse(
await account__registered.has(host.address),
"Failed to remove host from `accout.registered`"
);
return assert.isTrue(
await account__removed.has(host.address),
"Failed to set host to `accout.removed`"
);
});
//
it("Account.hostRemove disallowed from non-owner", async () => {
const account = await Account.deployed();
const host = await Host.deployed();
try {
await account.hostRemove(host.address, {
from: Account__new_owner,
});
} catch (error) {
if (error.reason === "Account.hostRemove: message sender not an owner") {
return assert.isTrue(true, "Wat!?");
}
console.error(error);
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
/**
* @note The `.call` bit is required to get `value` instead of `tx` Object
*/
it("Account.data().remove allowed by owner", async () => {
const account = await Account.deployed();
const account__data = await StringStorage.at(await account.data());
await account__data.set(Account__data__key, Account__data__value, {
from: Account__owner,
});
const got_value = await account__data.remove.call(Account__data__key, {
from: Account__owner,
});
return assert.equal(
got_value,
Account__data__value,
"Failed to remove expected value"
);
});
});