Skip to content

Commit

Permalink
fix: bug where context state was overwritten at install (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
bflorian authored Oct 17, 2023
1 parent c7a5207 commit 13b1c56
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/smart-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,14 +593,21 @@ class SmartApp {
this._log.event(evt)
await this._installedHandler(context, evt.installData)
if (this._contextStore) {
await this._contextStore.put({
const contextRecord = {
installedAppId: context.installedAppId,
locationId: context.locationId,
authToken: context.authToken,
refreshToken: context.refreshToken,
config: context.config,
locale: context.locale,
})
}

const storedContext = await this._contextStore.get(context.installedAppId)
if (storedContext) {
await this._contextStore.update(context.installedAppId, contextRecord)
} else {
await this._contextStore.put(contextRecord)
}
}

responder.respond({statusCode: 200, installData: {}})
Expand Down
49 changes: 48 additions & 1 deletion test/unit/smartapp-context-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('smartapp-context-spec', () => {
app = new SmartApp({logUnhandledRejections: false})
})

it('endpoint app with context store', async () => {
it('endpoint app with unitialized context store', async () => {
const installData = {
authToken: 'xxx',
refreshToken: 'yyy',
Expand Down Expand Up @@ -51,6 +51,53 @@ describe('smartapp-context-spec', () => {
assert.equal(installData.refreshToken, ctx.refreshToken)
})

it('endpoint app with populated context store', async () => {
const installData = {
authToken: 'xxx',
refreshToken: 'yyy',
installedApp: {
installedAppId: 'd692699d-e7a6-400d-a0b7-d5be96e7a564',
locationId: 'e675a3d9-2499-406c-86dc-8a492a886494',
config: {}
}
}

const contextStore = new ContextStore({
'd692699d-e7a6-400d-a0b7-d5be96e7a564': {
locationId: 'e675a3d9-2499-406c-86dc-8a492a886494',
installedAppId: 'd692699d-e7a6-400d-a0b7-d5be96e7a564',
state: {
accessToken: 'xxx'
}
}
})
app.contextStore(contextStore)

await app.handleMockCallback({
lifecycle: 'INSTALL',
executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc',
locale: 'en',
version: '0.1.0',
client: {
os: 'ios',
version: '0.0.0',
language: 'en-US'
},
installData,
settings: {}
})

const ctx = await app.withContext('d692699d-e7a6-400d-a0b7-d5be96e7a564')

expect(ctx).toBeInstanceOf(SmartAppContext)
expect(ctx.api.config.authenticator).toBeInstanceOf(SequentialRefreshTokenAuthenticator)
assert.equal(installData.installedApp.installedAppId, ctx.installedAppId)
assert.equal(installData.installedApp.locationId, ctx.locationId)
assert.equal(installData.authToken, ctx.authToken)
assert.equal(installData.refreshToken, ctx.refreshToken)
assert.equal(await ctx.getItem('accessToken'), 'xxx')
})

it('endpoint app with context object', async () => {
const params = {
authToken: 'xxx',
Expand Down

0 comments on commit 13b1c56

Please sign in to comment.