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'); + }); +});