From 6583f758ef1f3ef044cba6fad21707894b024d4d Mon Sep 17 00:00:00 2001 From: William Calderipe Date: Wed, 28 Feb 2024 16:14:17 +0100 Subject: [PATCH] Add test for dev fixture Refactor message of entity validation issues --- .../src/lib/__test__/unit/dev.fixture.spec.ts | 8 +++ .../src/lib/dev.fixture.ts | 6 -- .../util/__test__/unit/entity.util.spec.ts | 64 +++++++++---------- .../src/lib/util/entity.util.ts | 30 ++++----- 4 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 packages/policy-engine-shared/src/lib/__test__/unit/dev.fixture.spec.ts diff --git a/packages/policy-engine-shared/src/lib/__test__/unit/dev.fixture.spec.ts b/packages/policy-engine-shared/src/lib/__test__/unit/dev.fixture.spec.ts new file mode 100644 index 000000000..a41b316e2 --- /dev/null +++ b/packages/policy-engine-shared/src/lib/__test__/unit/dev.fixture.spec.ts @@ -0,0 +1,8 @@ +import { ENTITIES } from '../../dev.fixture' +import { validate } from '../../util/entity.util' + +describe('dev fixture', () => { + it('defines valid entities', () => { + expect(validate(ENTITIES)).toEqual({ success: true }) + }) +}) diff --git a/packages/policy-engine-shared/src/lib/dev.fixture.ts b/packages/policy-engine-shared/src/lib/dev.fixture.ts index 8234b9cbf..e0a12b579 100644 --- a/packages/policy-engine-shared/src/lib/dev.fixture.ts +++ b/packages/policy-engine-shared/src/lib/dev.fixture.ts @@ -218,12 +218,6 @@ export const ADDRESS_BOOK: AddressBookAccountEntity[] = [ chainId: 137, classification: AccountClassification.WALLET }, - { - id: `eip155:137:${WALLET.Engineering.address}`, - address: WALLET.Engineering.address, - chainId: 137, - classification: AccountClassification.WALLET - }, { id: `eip155:1:${WALLET.Treasury.address}`, address: WALLET.Treasury.address, diff --git a/packages/policy-engine-shared/src/lib/util/__test__/unit/entity.util.spec.ts b/packages/policy-engine-shared/src/lib/util/__test__/unit/entity.util.spec.ts index 38a07bd34..28cc836d6 100644 --- a/packages/policy-engine-shared/src/lib/util/__test__/unit/entity.util.spec.ts +++ b/packages/policy-engine-shared/src/lib/util/__test__/unit/entity.util.spec.ts @@ -22,8 +22,8 @@ describe('validate', () => { ...emptyEntities, userGroupMembers: [ { - groupId: USER_GROUP.Engineering.uid, - userId: USER.Alice.uid + groupId: USER_GROUP.Engineering.id, + userId: USER.Alice.id } ], users: [USER.Alice] @@ -35,7 +35,7 @@ describe('validate', () => { { code: 'ENTITY_NOT_FOUND', message: - "Couldn't create the user group member because the group test-engineering-user-group-uid is undefined" + "couldn't create the user group member because the group test-engineering-user-group-uid is undefined" } ] }) @@ -47,8 +47,8 @@ describe('validate', () => { userGroups: [USER_GROUP.Engineering], userGroupMembers: [ { - groupId: USER_GROUP.Engineering.uid, - userId: USER.Alice.uid + groupId: USER_GROUP.Engineering.id, + userId: USER.Alice.id } ] }) @@ -59,7 +59,7 @@ describe('validate', () => { { code: 'ENTITY_NOT_FOUND', message: - "Couldn't create the user group member for group test-engineering-user-group-uid because the user test-alice-user-uid is undefined" + "couldn't create the user group member for group test-engineering-user-group-uid because the user test-alice-user-uid is undefined" } ] }) @@ -71,8 +71,8 @@ describe('validate', () => { wallets: [WALLET.Engineering], walletGroupMembers: [ { - walletId: WALLET.Engineering.uid, - groupId: WALLET_GROUP.Engineering.uid + walletId: WALLET.Engineering.id, + groupId: WALLET_GROUP.Engineering.id } ] }) @@ -83,7 +83,7 @@ describe('validate', () => { { code: 'ENTITY_NOT_FOUND', message: - "Couldn't create the wallet group member because the group test-engineering-wallet-group-uid is undefined" + "couldn't create the wallet group member because the group test-engineering-wallet-group-uid is undefined" } ] }) @@ -95,8 +95,8 @@ describe('validate', () => { walletGroups: [WALLET_GROUP.Engineering], walletGroupMembers: [ { - walletId: WALLET.Engineering.uid, - groupId: WALLET_GROUP.Engineering.uid + walletId: WALLET.Engineering.id, + groupId: WALLET_GROUP.Engineering.id } ] }) @@ -107,7 +107,7 @@ describe('validate', () => { { code: 'ENTITY_NOT_FOUND', message: - "Couldn't create the wallet group member for group test-engineering-wallet-group-uid because the wallet eip155:eoa:0x22228d0504d4f3363a5b7fda1f5fff1c7bca8ad4 is undefined" + "couldn't create the wallet group member for group test-engineering-wallet-group-uid because the wallet eip155:eoa:0x22228d0504d4f3363a5b7fda1f5fff1c7bca8ad4 is undefined" } ] }) @@ -119,8 +119,8 @@ describe('validate', () => { wallets: [WALLET.Engineering], userWallets: [ { - userId: USER.Alice.uid, - walletId: WALLET.Engineering.uid + userId: USER.Alice.id, + walletId: WALLET.Engineering.id } ] }) @@ -130,7 +130,7 @@ describe('validate', () => { issues: [ { code: 'ENTITY_NOT_FOUND', - message: `Couldn't assign the wallet ${WALLET.Engineering.uid} because the user ${USER.Alice.uid} is undefined` + message: `couldn't assign the wallet ${WALLET.Engineering.id} because the user ${USER.Alice.id} is undefined` } ] }) @@ -142,8 +142,8 @@ describe('validate', () => { users: [USER.Alice], userWallets: [ { - userId: USER.Alice.uid, - walletId: WALLET.Engineering.uid + userId: USER.Alice.id, + walletId: WALLET.Engineering.id } ] }) @@ -153,7 +153,7 @@ describe('validate', () => { issues: [ { code: 'ENTITY_NOT_FOUND', - message: `Couldn't assign the wallet ${WALLET.Engineering.uid} because it's undefined` + message: `couldn't assign the wallet ${WALLET.Engineering.id} because it's undefined` } ] }) @@ -172,7 +172,7 @@ describe('validate', () => { issues: [ { code: 'UNIQUE_IDENTIFIER_DUPLICATION', - message: `The address book account ${ADDRESS_BOOK[0].uid} is duplicated` + message: `the address book account ${ADDRESS_BOOK[0].id} is duplicated` } ] }) @@ -189,7 +189,7 @@ describe('validate', () => { issues: [ { code: 'UNIQUE_IDENTIFIER_DUPLICATION', - message: `The credential ${CREDENTIAL.Alice.uid} is duplicated` + message: `the credential ${CREDENTIAL.Alice.id} is duplicated` } ] }) @@ -206,7 +206,7 @@ describe('validate', () => { issues: [ { code: 'UNIQUE_IDENTIFIER_DUPLICATION', - message: `The token ${TOKEN.usdc1.uid} is duplicated` + message: `the token ${TOKEN.usdc1.id} is duplicated` } ] }) @@ -223,7 +223,7 @@ describe('validate', () => { issues: [ { code: 'UNIQUE_IDENTIFIER_DUPLICATION', - message: `The user group ${USER_GROUP.Engineering.uid} is duplicated` + message: `the user group ${USER_GROUP.Engineering.id} is duplicated` } ] }) @@ -240,7 +240,7 @@ describe('validate', () => { issues: [ { code: 'UNIQUE_IDENTIFIER_DUPLICATION', - message: `The user ${USER.Alice.uid} is duplicated` + message: `the user ${USER.Alice.id} is duplicated` } ] }) @@ -257,7 +257,7 @@ describe('validate', () => { issues: [ { code: 'UNIQUE_IDENTIFIER_DUPLICATION', - message: `The wallet group ${WALLET_GROUP.Engineering.uid} is duplicated` + message: `the wallet group ${WALLET_GROUP.Engineering.id} is duplicated` } ] }) @@ -274,21 +274,21 @@ describe('validate', () => { issues: [ { code: 'UNIQUE_IDENTIFIER_DUPLICATION', - message: `The wallet ${WALLET.Engineering.uid} is duplicated` + message: `the wallet ${WALLET.Engineering.id} is duplicated` } ] }) }) }) - describe('uid format', () => { - it('fails when address book account uid is not an account id', () => { + describe('id format', () => { + it('fails when address book account id is not an account id', () => { const invalidAccountId = '16aba381-c54a-4f72-89bd-bd1e7c46ed29' const result = validate({ ...emptyEntities, addressBook: [ { - uid: invalidAccountId, + id: invalidAccountId, address: WALLET.Engineering.address, chainId: 137, classification: AccountClassification.WALLET @@ -302,20 +302,20 @@ describe('validate', () => { issues: [ { code: 'INVALID_UID_FORMAT', - message: `address book account uid ${invalidAccountId} is not a valid account id` + message: `address book account id ${invalidAccountId} is not a valid account id` } ] }) }) - it('fails when token uid is not an asset id', () => { + it('fails when token id is not an asset id', () => { const invalidAccountId = '16aba381-c54a-4f72-89bd-bd1e7c46ed29' const result = validate({ ...emptyEntities, tokens: [ { ...TOKEN.usdc1, - uid: invalidAccountId + id: invalidAccountId }, TOKEN.usdc137 ] @@ -326,7 +326,7 @@ describe('validate', () => { issues: [ { code: 'INVALID_UID_FORMAT', - message: `token uid ${invalidAccountId} is not a valid asset id` + message: `token id ${invalidAccountId} is not a valid asset id` } ] }) diff --git a/packages/policy-engine-shared/src/lib/util/entity.util.ts b/packages/policy-engine-shared/src/lib/util/entity.util.ts index 7ff928bd6..be265df34 100644 --- a/packages/policy-engine-shared/src/lib/util/entity.util.ts +++ b/packages/policy-engine-shared/src/lib/util/entity.util.ts @@ -23,14 +23,14 @@ const validateUserGroupMemberIntegrity: Validator = (entities: Entities): Valida .filter(({ userId }) => !users[userId]) .map(({ userId, groupId }) => ({ code: 'ENTITY_NOT_FOUND', - message: `Couldn't create the user group member for group ${groupId} because the user ${userId} is undefined` + message: `couldn't create the user group member for group ${groupId} because the user ${userId} is undefined` })) const groupIssues: ValidationIssue[] = entities.userGroupMembers .filter(({ groupId }) => !userGroups[groupId]) .map(({ groupId }) => ({ code: 'ENTITY_NOT_FOUND', - message: `Couldn't create the user group member because the group ${groupId} is undefined` + message: `couldn't create the user group member because the group ${groupId} is undefined` })) return [...userIssues, ...groupIssues] @@ -44,14 +44,14 @@ const validateWalletGroupMemberIntegrity: Validator = (entities: Entities): Vali .filter(({ walletId }) => !wallets[walletId]) .map(({ walletId, groupId }) => ({ code: 'ENTITY_NOT_FOUND', - message: `Couldn't create the wallet group member for group ${groupId} because the wallet ${walletId} is undefined` + message: `couldn't create the wallet group member for group ${groupId} because the wallet ${walletId} is undefined` })) const groupIssues: ValidationIssue[] = entities.walletGroupMembers .filter(({ groupId }) => !walletGroups[groupId]) .map(({ groupId }) => ({ code: 'ENTITY_NOT_FOUND', - message: `Couldn't create the wallet group member because the group ${groupId} is undefined` + message: `couldn't create the wallet group member because the group ${groupId} is undefined` })) return [...walletIssues, ...groupIssues] @@ -65,14 +65,14 @@ const validateUserWalletIntegrity: Validator = (entities: Entities): ValidationI .filter(({ userId }) => !users[userId]) .map(({ userId, walletId }) => ({ code: 'ENTITY_NOT_FOUND', - message: `Couldn't assign the wallet ${walletId} because the user ${userId} is undefined` + message: `couldn't assign the wallet ${walletId} because the user ${userId} is undefined` })) const walletIssues: ValidationIssue[] = entities.userWallets .filter(({ walletId }) => !wallets[walletId]) .map(({ walletId }) => ({ code: 'ENTITY_NOT_FOUND', - message: `Couldn't assign the wallet ${walletId} because it's undefined` + message: `couldn't assign the wallet ${walletId} because it's undefined` })) return [...userIssues, ...walletIssues] @@ -92,13 +92,13 @@ const validateUniqueIdDuplication: Validator = (entities: Entities): ValidationI } return flatten([ - findIssues(entities.addressBook, (id) => `The address book account ${id} is duplicated`), - findIssues(entities.credentials, (id) => `The credential ${id} is duplicated`), - findIssues(entities.tokens, (id) => `The token ${id} is duplicated`), - findIssues(entities.userGroups, (id) => `The user group ${id} is duplicated`), - findIssues(entities.users, (id) => `The user ${id} is duplicated`), - findIssues(entities.walletGroups, (id) => `The wallet group ${id} is duplicated`), - findIssues(entities.wallets, (id) => `The wallet ${id} is duplicated`) + findIssues(entities.addressBook, (id) => `the address book account ${id} is duplicated`), + findIssues(entities.credentials, (id) => `the credential ${id} is duplicated`), + findIssues(entities.tokens, (id) => `the token ${id} is duplicated`), + findIssues(entities.userGroups, (id) => `the user group ${id} is duplicated`), + findIssues(entities.users, (id) => `the user ${id} is duplicated`), + findIssues(entities.walletGroups, (id) => `the wallet group ${id} is duplicated`), + findIssues(entities.wallets, (id) => `the wallet ${id} is duplicated`) ]) } @@ -108,7 +108,7 @@ const validateAddressBookUniqueIdFormat: Validator = (entities: Entities): Valid .map(({ id: uid }) => { return { code: 'INVALID_UID_FORMAT', - message: `address book account uid ${uid} is not a valid account id` + message: `address book account id ${uid} is not a valid account id` } }) } @@ -119,7 +119,7 @@ const validateTokenUniqueIdFormat: Validator = (entities: Entities): ValidationI .map(({ id: uid }) => { return { code: 'INVALID_UID_FORMAT', - message: `token uid ${uid} is not a valid asset id` + message: `token id ${uid} is not a valid asset id` } }) }