Добавил тесты для создания присоединенной подписи

This commit is contained in:
vgoma 2020-10-17 12:06:10 +03:00
parent e4d93c64dd
commit cfd244df02
5 changed files with 80 additions and 2 deletions

View File

@ -1,2 +1,3 @@
// suppress logging errors to stdout while running tests // suppress logging errors to stdout while running tests
window.console.error = jest.fn(); window.console.error = jest.fn();
window.console.warn = jest.fn();

View File

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

View File

@ -1,4 +1,5 @@
import 'cadesplugin'; import 'cadesplugin';
import 'console-mock';
import { rawCertificates, parsedCertificates } from '../__mocks__/certificates'; import { rawCertificates, parsedCertificates } from '../__mocks__/certificates';
import { createSignature } from './createSignature'; import { createSignature } from './createSignature';
import { _getCadesCert } from '../helpers/_getCadesCert'; import { _getCadesCert } from '../helpers/_getCadesCert';

View File

@ -1,5 +1,5 @@
import 'cadesplugin'; import 'cadesplugin';
import 'console-error'; import 'console-mock';
import { isValidSystemSetup } from './isValidSystemSetup'; import { isValidSystemSetup } from './isValidSystemSetup';
import { getSystemInfo } from './getSystemInfo'; import { getSystemInfo } from './getSystemInfo';
import { _isSupportedCadesVersion } from '../helpers/_isSupportedCadesVersion'; import { _isSupportedCadesVersion } from '../helpers/_isSupportedCadesVersion';

View File

@ -1,5 +1,5 @@
import 'cadesplugin'; import 'cadesplugin';
import 'console-error'; import 'console-mock';
import { _afterPluginsLoaded } from './_afterPluginsLoaded'; import { _afterPluginsLoaded } from './_afterPluginsLoaded';
const cadesPluginMock = window.cadesplugin; const cadesPluginMock = window.cadesplugin;