add getPluginVersion & getCspVersion methods

This commit is contained in:
Alexander Zakharov 2021-05-12 20:53:44 +03:00
parent 4f29faf3c3
commit a0d60b0de8
6 changed files with 158 additions and 2 deletions

4
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "crypto-pro",
"version": "2.2.0",
"name": "@gaarutyunov/crypto-pro-fork",
"version": "2.2.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -0,0 +1,35 @@
import 'cadesplugin';
import { getCspVersion } from './getCspVersion';
const executionSteps = [
Symbol('step 0'),
Symbol('step 1'),
Symbol('step 2'),
Symbol('step 3'),
Symbol('step 4'),
Symbol('step 5'),
];
// "any" because of using toString on the literal
const executionFlow: any = {
[executionSteps[0]]: {
CSPVersion: jest.fn(() => executionSteps[1]),
},
[executionSteps[1]]: {
toString: jest.fn(() => executionSteps[2]),
},
[executionSteps[2]]: '4.0.9971',
};
window.cadesplugin.__defineExecutionFlow(executionFlow);
window.cadesplugin.CreateObjectAsync.mockImplementation(
() => executionSteps[0],
);
describe('getCspVersion', () => {
test('returns information about CSP', async () => {
const cspVersion = await getCspVersion();
expect(cspVersion).toStrictEqual('4.0.9971');
});
});

40
src/api/getCspVersion.ts Normal file
View File

@ -0,0 +1,40 @@
import { _afterPluginsLoaded } from '../helpers/_afterPluginsLoaded';
import { _extractMeaningfulErrorMessage } from '../helpers/_extractMeaningfulErrorMessage';
import {
__cadesAsyncToken__,
__createCadesPluginObject__,
_generateCadesFn,
} from '../helpers/_generateCadesFn';
/**
* Предоставляет информацию о системе
*
* @returns информацию о CSP
*/
export const getCspVersion = _afterPluginsLoaded((): string => {
let cspVersion = null;
return eval(
_generateCadesFn(function getCspVersion(): string {
let cadesAbout;
try {
cadesAbout =
__cadesAsyncToken__ + __createCadesPluginObject__('CAdESCOM.About');
cspVersion = __cadesAsyncToken__ + cadesAbout.CSPVersion();
cspVersion = __cadesAsyncToken__ + cspVersion.toString();
} catch (error) {
console.error(error);
throw new Error(
_extractMeaningfulErrorMessage(error) ||
'Ошибка при получении версии системы',
);
}
return cspVersion;
}),
);
});

View File

@ -0,0 +1,35 @@
import 'cadesplugin';
import { getPluginVersion } from './getPluginVersion';
const executionSteps = [
Symbol('step 0'),
Symbol('step 1'),
Symbol('step 2'),
Symbol('step 3'),
];
// "any" because of using toString on the literal
const executionFlow: any = {
[executionSteps[0]]: {
PluginVersion: executionSteps[1],
Version: executionSteps[2],
},
[executionSteps[1]]: undefined,
[executionSteps[2]]: {
toString: jest.fn(() => executionSteps[3]),
},
[executionSteps[3]]: '2.0.13771',
};
window.cadesplugin.__defineExecutionFlow(executionFlow);
window.cadesplugin.CreateObjectAsync.mockImplementation(
() => executionSteps[0],
);
describe('getPluginVersion', () => {
test('returns information about plugin', async () => {
const pluginVersion = await getPluginVersion();
expect(pluginVersion).toStrictEqual('2.0.13771');
});
});

View File

@ -0,0 +1,44 @@
import { _afterPluginsLoaded } from '../helpers/_afterPluginsLoaded';
import {
__cadesAsyncToken__,
__createCadesPluginObject__,
_generateCadesFn,
} from '../helpers/_generateCadesFn';
import { _extractMeaningfulErrorMessage } from '../helpers/_extractMeaningfulErrorMessage';
/**
* Предоставляет информацию о системе
*
* @returns информацию о плагине
*/
export const getPluginVersion = _afterPluginsLoaded((): string => {
let cadesVersion = null;
return eval(
_generateCadesFn(function getPluginVersion(): string {
let cadesAbout;
try {
cadesAbout =
__cadesAsyncToken__ + __createCadesPluginObject__('CAdESCOM.About');
cadesVersion = __cadesAsyncToken__ + cadesAbout.PluginVersion;
if (!cadesVersion) {
cadesVersion = __cadesAsyncToken__ + cadesAbout.Version;
}
cadesVersion = __cadesAsyncToken__ + cadesVersion.toString();
} catch (error) {
console.error(error);
throw new Error(
_extractMeaningfulErrorMessage(error) ||
'Ошибка при получении информации о плагине',
);
}
return cadesVersion;
}),
);
});

View File

@ -10,3 +10,5 @@ export * from './createAttachedSignature';
export * from './addAttachedSignature';
export * from './createHash';
export * from './certificate';
export * from './getCspVersion';
export * from './getPluginVersion';