passbolt-styleguide 4.6.2 → 4.7.0-alpha-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/css/help.min.css +1 -1
- package/build/css/public.min.css +1 -1
- package/build/css/themes/default/api_authentication.min.css +1 -1
- package/build/css/themes/default/api_cloud.min.css +1 -1
- package/build/css/themes/default/api_main.min.css +1 -1
- package/build/css/themes/default/api_reports.min.css +1 -1
- package/build/css/themes/default/api_webinstaller.min.css +1 -1
- package/build/css/themes/default/ext_app.min.css +1 -1
- package/build/css/themes/default/ext_authentication.min.css +1 -1
- package/build/css/themes/default/ext_external.min.css +1 -1
- package/build/css/themes/default/ext_in_form_cta.min.css +1 -1
- package/build/css/themes/default/ext_in_form_menu.min.css +1 -1
- package/build/css/themes/default/ext_quickaccess.min.css +1 -1
- package/build/css/themes/midgar/api_authentication.min.css +1 -1
- package/build/css/themes/midgar/api_main.min.css +1 -1
- package/build/css/themes/midgar/api_reports.min.css +1 -1
- package/build/css/themes/midgar/ext_app.min.css +1 -1
- package/build/css/themes/midgar/ext_authentication.min.css +1 -1
- package/build/css/themes/midgar/ext_in_form_cta.min.css +1 -1
- package/build/css/themes/midgar/ext_in_form_menu.min.css +1 -1
- package/build/css/themes/midgar/ext_quickaccess.min.css +1 -1
- package/build/js/dist/api-account-recovery.js +1 -1
- package/build/js/dist/api-app.js +1 -1
- package/build/js/dist/api-recover.js +1 -1
- package/build/js/dist/api-setup.js +1 -1
- package/build/js/dist/api-triage.js +1 -1
- package/package.json +1 -1
- package/src/react-extension/components/Administration/DisplayUserDirectoryAdministration/DisplayUserDirectoryAdministration.test.data.js +1 -1
- package/src/react-extension/components/Administration/DisplayUserDirectoryAdministration/DisplayUserDirectoryAdministration.test.js +22 -31
- package/src/react-extension/components/Administration/DisplayUserDirectoryAdministration/DisplayUserDirectoryAdministration.test.page.js +5 -9
- package/src/react-extension/contexts/Administration/AdministrationUserDirectory/AdministrationUserDirectoryContext.test.js +1 -1
- package/src/react-extension/contexts/ApiAccountRecoveryContext.js +3 -3
- package/src/react-extension/contexts/ApiAppContext.js +3 -3
- package/src/react-extension/contexts/ApiRecoverContext.js +3 -3
- package/src/react-extension/contexts/ApiSetupContext.js +3 -3
- package/src/shared/lib/apiClient/apiClient.js +11 -4
- package/src/shared/lib/apiClient/apiClient.test.js +20 -1
- package/src/shared/models/entity/abstract/collectionValidationError.js +57 -0
- package/src/shared/models/entity/abstract/entity.test.data.js +98 -0
- package/src/shared/models/entity/abstract/entityCollection.js +28 -3
- package/src/shared/models/entity/abstract/entityCollection.test.js +41 -60
- package/src/shared/models/entity/abstract/entityCollectionError.js +4 -0
- package/src/shared/models/entity/abstract/entityV2Collection.js +108 -0
- package/src/shared/models/entity/abstract/entityV2Collection.test.data.js +48 -0
- package/src/shared/models/entity/abstract/entityV2Collection.test.js +281 -0
- package/src/shared/models/entity/accountRecovery/accountRecoveryPrivateKeyEntity.test.data.js +76 -0
- package/src/shared/models/entity/accountRecovery/accountRecoveryRequestEntity.test.data.js +102 -0
- package/src/shared/models/entity/accountRecovery/accountRecoveryResponseEntity.test.data.js +93 -0
- package/src/shared/models/entity/accountRecovery/accountRecoveryUserSettingEntity.test.data.js +45 -0
- package/src/shared/models/entity/accountRecovery/pendingAccountRecoveryRequestEntity.test.data.js +25 -0
- package/src/shared/models/entity/avatar/avatarEntity.test.data.js +1 -1
- package/src/shared/models/entity/gpgkey/gpgkeyEntity.test.data.js +31 -0
- package/src/shared/models/entity/profile/ProfileEntity.test.data.js +3 -1
- package/src/shared/models/entity/user/groupUserEntity.test.data.js +0 -3
- package/src/shared/models/entity/user/userEntity.test.data.js +47 -16
- package/src/shared/services/api/auth/AuthLogoutService.js +66 -0
- package/src/shared/services/api/auth/{authService.test.js → AuthLogoutService.test.js} +9 -9
- package/src/shared/services/api/auth/AuthService.js +2 -106
- package/test/assert/assertEntityProperty.js +179 -0
- package/test/fixture/pgpKeys/keys.js +419 -0
|
@@ -11,67 +11,10 @@
|
|
|
11
11
|
* @link https://www.passbolt.com Passbolt(tm)
|
|
12
12
|
* @since 2.13.0
|
|
13
13
|
*/
|
|
14
|
-
import Entity from "./entity";
|
|
15
14
|
import EntityCollection from "./entityCollection";
|
|
16
|
-
import EntitySchema from "./entitySchema";
|
|
17
15
|
import EntityCollectionError from "./entityCollectionError";
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* ===========================================
|
|
21
|
-
* Fixture classes
|
|
22
|
-
* ===========================================
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
class TestEntity extends Entity {
|
|
26
|
-
constructor(dto, options = {}) {
|
|
27
|
-
super(EntitySchema.validate('TestEntity', dto, TestEntity.getSchema()), options);
|
|
28
|
-
}
|
|
29
|
-
get name() {
|
|
30
|
-
return this._props.name;
|
|
31
|
-
}
|
|
32
|
-
static getSchema() {
|
|
33
|
-
return {
|
|
34
|
-
"type": "object",
|
|
35
|
-
"required": [],
|
|
36
|
-
"properties": {
|
|
37
|
-
"id": {
|
|
38
|
-
"anyOf": [{
|
|
39
|
-
"type": "string",
|
|
40
|
-
"format": "uuid"
|
|
41
|
-
}, {
|
|
42
|
-
"type": "null"
|
|
43
|
-
}],
|
|
44
|
-
}, "name": {
|
|
45
|
-
"anyOf": [{
|
|
46
|
-
"type": "string"
|
|
47
|
-
}, {
|
|
48
|
-
"type": "null"
|
|
49
|
-
}],
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/* eslint-disable no-unused-vars */
|
|
57
|
-
class TestEntityCollection extends Entity {
|
|
58
|
-
constructor(dto, options = {}) {
|
|
59
|
-
super(EntitySchema.validate('TestEntityCollection', dto, TestEntityCollection.getSchema()), options);
|
|
60
|
-
}
|
|
61
|
-
static getSchema() {
|
|
62
|
-
return {
|
|
63
|
-
"type": "array",
|
|
64
|
-
"items": TestEntity.getSchema(),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/* eslint-enable no-unused-vars */
|
|
69
|
-
|
|
70
|
-
/*
|
|
71
|
-
* ===========================================
|
|
72
|
-
* Tests
|
|
73
|
-
* ===========================================
|
|
74
|
-
*/
|
|
16
|
+
import {TestEntity} from "./entity.test.data";
|
|
17
|
+
import EntityValidationError from "./entityValidationError";
|
|
75
18
|
|
|
76
19
|
describe("EntityCollection", () => {
|
|
77
20
|
describe("EntityCollection::constructor", () => {
|
|
@@ -366,7 +309,7 @@ describe("EntityCollection", () => {
|
|
|
366
309
|
try {
|
|
367
310
|
collection.assertUniqueByProperty('name');
|
|
368
311
|
} catch (error) {
|
|
369
|
-
expect(error.position).toEqual(
|
|
312
|
+
expect(error.position).toEqual(3);
|
|
370
313
|
expect(error.rule).toEqual('unique_name');
|
|
371
314
|
}
|
|
372
315
|
});
|
|
@@ -381,4 +324,42 @@ describe("EntityCollection", () => {
|
|
|
381
324
|
expect(() => collection.assertUniqueByProperty('name')).toThrow(EntityCollectionError);
|
|
382
325
|
});
|
|
383
326
|
});
|
|
327
|
+
|
|
328
|
+
describe("EntityCollection::assertNotExist", () => {
|
|
329
|
+
it("should not throw if no item exists for the given property and value", () => {
|
|
330
|
+
const collection = new EntityCollection();
|
|
331
|
+
collection.push(new TestEntity({name: 'first'}));
|
|
332
|
+
collection.push(new TestEntity({name: 'second'}));
|
|
333
|
+
collection.push(new TestEntity({name: 'third'}));
|
|
334
|
+
collection.push(new TestEntity({}));
|
|
335
|
+
collection.push(new TestEntity({name: null}));
|
|
336
|
+
|
|
337
|
+
expect.assertions(1);
|
|
338
|
+
expect(() => collection.assertNotExist('name', 'zero')).not.toThrow();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("should throw if an item exists for the given property and value", () => {
|
|
342
|
+
const collection = new EntityCollection();
|
|
343
|
+
collection.push(new TestEntity({name: 'first'}));
|
|
344
|
+
collection.push(new TestEntity({name: 'second'}));
|
|
345
|
+
|
|
346
|
+
expect.assertions(2);
|
|
347
|
+
try {
|
|
348
|
+
collection.assertNotExist('name', 'second');
|
|
349
|
+
} catch (error) {
|
|
350
|
+
expect(error).toBeInstanceOf(EntityValidationError);
|
|
351
|
+
expect(error?.details?.name?.unique).toBeTruthy();
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("should not throw if an item exists for the same property but another value", () => {
|
|
356
|
+
const collection = new EntityCollection();
|
|
357
|
+
collection.push(new TestEntity({name: 'first'}));
|
|
358
|
+
collection.push(new TestEntity({name: 'second'}));
|
|
359
|
+
collection.push(new TestEntity({name: 'second'}));
|
|
360
|
+
|
|
361
|
+
expect.assertions(1);
|
|
362
|
+
expect(() => collection.assertNotExist('name', 'zero')).not.toThrow();
|
|
363
|
+
});
|
|
364
|
+
});
|
|
384
365
|
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passbolt ~ Open source password manager for teams
|
|
3
|
+
* Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
4
|
+
*
|
|
5
|
+
* Licensed under GNU Affero General Public License version 3 of the or any later version.
|
|
6
|
+
* For full copyright and license information, please see the LICENSE.txt
|
|
7
|
+
* Redistributions of files must retain the above copyright notice.
|
|
8
|
+
*
|
|
9
|
+
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
10
|
+
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
|
|
11
|
+
* @link https://www.passbolt.com Passbolt(tm)
|
|
12
|
+
* @since 4.7.0
|
|
13
|
+
*/
|
|
14
|
+
import EntityValidationError from "./entityValidationError";
|
|
15
|
+
import EntityCollection from "./entityCollection";
|
|
16
|
+
import CollectionValidationError from "./collectionValidationError";
|
|
17
|
+
import EntityCollectionError from "./entityCollectionError";
|
|
18
|
+
|
|
19
|
+
class EntityV2Collection extends EntityCollection {
|
|
20
|
+
/**
|
|
21
|
+
* Retrieve the entity class this collection is handling
|
|
22
|
+
* @return {Class}
|
|
23
|
+
*/
|
|
24
|
+
get entityClass() {
|
|
25
|
+
throw new Error("The collection class should declare the entity class that is handled.");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* ==================================================
|
|
30
|
+
* Validation
|
|
31
|
+
* ==================================================
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Validate the collection build rules.
|
|
35
|
+
* @param {Entity} item The entity to validate the build rules for.
|
|
36
|
+
*/
|
|
37
|
+
// eslint-disable-next-line no-unused-vars
|
|
38
|
+
validateBuildRules(item) {
|
|
39
|
+
// Override this method to add entity validation build rules.
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* ==================================================
|
|
44
|
+
* Setters
|
|
45
|
+
* ==================================================
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* Push an item to the list.
|
|
49
|
+
* Note: This method overrides the parent method and does not call the inherited version.
|
|
50
|
+
*
|
|
51
|
+
* @param {object|Entity} data The data of the item to push
|
|
52
|
+
* @param {object} [entityOptions] Options for constructing the entity, identical to those accepted by the Entity
|
|
53
|
+
* constructor that will be utilized for its creation.
|
|
54
|
+
* @throws {EntityValidationError} If the item doesn't validate.
|
|
55
|
+
*/
|
|
56
|
+
push(data, entityOptions = {}) {
|
|
57
|
+
if (!data || typeof data !== 'object') {
|
|
58
|
+
throw new TypeError(`Collection push expects "data" to be an object.`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (data instanceof this.entityClass) {
|
|
62
|
+
data = data.toDto(this.entityClass?.ALL_CONTAIN_OPTIONS); // deep clone
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const entity = new this.entityClass(data, entityOptions);
|
|
66
|
+
this.validateBuildRules(entity);
|
|
67
|
+
this._items.push(entity);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Push multiple items to the list.
|
|
72
|
+
* @param {object|Entity|array} data The item(s) to add to the collection should be in the form of a DTO, an entity,
|
|
73
|
+
* or an array comprising any of the aforementioned.
|
|
74
|
+
* @param {object} [entityOptions] Options for constructing the entity, identical to those accepted by the Entity
|
|
75
|
+
* constructor that will be utilized for its creation.
|
|
76
|
+
* @throws {CollectionValidationError} If one item doesn't validate.
|
|
77
|
+
*/
|
|
78
|
+
pushMany(data, entityOptions = {}) {
|
|
79
|
+
if (!Array.isArray(data)) {
|
|
80
|
+
throw new TypeError(`${this.constructor.name} pushMany expects "data" to be an array.`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
data.forEach((itemDto, index) => {
|
|
84
|
+
try {
|
|
85
|
+
this.push(itemDto, entityOptions);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (error instanceof EntityValidationError || error instanceof CollectionValidationError || error instanceof EntityCollectionError) {
|
|
88
|
+
if (!entityOptions?.ignoreInvalidEntity) {
|
|
89
|
+
/*
|
|
90
|
+
* The validation process for checking entity associations in the collection is functional. However, the error
|
|
91
|
+
* details provided is not fully detailed. While it identifies the correct data item that fails validation in
|
|
92
|
+
* the collection, it fails to clearly indicate which specific property of the parent entity is problematic.
|
|
93
|
+
*/
|
|
94
|
+
const collectionValidationError = new CollectionValidationError();
|
|
95
|
+
collectionValidationError.addEntityValidationError(index, error);
|
|
96
|
+
throw collectionValidationError;
|
|
97
|
+
} else {
|
|
98
|
+
console.debug(`${this.entityClass.name}::pushMany ignore item (${index}) due to validation error ${JSON.stringify(error?.details)}`);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default EntityV2Collection;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passbolt ~ Open source password manager for teams
|
|
3
|
+
* Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
4
|
+
*
|
|
5
|
+
* Licensed under GNU Affero General Public License version 3 of the or any later version.
|
|
6
|
+
* For full copyright and license information, please see the LICENSE.txt
|
|
7
|
+
* Redistributions of files must retain the above copyright notice.
|
|
8
|
+
*
|
|
9
|
+
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
10
|
+
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
|
|
11
|
+
* @link https://www.passbolt.com Passbolt(tm)
|
|
12
|
+
* @since 4.7.0
|
|
13
|
+
*/
|
|
14
|
+
import EntitySchema from "./entitySchema";
|
|
15
|
+
import EntityV2Collection from "./entityV2Collection";
|
|
16
|
+
import {TestEntity} from "./entity.test.data";
|
|
17
|
+
|
|
18
|
+
export class TestEntityV2Collection extends EntityV2Collection {
|
|
19
|
+
get entityClass() {
|
|
20
|
+
return TestEntity;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
constructor(dto, options = {}) {
|
|
24
|
+
super(EntitySchema.validate(TestEntityV2Collection.name, dto, TestEntityV2Collection.getSchema()), options);
|
|
25
|
+
this.pushMany(this._props, {...options, clone: false});
|
|
26
|
+
this._props = null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Get the collection schema
|
|
31
|
+
* @returns {object}
|
|
32
|
+
*/
|
|
33
|
+
static getSchema() {
|
|
34
|
+
return {
|
|
35
|
+
"type": "array",
|
|
36
|
+
"items": TestEntity.getSchema(),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Validate the collection build rules.
|
|
42
|
+
* @param {Entity} item The entity to validate the build rules for.
|
|
43
|
+
* @throws {EntityValidationError} If an item already exists with the same id.
|
|
44
|
+
*/
|
|
45
|
+
validateBuildRules(item) {
|
|
46
|
+
this.assertNotExist("id", item._props.id);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passbolt ~ Open source password manager for teams
|
|
3
|
+
* Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
4
|
+
*
|
|
5
|
+
* Licensed under GNU Affero General Public License version 3 of the or any later version.
|
|
6
|
+
* For full copyright and license information, please see the LICENSE.txt
|
|
7
|
+
* Redistributions of files must retain the above copyright notice.
|
|
8
|
+
*
|
|
9
|
+
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
10
|
+
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
|
|
11
|
+
* @link https://www.passbolt.com Passbolt(tm)
|
|
12
|
+
* @since 4.7.0
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import CollectionValidationError from "./collectionValidationError";
|
|
16
|
+
import EntityValidationError from "./entityValidationError";
|
|
17
|
+
import {TestEntityV2Collection} from "./entityV2Collection.test.data";
|
|
18
|
+
import {defaultAssociatedTestEntityDto, defaultTestEntityDto, TestEntity} from "./entity.test.data";
|
|
19
|
+
|
|
20
|
+
describe("EntityV2Collection", () => {
|
|
21
|
+
describe("EntityV2Collection:push", () => {
|
|
22
|
+
it("should throw an exception if the data parameter is not an object.", () => {
|
|
23
|
+
const collection = new TestEntityV2Collection([]);
|
|
24
|
+
expect.assertions(1);
|
|
25
|
+
expect(() => collection.push(42)).toThrow(TypeError);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should accept dto as data parameter", () => {
|
|
29
|
+
const collection = new TestEntityV2Collection([]);
|
|
30
|
+
const entityDto1 = defaultTestEntityDto();
|
|
31
|
+
const entityDto2 = defaultTestEntityDto();
|
|
32
|
+
const entityDto3 = defaultTestEntityDto();
|
|
33
|
+
|
|
34
|
+
expect.assertions(12);
|
|
35
|
+
collection.push(entityDto1);
|
|
36
|
+
expect(collection.items).toHaveLength(1);
|
|
37
|
+
expect(collection.items[0]).toBeInstanceOf(TestEntity);
|
|
38
|
+
expect(collection.items[0].id).toEqual(entityDto1.id);
|
|
39
|
+
expect(collection.items[0].name).toEqual(entityDto1.name);
|
|
40
|
+
collection.push(entityDto2);
|
|
41
|
+
expect(collection).toHaveLength(2);
|
|
42
|
+
expect(collection.items[1]).toBeInstanceOf(TestEntity);
|
|
43
|
+
expect(collection.items[1].id).toEqual(entityDto2.id);
|
|
44
|
+
expect(collection.items[1].name).toEqual(entityDto2.name);
|
|
45
|
+
collection.push(entityDto3);
|
|
46
|
+
expect(collection).toHaveLength(3);
|
|
47
|
+
expect(collection.items[2]).toBeInstanceOf(TestEntity);
|
|
48
|
+
expect(collection.items[2].id).toEqual(entityDto3.id);
|
|
49
|
+
expect(collection.items[2].name).toEqual(entityDto3.name);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should protect collection entities against original data alteration", () => {
|
|
53
|
+
const collection = new TestEntityV2Collection([]);
|
|
54
|
+
const entityDto1 = defaultTestEntityDto();
|
|
55
|
+
|
|
56
|
+
expect.assertions(2);
|
|
57
|
+
collection.push(entityDto1);
|
|
58
|
+
const originalDtoValue = entityDto1.name;
|
|
59
|
+
expect(collection.items[0]._props.name).toEqual(originalDtoValue);
|
|
60
|
+
entityDto1.name = "name altered";
|
|
61
|
+
expect(collection.items[0]._props.name).toEqual(originalDtoValue);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should accept entity as data parameter", () => {
|
|
65
|
+
const collection = new TestEntityV2Collection([]);
|
|
66
|
+
const entity1 = new TestEntity(defaultTestEntityDto());
|
|
67
|
+
const entity2 = new TestEntity(defaultTestEntityDto());
|
|
68
|
+
const entity3 = new TestEntity(defaultTestEntityDto());
|
|
69
|
+
|
|
70
|
+
expect.assertions(12);
|
|
71
|
+
collection.push(entity1);
|
|
72
|
+
expect(collection.items).toHaveLength(1);
|
|
73
|
+
expect(collection.items[0]).toBeInstanceOf(TestEntity);
|
|
74
|
+
expect(collection.items[0].id).toEqual(entity1.id);
|
|
75
|
+
expect(collection.items[0].name).toEqual(entity1.name);
|
|
76
|
+
collection.push(entity2);
|
|
77
|
+
expect(collection).toHaveLength(2);
|
|
78
|
+
expect(collection.items[1]).toBeInstanceOf(TestEntity);
|
|
79
|
+
expect(collection.items[1].id).toEqual(entity2.id);
|
|
80
|
+
expect(collection.items[1].name).toEqual(entity2.name);
|
|
81
|
+
collection.push(entity3);
|
|
82
|
+
expect(collection).toHaveLength(3);
|
|
83
|
+
expect(collection.items[2]).toBeInstanceOf(TestEntity);
|
|
84
|
+
expect(collection.items[2].id).toEqual(entity3.id);
|
|
85
|
+
expect(collection.items[2].name).toEqual(entity3.name);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should throw if the data does not validate the collection entity schema", () => {
|
|
89
|
+
const collection = new TestEntityV2Collection([]);
|
|
90
|
+
const dto = defaultTestEntityDto({id: 42});
|
|
91
|
+
|
|
92
|
+
expect.assertions(2);
|
|
93
|
+
try {
|
|
94
|
+
collection.push(dto);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
expect(error).toBeInstanceOf(EntityValidationError);
|
|
97
|
+
expect(error.details.id.type).toBeTruthy();
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should throw if the build rules do not validate", () => {
|
|
102
|
+
const collection = new TestEntityV2Collection([]);
|
|
103
|
+
const entity1 = new TestEntity(defaultTestEntityDto());
|
|
104
|
+
const entity2 = new TestEntity(defaultTestEntityDto());
|
|
105
|
+
const entity3 = new TestEntity(defaultTestEntityDto({id: entity1.id}));
|
|
106
|
+
|
|
107
|
+
expect.assertions(4);
|
|
108
|
+
expect(() => collection.push(entity1)).not.toThrow();
|
|
109
|
+
expect(() => collection.push(entity2)).not.toThrow();
|
|
110
|
+
try {
|
|
111
|
+
collection.push(entity3);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
expect(error).toBeInstanceOf(EntityValidationError);
|
|
114
|
+
expect(error.details.id.unique).toBeTruthy();
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/*
|
|
119
|
+
* This test is expected to fail as the data is copied even though the original dto is not cloned.
|
|
120
|
+
* Any change on the schema validation might change the default behavior of Entity and not protect by default
|
|
121
|
+
* the entities against original data alteraiton.
|
|
122
|
+
*/
|
|
123
|
+
it.failing("should, with disabling clone option, not protect collection entities against original data alteration", () => {
|
|
124
|
+
const collection = new TestEntityV2Collection([]);
|
|
125
|
+
const dto = defaultTestEntityDto();
|
|
126
|
+
|
|
127
|
+
expect.assertions(2);
|
|
128
|
+
collection.push(dto, {clone: false});
|
|
129
|
+
expect(collection.items[0]._props.name).toEqual(dto.name);
|
|
130
|
+
dto.name = "name altered";
|
|
131
|
+
expect(collection.items[0]._props.name).toEqual(dto.name);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
/*
|
|
135
|
+
* Same comment as the previous test
|
|
136
|
+
*/
|
|
137
|
+
it.failing("should, with disabling clone option, not protect collection entities against original entity alteration", () => {
|
|
138
|
+
const collection = new TestEntityV2Collection([]);
|
|
139
|
+
const entity = new TestEntity(defaultTestEntityDto());
|
|
140
|
+
|
|
141
|
+
expect.assertions(2);
|
|
142
|
+
collection.push(entity, {clone: false});
|
|
143
|
+
expect(collection.items[0]._props.name).toEqual(entity.name);
|
|
144
|
+
entity._props.name = "name altered";
|
|
145
|
+
expect(collection.items[0]._props.name).toEqual(entity.name);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// @todo do we want this capability on this function?
|
|
149
|
+
it.todo("should ignore invalid content");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("GroupsCollection:pushMany", () => {
|
|
153
|
+
it("should throw an exception if the data parameter is not an array.", () => {
|
|
154
|
+
const collection = new TestEntityV2Collection([]);
|
|
155
|
+
expect.assertions(1);
|
|
156
|
+
expect(() => collection.pushMany(42)).toThrow(TypeError);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("should accept array of dtos as data parameter", () => {
|
|
160
|
+
const collection = new TestEntityV2Collection([]);
|
|
161
|
+
const entityDto1 = defaultTestEntityDto();
|
|
162
|
+
const entityDto2 = defaultTestEntityDto();
|
|
163
|
+
const entityDto3 = defaultTestEntityDto();
|
|
164
|
+
|
|
165
|
+
expect.assertions(10);
|
|
166
|
+
collection.pushMany([entityDto1, entityDto2, entityDto3]);
|
|
167
|
+
expect(collection.items).toHaveLength(3);
|
|
168
|
+
expect(collection.items[0]).toBeInstanceOf(TestEntity);
|
|
169
|
+
expect(collection.items[0].id).toEqual(entityDto1.id);
|
|
170
|
+
expect(collection.items[0].name).toEqual(entityDto1.name);
|
|
171
|
+
expect(collection.items[1]).toBeInstanceOf(TestEntity);
|
|
172
|
+
expect(collection.items[1].id).toEqual(entityDto2.id);
|
|
173
|
+
expect(collection.items[1].name).toEqual(entityDto2.name);
|
|
174
|
+
expect(collection.items[2]).toBeInstanceOf(TestEntity);
|
|
175
|
+
expect(collection.items[2].id).toEqual(entityDto3.id);
|
|
176
|
+
expect(collection.items[2].name).toEqual(entityDto3.name);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("should accept array of entities as data parameter", () => {
|
|
180
|
+
const collection = new TestEntityV2Collection([]);
|
|
181
|
+
const entity1 = new TestEntity(defaultTestEntityDto());
|
|
182
|
+
const entity2 = new TestEntity(defaultTestEntityDto());
|
|
183
|
+
const entity3 = new TestEntity(defaultTestEntityDto());
|
|
184
|
+
|
|
185
|
+
expect.assertions(10);
|
|
186
|
+
collection.pushMany([entity1, entity2, entity3]);
|
|
187
|
+
expect(collection.items).toHaveLength(3);
|
|
188
|
+
expect(collection.items[0]).toBeInstanceOf(TestEntity);
|
|
189
|
+
expect(collection.items[0].id).toEqual(entity1.id);
|
|
190
|
+
expect(collection.items[0].name).toEqual(entity1.name);
|
|
191
|
+
expect(collection.items[1]).toBeInstanceOf(TestEntity);
|
|
192
|
+
expect(collection.items[1].id).toEqual(entity2.id);
|
|
193
|
+
expect(collection.items[1].name).toEqual(entity2.name);
|
|
194
|
+
expect(collection.items[2]).toBeInstanceOf(TestEntity);
|
|
195
|
+
expect(collection.items[2].id).toEqual(entity3.id);
|
|
196
|
+
expect(collection.items[2].name).toEqual(entity3.name);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should throw if the data does not validate the collection entity schema", () => {
|
|
200
|
+
const collection = new TestEntityV2Collection([]);
|
|
201
|
+
const entityDto1 = defaultTestEntityDto();
|
|
202
|
+
const entityDto2 = defaultTestEntityDto({id: 42});
|
|
203
|
+
|
|
204
|
+
expect.assertions(2);
|
|
205
|
+
try {
|
|
206
|
+
collection.pushMany([entityDto1, entityDto2]);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
expect(error).toBeInstanceOf(CollectionValidationError);
|
|
209
|
+
expect(error.details?.[1]?.id?.type).toBeTruthy();
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
/*
|
|
214
|
+
* The validation process for checking entity associations in the collection is functional. However, the error
|
|
215
|
+
* details provided is not fully detailed. While it identifies the correct data item that fails validation in
|
|
216
|
+
* the collection, it fails to clearly indicate which specific property of the parent entity is problematic.
|
|
217
|
+
* @todo Associated entities validation error details to review when collection will aggregate them.
|
|
218
|
+
*/
|
|
219
|
+
it("should throw if the data does not validate the collection entity associated entity schema", () => {
|
|
220
|
+
const collection = new TestEntityV2Collection([]);
|
|
221
|
+
const entityDto1 = defaultTestEntityDto();
|
|
222
|
+
const entityDto2 = defaultTestEntityDto({associated_entity: defaultAssociatedTestEntityDto({id: 42})});
|
|
223
|
+
|
|
224
|
+
expect.assertions(3);
|
|
225
|
+
try {
|
|
226
|
+
collection.pushMany([entityDto1, entityDto2]);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
expect(error).toBeInstanceOf(CollectionValidationError);
|
|
229
|
+
// This expect should be falsy.
|
|
230
|
+
expect(error.details?.[1]?.id?.type).toBeTruthy();
|
|
231
|
+
// This expect should be truthy.
|
|
232
|
+
expect(error.details?.[1]?.associated_entity?.id?.type).toBeFalsy();
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("should throw if the build rules do not validate", () => {
|
|
237
|
+
const collection = new TestEntityV2Collection([]);
|
|
238
|
+
const entity1 = new TestEntity(defaultTestEntityDto());
|
|
239
|
+
const entity2 = new TestEntity(defaultTestEntityDto({id: entity1.id}));
|
|
240
|
+
const entity3 = new TestEntity(defaultTestEntityDto());
|
|
241
|
+
|
|
242
|
+
expect.assertions(2);
|
|
243
|
+
try {
|
|
244
|
+
collection.pushMany([entity1, entity2, entity3]);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
expect(error).toBeInstanceOf(CollectionValidationError);
|
|
247
|
+
expect(error.details?.[1]?.id?.unique).toBeTruthy();
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("should, with enabling the ignore invalid option, ignore entities not validating their schema", () => {
|
|
252
|
+
const collection = new TestEntityV2Collection([]);
|
|
253
|
+
const entity1 = defaultTestEntityDto();
|
|
254
|
+
const entity2 = defaultTestEntityDto({id: 42});
|
|
255
|
+
const entity3 = defaultTestEntityDto();
|
|
256
|
+
|
|
257
|
+
expect.assertions(5);
|
|
258
|
+
collection.pushMany([entity1, entity2, entity3], {ignoreInvalidEntity: true});
|
|
259
|
+
expect(collection.items).toHaveLength(2);
|
|
260
|
+
expect(collection.items[0]).toBeInstanceOf(TestEntity);
|
|
261
|
+
expect(collection.items[0].id).toEqual(entity1.id);
|
|
262
|
+
expect(collection.items[1]).toBeInstanceOf(TestEntity);
|
|
263
|
+
expect(collection.items[1].id).toEqual(entity3.id);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("should, with enabling the ignore invalid option, ignore entities not validating the collection build rules", () => {
|
|
267
|
+
const collection = new TestEntityV2Collection([]);
|
|
268
|
+
const entity1 = defaultTestEntityDto();
|
|
269
|
+
const entity2 = defaultTestEntityDto({id: entity1.id});
|
|
270
|
+
const entity3 = defaultTestEntityDto();
|
|
271
|
+
|
|
272
|
+
expect.assertions(5);
|
|
273
|
+
collection.pushMany([entity1, entity2, entity3], {ignoreInvalidEntity: true});
|
|
274
|
+
expect(collection.items).toHaveLength(2);
|
|
275
|
+
expect(collection.items[0]).toBeInstanceOf(TestEntity);
|
|
276
|
+
expect(collection.items[0].id).toEqual(entity1.id);
|
|
277
|
+
expect(collection.items[1]).toBeInstanceOf(TestEntity);
|
|
278
|
+
expect(collection.items[1].id).toEqual(entity3.id);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
});
|