Skip to content

Commit 13b1c56

Browse files
authored
fix: bug where context state was overwritten at install (#251)
1 parent c7a5207 commit 13b1c56

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/smart-app.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -593,14 +593,21 @@ class SmartApp {
593593
this._log.event(evt)
594594
await this._installedHandler(context, evt.installData)
595595
if (this._contextStore) {
596-
await this._contextStore.put({
596+
const contextRecord = {
597597
installedAppId: context.installedAppId,
598598
locationId: context.locationId,
599599
authToken: context.authToken,
600600
refreshToken: context.refreshToken,
601601
config: context.config,
602602
locale: context.locale,
603-
})
603+
}
604+
605+
const storedContext = await this._contextStore.get(context.installedAppId)
606+
if (storedContext) {
607+
await this._contextStore.update(context.installedAppId, contextRecord)
608+
} else {
609+
await this._contextStore.put(contextRecord)
610+
}
604611
}
605612

606613
responder.respond({statusCode: 200, installData: {}})

test/unit/smartapp-context-spec.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('smartapp-context-spec', () => {
1313
app = new SmartApp({logUnhandledRejections: false})
1414
})
1515

16-
it('endpoint app with context store', async () => {
16+
it('endpoint app with unitialized context store', async () => {
1717
const installData = {
1818
authToken: 'xxx',
1919
refreshToken: 'yyy',
@@ -51,6 +51,53 @@ describe('smartapp-context-spec', () => {
5151
assert.equal(installData.refreshToken, ctx.refreshToken)
5252
})
5353

54+
it('endpoint app with populated context store', async () => {
55+
const installData = {
56+
authToken: 'xxx',
57+
refreshToken: 'yyy',
58+
installedApp: {
59+
installedAppId: 'd692699d-e7a6-400d-a0b7-d5be96e7a564',
60+
locationId: 'e675a3d9-2499-406c-86dc-8a492a886494',
61+
config: {}
62+
}
63+
}
64+
65+
const contextStore = new ContextStore({
66+
'd692699d-e7a6-400d-a0b7-d5be96e7a564': {
67+
locationId: 'e675a3d9-2499-406c-86dc-8a492a886494',
68+
installedAppId: 'd692699d-e7a6-400d-a0b7-d5be96e7a564',
69+
state: {
70+
accessToken: 'xxx'
71+
}
72+
}
73+
})
74+
app.contextStore(contextStore)
75+
76+
await app.handleMockCallback({
77+
lifecycle: 'INSTALL',
78+
executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc',
79+
locale: 'en',
80+
version: '0.1.0',
81+
client: {
82+
os: 'ios',
83+
version: '0.0.0',
84+
language: 'en-US'
85+
},
86+
installData,
87+
settings: {}
88+
})
89+
90+
const ctx = await app.withContext('d692699d-e7a6-400d-a0b7-d5be96e7a564')
91+
92+
expect(ctx).toBeInstanceOf(SmartAppContext)
93+
expect(ctx.api.config.authenticator).toBeInstanceOf(SequentialRefreshTokenAuthenticator)
94+
assert.equal(installData.installedApp.installedAppId, ctx.installedAppId)
95+
assert.equal(installData.installedApp.locationId, ctx.locationId)
96+
assert.equal(installData.authToken, ctx.authToken)
97+
assert.equal(installData.refreshToken, ctx.refreshToken)
98+
assert.equal(await ctx.getItem('accessToken'), 'xxx')
99+
})
100+
54101
it('endpoint app with context object', async () => {
55102
const params = {
56103
authToken: 'xxx',

0 commit comments

Comments
 (0)