import { sanitizeText, removeScript, parseGenericTypes, countOccurrence } from './common.js';
describe('when securityLevel is antiscript, all script must be removed', () => {
/**
* @param original - The original text
* @param result - The expected sanitized text
*/
function compareRemoveScript(original: string, result: string) {
expect(removeScript(original).trim()).toEqual(result);
}
it('should remove all script block, script inline.', () => {
const labelString = `1
Act1: Hello 11
Act2:
11
1`;
const exactlyString = `1
Act1: Hello 11
Act2:
11
1`;
compareRemoveScript(labelString, exactlyString);
});
it('should remove all javascript urls', () => {
compareRemoveScript(
`This is a clean link + clean link
and me too`,
`This is a clean link + clean link
and me too`
);
});
it('should detect malicious images', () => {
compareRemoveScript(`
`, `
`);
});
it('should detect unsecured target attribute, if value is _blank then generate a secured link', () => {
compareRemoveScript(
`note about mermaid`,
`note about mermaid`
);
});
it('should detect unsecured target attribute from links', () => {
compareRemoveScript(
`note about mermaid`,
`note about mermaid`
);
});
it('should detect iframes', () => {
compareRemoveScript(
`
`,
''
);
});
});
describe('Sanitize text', () => {
it('should remove script tag', () => {
const maliciousStr = 'javajavascript:script:alert(1)';
const result = sanitizeText(maliciousStr, {
securityLevel: 'strict',
flowchart: { htmlLabels: true },
});
expect(result).not.toContain('javascript:alert(1)');
});
});
describe('generic parser', () => {
it.each([
['test~T~', 'test'],
['test~Array~Array~string~~~', 'test>>'],
['test~Array~Array~string[]~~~', 'test>>'],
['test ~Array~Array~string[]~~~', 'test >>'],
['~test', '~test'],
['~test~T~', '~test'],
])('should parse generic types: %s to %s', (input: string, expected: string) => {
expect(parseGenericTypes(input)).toEqual(expected);
});
});
it.each([
['', '', 0],
['', 'x', 0],
['test', 'x', 0],
['test', 't', 2],
['test', 'te', 1],
['test~T~', '~', 2],
['test~Array~Array~string~~~', '~', 6],
])(
'should count `%s` to contain occurrences of `%s` to be `%i`',
(str: string, substring: string, count: number) => {
expect(countOccurrence(str, substring)).toEqual(count);
}
);