Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-5051] Expo: use device id as default user id #1454

Merged
merged 4 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/plugin-expo-device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = {
client.addOnSession(session => {
session.device = { ...session.device, ...device }
addDefaultAppType(session)
addDefaultUserId(session)
})

client.addOnError(event => {
Expand All @@ -54,6 +55,7 @@ module.exports = {
appOwnership: Constants.appOwnership
})
addDefaultAppType(event)
addDefaultUserId(event)
}, true)
}
}
Expand All @@ -68,3 +70,11 @@ function addDefaultAppType (eventOrSession) {
}
}
}

function addDefaultUserId (eventOrSession) {
// device id is also used to populate the user id field, if it's not already set
const user = eventOrSession.getUser()
if (!user || !user.id) {
eventOrSession.setUser(eventOrSession.device.id)
}
}
54 changes: 54 additions & 0 deletions packages/plugin-expo-device/test/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('plugin: expo device', () => {

jest.doMock('expo-constants', () => ({
default: {
installationId: '123',
platform: { android: {} },
manifest: { sdkVersion: SDK_VERSION },
expoVersion: EXPO_VERSION,
Expand Down Expand Up @@ -64,6 +65,8 @@ describe('plugin: expo device', () => {
})
expect(r.events[0].metaData.device.isDevice).toBe(true)
expect(r.events[0].metaData.device.appOwnership).toBe('standalone')
expect(r.events[0].device.id).toBe('123')
expect(r.events[0].user.id).toBe('123')
done()
},
sendSession: () => {}
Expand Down Expand Up @@ -184,6 +187,57 @@ describe('plugin: expo device', () => {
c.notify(new Error('device testing'))
})

it('does not overwrite user.id when one is already set', done => {
const REACT_NATIVE_VERSION = '0.57.1'
const SDK_VERSION = '32.3.0'
const EXPO_VERSION = '2.10.4'
const IOS_MODEL = 'iPhone 7 Plus'
const IOS_PLATFORM = 'iPhone1,1'
const IOS_VERSION = '11.2'

jest.doMock('expo-constants', () => ({
default: {
installationId: '123',
platform: { ios: { model: IOS_MODEL, platform: IOS_PLATFORM, systemVersion: IOS_VERSION } },
manifest: { sdkVersion: SDK_VERSION },
expoVersion: EXPO_VERSION,
isDevice: false,
appOwnership: 'expo'
}
}))
jest.doMock('expo-device', () => ({
manufacturer: 'Apple'
}))
jest.doMock('react-native', () => ({
Dimensions: {
addEventListener: function () {},
get: function () {
return { width: 1024, height: 768 }
}
},
Platform: { OS: 'ios' }
}))
jest.doMock('react-native/package.json', () => ({ version: REACT_NATIVE_VERSION }))

const plugin = require('..')

const c = new Client({ apiKey: 'api_key', plugins: [plugin] })
c.setUser('345')
c._setDelivery(client => ({
sendEvent: (payload) => {
const r = JSON.parse(JSON.stringify(payload))
expect(r).toBeTruthy()
expect(r.events[0].device).toBeTruthy()
expect(r.events[0].device.osName).toBe('ios')
expect(r.events[0].user.id).toBe('345')
done()
},
sendSession: () => {}

}))
c.notify(new Error('device testing'))
})

it('updates orientation when the screen dimensions change', done => {
const REACT_NATIVE_VERSION = '0.57.1'
const SDK_VERSION = '32.3.0'
Expand Down
12 changes: 10 additions & 2 deletions test/expo/features/fixtures/test-app/app/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import { bugsnagClient } from './bugsnag'

export default class User extends Component {
userClient = () => {
bugsnagClient.setUser(null, null, "userClientName")
bugsnagClient.setUser('123', '[email protected]', "userClientName")
bugsnagClient.notify(new Error('UserClientError'))
}

userCallback = () => {
bugsnagClient.notify(new Error('UserCallbackError'), event => {
event.setUser(null, null, "userCallbackName")
event.setUser('123', '[email protected]', "userCallbackName")
})
}

userDefault = () => {
bugsnagClient.notify(new Error('UserDefaultError'))
}

render() {
return (
<View>
Expand All @@ -25,6 +29,10 @@ export default class User extends Component {
title="userCallback"
onPress={this.userCallback}
/>
<Button accessibilityLabel="userDefaultButton"
title="userDefault"
onPress={this.userDefault}
/>
</View>
)
}
Expand Down
9 changes: 9 additions & 0 deletions test/expo/features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ Scenario: User data can be set via a callback
And the exception "message" equals "UserCallbackError"
And the event "user.name" equals "userCallbackName"
And the error Bugsnag-Integrity header is valid

Scenario: User id defaults to device id
Given the element "userDefaultButton" is present
When I click the element "userDefaultButton"
Then I wait to receive an error
And the exception "errorClass" equals "Error"
And the exception "message" equals "UserDefaultError"
And the event "user.id" is not null
And the error Bugsnag-Integrity header is valid