|
1 |
| -import type { Event, EventHint, Package } from '@sentry/core'; |
| 1 | +import type { Client, Event, EventHint, Package } from '@sentry/core'; |
2 | 2 |
|
3 | 3 | import { SDK_NAME, SDK_VERSION } from '../../src/js';
|
4 | 4 | import { sdkInfoIntegration } from '../../src/js/integrations/sdkinfo';
|
@@ -86,9 +86,93 @@ describe('Sdk Info', () => {
|
86 | 86 | expect(processedEvent?.sdk?.name).toEqual(SDK_NAME);
|
87 | 87 | expect(processedEvent?.sdk?.version).toEqual(SDK_VERSION);
|
88 | 88 | });
|
| 89 | + |
| 90 | + it('Add none setting when defaultIp is undefined', async () => { |
| 91 | + mockedFetchNativeSdkInfo = jest.fn().mockResolvedValue(null); |
| 92 | + const mockEvent: Event = {}; |
| 93 | + const processedEvent = await processEvent(mockEvent, {}, undefined); |
| 94 | + |
| 95 | + expect(processedEvent?.sdk?.name).toEqual(SDK_NAME); |
| 96 | + expect(processedEvent?.sdk?.version).toEqual(SDK_VERSION); |
| 97 | + // @ts-expect-error injected type. |
| 98 | + expect(processedEvent?.sdk?.settings?.infer_ip).toEqual('never'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Add none setting when defaultIp is false', async () => { |
| 102 | + mockedFetchNativeSdkInfo = jest.fn().mockResolvedValue(null); |
| 103 | + const mockEvent: Event = {}; |
| 104 | + const processedEvent = await processEvent(mockEvent, {}, false); |
| 105 | + |
| 106 | + expect(processedEvent?.sdk?.name).toEqual(SDK_NAME); |
| 107 | + expect(processedEvent?.sdk?.version).toEqual(SDK_VERSION); |
| 108 | + // @ts-expect-error injected type. |
| 109 | + expect(processedEvent?.sdk?.settings?.infer_ip).toEqual('never'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('Add auto setting when defaultIp is true', async () => { |
| 113 | + mockedFetchNativeSdkInfo = jest.fn().mockResolvedValue(null); |
| 114 | + const mockEvent: Event = {}; |
| 115 | + const processedEvent = await processEvent(mockEvent, {}, true); |
| 116 | + |
| 117 | + expect(processedEvent?.sdk?.name).toEqual(SDK_NAME); |
| 118 | + expect(processedEvent?.sdk?.version).toEqual(SDK_VERSION); |
| 119 | + // @ts-expect-error injected type. |
| 120 | + expect(processedEvent?.sdk?.settings?.infer_ip).toEqual('auto'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('removes ip_address if it is "{{auto}}"', () => { |
| 124 | + const mockHandler = jest.fn(); |
| 125 | + |
| 126 | + const client = { |
| 127 | + getOptions: () => ({ sendDefaultPii: true }), |
| 128 | + on: (eventName: string, cb: (event: any) => void) => { |
| 129 | + if (eventName === 'beforeSendEvent') { |
| 130 | + mockHandler.mockImplementation(cb); |
| 131 | + } |
| 132 | + }, |
| 133 | + }; |
| 134 | + |
| 135 | + sdkInfoIntegration().setup!(client as any); |
| 136 | + |
| 137 | + const testEvent = { user: { ip_address: '{{auto}}' } }; |
| 138 | + mockHandler(testEvent); |
| 139 | + |
| 140 | + expect(testEvent.user.ip_address).toBeUndefined(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('keeps ip_address if it is not "{{auto}}"', () => { |
| 144 | + const mockHandler = jest.fn(); |
| 145 | + |
| 146 | + const client = { |
| 147 | + getOptions: () => ({ sendDefaultPii: true }), |
| 148 | + on: (eventName: string, cb: (event: any) => void) => { |
| 149 | + if (eventName === 'beforeSendEvent') { |
| 150 | + mockHandler.mockImplementation(cb); |
| 151 | + } |
| 152 | + }, |
| 153 | + }; |
| 154 | + |
| 155 | + sdkInfoIntegration().setup!(client as any); |
| 156 | + |
| 157 | + const testEvent = { user: { ip_address: '1.2.3.4' } }; |
| 158 | + mockHandler(testEvent); |
| 159 | + |
| 160 | + expect(testEvent.user.ip_address).toBe('1.2.3.4'); |
| 161 | + }); |
89 | 162 | });
|
90 | 163 |
|
91 |
| -function processEvent(mockedEvent: Event, mockedHint: EventHint = {}): Event | null | PromiseLike<Event | null> { |
| 164 | +function processEvent( |
| 165 | + mockedEvent: Event, |
| 166 | + mockedHint: EventHint = {}, |
| 167 | + sendDefaultPii?: boolean, |
| 168 | +): Event | null | PromiseLike<Event | null> { |
92 | 169 | const integration = sdkInfoIntegration();
|
| 170 | + if (sendDefaultPii != null) { |
| 171 | + const mockClient: jest.Mocked<Client> = { |
| 172 | + getOptions: jest.fn().mockReturnValue({ sendDefaultPii: sendDefaultPii }), |
| 173 | + on: jest.fn(), |
| 174 | + } as any; |
| 175 | + integration.setup!(mockClient); |
| 176 | + } |
93 | 177 | return integration.processEvent!(mockedEvent, mockedHint, {} as any);
|
94 | 178 | }
|
0 commit comments