Skip to content

Commit

Permalink
fix: Fixes an issue when cookieless acquire fails
Browse files Browse the repository at this point in the history
Prior to this change the acquire cookieless session could only be
called once which meant that the only way to recover from an acquire
error was to reload the page. This fix now allows the acquire
cookieless session to be called multiple times.
  • Loading branch information
bryans99 committed Sep 13, 2023
1 parent af43bc3 commit d764943
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,15 @@ export class EmbedClient<T> {
}
EmbedClient.acquireSessionPromise =
this.acquireCookielessEmbedSessionInternal()
return EmbedClient.acquireSessionPromise.then((url) => {
EmbedClient.sessionAcquired = true
return url
})
return EmbedClient.acquireSessionPromise
.then((url) => {
EmbedClient.sessionAcquired = true
return url
})
.catch((error) => {
EmbedClient.acquireSessionPromise = undefined
throw error
})
}

private async acquireCookielessEmbedSessionInternal(): Promise<string> {
Expand Down Expand Up @@ -384,9 +389,12 @@ export class EmbedClient<T> {
this._connection = this.createIframe(this._builder.url)
} else {
if (this._builder.isCookielessEmbed) {
this._connection = this.acquireCookielessEmbedSession().then(
async (url) => this.createIframe(url)
)
this._connection = this.acquireCookielessEmbedSession()
.then(async (url) => this.createIframe(url))
.catch((_error) => {
this._connection = null
throw _error
})
} else {
this._connection = this.createUrl().then(async (url) =>
this.createIframe(url)
Expand Down
65 changes: 63 additions & 2 deletions tests/embed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ describe('LookerEmbed', () => {
})

describe('receiving messages', () => {
let mockDashboardClient: any
let embedDashboard: any

const startFn = jasmine.createSpy('onStart').and.callFake(function () {
Expand All @@ -410,7 +409,6 @@ describe('LookerEmbed', () => {
) {
return Promise.resolve({})
})
mockDashboardClient = {}
builder = LookerEmbedSDK.createDashboardWithUrl(testUrl)
builder.on('dashboard:run:start', startFn)
client = builder.build()
Expand Down Expand Up @@ -534,4 +532,67 @@ describe('LookerEmbed', () => {
}
})
})

describe('cookieless embed error', () => {
let fetchSpy: any
let fakeDashboardClient: any
const acquireData = [
{},
{
api_token: 'abcdef-api',
authentication_token: 'abcdef-auth',
navigation_token: 'abcdef-nav',
},
]
const acquire = () => {
const data = acquireData.splice(0, 1)[0]
return Promise.resolve(data)
}
// Not possible to test generate callback as it is tied to chatty
// which is overridden by the createIFrame spy.
const generate = () =>
Promise.resolve({
api_token: 'mnopqr-api',
navigation_token: 'mnopqr-nav',
})

beforeEach(() => {
fetchSpy = spyOn(window, 'fetch').and.returnValue({
json: () => ({}),
ok: true,
status: 200,
})
fakeDashboardClient = {}
builder = LookerEmbedSDK.createDashboardWithId(11)
client = builder.build()
spyOn<any>(client, 'createIframe').and.returnValue(
Promise.resolve(fakeDashboardClient)
)
})

afterEach(() => {
fetchSpy.calls.reset()
})

it('should allow connect to be called successfully after an error', async () => {
LookerEmbedSDK.initCookieless('host.looker.com:9999', acquire, generate)
try {
await client.connect()
fail()
return
} catch (error) {
expect(error.message).toEqual(
'failed to prepare cookieless embed session'
)
}
try {
await client.connect()
expect(client.createIframe).toHaveBeenCalledWith(
'https://host.looker.com:9999/login/embed/%2Fembed%2Fdashboards%2F11%3Fembed_domain%3Dhttp%253A%252F%252Flocalhost%253A9876%26sdk%3D2%26embed_navigation_token%3Dabcdef-nav?embed_authentication_token=abcdef-auth'
)
} catch (_error) {
fail()
}
})
})
})

0 comments on commit d764943

Please sign in to comment.