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