From 72eba37cecbaacb40fc03f13a24a59bb1aaf7c7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Tue, 9 May 2023 14:20:39 +0100 Subject: [PATCH] test: add anonymise unit tests (#3722) Adds unit tests to `anonymise` functions. See: https://github.com/Unleash/unleash/pull/3683#pullrequestreview-1418223563 --- src/lib/util/anonymise.test.ts | 132 +++++++++++++++++++++++++++++++++ src/lib/util/anonymise.ts | 2 +- 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/lib/util/anonymise.test.ts diff --git a/src/lib/util/anonymise.test.ts b/src/lib/util/anonymise.test.ts new file mode 100644 index 0000000000..7504d0128b --- /dev/null +++ b/src/lib/util/anonymise.test.ts @@ -0,0 +1,132 @@ +import { anonymise, anonymiseKeys } from './anonymise'; + +const REGEX_MATCH = /^[a-f0-9]{9}@unleash\.run$/; + +test('anonymise', () => { + expect(anonymise('test')).toMatch(REGEX_MATCH); +}); + +describe('anonymiseKeys', () => { + test.each([ + null, + undefined, + true, + false, + 'test', + 123, + [1, 2, 3], + { test: 'test' }, + ])( + 'A parameter without keys (non-object, non-array) should return the same value', + (obj) => expect(anonymiseKeys(obj!, [])).toStrictEqual(obj), + ); + + test('An object should anonymise the specified keys', () => { + expect( + anonymiseKeys({ test: 'test', test2: 'test2' }, ['test']), + ).toStrictEqual({ + test: expect.stringMatching(REGEX_MATCH), + test2: 'test2', + }); + }); + + test('An array of objects should anonymise the specified keys', () => { + expect( + anonymiseKeys( + [ + { test: 'test', test2: 'test2' }, + { test: 'test', test2: 'test2' }, + ], + ['test'], + ), + ).toStrictEqual([ + { test: expect.stringMatching(REGEX_MATCH), test2: 'test2' }, + { test: expect.stringMatching(REGEX_MATCH), test2: 'test2' }, + ]); + }); + + test('A complex array/object should anonymise the specified keys, no matter the depth', () => { + expect( + anonymiseKeys( + [ + { + test1: [], + anon: 'secret', + other: 'other', + test2: { + test3: 'test3', + test4: [ + { + test5: { + anon2: 'secret2', + }, + }, + ], + }, + }, + { + test6: [ + { + test7: [ + { + test8: { + anon: 'secret3', + other2: 'other2', + }, + }, + ], + test9: { + other3: 'other3', + anon2: 'secret4', + test10: { + anon: 'secret5', + }, + }, + other4: 'other4', + }, + ], + }, + ], + ['anon', 'anon2'], + ), + ).toStrictEqual([ + { + test1: [], + anon: expect.stringMatching(REGEX_MATCH), + other: 'other', + test2: { + test3: 'test3', + test4: [ + { + test5: { + anon2: expect.stringMatching(REGEX_MATCH), + }, + }, + ], + }, + }, + { + test6: [ + { + test7: [ + { + test8: { + anon: expect.stringMatching(REGEX_MATCH), + other2: 'other2', + }, + }, + ], + test9: { + other3: 'other3', + anon2: expect.stringMatching(REGEX_MATCH), + test10: { + anon: expect.stringMatching(REGEX_MATCH), + }, + }, + other4: 'other4', + }, + ], + }, + ]); + }); +}); diff --git a/src/lib/util/anonymise.ts b/src/lib/util/anonymise.ts index af8acf124c..9b16d2bbdf 100644 --- a/src/lib/util/anonymise.ts +++ b/src/lib/util/anonymise.ts @@ -14,7 +14,7 @@ export function anonymiseKeys(object: T, keys: string[]): T { } if (Array.isArray(object)) { - return object.map((item) => anonymiseKeys(item, keys)) as unknown as T; + return object.map((item) => anonymiseKeys(item, keys)) as T; } else { return Object.keys(object).reduce((result, key) => { if (