From 56e5e2ff179dd7741a5bb35e3934c9b197ba2fc7 Mon Sep 17 00:00:00 2001 From: vgoma Date: Sat, 17 Oct 2020 12:29:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=85=D0=B5=D1=88?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/createAttachedSignature.test.ts | 8 ++--- src/api/createHash.test.ts | 44 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/api/createHash.test.ts diff --git a/src/api/createAttachedSignature.test.ts b/src/api/createAttachedSignature.test.ts index b71d97b..582e192 100644 --- a/src/api/createAttachedSignature.test.ts +++ b/src/api/createAttachedSignature.test.ts @@ -49,17 +49,17 @@ window.cadesplugin.CreateObjectAsync.mockImplementation((object) => { describe('createAttachedSignature', () => { test('uses Buffer to encrypt the message', async () => { - const originalBufferFrom = (window as any).Buffer.from; + const originalBufferFrom = global.Buffer.from; - (window as any).Buffer.from = jest.fn(() => ({ + (global.Buffer.from as jest.Mock) = jest.fn(() => ({ toString: jest.fn(), })); await createAttachedSignature(parsedCertificateMock.thumbprint, 'message'); - expect((window as any).Buffer.from).toHaveBeenCalledTimes(1); + expect(global.Buffer.from).toHaveBeenCalledTimes(1); - (window as any).Buffer.from = originalBufferFrom; + global.Buffer.from = originalBufferFrom; }); test('uses specified certificate', async () => { diff --git a/src/api/createHash.test.ts b/src/api/createHash.test.ts new file mode 100644 index 0000000..baf00af --- /dev/null +++ b/src/api/createHash.test.ts @@ -0,0 +1,44 @@ +import 'cadesplugin'; +import { createHash } from './createHash'; + +const executionSteps = [Symbol('step 0'), Symbol('step 1')]; + +const executionFlow = { + [executionSteps[0]]: { + propset_Algorithm: jest.fn(), + propset_DataEncoding: jest.fn(), + Hash: jest.fn(), + Value: executionSteps[1], + }, + [executionSteps[1]]: 'hash', +}; + +window.cadesplugin.__defineExecutionFlow(executionFlow); +window.cadesplugin.CreateObjectAsync.mockImplementation((object) => { + switch (object) { + case 'CAdESCOM.HashedData': + return executionSteps[0]; + } +}); + +describe('createHash', () => { + test('uses Buffer to encrypt the message', async () => { + const originalBufferFrom = global.Buffer.from; + + (global.Buffer.from as jest.Mock) = jest.fn(() => ({ + toString: jest.fn(), + })); + + await createHash('message'); + + expect(global.Buffer.from).toHaveBeenCalledTimes(1); + + global.Buffer.from = originalBufferFrom; + }); + + test('returns created hash', async () => { + const hash = await createHash('message'); + + expect(hash).toEqual('hash'); + }); +});