Skip to content

Commit eb29c74

Browse files
committed
fix & tests
1 parent 0bef6ed commit eb29c74

File tree

10 files changed

+63
-17
lines changed

10 files changed

+63
-17
lines changed

dev-packages/browser-integration-tests/suites/feedback/attachTo/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ sentryTest('should capture feedback with custom button', async ({ getLocalTestUr
5757
event_id: expect.stringMatching(/\w{32}/),
5858
environment: 'production',
5959
tags: {},
60+
user: {
61+
ip_address: '{{auto}}',
62+
},
6063
sdk: {
6164
integrations: expect.arrayContaining(['Feedback']),
6265
version: expect.any(String),

dev-packages/browser-integration-tests/suites/feedback/captureFeedback/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ sentryTest('should capture feedback', async ({ getLocalTestUrl, page }) => {
6969
'User-Agent': expect.stringContaining(''),
7070
},
7171
},
72+
user: {
73+
ip_address: '{{auto}}',
74+
},
7275
platform: 'javascript',
7376
});
7477
});

dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ sentryTest('should capture feedback', async ({ getLocalTestUrl, page }) => {
6969
'User-Agent': expect.stringContaining(''),
7070
},
7171
},
72+
user: {
73+
ip_address: '{{auto}}',
74+
},
7275
platform: 'javascript',
7376
});
7477
const cspViolation = await page.evaluate<boolean>('window.__CSPVIOLATION__');

dev-packages/browser-integration-tests/suites/manual-client/browser-context/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ sentryTest('allows to setup a client manually & capture exceptions', async ({ ge
3434
'User-Agent': expect.any(String),
3535
}),
3636
},
37+
user: {
38+
ip_address: '{{auto}}',
39+
},
3740
timestamp: expect.any(Number),
3841
environment: 'local',
3942
release: '0.0.1',
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Sentry.setUser({
2+
id: 'foo',
3+
ip_address: null,
4+
});
5+
6+
Sentry.captureMessage('first_user');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/core';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers';
6+
7+
sentryTest('should allow to set ip_address to null', async ({ getLocalTestUrl, page }) => {
8+
const url = await getLocalTestUrl({ testDir: __dirname });
9+
10+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
11+
12+
expect(eventData.message).toBe('first_user');
13+
expect(eventData.user).toEqual({
14+
id: 'foo',
15+
ip_address: null,
16+
});
17+
});

dev-packages/browser-integration-tests/suites/public-api/setUser/unset_user/test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ sentryTest('should unset user', async ({ getLocalTestUrl, page }) => {
1010
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 3, { url });
1111

1212
expect(eventData[0].message).toBe('no_user');
13-
expect(eventData[0].user).toBeUndefined();
13+
expect(eventData[0].user).toEqual({ ip_address: '{{auto}}' });
1414

1515
expect(eventData[1].message).toBe('user');
16-
expect(eventData[1].user).toMatchObject({
16+
expect(eventData[1].user).toEqual({
1717
id: 'foo',
1818
ip_address: 'bar',
1919
other_key: 'baz',
2020
});
2121

2222
expect(eventData[2].message).toBe('unset_user');
23-
expect(eventData[2].user).toBeUndefined();
23+
expect(eventData[2].user).toEqual({
24+
ip_address: '{{auto}}',
25+
});
2426
});

dev-packages/browser-integration-tests/suites/public-api/setUser/update_user/test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ sentryTest('should update user', async ({ getLocalTestUrl, page }) => {
1010
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url });
1111

1212
expect(eventData[0].message).toBe('first_user');
13-
expect(eventData[0].user).toMatchObject({
13+
expect(eventData[0].user).toEqual({
1414
id: 'foo',
1515
ip_address: 'bar',
1616
});
1717

1818
expect(eventData[1].message).toBe('second_user');
19-
expect(eventData[1].user).toMatchObject({
19+
expect(eventData[1].user).toEqual({
2020
id: 'baz',
21+
ip_address: '{{auto}}',
2122
});
2223
});

dev-packages/browser-integration-tests/suites/public-api/withScope/nested_scopes/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ sentryTest('should allow nested scoping', async ({ getLocalTestUrl, page }) => {
1010
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 5, { url });
1111

1212
expect(eventData[0].message).toBe('root_before');
13-
expect(eventData[0].user).toMatchObject({ id: 'qux' });
13+
expect(eventData[0].user).toEqual({ id: 'qux' });
1414
expect(eventData[0].tags).toBeUndefined();
1515

1616
expect(eventData[1].message).toBe('outer_before');
17-
expect(eventData[1].user).toMatchObject({ id: 'qux' });
17+
expect(eventData[1].user).toEqual({ id: 'qux' });
1818
expect(eventData[1].tags).toMatchObject({ foo: false });
1919

2020
expect(eventData[2].message).toBe('inner');
2121
expect(eventData[2].user).toBeUndefined();
2222
expect(eventData[2].tags).toMatchObject({ foo: false, bar: 10 });
2323

2424
expect(eventData[3].message).toBe('outer_after');
25-
expect(eventData[3].user).toMatchObject({ id: 'baz' });
25+
expect(eventData[3].user).toEqual({ id: 'baz' });
2626
expect(eventData[3].tags).toMatchObject({ foo: false });
2727

2828
expect(eventData[4].message).toBe('root_after');
29-
expect(eventData[4].user).toMatchObject({ id: 'qux' });
29+
expect(eventData[4].user).toEqual({ id: 'qux' });
3030
expect(eventData[4].tags).toBeUndefined();
3131
});

packages/browser/src/client.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,22 @@ export class BrowserClient extends Client<BrowserClientOptions> {
113113
): PromiseLike<Event | null> {
114114
event.platform = event.platform || 'javascript';
115115

116-
// By default, we want to infer the IP address, unless this is explicitly set to `null`
117-
if (typeof event.user?.ip_address === 'undefined') {
118-
event.user = {
119-
...event.user,
120-
ip_address: '{{auto}}',
121-
};
122-
}
116+
return super._prepareEvent(event, hint, currentScope, isolationScope).then(prepared => {
117+
if (!prepared) {
118+
return prepared;
119+
}
120+
121+
// By default, we want to infer the IP address, unless this is explicitly set to `null`
122+
// We do this after all other processing is done
123+
// If `ip_address` is explicitly set to `null` or a value, we leave it as is
124+
if (prepared.user?.ip_address === undefined) {
125+
prepared.user = {
126+
...prepared.user,
127+
ip_address: '{{auto}}',
128+
};
129+
}
123130

124-
return super._prepareEvent(event, hint, currentScope, isolationScope);
131+
return prepared;
132+
});
125133
}
126134
}

0 commit comments

Comments
 (0)