mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	test: add anonymise unit tests (#3722)
Adds unit tests to `anonymise` functions. See: https://github.com/Unleash/unleash/pull/3683#pullrequestreview-1418223563
This commit is contained in:
		
							parent
							
								
									b4d91f793e
								
							
						
					
					
						commit
						72eba37cec
					
				
							
								
								
									
										132
									
								
								src/lib/util/anonymise.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								src/lib/util/anonymise.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -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',
 | 
			
		||||
                    },
 | 
			
		||||
                ],
 | 
			
		||||
            },
 | 
			
		||||
        ]);
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
@ -14,7 +14,7 @@ export function anonymiseKeys<T>(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 (
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user