Skip to content

Commit

Permalink
Merge pull request #104 from local-first-web/preserve-deviceInfo
Browse files Browse the repository at this point in the history
Preserve device info
  • Loading branch information
HerbCaudill committed May 8, 2024
2 parents e0e1e00 + 968d38e commit e36e8f5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/auth/src/connection/test/authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ describe('connection', () => {
expect(() => bob.team.teamKeys()).not.toThrow()
})

it('after an invitee is admitted, the device recorded on the team includes user-agent metadata', async () => {
const { alice, bob } = setup('alice', { user: 'bob', member: false })

// 👩🏾📧👨🏻‍🦲 Alice invites Bob
const { seed } = alice.team.inviteMember()

// 👨🏻‍🦲📧<->👩🏾 Bob connects to Alice and uses his invitation to join
await connectWithInvitation(alice, bob, seed)

// Update the team from the connection
const connection = bob.connection[alice.deviceId]
bob.team = connection.team!

// 👨🏻‍🦲 Bob's device was recorded with user-agent metadata
const { deviceId } = bob.device
const device = bob.team.device(deviceId)
expect(device?.deviceInfo).toEqual(bob.device.deviceInfo)
})

it("doesn't allow two invitees to connect", async () => {
const { alice, charlie, dwight } = setup([
'alice',
Expand Down
1 change: 1 addition & 0 deletions packages/auth/src/device/redact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const redactDevice = (device: DeviceWithSecrets): Device => ({
userId: device.userId,
deviceId: device.deviceId,
deviceName: device.deviceName,
deviceInfo: device.deviceInfo,
keys: redactKeys(device.keys),
})
11 changes: 11 additions & 0 deletions packages/auth/src/team/test/createTeam.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,16 @@ describe('Team', () => {
alice.team.setTeamName(`Sgt. Pepper's Lonely Hearts Club Band`)
expect(alice.team.teamName).toBe(`Sgt. Pepper's Lonely Hearts Club Band`)
})

it('the team preserves device metadata if provided', () => {
const user = createUser('alice')
const device = createDevice({
userId: user.userId,
deviceName: 'laptop',
deviceInfo: { foo: 'bar' },
})
const team = createTeam('Spies Я Us', { user, device })
expect(team.device(device.deviceId).deviceInfo.foo).toBe('bar')
})
})
})
15 changes: 15 additions & 0 deletions packages/auth/src/util/testing/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const laptopInfo = {
ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
browser: { name: 'Chrome', version: '124.0.0.0', major: '124' },
engine: { name: 'Blink', version: '124.0.0.0' },
os: { name: 'Mac OS', version: '10.15.7' },
device: { vendor: 'Apple', model: 'Macintosh' },
}

export const phoneInfo = {
ua: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1',
browser: { name: 'Mobile Safari', version: '17.4.1', major: '17' },
engine: { name: 'WebKit', version: '605.1.15' },
os: { name: 'iOS', version: '17.4.1' },
device: { vendor: 'Apple', model: 'iPhone', type: 'mobile' },
}
4 changes: 3 additions & 1 deletion packages/auth/src/util/testing/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { LocalUserContext } from 'team/context.js'
import type { Team, TeamContext } from 'team/index.js'
import * as teams from 'team/index.js'
import { arrayToMap } from 'util/index.js'
import { phoneInfo, laptopInfo } from './constants.js'

export type SetupConfig = Array<Array<TestUserSettings | string> | TestUserSettings | string>

Expand Down Expand Up @@ -51,7 +52,8 @@ export const setup = (..._config: SetupConfig) => {
const makeDevice = (userId: string, deviceName: string) => {
const key = `${userId}-${deviceName}`
const randomSeed = key
const device = devices.createDevice({ userId, deviceName, seed: randomSeed })
const deviceInfo = deviceName === 'phone' ? phoneInfo : laptopInfo
const device = devices.createDevice({ userId, deviceName, seed: randomSeed, deviceInfo })
return device
}

Expand Down

0 comments on commit e36e8f5

Please sign in to comment.