diff --git a/src/script/util/StringUtil.ts b/src/script/util/StringUtil.ts index a51542c64b0..a872dd7516b 100644 --- a/src/script/util/StringUtil.ts +++ b/src/script/util/StringUtil.ts @@ -50,9 +50,13 @@ export const getRandomChar = (): string => { }; export const obfuscate = (text: string): string => { - /* cspell:disable-next-line */ - const alphabet = Array.from('abcdefghijklmnopqrstuvwxyz'); - return Array.from(text, char => (/\s/.test(char) ? char : randomElement(alphabet))).join(''); + const alphabet = Array.from('abcdefghijklmnopqrstuvwxyz '); + + const obfuscatedText = Array.from({length: text.length + Math.floor((1 + Math.random()) * 10)}, () => + randomElement(alphabet), + ).join(''); + + return obfuscatedText; }; /** diff --git a/test/unit_tests/util/StringUtilSpec.js b/test/unit_tests/util/StringUtilSpec.js index 11a4e8ebea2..b442369d483 100644 --- a/test/unit_tests/util/StringUtilSpec.js +++ b/test/unit_tests/util/StringUtilSpec.js @@ -86,13 +86,14 @@ describe('StringUtil', () => { }); describe('obfuscate', () => { - it("obfuscates a text preserving it's whitespaces", () => { - const text = 'You Are The Sunshine Of My Life'; - const obfuscated = obfuscate(text); - const whitespaces = obfuscated.match(/[\n\r\s]+/gi); - - expect(obfuscated).not.toBe(text); - expect(whitespaces.length).toBe(6); + it('obfuscates a text returning a text with greater length', () => { + expect(obfuscate('a').length).toBeGreaterThan(1); + expect(obfuscate('ab').length).toBeGreaterThan(2); + expect( + obfuscate( + 'Bacon ipsum dolor amet sausage landjaeger ball tip brisket filet mignon, t-bone tenderloin tri-tip beef drumstick fatback burgdoggen ground round meatball. Tri-tip spare ribs ground round bresaola ball tip tail, sirloin chicken doner boudin turkey leberkas bacon alcatra. ', + ).length, + ).toBeGreaterThan(272); }); it('obfuscates a text keeping its length', () => { @@ -101,7 +102,6 @@ describe('StringUtil', () => { const obfuscated = obfuscate(text); expect(obfuscated).not.toBe(text); - expect(obfuscated.length).toBe(text.length); }); it('obfuscates a text keeping its length (commas)', () => { @@ -109,7 +109,6 @@ describe('StringUtil', () => { const obfuscated = obfuscate(text); expect(obfuscated).not.toBe(text); - expect(obfuscated.length).toBe(text.length); }); it('obfuscates a text keeping its length (dots)', () => { @@ -117,7 +116,6 @@ describe('StringUtil', () => { const obfuscated = obfuscate(text); expect(obfuscated).not.toBe(text); - expect(obfuscated.length).toBe(text.length); }); });