1
- const { expect } = require ( "chai" ) . use ( require ( "chai-as-promised" ) ) ;
1
+ const { expect, assert } = require ( "chai" ) . use ( require ( "chai-as-promised" ) ) ;
2
2
const { ethers } = require ( "hardhat" ) ;
3
3
const { get } = require ( "lodash" ) ;
4
4
const config = require ( "../config.js" ) ;
@@ -8,6 +8,10 @@ describe("DocumentStore", async () => {
8
8
let DocumentStore ;
9
9
let DocumentStoreInstance ;
10
10
11
+ const adminRole = ethers . constants . HashZero ;
12
+ const issuerRole = ethers . utils . id ( "ISSUER_ROLE" ) ;
13
+ const revokerRole = ethers . utils . id ( "REVOKER_ROLE" ) ;
14
+
11
15
beforeEach ( "" , async ( ) => {
12
16
Accounts = await ethers . getSigners ( ) ;
13
17
DocumentStore = await ethers . getContractFactory ( "DocumentStore" ) ;
@@ -20,10 +24,35 @@ describe("DocumentStore", async () => {
20
24
const name = await DocumentStoreInstance . name ( ) ;
21
25
expect ( name ) . to . be . equal ( config . INSTITUTE_NAME , "Name of institute does not match" ) ;
22
26
} ) ;
27
+ } ) ;
28
+
29
+ describe ( "Access Control" , ( ) => {
30
+ describe ( "Initialisation" , ( ) => {
31
+ it ( "should revert if owner is zero address" , async ( ) => {
32
+ const tx = DocumentStore . connect ( Accounts [ 0 ] ) . deploy ( config . INSTITUTE_NAME , ethers . constants . AddressZero ) ;
33
+
34
+ await expect ( tx ) . to . be . revertedWith ( "Owner is zero" ) ;
35
+ } ) ;
36
+
37
+ describe ( "Owner Default Roles" , ( ) => {
38
+ it ( "should have default admin role" , async ( ) => {
39
+ const hasRole = await DocumentStoreInstance . hasRole ( adminRole , Accounts [ 0 ] . address ) ;
40
+
41
+ expect ( hasRole ) . to . be . true ;
42
+ } ) ;
23
43
24
- it ( "it should have the corrent owner" , async ( ) => {
25
- const owner = await DocumentStoreInstance . owner ( ) ;
26
- expect ( owner ) . to . be . equal ( Accounts [ 0 ] . address ) ;
44
+ it ( "should have issuer role" , async ( ) => {
45
+ const hasRole = await DocumentStoreInstance . hasRole ( issuerRole , Accounts [ 0 ] . address ) ;
46
+
47
+ expect ( hasRole ) . to . be . true ;
48
+ } ) ;
49
+
50
+ it ( "should have revoker role" , async ( ) => {
51
+ const hasRole = await DocumentStoreInstance . hasRole ( revokerRole , Accounts [ 0 ] . address ) ;
52
+
53
+ expect ( hasRole ) . to . be . true ;
54
+ } ) ;
55
+ } ) ;
27
56
} ) ;
28
57
} ) ;
29
58
@@ -35,8 +64,9 @@ describe("DocumentStore", async () => {
35
64
} ) ;
36
65
37
66
describe ( "issue" , ( ) => {
67
+ const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
68
+
38
69
it ( "should be able to issue a document" , async ( ) => {
39
- const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
40
70
const tx = await DocumentStoreInstance . issue ( documentMerkleRoot ) ;
41
71
const receipt = await tx . wait ( ) ;
42
72
@@ -49,7 +79,6 @@ describe("DocumentStore", async () => {
49
79
} ) ;
50
80
51
81
it ( "should not allow duplicate issues" , async ( ) => {
52
- const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
53
82
await DocumentStoreInstance . issue ( documentMerkleRoot ) ;
54
83
55
84
// Check that reissue is rejected
@@ -59,13 +88,25 @@ describe("DocumentStore", async () => {
59
88
) ;
60
89
} ) ;
61
90
62
- it ( "only allows the owner to issue " , async ( ) => {
63
- const nonOwner = Accounts [ 1 ] ;
64
- const owner = await DocumentStoreInstance . owner ( ) ;
65
- expect ( nonOwner ) . to . not . be . equal ( owner ) ;
91
+ it ( "should revert when caller has no issuer role " , async ( ) => {
92
+ const account = Accounts [ 1 ] ;
93
+ const hasNoIssuerRole = await DocumentStoreInstance . hasRole ( issuerRole , account . address ) ;
94
+ assert . isFalse ( hasNoIssuerRole , "Non-Issuer Account has issuer role" ) ;
66
95
67
- const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
68
- await expect ( DocumentStoreInstance . connect ( nonOwner ) . issue ( documentMerkleRoot ) ) . to . be . rejectedWith ( / r e v e r t / ) ;
96
+ await expect ( DocumentStoreInstance . connect ( account ) . issue ( documentMerkleRoot ) ) . to . be . rejectedWith (
97
+ / A c c e s s C o n t r o l /
98
+ ) ;
99
+ } ) ;
100
+
101
+ it ( "should issue successfully when caller has issuer role" , async ( ) => {
102
+ const account = Accounts [ 0 ] ;
103
+ const hasIssuerRole = await DocumentStoreInstance . hasRole ( issuerRole , account . address ) ;
104
+ assert . isTrue ( hasIssuerRole , "Issuer Account has issuer role" ) ;
105
+
106
+ await DocumentStoreInstance . connect ( account ) . issue ( documentMerkleRoot ) ;
107
+ const issued = await DocumentStoreInstance . isIssued ( documentMerkleRoot ) ;
108
+
109
+ expect ( issued ) . to . be . true ;
69
110
} ) ;
70
111
} ) ;
71
112
@@ -116,15 +157,30 @@ describe("DocumentStore", async () => {
116
157
) ;
117
158
} ) ;
118
159
119
- it ( "only allows the owner to issue " , async ( ) => {
120
- const nonOwner = Accounts [ 1 ] ;
121
- const owner = await DocumentStoreInstance . owner ( ) ;
122
- expect ( nonOwner ) . to . not . be . equal ( owner ) ;
160
+ it ( "should revert when caller has no issuer role " , async ( ) => {
161
+ const nonIssuerAccount = Accounts [ 1 ] ;
162
+ const hasNoIssuerRole = await DocumentStoreInstance . hasRole ( issuerRole , nonIssuerAccount . address ) ;
163
+ assert . isFalse ( hasNoIssuerRole , "Non-Issuer Account has issuer role" ) ;
123
164
124
165
const documentMerkleRoots = [ "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ] ;
125
166
126
167
// FIXME:
127
- await expect ( DocumentStoreInstance . connect ( nonOwner ) . bulkIssue ( documentMerkleRoots ) ) . to . be . rejectedWith ( / r e v e r t / ) ;
168
+ await expect ( DocumentStoreInstance . connect ( nonIssuerAccount ) . bulkIssue ( documentMerkleRoots ) ) . to . be . rejectedWith (
169
+ / A c c e s s C o n t r o l /
170
+ ) ;
171
+ } ) ;
172
+
173
+ it ( "should bulk issue successfully when caller has issuer role" , async ( ) => {
174
+ const documentMerkleRoots = [ "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ] ;
175
+
176
+ const account = Accounts [ 0 ] ;
177
+ const hasIssuerRole = await DocumentStoreInstance . hasRole ( issuerRole , account . address ) ;
178
+ assert . isTrue ( hasIssuerRole , "Issuer Account has no issuer role" ) ;
179
+
180
+ await DocumentStoreInstance . connect ( account ) . bulkIssue ( documentMerkleRoots ) ;
181
+ const issued = await DocumentStoreInstance . isIssued ( documentMerkleRoots [ 0 ] ) ;
182
+
183
+ expect ( issued ) . to . be . true ;
128
184
} ) ;
129
185
} ) ;
130
186
@@ -166,8 +222,9 @@ describe("DocumentStore", async () => {
166
222
} ) ;
167
223
168
224
describe ( "revoke" , ( ) => {
225
+ const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
226
+
169
227
it ( "should allow the revocation of a valid and issued document" , async ( ) => {
170
- const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
171
228
const documentHash = "0x10327d7f904ee3ee0e69d592937be37a33692a78550bd100d635cdea2344e6c7" ;
172
229
173
230
await DocumentStoreInstance . issue ( documentMerkleRoot ) ;
@@ -181,7 +238,6 @@ describe("DocumentStore", async () => {
181
238
} ) ;
182
239
183
240
it ( "should allow the revocation of an issued root" , async ( ) => {
184
- const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
185
241
const documentHash = documentMerkleRoot ;
186
242
187
243
await DocumentStoreInstance . issue ( documentMerkleRoot ) ;
@@ -195,7 +251,6 @@ describe("DocumentStore", async () => {
195
251
} ) ;
196
252
197
253
it ( "should not allow repeated revocation of a valid and issued document" , async ( ) => {
198
- const documentMerkleRoot = "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ;
199
254
const documentHash = "0x10327d7f904ee3ee0e69d592937be37a33692a78550bd100d635cdea2344e6c7" ;
200
255
201
256
await DocumentStoreInstance . issue ( documentMerkleRoot ) ;
@@ -215,6 +270,27 @@ describe("DocumentStore", async () => {
215
270
expect ( receipt . events [ 0 ] . event ) . to . be . equal ( "DocumentRevoked" ) ;
216
271
expect ( receipt . events [ 0 ] . args . document ) . to . be . equal ( documentHash ) ;
217
272
} ) ;
273
+
274
+ it ( "should revert when caller has no revoker role" , async ( ) => {
275
+ const nonRevokerAccount = Accounts [ 1 ] ;
276
+ const hasNoRevokerRole = await DocumentStoreInstance . hasRole ( revokerRole , nonRevokerAccount . address ) ;
277
+ assert . isFalse ( hasNoRevokerRole , "Non-Revoker Account has revoker role" ) ;
278
+
279
+ await expect ( DocumentStoreInstance . connect ( nonRevokerAccount ) . revoke ( documentMerkleRoot ) ) . to . be . rejectedWith (
280
+ / A c c e s s C o n t r o l /
281
+ ) ;
282
+ } ) ;
283
+
284
+ it ( "should revoke successfully when caller has issuer role" , async ( ) => {
285
+ const account = Accounts [ 0 ] ;
286
+ const hasIssuerRole = await DocumentStoreInstance . hasRole ( issuerRole , account . address ) ;
287
+ assert . isTrue ( hasIssuerRole , "Revoker Account has no revoker role" ) ;
288
+
289
+ await DocumentStoreInstance . connect ( account ) . revoke ( documentMerkleRoot ) ;
290
+ const issued = await DocumentStoreInstance . isRevoked ( documentMerkleRoot ) ;
291
+
292
+ expect ( issued ) . to . be . true ;
293
+ } ) ;
218
294
} ) ;
219
295
220
296
describe ( "bulkRevoke" , ( ) => {
@@ -266,16 +342,29 @@ describe("DocumentStore", async () => {
266
342
) ;
267
343
} ) ;
268
344
269
- it ( "only allows the owner to revoke " , async ( ) => {
270
- const nonOwner = Accounts [ 1 ] ;
271
- const owner = await DocumentStoreInstance . owner ( ) ;
272
- expect ( nonOwner ) . to . not . be . equal ( owner ) ;
345
+ it ( "should revert when caller has no revoker role " , async ( ) => {
346
+ const nonRevokerAccount = Accounts [ 1 ] ;
347
+ const hasNoRevokerRole = await DocumentStoreInstance . hasRole ( revokerRole , nonRevokerAccount . address ) ;
348
+ assert . isFalse ( hasNoRevokerRole , "Non-Revoker Account has revoker role" ) ;
273
349
274
350
const documentMerkleRoots = [ "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ] ;
275
- await expect ( DocumentStoreInstance . connect ( nonOwner ) . bulkRevoke ( documentMerkleRoots ) ) . to . be . rejectedWith (
276
- / r e v e r t /
351
+ await expect ( DocumentStoreInstance . connect ( nonRevokerAccount ) . bulkRevoke ( documentMerkleRoots ) ) . to . be . rejectedWith (
352
+ / A c c e s s C o n t r o l /
277
353
) ;
278
354
} ) ;
355
+
356
+ it ( "should bulk revoke successfully when caller has issuer role" , async ( ) => {
357
+ const documentMerkleRoots = [ "0x3a267813bea8120f55a7b9ca814c34dd89f237502544d7c75dfd709a659f6330" ] ;
358
+
359
+ const account = Accounts [ 0 ] ;
360
+ const hasIssuerRole = await DocumentStoreInstance . hasRole ( issuerRole , account . address ) ;
361
+ assert . isTrue ( hasIssuerRole , "Revoker Account has no revoker role" ) ;
362
+
363
+ await DocumentStoreInstance . connect ( account ) . bulkRevoke ( documentMerkleRoots ) ;
364
+ const issued = await DocumentStoreInstance . isRevoked ( documentMerkleRoots [ 0 ] ) ;
365
+
366
+ expect ( issued ) . to . be . true ;
367
+ } ) ;
279
368
} ) ;
280
369
281
370
describe ( "isRevoked" , ( ) => {
0 commit comments