From d00dcddaf69eae26445f7b2ac1fe79b0027e297c Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Thu, 19 Dec 2019 13:28:56 +0200 Subject: [PATCH] fix: ValidateMessage can throw an error Fixes: #10 --- packages/daf-core/src/core.ts | 4 +- packages/daf-core/src/index.ts | 2 +- .../abstract-message-validator.test.ts | 48 +++++++++++++++++++ ...dator.ts => abstract-message-validator.ts} | 4 +- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 packages/daf-core/src/message/__tests__/abstract-message-validator.test.ts rename packages/daf-core/src/message/{message-validator.ts => abstract-message-validator.ts} (84%) diff --git a/packages/daf-core/src/core.ts b/packages/daf-core/src/core.ts index 66814f7a2..ea1f04fe5 100644 --- a/packages/daf-core/src/core.ts +++ b/packages/daf-core/src/core.ts @@ -3,7 +3,7 @@ import { DIDDocument } from 'did-resolver' import { IdentityManager, IdentityController } from './identity/identity-manager' import { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager' import { ServiceControllerDerived } from './service/abstract-service-controller' -import { MessageValidator } from './message/message-validator' +import { MessageValidator, unsupportedMessageTypeError } from './message/abstract-message-validator' import { ActionHandler } from './action/action-handler' import { Action } from './types' import { EncryptionKeyManager } from './encryption-manager' @@ -99,7 +99,7 @@ export class Core extends EventEmitter { this.emit(EventTypes.error, error, message) return Promise.reject(error) } - return Promise.reject('Unsupported message type') + return Promise.reject(unsupportedMessageTypeError) } public async handleAction(action: Action): Promise { diff --git a/packages/daf-core/src/index.ts b/packages/daf-core/src/index.ts index b52120588..7fa91c7d7 100644 --- a/packages/daf-core/src/index.ts +++ b/packages/daf-core/src/index.ts @@ -2,7 +2,7 @@ export { Core, EventTypes, Resolver } from './core' export { AbstractActionHandler } from './action/action-handler' export { EncryptionKeyManager, KeyPair } from './encryption-manager' export { IdentityController, IdentityManager, Issuer } from './identity/identity-manager' -export { AbstractMessageValidator } from './message/message-validator' +export { AbstractMessageValidator } from './message/abstract-message-validator' export { Message } from './message/message' export { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager' export { AbstractServiceController } from './service/abstract-service-controller' diff --git a/packages/daf-core/src/message/__tests__/abstract-message-validator.test.ts b/packages/daf-core/src/message/__tests__/abstract-message-validator.test.ts new file mode 100644 index 000000000..994831081 --- /dev/null +++ b/packages/daf-core/src/message/__tests__/abstract-message-validator.test.ts @@ -0,0 +1,48 @@ +import { AbstractMessageValidator, unsupportedMessageTypeError } from '../abstract-message-validator' +import { Core } from '../../core' +import { Message } from '../message' + +class MockMessageValidator extends AbstractMessageValidator { + async validate(message: Message, core: Core) { + if (message.raw === 'mock') { + message.type = 'mock' + return message + } + return super.validate(message, core) + } +} + +class MockMessageValidatorWithError extends AbstractMessageValidator { + async validate(message: Message, core: Core) { + // This simulates a scenario when validation process encounters an error, + // such as a network error + + throw Error('Network error') + + return message + } +} + +it('should return a promise and resolve it if the massage is of known type', async () => { + const msg = new Message({ raw: 'mock', meta: { type: 'test' } }) + const validator = new MockMessageValidator() + const validated = await validator.validate(msg, null) + expect(validated.type).toEqual('mock') + expect(validated.isValid()).toEqual(true) +}) + +it('should return a promise and reject it if the massage is of unknown type', async () => { + const msg = new Message({ raw: 'unknown', meta: { type: 'test2' } }) + const validator = new MockMessageValidator() + await expect(validator.validate(msg, null)).rejects.toEqual(unsupportedMessageTypeError) +}) + +it('can throw an error', async () => { + const msg = new Message({ raw: 'mock', meta: { type: 'test3' } }) + const validator = new MockMessageValidatorWithError() + try { + const validated = await validator.validate(msg, null) + } catch (e) { + expect(e !== unsupportedMessageTypeError).toEqual(true) + } +}) diff --git a/packages/daf-core/src/message/message-validator.ts b/packages/daf-core/src/message/abstract-message-validator.ts similarity index 84% rename from packages/daf-core/src/message/message-validator.ts rename to packages/daf-core/src/message/abstract-message-validator.ts index c650ef7ba..80d7a6fec 100644 --- a/packages/daf-core/src/message/message-validator.ts +++ b/packages/daf-core/src/message/abstract-message-validator.ts @@ -6,6 +6,8 @@ export interface MessageValidator { validate: (message: Message, core: Core) => Promise } +export const unsupportedMessageTypeError = 'Unsupported message type' + export abstract class AbstractMessageValidator implements MessageValidator { public nextMessageValidator?: MessageValidator @@ -18,6 +20,6 @@ export abstract class AbstractMessageValidator implements MessageValidator { if (this.nextMessageValidator) { return this.nextMessageValidator.validate(message, core) } - return Promise.reject('Unsupported message type') + return Promise.reject(unsupportedMessageTypeError) } }