/
container.spec.ts
1550 lines (1364 loc) · 68.2 KB
/
container.spec.ts
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2018-present evan GmbH.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License, version 3,
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see http://www.gnu.org/licenses/ or
write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA, 02110-1301 USA, or download the license from
the following URL: https://evan.network/license/
*/
import 'mocha';
import * as chaiAsPromised from 'chai-as-promised';
import { expect, use } from 'chai';
import { promisify } from 'util';
import { readFile } from 'fs';
import { identities, useIdentity, accounts } from '../../test/accounts';
import { TestUtils } from '../../test/test-utils';
import { VerificationsStatus } from '../../verifications/verifications';
import {
Container,
ContainerConfig,
ContainerOptions,
ContainerPlugin,
ContainerVerificationEntry,
} from './container';
import { Runtime } from '../../runtime';
use(chaiAsPromised);
describe('Container', function test() {
this.timeout(600000);
const [owner, consumer, otherUser] = identities;
let createRuntime: Function;
let containerConfigOwner: ContainerOptions;
let containerConfigConsumer: ContainerOptions;
let defaultConfig: ContainerConfig;
const description = {
name: 'test container',
description: 'container from test run',
author: 'evan GmbH',
version: '0.1.0',
dbcpVersion: 2,
};
const runtimes: { [id: string]: Runtime } = {};
let sha3: Function;
/**
* Returns the container instance for the consumer based on an existing container.
*
* @param {Container} container container to setup the container runtime for
*/
async function getConsumerContainer(container: Container) {
return new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
}
/**
* Create a test container with the configured test accounts
* adds applied properties and sets random values
*
* @param {string} properties The properties
*/
async function createTestContainerWithProperties(properties: string[]) {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
const randomValues = { };
// setup custom properties
properties.forEach((property) => {
plugin.template.properties[property] = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
});
// create the container with these properties
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig, plugin,
},
);
// set random values
await Promise.all(properties.map(async (property: string) => {
randomValues[property] = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry(property, randomValues[property]);
expect(await container.getEntry(property)).to.eq(randomValues[property]);
}));
return { container, randomValues };
}
before(async () => {
const web3 = await TestUtils.getWeb3();
sha3 = (...args) => web3.utils.soliditySha3(...args);
const sha9 = (accountId1, accountId2) => sha3(...[sha3(accountId1), sha3(accountId2)].sort());
createRuntime = async (accountId, identityId, useIdentityFlag) => {
// data contract instance has sha3 self key and edges to self and other accounts
const requestedKeys = [
sha3(identityId),
...identities.map((partner) => sha9(identityId, partner)),
];
const runtime = await TestUtils.getRuntime(
accountId,
requestedKeys,
{
useIdentity: useIdentityFlag,
},
);
return runtime;
};
for (let i = 0; i < identities.length; i += 1) {
runtimes[identities[i]] = await createRuntime(accounts[i], identities[i], useIdentity);
}
defaultConfig = {
accountId: identities[0],
description,
plugin: 'metadata',
};
// create factory for test
const factory = await runtimes[owner].executor.createContract(
'ContainerDataContractFactory', [], { from: identities[0], gas: 6e6 },
);
defaultConfig.factoryAddress = factory.options.address;
containerConfigOwner = { ...(runtimes[owner] as any) };
containerConfigConsumer = { ...(runtimes[consumer] as any) };
});
describe('when setting entries', async () => {
it('can create new contracts', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
expect(await container.getContractAddress()).to.match(/0x[0-9a-f]{40}/i);
});
it('ensures that dbcpVersion 2 is used', async () => {
// create custom config without dbcpVersion
const customConfig = JSON.parse(JSON.stringify(defaultConfig));
delete customConfig.description.dbcpVersion;
const container = await Container.create(containerConfigOwner, defaultConfig);
const newDesc = await container.getDescription();
expect(newDesc.dbcpVersion).to.be.eq(2);
});
it('can create multiple new contracts in parallel without colliding identities', async () => {
const [container1, container2, container3, container4, container5] = await Promise.all([
Container.create(containerConfigOwner, defaultConfig),
Container.create(containerConfigOwner, defaultConfig),
Container.create(containerConfigOwner, defaultConfig),
Container.create(containerConfigOwner, defaultConfig),
Container.create(containerConfigConsumer, {
accountId: consumer,
description,
plugin: 'metadata',
factoryAddress: defaultConfig.factoryAddress,
}),
]);
const desc1 = await container1.getDescription();
const desc2 = await container2.getDescription();
const desc3 = await container3.getDescription();
const desc4 = await container4.getDescription();
const desc5 = await container5.getDescription();
expect(desc1.identity).to.not.eq(desc2.identity);
expect(desc2.identity).to.not.eq(desc3.identity);
expect(desc1.identity).to.not.eq(desc3.identity);
expect(desc4.identity).to.not.eq(desc3.identity);
expect(desc5.identity).to.not.eq(desc3.identity);
expect(await container1.getContractAddress()).to.match(/0x[0-9a-f]{40}/i);
expect(await container2.getContractAddress()).to.match(/0x[0-9a-f]{40}/i);
expect(await container3.getContractAddress()).to.match(/0x[0-9a-f]{40}/i);
expect(await container4.getContractAddress()).to.match(/0x[0-9a-f]{40}/i);
expect(await container5.getContractAddress()).to.match(/0x[0-9a-f]{40}/i);
const verifications: ContainerVerificationEntry[] = [...Array(3)].map(
(_, i) => ({ topic: `verifcation_${i}` } as ContainerVerificationEntry),
);
for (const container of [container1, container2, container3]) {
await container.addVerifications(verifications);
const verificationsResults = await container.getVerifications();
expect(verificationsResults.length).to.eq(3);
// all verification lists should have at least 1 valid verification
const allValid = verificationsResults.every((vs) => vs.some((v) => v.valid));
expect(allValid).to.be.true;
// all verification should be confirmed, as issuing account is owner
const allConfirmed = verificationsResults.every(
(vs) => vs.some((v) => v.status === VerificationsStatus.Confirmed),
);
expect(allConfirmed).to.be.true;
}
});
it('can get the correct owner for contracts', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
expect(await container.getOwner()).to.be.eq(owner);
});
it('writes plugin type to automatic field "type"', async () => {
const plugin: ContainerPlugin = {
template: {
type: Math.floor(Math.random() * 1e12).toString(36),
properties: {
testField: {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
},
},
},
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
expect(await container.getEntry('type')).to.eq(plugin.template.type);
});
it('can add new entry properties', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('testField', Container.defaultSchemas.stringEntry);
await container.shareProperties([{ accountId: consumer, readWrite: ['testField'] }]);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
const consumerContainer = await getConsumerContainer(container);
await consumerContainer.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
expect(await consumerContainer.getEntry('testField')).to.eq(randomString);
});
it('can set and get entries for properties defined in (custom) plugin', async () => {
await expect(createTestContainerWithProperties(['testField'])).to.be.fulfilled;
});
it('can set entries if not defined in plugin template (auto adds properties)', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
const expectedSchema = {
$id: 'testField_schema',
type: 'string',
};
const containerDescription = await container.getDescription();
expect(containerDescription.dataSchema.testField).to.deep.eq(expectedSchema);
});
it('can handle files', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('sampleFiles', Container.defaultSchemas.filesEntry);
const file = await promisify(readFile)(
`${__dirname}/testfiles/animal-animal-photography-cat-96938.jpg`,
);
const sampleFiles = {
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file,
}],
};
const sampleFilesBackup = {
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file,
}],
};
await container.setEntry('sampleFiles', sampleFiles);
expect(await container.getEntry('sampleFiles')).to.deep.eq(sampleFilesBackup);
});
it('can handle files in complex objects', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.complexItem = {
dataSchema: {
type: 'object',
properties: {
description: Container.defaultSchemas.stringEntry,
images: Container.defaultSchemas.filesEntry,
},
},
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const file = await promisify(readFile)(
`${__dirname}/testfiles/animal-animal-photography-cat-96938.jpg`,
);
const sampleValue = {
description: 'what a cute kitten',
images: {
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file,
}],
},
};
const sampleValueBackup = {
description: 'what a cute kitten',
images: {
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file,
}],
},
};
await container.setEntry('complexItem', sampleValue);
expect(await container.getEntry('complexItem')).to.deep.eq(sampleValueBackup);
});
});
describe('when setting list entries', async () => {
it('can set and get entries for properties defined in (custom) template', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testList = {
dataSchema: { type: 'array', items: { type: 'number' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const randomNumbers = [...Array(8)].map(() => Math.floor(Math.random() * 1e12));
await container.addListEntries('testList', randomNumbers);
expect(await container.getListEntries('testList')).to.deep.eq(randomNumbers);
});
it('can set entries and get single entry for properties defined in (custom) template', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testList = {
dataSchema: { type: 'array', items: { type: 'number' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const randomNumbers = [...Array(8)].map(() => Math.floor(Math.random() * 1e12));
await container.addListEntries('testList', randomNumbers);
expect(await container.getListEntry('testList', 0)).to.deep.eq(randomNumbers[0]);
});
it('can set list entries if not defined in template (auto adds properties)', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.addListEntries('testList', [randomString]);
expect(await container.getListEntries('testList')).to.deep.eq([randomString]);
const expectedSchema = {
$id: 'testList_schema',
type: 'array',
items: { type: 'string' },
};
const containerDescription = await container.getDescription();
expect(containerDescription.dataSchema.testList).to.deep.eq(expectedSchema);
});
it('can add new list properties', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('testList', Container.defaultSchemas.stringList);
await container.shareProperties([{ accountId: consumer, readWrite: ['testList'] }]);
const randomStrings = [...Array(3)].map(() => Math.floor(Math.random() * 1e12).toString(36));
const consumerContainer = await getConsumerContainer(container);
await consumerContainer.addListEntries('testList', randomStrings);
expect(await container.getListEntries('testList')).to.deep.eq(randomStrings);
expect(await consumerContainer.getListEntries('testList')).to.deep.eq(randomStrings);
});
it('can handle files', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('testList', Container.defaultSchemas.filesList);
const file1 = await promisify(readFile)(
`${__dirname}/testfiles/animal-animal-photography-cat-96938.jpg`,
);
const file2 = await promisify(readFile)(
`${__dirname}/testfiles/adorable-animal-animal-photography-774731.jpg`,
);
const sampleFiles = [
{
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file: file1,
}],
},
{
files: [{
name: 'adorable-animal-animal-photography-774731.jpg',
fileType: 'image/jpeg',
file: file2,
}],
},
];
const sampleFilesBackup = [
{
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file: file1,
}],
},
{
files: [{
name: 'adorable-animal-animal-photography-774731.jpg',
fileType: 'image/jpeg',
file: file2,
}],
},
];
await container.addListEntries('testList', sampleFiles);
expect(await container.getListEntries('testList')).to.deep.eq(sampleFilesBackup);
});
it('can handle files in complex objects', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.complexItemList = {
dataSchema: {
type: 'array',
items: {
type: 'object',
properties: {
description: Container.defaultSchemas.stringEntry,
images: Container.defaultSchemas.filesEntry,
},
},
},
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const file1 = await promisify(readFile)(
`${__dirname}/testfiles/animal-animal-photography-cat-96938.jpg`,
);
const file2 = await promisify(readFile)(
`${__dirname}/testfiles/adorable-animal-animal-photography-774731.jpg`,
);
const sampleFiles = [
{
description: 'what a cute kitten',
images: {
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file: file1,
}],
},
},
{
description: 'this one is even cuter',
images: {
files: [{
name: 'adorable-animal-animal-photography-774731.jpg',
fileType: 'image/jpeg',
file: file2,
}],
},
},
];
const sampleFilesBackup = [
{
description: 'what a cute kitten',
images: {
files: [{
name: 'animal-animal-photography-cat-96938.jpg',
fileType: 'image/jpeg',
file: file1,
}],
},
},
{
description: 'this one is even cuter',
images: {
files: [{
name: 'adorable-animal-animal-photography-774731.jpg',
fileType: 'image/jpeg',
file: file2,
}],
},
},
];
await container.addListEntries('complexItemList', sampleFiles);
expect(await container.getListEntries('complexItemList')).to.deep.eq(sampleFilesBackup);
});
});
describe('when working with plugins', async () => {
it('can store current contract as plugin', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testEntry = {
dataSchema: { $id: 'testEntry_schema', type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
plugin.template.properties.testList = {
dataSchema: { $id: 'testList_schema', type: 'array', items: { type: 'number' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const exported = await container.toPlugin();
expect(exported.template).to.deep.eq(plugin.template);
});
it('can store current contract as plugin with values', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testEntry = {
dataSchema: { $id: 'testEntry_schema', type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
plugin.template.properties.testList = {
dataSchema: { $id: 'testList_schema', type: 'array', items: { type: 'number' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const exported = await container.toPlugin(true);
// we do not export values, so remove them
expect(exported.template).to.deep.eq(plugin.template);
});
it('can clone contracts', async () => {
const { container, randomValues } = await createTestContainerWithProperties(['testField']);
const clonedContainer = await Container.clone(
containerConfigOwner,
defaultConfig,
container,
true,
);
expect(await clonedContainer.getEntry('testField')).to.eq((randomValues as any).testField);
});
it('can save plugins to users profile', async () => {
const { profile } = runtimes[owner];
// setup template
const pluginName = 'awesometemplate';
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
// save it to the profile
await Container.saveContainerPlugin(profile, pluginName, plugin);
// load single template
const loadedPlugin = await Container.getContainerPlugin(profile, pluginName);
expect(loadedPlugin).to.deep.equal(plugin);
// load multiple plugins
let plugins = await Container.getContainerPlugins(profile);
expect(plugins).to.have.property(pluginName);
expect(plugins[pluginName]).to.deep.equal(plugin);
// remove template
await Container.deleteContainerPlugin(profile, pluginName);
plugins = await Container.getContainerPlugins(profile);
expect(plugins).to.not.have.property(pluginName);
});
});
describe('when setting multiple entries at once with storeData', async () => {
it('can save data to automatically generated entries', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('testField', Container.defaultSchemas.stringEntry);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
const anotherRandomString = Math.floor(Math.random() * 1e12).toString(36);
await container.storeData({
testField: anotherRandomString,
});
expect(await container.getEntry('testField')).to.eq(anotherRandomString);
});
it('can save data to automatically generated list entries', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('testList', Container.defaultSchemas.numberList);
const randomNumbers = [...Array(3)].map(() => Math.floor(Math.random() * 1e12));
await container.addListEntries('testList', randomNumbers);
expect(await container.getListEntries('testList')).to.deep.eq(randomNumbers);
const moreRandomNumbers = [...Array(3)].map(() => Math.floor(Math.random() * 1e12));
await container.storeData({
testList: moreRandomNumbers,
});
expect(await container.getListEntries('testList')).to.deep.eq([...randomNumbers, ...moreRandomNumbers]);
});
it('can save data to entries from plugins', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.storeData({
testField: randomString,
});
expect(await container.getEntry('testField')).to.eq(randomString);
});
it('can extend list data of lists from plugins', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testList = {
dataSchema: { type: 'array', items: { type: 'number' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(containerConfigOwner, { ...defaultConfig, plugin });
const randomNumbers = [...Array(3)].map(() => Math.floor(Math.random() * 1e12));
await container.addListEntries('testList', randomNumbers);
expect(await container.getListEntries('testList')).to.deep.eq(randomNumbers);
const moreRandomNumbers = [...Array(3)].map(() => Math.floor(Math.random() * 1e12));
await container.storeData({
testList: moreRandomNumbers,
});
expect(await container.getListEntries('testList')).to.deep.eq([...randomNumbers, ...moreRandomNumbers]);
});
it('can implicitly add new entries to data schema', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('testField', Container.defaultSchemas.stringEntry);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.storeData({
testField: randomString,
});
expect(await container.getEntry('testField')).to.eq(randomString);
});
it('can implicitly add new lists to data schema', async () => {
const container = await Container.create(containerConfigOwner, defaultConfig);
await container.ensureProperty('testList', Container.defaultSchemas.numberList);
const randomNumbers = [...Array(3)].map(() => Math.floor(Math.random() * 1e12));
await container.storeData({
testList: randomNumbers,
});
expect(await container.getListEntries('testList')).to.deep.eq([...randomNumbers]);
});
});
describe('when sharing properties', async () => {
describe('when sharing entries', async () => {
it('can share read access a property from owner to another user', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(
containerConfigOwner,
{ ...defaultConfig, plugin },
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
await container.shareProperties([{ accountId: consumer, read: ['testField'] }]);
const consumerContainer = new Container(
containerConfigConsumer,
{
...defaultConfig,
address: await container.getContractAddress(),
accountId: consumer,
},
);
expect(await consumerContainer.getEntry('testField')).to.eq(randomString);
const shareConfig = await container.getContainerShareConfigForAccount(consumer);
expect(shareConfig).to.haveOwnProperty('read');
expect(shareConfig.read).to.include('testField');
});
it('can share write access a property from owner to another user', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
await container.shareProperties([{ accountId: consumer, readWrite: ['testField'] }]);
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
expect(await consumerContainer.getEntry('testField')).to.eq(randomString);
// now change property with invited user
const newRandomString = Math.floor(Math.random() * 1e12).toString(36);
await consumerContainer.setEntry('testField', newRandomString);
expect(await consumerContainer.getEntry('testField')).to.eq(newRandomString);
expect(await container.getEntry('testField')).to.eq(newRandomString);
const shareConfig = await container.getContainerShareConfigForAccount(consumer);
expect(shareConfig).to.haveOwnProperty('readWrite');
expect(shareConfig.readWrite).to.include('testField');
});
});
describe('when sharing lists', async () => {
it('can share read access a property from owner to another user', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testList = {
dataSchema: { type: 'array', items: { type: 'string' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.addListEntries('testList', [randomString]);
expect(await container.getListEntries('testList')).to.deep.eq([randomString]);
await container.shareProperties([{ accountId: consumer, read: ['testList'] }]);
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
expect(await consumerContainer.getListEntries('testList')).to.deep.eq([randomString]);
});
it('can share write access a property from owner to another user', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testList = {
dataSchema: { type: 'array', items: { type: 'string' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.addListEntries('testList', [randomString]);
expect(await container.getListEntries('testList')).to.deep.eq([randomString]);
await container.shareProperties([{ accountId: consumer, readWrite: ['testList'] }]);
await TestUtils.nextBlock(containerConfigConsumer.executor, consumer);
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
expect(await consumerContainer.getListEntries('testList')).to.deep.eq([randomString]);
// now add list entry with invited user
const newRandomString = Math.floor(Math.random() * 1e12).toString(36);
await consumerContainer.addListEntries('testList', [newRandomString]);
expect(await consumerContainer.getListEntries('testList'))
.to.deep.eq([randomString, newRandomString]);
expect(await container.getListEntries('testList'))
.to.deep.eq([randomString, newRandomString]);
});
it('can share remove access a property from owner to another user', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testList = {
dataSchema: { type: 'array', items: { type: 'string' } },
permissions: { 0: ['set'] },
type: 'list',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.addListEntries('testList', [randomString]);
expect(await container.getListEntries('testList')).to.deep.eq([randomString]);
await container.shareProperties([{ accountId: consumer, readWrite: ['testList'], removeListEntries: ['testList'] }]);
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
expect(await consumerContainer.getListEntries('testList')).to.deep.eq([randomString]);
// now add list entry with invited user
const newRandomString = Math.floor(Math.random() * 1e12).toString(36);
await consumerContainer.addListEntries('testList', [newRandomString]);
const containerId = await consumerContainer.getContractAddress();
await containerConfigConsumer.dataContract.removeListEntry(containerId, 'testList', 1, consumer);
expect(await consumerContainer.getListEntries('testList'))
.to.deep.eq([randomString]);
expect(await container.getListEntries('testList'))
.to.deep.eq([randomString]);
});
});
describe('when working on shared containers', async () => {
it('cannot have other user access properties before sharing them', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
const getPromise = consumerContainer.getEntry('testField');
await expect(getPromise).to.be.rejectedWith(new RegExp('^could not get entry', 'i'));
});
it('cannot share access, when member, but not owner of the container', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
await container.shareProperties([{ accountId: consumer, read: ['testField'] }]);
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
expect(await consumerContainer.getEntry('testField')).to.eq(randomString);
const sharePromise = consumerContainer.shareProperties(
[{ accountId: otherUser, read: ['testField'] }],
);
await expect(sharePromise).to.be.rejectedWith(new RegExp(
'^current account "0x[0-9a-f]{40}" is unable to share properties, as it isn\'t owner of '
+ 'the underlying contract "0x[0-9a-f]{40}"$', 'i',
));
});
it('cannot share access from a non-member account to another user', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
await container;
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
const sharePromise = consumerContainer.shareProperties(
[{ accountId: consumer, read: ['testField'] }],
);
await expect(sharePromise).to.be.rejectedWith(
new RegExp('^current account "0x[0-9a-f]{40}" is unable to share properties, as it '
+ 'isn\'t owner of the underlying contract "0x[0-9a-f]{40}"$', 'i'),
);
});
it('can clone a partially shared container from the receiver of a sharing', async () => {
const plugin: ContainerPlugin = JSON.parse(JSON.stringify(Container.plugins.metadata));
plugin.template.properties.testField = {
dataSchema: { type: 'string' },
permissions: { 0: ['set'] },
type: 'entry',
};
const container = await Container.create(
containerConfigOwner,
{
...defaultConfig,
plugin,
},
);
const randomString = Math.floor(Math.random() * 1e12).toString(36);
await container.setEntry('testField', randomString);
expect(await container.getEntry('testField')).to.eq(randomString);
await container.shareProperties([{ accountId: consumer, read: ['testField'] }]);
const consumerContainer = new Container(
containerConfigConsumer,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);
expect(await consumerContainer.getEntry('testField')).to.eq(randomString);
const clonedContainer = await Container.clone(
containerConfigConsumer,
{
...defaultConfig,
accountId: consumer,
},
consumerContainer,
true,
);
expect(await clonedContainer.getEntry('testField')).to.eq(randomString);
});
});
});
describe('when unsharing properties', async () => {
// positive unshare
it('can revoke read access of one property from owner to another user', async () => {
const { container, randomValues } = await createTestContainerWithProperties(['testField']);
await container.shareProperties([{ accountId: consumer, read: ['testField'] }]);
let consumerContainer = await getConsumerContainer(container);
expect(await consumerContainer.getEntry('testField')).to.eq((randomValues as any).testField);
let shareConfig = await container.getContainerShareConfigForAccount(consumer);
expect(shareConfig).not.to.haveOwnProperty('readWrite');
expect(shareConfig).to.haveOwnProperty('read');
expect(shareConfig.read).to.include('testField');
// now unshare
let consumerSharing = ((consumerContainer as any).options as ContainerOptions).sharing;
await container.unshareProperties([{ accountId: consumer, readWrite: ['testField'] }]);
// create new consumer runtime to avoid cached keys, then check result
const newConsumerRuntime = await createRuntime(accounts[1], consumer, useIdentity);
consumerContainer = new Container(
newConsumerRuntime,
{ ...defaultConfig, address: await container.getContractAddress(), accountId: consumer },
);