From cfd244df021a63e9c39beec20e8730d676df00cb Mon Sep 17 00:00:00 2001 From: vgoma Date: Sat, 17 Oct 2020 12:06:10 +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=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D1=81=D0=BE=D0=B5=D0=B4=D0=B8=D0=BD=D0=B5=D0=BD=D0=BD=D0=BE?= =?UTF-8?q?=D0=B9=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{console-error.ts => console-mock.ts} | 1 + src/api/createAttachedSignature.test.ts | 76 +++++++++++++++++++ src/api/createSignature.test.ts | 1 + src/api/isValidSystemSetup.test.ts | 2 +- src/helpers/_afterPluginsLoaded.test.ts | 2 +- 5 files changed, 80 insertions(+), 2 deletions(-) rename src/__mocks__/{console-error.ts => console-mock.ts} (73%) create mode 100644 src/api/createAttachedSignature.test.ts diff --git a/src/__mocks__/console-error.ts b/src/__mocks__/console-mock.ts similarity index 73% rename from src/__mocks__/console-error.ts rename to src/__mocks__/console-mock.ts index 6905734..64ee7db 100644 --- a/src/__mocks__/console-error.ts +++ b/src/__mocks__/console-mock.ts @@ -1,2 +1,3 @@ // suppress logging errors to stdout while running tests window.console.error = jest.fn(); +window.console.warn = jest.fn(); diff --git a/src/api/createAttachedSignature.test.ts b/src/api/createAttachedSignature.test.ts new file mode 100644 index 0000000..b71d97b --- /dev/null +++ b/src/api/createAttachedSignature.test.ts @@ -0,0 +1,76 @@ +import 'cadesplugin'; +import { rawCertificates, parsedCertificates } from '../__mocks__/certificates'; +import { createAttachedSignature } from './createAttachedSignature'; +import { _getCadesCert } from '../helpers/_getCadesCert'; + +const [rawCertificateMock] = rawCertificates; +const [parsedCertificateMock] = parsedCertificates; + +jest.mock('../helpers/_getCadesCert', () => ({ _getCadesCert: jest.fn(() => rawCertificateMock) })); + +beforeEach(() => { + (_getCadesCert as jest.Mock).mockClear(); +}); + +const executionSteps = [Symbol('step 0'), Symbol('step 1'), Symbol('step 2'), Symbol('step 3'), Symbol('step 4')]; + +const executionFlow = { + [executionSteps[0]]: { + propset_Name: jest.fn(), + propset_Value: jest.fn(), + }, + [executionSteps[1]]: { + propset_ContentEncoding: jest.fn(), + propset_Content: jest.fn(), + SignCades: jest.fn(() => executionSteps[4]), + }, + [executionSteps[2]]: { + propset_Certificate: jest.fn(), + AuthenticatedAttributes2: executionSteps[3], + propset_Options: jest.fn(), + }, + [executionSteps[3]]: { + Add: jest.fn(), + }, + [executionSteps[4]]: 'signature', +}; + +window.cadesplugin.__defineExecutionFlow(executionFlow); +window.cadesplugin.CreateObjectAsync.mockImplementation((object) => { + switch (object) { + case 'CADESCOM.CPAttribute': + return executionSteps[0]; + case 'CAdESCOM.CadesSignedData': + return executionSteps[1]; + case 'CAdESCOM.CPSigner': + return executionSteps[2]; + } +}); + +describe('createAttachedSignature', () => { + test('uses Buffer to encrypt the message', async () => { + const originalBufferFrom = (window as any).Buffer.from; + + (window as any).Buffer.from = jest.fn(() => ({ + toString: jest.fn(), + })); + + await createAttachedSignature(parsedCertificateMock.thumbprint, 'message'); + + expect((window as any).Buffer.from).toHaveBeenCalledTimes(1); + + (window as any).Buffer.from = originalBufferFrom; + }); + + test('uses specified certificate', async () => { + await createAttachedSignature(parsedCertificateMock.thumbprint, 'message'); + + expect(_getCadesCert).toHaveBeenCalledWith(parsedCertificateMock.thumbprint); + }); + + test('returns signature', async () => { + const signature = await createAttachedSignature(parsedCertificateMock.thumbprint, 'message'); + + expect(signature).toEqual('signature'); + }); +}); diff --git a/src/api/createSignature.test.ts b/src/api/createSignature.test.ts index 80cbf46..fcf495d 100644 --- a/src/api/createSignature.test.ts +++ b/src/api/createSignature.test.ts @@ -1,4 +1,5 @@ import 'cadesplugin'; +import 'console-mock'; import { rawCertificates, parsedCertificates } from '../__mocks__/certificates'; import { createSignature } from './createSignature'; import { _getCadesCert } from '../helpers/_getCadesCert'; diff --git a/src/api/isValidSystemSetup.test.ts b/src/api/isValidSystemSetup.test.ts index ff255de..1bac2c3 100644 --- a/src/api/isValidSystemSetup.test.ts +++ b/src/api/isValidSystemSetup.test.ts @@ -1,5 +1,5 @@ import 'cadesplugin'; -import 'console-error'; +import 'console-mock'; import { isValidSystemSetup } from './isValidSystemSetup'; import { getSystemInfo } from './getSystemInfo'; import { _isSupportedCadesVersion } from '../helpers/_isSupportedCadesVersion'; diff --git a/src/helpers/_afterPluginsLoaded.test.ts b/src/helpers/_afterPluginsLoaded.test.ts index a27a7fb..22ce20c 100644 --- a/src/helpers/_afterPluginsLoaded.test.ts +++ b/src/helpers/_afterPluginsLoaded.test.ts @@ -1,5 +1,5 @@ import 'cadesplugin'; -import 'console-error'; +import 'console-mock'; import { _afterPluginsLoaded } from './_afterPluginsLoaded'; const cadesPluginMock = window.cadesplugin;