From bdcb3d7542ef68688820e4c5a20c81db2bd7ffcf Mon Sep 17 00:00:00 2001 From: vgoma Date: Sat, 17 Oct 2020 12:14:34 +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=BE=D1=82=D1=81?= =?UTF-8?q?=D0=BE=D0=B5=D0=B4=D0=B8=D0=BD=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=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 --- src/api/createDetachedSignature.test.ts | 75 +++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/api/createDetachedSignature.test.ts diff --git a/src/api/createDetachedSignature.test.ts b/src/api/createDetachedSignature.test.ts new file mode 100644 index 0000000..4d704e5 --- /dev/null +++ b/src/api/createDetachedSignature.test.ts @@ -0,0 +1,75 @@ +import 'cadesplugin'; +import { rawCertificates, parsedCertificates } from '../__mocks__/certificates'; +import { createDetachedSignature } from './createDetachedSignature'; +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'), + Symbol('step 5'), +]; + +const executionFlow = { + [executionSteps[0]]: { + propset_Name: jest.fn(), + propset_Value: jest.fn(), + }, + [executionSteps[1]]: { + propset_ContentEncoding: jest.fn(), + propset_Content: jest.fn(), + SignHash: 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', + [executionSteps[5]]: { + propset_Algorithm: jest.fn(), + SetHashValue: jest.fn(), + }, +}; + +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]; + case 'CAdESCOM.HashedData': + return executionSteps[5]; + } +}); + +describe('createDetachedSignature', () => { + test('uses specified certificate', async () => { + await createDetachedSignature(parsedCertificateMock.thumbprint, 'message'); + + expect(_getCadesCert).toHaveBeenCalledWith(parsedCertificateMock.thumbprint); + }); + + test('returns signature', async () => { + const signature = await createDetachedSignature(parsedCertificateMock.thumbprint, 'message'); + + expect(signature).toEqual('signature'); + }); +});