mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 00:08:25 +00:00
* feat(builder): support custom HTML in the document head * fix(builder): place head HTML below custom JavaScript * fix(builder): autosave custom head HTML * fix(builder): remove redundant head HTML help text * feat(builder): add an expanded editor for head HTML * refactor(builder): reuse preview credentials for head HTML * refactor(builder): reuse custom code paths for head HTML * fix: say when head HTML is left out of Preview --------- Co-authored-by: peter_baserow <peter@baserow.io>
142 lines
4.9 KiB
JavaScript
142 lines
4.9 KiB
JavaScript
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
import { prepareRequestHeaders } from '@baserow/modules/core/plugins/clientHandler'
|
|
import { prepareUserSourceRequestHeaders } from '@baserow/modules/core/plugins/userSourceClientHandler'
|
|
import { prepareBuilderPreviewRequest } from '@baserow/modules/builder/plugins/previewClientHandler'
|
|
import PublishedBuilderService from '@baserow/modules/builder/services/publishedBuilder'
|
|
|
|
const makeStore = (getterOverrides = {}) => ({
|
|
getters: {
|
|
'userSourceUser/getCurrentApplication': null,
|
|
'userSourceUser/isAuthenticated': () => false,
|
|
'userSourceUser/isRefreshing': () => false,
|
|
'auth/isAuthenticated': false,
|
|
'auth/webSocketId': null,
|
|
...getterOverrides,
|
|
},
|
|
})
|
|
|
|
const prepareConfig = (
|
|
url,
|
|
previewSsrCookieHeader = null,
|
|
getterOverrides = {},
|
|
configOverrides = {}
|
|
) => {
|
|
const store = makeStore(getterOverrides)
|
|
let config = {
|
|
headers: {},
|
|
url,
|
|
withCredentials: false,
|
|
...configOverrides,
|
|
}
|
|
|
|
// Axios executes request interceptors in reverse registration order.
|
|
config = prepareBuilderPreviewRequest(previewSsrCookieHeader)(config)
|
|
config = prepareUserSourceRequestHeaders(store)(config)
|
|
return prepareRequestHeaders(store)(config)
|
|
}
|
|
|
|
describe('builder preview request headers', () => {
|
|
test('exempts preview grant creation from preview authentication', () => {
|
|
const client = { post: vi.fn() }
|
|
|
|
PublishedBuilderService(client).createPreviewGrant(123, '/products')
|
|
|
|
expect(client.post).toHaveBeenCalledWith(
|
|
'builder/preview/123/grant/',
|
|
{ path: '/products' },
|
|
{ skipBuilderPreviewRequestHandling: true }
|
|
)
|
|
})
|
|
|
|
test('sends credentials for builder preview API URLs', () => {
|
|
const config = prepareConfig('builder/preview/123/pages/456/elements/')
|
|
|
|
expect(config.withCredentials).toBe(true)
|
|
})
|
|
|
|
test('does not mark published URLs', () => {
|
|
const config = prepareConfig('builder/domains/published/page/456/elements/')
|
|
|
|
expect(config.withCredentials).toBe(false)
|
|
})
|
|
|
|
test('keeps user source authentication secondary on preview URLs', () => {
|
|
const application = { id: 123 }
|
|
const config = prepareConfig(
|
|
'builder/preview/123/data-sources/456/dispatch/',
|
|
null,
|
|
{
|
|
'userSourceUser/getCurrentApplication': application,
|
|
'userSourceUser/isAuthenticated': () => true,
|
|
'userSourceUser/accessToken': () => 'user-source-token',
|
|
'auth/isAuthenticated': true,
|
|
'auth/token': 'editor-token',
|
|
}
|
|
)
|
|
|
|
expect(config.withCredentials).toBe(true)
|
|
expect(config.headers.Authorization).toBe('JWT user-source-token')
|
|
expect(config.headers).not.toHaveProperty('UserSourceAuthorization')
|
|
expect(config.headers).not.toHaveProperty('ClientSessionId')
|
|
})
|
|
|
|
test('keeps both authentication contexts on non-preview URLs', () => {
|
|
const application = { id: 123 }
|
|
const config = prepareConfig(
|
|
'builder/domains/published/page/456/elements/',
|
|
null,
|
|
{
|
|
'userSourceUser/getCurrentApplication': application,
|
|
'userSourceUser/isAuthenticated': () => true,
|
|
'userSourceUser/accessToken': () => 'user-source-token',
|
|
'auth/isAuthenticated': true,
|
|
'auth/token': 'editor-token',
|
|
'auth/getUntrustedClientSessionId': 'client-session',
|
|
}
|
|
)
|
|
|
|
expect(config.headers.Authorization).toBe('JWT editor-token')
|
|
expect(config.headers.UserSourceAuthorization).toBe('JWT user-source-token')
|
|
expect(config.headers.ClientSessionId).toBe('client-session')
|
|
})
|
|
|
|
test('keeps editor authentication primary when creating a preview grant', () => {
|
|
const application = { id: 123 }
|
|
const config = prepareConfig(
|
|
'builder/preview/123/grant/',
|
|
null,
|
|
{
|
|
'userSourceUser/getCurrentApplication': application,
|
|
'userSourceUser/isAuthenticated': () => true,
|
|
'userSourceUser/accessToken': () => 'user-source-token',
|
|
'auth/isAuthenticated': true,
|
|
'auth/token': 'editor-token',
|
|
'auth/getUntrustedClientSessionId': 'client-session',
|
|
},
|
|
{ skipBuilderPreviewRequestHandling: true }
|
|
)
|
|
|
|
expect(config.withCredentials).toBe(false)
|
|
expect(config.headers.Authorization).toBe('JWT editor-token')
|
|
expect(config.headers.UserSourceAuthorization).toBe('JWT user-source-token')
|
|
expect(config.headers.ClientSessionId).toBe('client-session')
|
|
})
|
|
|
|
test('forwards only the companion credential for preview-auth SSR requests', () => {
|
|
const previewSsrCookieHeader = 'test_baserow_builder_preview=signed-session'
|
|
|
|
const previewUrl = 'builder/preview/123/pages/456/elements/'
|
|
const previewConfig = prepareConfig(previewUrl, previewSsrCookieHeader)
|
|
const unmarkedConfig = prepareConfig(
|
|
'builder/domains/published/page/456/elements/',
|
|
previewSsrCookieHeader
|
|
)
|
|
|
|
expect(previewConfig.headers.Cookie).toBe(
|
|
'test_baserow_builder_preview=signed-session'
|
|
)
|
|
expect(unmarkedConfig.headers).not.toHaveProperty('Cookie')
|
|
})
|
|
})
|