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;