-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacl.js
373 lines (363 loc) · 14.2 KB
/
acl.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// ERC721: AccessControl (ACL) Tests
// Zeppelin test helpers
const {
BN,
constants,
expectEvent,
expectRevert,
} = require("@openzeppelin/test-helpers");
const {
assert,
expect,
} = require("chai");
const {
ZERO_ADDRESS,
ZERO_BYTES32,
MAX_UINT256,
} = constants;
// ACL token features and roles
const {
not,
FEATURE_TRANSFERS,
FEATURE_TRANSFERS_ON_BEHALF,
FEATURE_OWN_BURNS,
FEATURE_BURNS_ON_BEHALF,
ROLE_TOKEN_CREATOR,
ROLE_TOKEN_DESTROYER,
ROLE_METADATA_PROVIDER,
ROLE_URI_MANAGER,
ROLE_RESCUE_MANAGER,
} = require("../../scripts/include/features_roles");
// land data utils
const {
generate_land_plot,
generate_land_plot_metadata,
} = require("./include/land_data_utils");
// deployment routines in use
const {
land_nft_deploy_restricted,
} = require("./include/deployment_routines");
const {
erc20_deploy,
} = require("../erc20/include/deployment_routines");
// run AccessControl (ACL) tests
contract("LandERC721: AccessControl (ACL) tests", function(accounts) {
// extract accounts to be used:
// A0 – special default zero account accounts[0] used by Truffle, reserved
// a0 – deployment account having all the permissions, reserved
// H0 – initial token holder account
// a1, a2,... – working accounts to perform tests on
const [A0, a0, H0, a1, a2, a3] = accounts;
function acl_suite(contract_name, deployment_fn, burnable) {
describe(`${contract_name}: ACL`, function() {
// deploy token
let token;
beforeEach(async function() {
token = await deployment_fn.call(this, a0);
});
// run the suite
const from = a1;
const to = a2;
const by = a3;
const tokenId = 0xF001_0001;
const nonExistentId = 0xF001_0002;
beforeEach(async function() {
await token.mintWithMetadata(from, tokenId, generate_land_plot_metadata(), {from: a0})
});
// transfers
describe("when FEATURE_TRANSFERS is enabled", function() {
beforeEach(async function() {
await token.updateFeatures(FEATURE_TRANSFERS, {from: a0});
});
it("direct transfer succeeds", async function() {
await token.transferFrom(from, to, tokenId, {from});
});
it("safe direct transfer succeeds", async function() {
await token.safeTransferFrom(from, to, tokenId, {from});
});
});
describe("when FEATURE_TRANSFERS is disabled", function() {
beforeEach(async function() {
await token.updateFeatures(not(FEATURE_TRANSFERS), {from: a0});
});
it("direct transfer reverts", async function() {
await expectRevert(token.transferFrom(from, to, tokenId, {from}), "transfers are disabled");
});
it("safe direct transfer reverts", async function() {
await expectRevert(token.safeTransferFrom(from, to, tokenId, {from}), "transfers are disabled");
});
});
// transfers on behalf
describe("when token transfer is approved", function() {
beforeEach(async function() {
await token.approve(by, tokenId, {from});
});
describe("when FEATURE_TRANSFERS_ON_BEHALF is enabled", function() {
beforeEach(async function() {
await token.updateFeatures(FEATURE_TRANSFERS_ON_BEHALF, {from: a0});
});
it("transfer on behalf succeeds", async function() {
await token.transferFrom(from, to, tokenId, {from: by});
});
it("safe transfer on behalf succeeds", async function() {
await token.safeTransferFrom(from, to, tokenId, {from: by});
});
});
describe("when FEATURE_TRANSFERS_ON_BEHALF is disabled", function() {
beforeEach(async function() {
await token.updateFeatures(not(FEATURE_TRANSFERS_ON_BEHALF), {from: a0});
});
it("transfer on behalf reverts", async function() {
await expectRevert(token.transferFrom(from, to, tokenId, {from: by}), "transfers on behalf are disabled");
});
it("safe transfer on behalf reverts", async function() {
await expectRevert(token.safeTransferFrom(from, to, tokenId, {from: by}), "transfers on behalf are disabled");
});
});
});
// transfers on behalf by operator
describe("when transfer operator is set", function() {
beforeEach(async function() {
await token.setApprovalForAll(by, true, {from});
});
describe("when FEATURE_TRANSFERS_ON_BEHALF is enabled", function() {
beforeEach(async function() {
await token.updateFeatures(FEATURE_TRANSFERS_ON_BEHALF, {from: a0});
});
it("transfer on behalf by operator succeeds", async function() {
await token.transferFrom(from, to, tokenId, {from: by});
});
it("safe transfer on behalf by operator succeeds", async function() {
await token.safeTransferFrom(from, to, tokenId, {from: by});
});
});
describe("when FEATURE_TRANSFERS_ON_BEHALF is disabled", function() {
beforeEach(async function() {
await token.updateFeatures(not(FEATURE_TRANSFERS_ON_BEHALF), {from: a0});
});
it("transfer on behalf by operator reverts", async function() {
await expectRevert(token.transferFrom(from, to, tokenId, {from: by}), "transfers on behalf are disabled");
});
it("safe transfer on behalf by operator reverts", async function() {
await expectRevert(token.safeTransferFrom(from, to, tokenId, {from: by}), "transfers on behalf are disabled");
});
});
});
if(burnable) {
// burns
describe("when FEATURE_OWN_BURNS is enabled", function() {
beforeEach(async function() {
await token.updateFeatures(FEATURE_OWN_BURNS, {from: a0});
});
it("burn succeeds", async function() {
await token.burn(tokenId, {from});
});
});
describe("when FEATURE_OWN_BURNS is disabled", function() {
beforeEach(async function() {
await token.updateFeatures(not(FEATURE_OWN_BURNS), {from: a0});
});
it("burn reverts", async function() {
await expectRevert(token.burn(tokenId, {from}), "burns are disabled");
});
});
// burns on behalf
describe("when token transfer is approved", function() {
beforeEach(async function() {
await token.approve(by, tokenId, {from});
});
describe("when FEATURE_BURNS_ON_BEHALF is enabled", function() {
beforeEach(async function() {
await token.updateFeatures(FEATURE_BURNS_ON_BEHALF, {from: a0});
});
it("burn on behalf succeeds", async function() {
await token.burn(tokenId, {from: by});
});
});
describe("when FEATURE_BURNS_ON_BEHALF is disabled", function() {
beforeEach(async function() {
await token.updateFeatures(not(FEATURE_BURNS_ON_BEHALF), {from: a0});
});
it("burn on behalf reverts", async function() {
await expectRevert(token.burn(tokenId, {from: by}), "burns on behalf are disabled");
});
});
});
// burns on behalf by operator
describe("when transfer operator is set", function() {
beforeEach(async function() {
await token.setApprovalForAll(by, true, {from});
});
describe("when FEATURE_BURNS_ON_BEHALF is enabled", function() {
beforeEach(async function() {
await token.updateFeatures(FEATURE_BURNS_ON_BEHALF, {from: a0});
});
it("burn on behalf by operator succeeds", async function() {
await token.burn(tokenId, {from: by});
});
});
describe("when FEATURE_BURNS_ON_BEHALF is disabled", function() {
beforeEach(async function() {
await token.updateFeatures(not(FEATURE_BURNS_ON_BEHALF), {from: a0});
});
it("burn on behalf by operator reverts", async function() {
await expectRevert(token.burn(tokenId, {from: by}), "burns on behalf are disabled");
});
});
});
}
// metadata
describe("when sender has ROLE_METADATA_PROVIDER permission", function() {
beforeEach(async function() {
await token.updateRole(from, ROLE_METADATA_PROVIDER, {from: a0});
});
it("sender can set token metadata", async function() {
await token.setMetadata(nonExistentId, generate_land_plot_metadata(), {from});
});
it("sender can remove token metadata", async function() {
await token.removeMetadata(nonExistentId, {from});
});
});
describe("when sender doesn't have ROLE_METADATA_PROVIDER permission", function() {
beforeEach(async function() {
await token.updateRole(from, not(ROLE_METADATA_PROVIDER), {from: a0});
});
it("sender can't set token metadata", async function() {
await expectRevert(token.setMetadata(nonExistentId, generate_land_plot_metadata(), {from}), "access denied");
});
it("sender can't remove token metadata", async function() {
await expectRevert(token.removeMetadata(nonExistentId, {from}), "access denied");
});
});
describe("when sender has ROLE_TOKEN_CREATOR and ROLE_METADATA_PROVIDER permissions", function() {
beforeEach(async function() {
await token.updateRole(from, ROLE_TOKEN_CREATOR | ROLE_METADATA_PROVIDER, {from: a0});
});
it("sender can mint a token with metadata", async function() {
await token.mintWithMetadata(to, nonExistentId, generate_land_plot_metadata(), {from});
});
});
describe("when sender doesn't have ROLE_TOKEN_CREATOR permission", function() {
beforeEach(async function() {
await token.updateRole(from, not(ROLE_TOKEN_CREATOR), {from: a0});
});
it("sender can't mint a token with metadata", async function() {
await expectRevert(token.mintWithMetadata(to, nonExistentId, generate_land_plot_metadata(), {from}), "access denied");
});
});
describe("when sender doesn't have ROLE_METADATA_PROVIDER permission", function() {
beforeEach(async function() {
await token.updateRole(from, not(ROLE_METADATA_PROVIDER), {from: a0});
});
it("sender can't mint a token with metadata", async function() {
await expectRevert(token.mintWithMetadata(to, nonExistentId, generate_land_plot_metadata(), {from}), "access denied");
});
});
// mints
describe("when sender has ROLE_TOKEN_CREATOR permission", function() {
beforeEach(async function() {
await token.updateRole(from, ROLE_TOKEN_CREATOR, {from: a0});
await token.setMetadata(nonExistentId, generate_land_plot_metadata(), {from: a0});
});
it("sender can mint a token", async function() {
await token.mint(to, nonExistentId, {from});
});
});
describe("when sender doesn't have ROLE_TOKEN_CREATOR permission", function() {
beforeEach(async function() {
await token.updateRole(from, not(ROLE_TOKEN_CREATOR), {from: a0});
await token.setMetadata(nonExistentId, generate_land_plot_metadata(), {from: a0});
});
it("sender can't mint a token", async function() {
await expectRevert(token.mint(to, nonExistentId, {from}), "access denied");
});
});
// burns by destroyer
if(burnable) {
describe("when sender has ROLE_TOKEN_DESTROYER permission and FEATURE_OWN_BURNS/FEATURE_BURNS_ON_BEHALF are disabled", function() {
beforeEach(async function() {
await token.updateRole(from, ROLE_TOKEN_DESTROYER, {from: a0});
await token.updateFeatures(not(FEATURE_OWN_BURNS, FEATURE_BURNS_ON_BEHALF), {from: a0});
});
it("sender can burn own token", async function() {
await token.burn(tokenId, {from});
});
const anotherTokenId = nonExistentId;
beforeEach(async function() {
await token.mintWithMetadata(to, anotherTokenId, generate_land_plot_metadata(), {from: a0});
});
it("sender can burn someone else's token", async function() {
await token.burn(anotherTokenId, {from});
});
});
describe("when sender doesn't have ROLE_TOKEN_DESTROYER permission and FEATURE_OWN_BURNS/FEATURE_BURNS_ON_BEHALF are enabled", function() {
beforeEach(async function() {
await token.updateRole(from, not(ROLE_TOKEN_DESTROYER), {from: a0});
await token.updateFeatures(FEATURE_OWN_BURNS | FEATURE_BURNS_ON_BEHALF, {from: a0});
});
const anotherTokenId = nonExistentId;
beforeEach(async function() {
await token.mintWithMetadata(to, anotherTokenId, generate_land_plot_metadata(), {from: a0});
});
it("sender can't burn someone else's token", async function() {
await expectRevert(token.burn(anotherTokenId, {from}), "access denied");
});
});
}
// URI setup
describe("when sender has ROLE_URI_MANAGER permission", function() {
beforeEach(async function() {
await token.updateRole(from, ROLE_URI_MANAGER, {from: a0});
});
it("sender can set baseURI", async function() {
await token.setBaseURI("abc", {from});
});
it("sender can set tokenURI", async function() {
await token.setTokenURI(tokenId, "abc", {from});
});
it("sender can update landDescriptor", async function() {
await token.setLandDescriptor(ZERO_ADDRESS, {from});
});
});
describe("when sender doesn't have ROLE_URI_MANAGER permission", function() {
beforeEach(async function() {
await token.updateRole(from, not(ROLE_URI_MANAGER), {from: a0});
});
it("sender can't set baseURI", async function() {
await expectRevert(token.setBaseURI("abc", {from}), "access denied");
});
it("sender can't set tokenURI", async function() {
await expectRevert(token.setTokenURI(tokenId, "abc", {from}), "access denied");
});
it("sender can't update landDescriptor", async function() {
await expectRevert(token.setLandDescriptor(ZERO_ADDRESS, {from}), "access denied");
});
});
// Rescuing ERC20 tokens
describe("when ERC20 tokens are lost", function() {
let erc20Contract;
beforeEach(async function() {
erc20Contract = await erc20_deploy(a0, H0);
await erc20Contract.transfer(token.address, 1, {from: H0});
});
describe("when sender has ROLE_RESCUE_MANAGER permission", function() {
beforeEach(async function() {
await token.updateRole(from, ROLE_RESCUE_MANAGER, {from: a0});
});
it("sender can rescue ERC20 tokens", async function() {
await token.rescueErc20(erc20Contract.address, H0, 1, {from});
});
});
describe("when sender doesn't have ROLE_RESCUE_MANAGER permission", function() {
beforeEach(async function() {
await token.updateRole(from, not(ROLE_RESCUE_MANAGER), {from: a0});
});
it("sender can't rescue ERC20 tokens", async function() {
await expectRevert(token.rescueErc20(erc20Contract.address, H0, 1, {from}), "access denied");
});
});
});
});
}
acl_suite("LandERC721", land_nft_deploy_restricted, true);
});