|
| 1 | +import { ScrubData } from '../src/scrubdata'; |
| 2 | + |
| 3 | +/** JSDoc */ |
| 4 | +function clone<T>(data: T): T { |
| 5 | + return JSON.parse(JSON.stringify(data)); |
| 6 | +} |
| 7 | + |
| 8 | +let scrubData: ScrubData; |
| 9 | +const sanitizeMask = '********'; |
| 10 | +const messageEvent = { |
| 11 | + fingerprint: ['MrSnuffles'], |
| 12 | + message: 'PickleRick', |
| 13 | + stacktrace: { |
| 14 | + frames: [ |
| 15 | + { |
| 16 | + colno: 1, |
| 17 | + filename: 'filename.js', |
| 18 | + function: 'function', |
| 19 | + lineno: 1, |
| 20 | + }, |
| 21 | + { |
| 22 | + colno: 2, |
| 23 | + filename: 'filename.js', |
| 24 | + function: 'function', |
| 25 | + lineno: 2, |
| 26 | + }, |
| 27 | + ], |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +describe('ScrubData', () => { |
| 32 | + describe('sanitizeKeys option is empty', () => { |
| 33 | + beforeEach(() => { |
| 34 | + scrubData = new ScrubData({ |
| 35 | + sanitizeKeys: [], |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should not affect any changes', () => { |
| 40 | + const event = clone(messageEvent); |
| 41 | + const processedEvent = scrubData.process(event); |
| 42 | + expect(processedEvent).toEqual(event); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('sanitizeKeys option has type of string', () => { |
| 47 | + beforeEach(() => { |
| 48 | + scrubData = new ScrubData({ |
| 49 | + sanitizeKeys: ['message', 'filename'], |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should mask matched value in object', () => { |
| 54 | + const event = scrubData.process(clone(messageEvent)); |
| 55 | + expect(event.message).toEqual(sanitizeMask); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not mask unmatched value in object', () => { |
| 59 | + const event = scrubData.process(clone(messageEvent)); |
| 60 | + expect(event.fingerprint).toEqual(messageEvent.fingerprint); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should mask matched value in Array', () => { |
| 64 | + const event: any = scrubData.process(clone(messageEvent)); |
| 65 | + expect(event.stacktrace.frames[0].filename).toEqual(sanitizeMask); |
| 66 | + expect(event.stacktrace.frames[1].filename).toEqual(sanitizeMask); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should not mask unmatched value in Array', () => { |
| 70 | + const event: any = scrubData.process(clone(messageEvent)); |
| 71 | + expect(event.stacktrace.frames[0].function).toEqual(messageEvent.stacktrace.frames[0].function); |
| 72 | + expect(event.stacktrace.frames[1].function).toEqual(messageEvent.stacktrace.frames[1].function); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('sanitizeKeys option has type of RegExp', () => { |
| 77 | + beforeEach(() => { |
| 78 | + scrubData = new ScrubData({ |
| 79 | + sanitizeKeys: [/^name$/], |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should mask only matched value', () => { |
| 84 | + const testEvent: any = { |
| 85 | + filename: 'to be show', |
| 86 | + name: 'do not show', |
| 87 | + }; |
| 88 | + const event: any = scrubData.process(testEvent); |
| 89 | + expect(event.filename).toEqual(testEvent.filename); |
| 90 | + expect(event.name).toEqual(sanitizeMask); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('sanitizeKeys option has mixed type of RegExp and string', () => { |
| 95 | + beforeEach(() => { |
| 96 | + scrubData = new ScrubData({ |
| 97 | + sanitizeKeys: [/^filename$/, 'function'], |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should mask only matched value', () => { |
| 102 | + const event: any = scrubData.process(clone(messageEvent)); |
| 103 | + expect(event.stacktrace.frames[0].function).toEqual(sanitizeMask); |
| 104 | + expect(event.stacktrace.frames[1].function).toEqual(sanitizeMask); |
| 105 | + expect(event.stacktrace.frames[0].filename).toEqual(sanitizeMask); |
| 106 | + expect(event.stacktrace.frames[1].filename).toEqual(sanitizeMask); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should not mask unmatched value', () => { |
| 110 | + const event: any = scrubData.process(clone(messageEvent)); |
| 111 | + expect(event.stacktrace.frames[0].colno).toEqual(messageEvent.stacktrace.frames[0].colno); |
| 112 | + expect(event.stacktrace.frames[1].colno).toEqual(messageEvent.stacktrace.frames[1].colno); |
| 113 | + expect(event.stacktrace.frames[0].lineno).toEqual(messageEvent.stacktrace.frames[0].lineno); |
| 114 | + expect(event.stacktrace.frames[1].lineno).toEqual(messageEvent.stacktrace.frames[1].lineno); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('event has circular objects', () => { |
| 119 | + beforeEach(() => { |
| 120 | + scrubData = new ScrubData({ |
| 121 | + sanitizeKeys: [/message/], |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should not show call stack size exceeded when circular reference in object', () => { |
| 126 | + const event: any = { |
| 127 | + contexts: {}, |
| 128 | + extra: { |
| 129 | + message: 'do not show', |
| 130 | + }, |
| 131 | + }; |
| 132 | + event.contexts.circular = event.contexts; |
| 133 | + |
| 134 | + const actual: any = scrubData.process(event); |
| 135 | + expect(actual.extra.message).toEqual(sanitizeMask); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should not show call stack size exceeded when circular reference in Array', () => { |
| 139 | + const event: any = { |
| 140 | + contexts: [], |
| 141 | + extra: { |
| 142 | + message: 'do not show', |
| 143 | + }, |
| 144 | + }; |
| 145 | + event.contexts[0] = event.contexts; |
| 146 | + |
| 147 | + const actual: any = scrubData.process(event); |
| 148 | + expect(actual.extra.message).toEqual(sanitizeMask); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments