From f374c2f9b592b4f39ac71bfa021ea15c53f2b5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B0=94-Rain?= <958414905@qq.com> Date: Mon, 30 Oct 2023 18:08:07 +0800 Subject: [PATCH] fix(e2e): APP_BASE_URL (#2938) * fix: should use APP_BASE_URL * chore: should not start local service when setting APP_BASE_URL * fix: should use process.env.APP_BASE_URL --- packages/core/test/src/client/e2eUtils.ts | 75 +++++++++++++++-------- scripts/utils.ts | 10 ++- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/packages/core/test/src/client/e2eUtils.ts b/packages/core/test/src/client/e2eUtils.ts index 1311369d8d..253c911593 100644 --- a/packages/core/test/src/client/e2eUtils.ts +++ b/packages/core/test/src/client/e2eUtils.ts @@ -213,7 +213,7 @@ export const test = Object.assign(_test, { const getStorageItem = (key: string, storageState: any) => { return storageState.origins - .find((item) => item.origin === `http://localhost:${process.env.APP_PORT}`) + .find((item) => item.origin === process.env.APP_BASE_URL) ?.localStorage.find((item) => item.name === key)?.value; }; @@ -248,12 +248,10 @@ const createPage = async (page: Page, options?: CreatePageOptions) => { }); const state = await api.storageState(); - const token = getStorageItem('NOCOBASE_TOKEN', state); + const headers = getHeaders(state); const systemSettings = await api.get(`/api/systemSettings:get/1`, { - headers: { - Authorization: `Bearer ${token}`, - }, + headers, }); const pageUid = uid(); @@ -262,9 +260,7 @@ const createPage = async (page: Page, options?: CreatePageOptions) => { if (systemSettings.ok()) { const { data } = await systemSettings.json(); const result = await api.post(`/api/uiSchemas:insertAdjacent/${data.options.adminSchemaUid}?position=beforeEnd`, { - headers: { - Authorization: `Bearer ${token}`, - }, + headers, data: { schema: { _isJSONSchemaObject: true, @@ -329,12 +325,10 @@ const deletePage = async (pageUid: string) => { }); const state = await api.storageState(); - const token = getStorageItem('NOCOBASE_TOKEN', state); + const headers = getHeaders(state); const result = await api.post(`/api/uiSchemas:remove/${pageUid}`, { - headers: { - Authorization: `Bearer ${token}`, - }, + headers, }); if (!result.ok()) { @@ -348,13 +342,11 @@ const deleteCollections = async (collectionNames: string[]) => { }); const state = await api.storageState(); - const token = getStorageItem('NOCOBASE_TOKEN', state); + const headers = getHeaders(state); const params = collectionNames.map((name) => `filterByTk[]=${name}`).join('&'); const result = await api.post(`/api/collections:destroy?${params}`, { - headers: { - Authorization: `Bearer ${token}`, - }, + headers, }); if (!result.ok()) { @@ -388,7 +380,7 @@ const createCollections = async (collectionSettings: CollectionSetting | Collect }); const state = await api.storageState(); - const token = getStorageItem('NOCOBASE_TOKEN', state); + const headers = getHeaders(state); // const defaultCollectionSetting: Partial = { // template: 'general', // logging: true, @@ -404,9 +396,7 @@ const createCollections = async (collectionSettings: CollectionSetting | Collect collectionSettings = Array.isArray(collectionSettings) ? collectionSettings : [collectionSettings]; const result = await api.post(`/api/collections:create`, { - headers: { - Authorization: `Bearer ${token}`, - }, + headers, // data: collectionSettings.map((item) => Object.assign(defaultCollectionSetting, item)), data: collectionSettings.filter((item) => !['users', 'roles'].includes(item.name)), }); @@ -465,14 +455,12 @@ const createFakerData = async (collectionSettings: CollectionSetting[]) => { }); const state = await api.storageState(); - const token = getStorageItem('NOCOBASE_TOKEN', state); + const headers = getHeaders(state); for (const item of collectionSettings) { const data = generateFakerData(item); const result = await api.post(`/api/${item.name}:create`, { - headers: { - Authorization: `Bearer ${token}`, - }, + headers, form: data, }); @@ -490,3 +478,42 @@ export async function enableToConfig(page: Page) { await page.getByRole('button', { name: 'highlight' }).click(); } } + +function getHeaders(storageState: any) { + const headers: any = {}; + const token = getStorageItem('NOCOBASE_TOKEN', storageState); + const auth = getStorageItem('NOCOBASE_AUTH', storageState); + const subAppName = new URL(process.env.APP_BASE_URL).pathname.match(/^\/apps\/([^/]*)\/*/)?.[1]; + const hostName = new URL(process.env.APP_BASE_URL).host; + const locale = getStorageItem('NOCOBASE_LOCALE', storageState); + const timezone = '+08:00'; + const withAclMeta = 'true'; + const role = getStorageItem('NOCOBASE_ROLE', storageState); + + if (token) { + headers.Authorization = `Bearer ${token}`; + } + if (auth) { + headers['X-Authenticator'] = auth; + } + if (subAppName) { + headers['X-App'] = subAppName; + } + if (hostName) { + headers['X-Hostname'] = hostName; + } + if (locale) { + headers['X-Locale'] = locale; + } + if (timezone) { + headers['X-Timezone'] = timezone; + } + if (withAclMeta) { + headers['X-With-Acl-Meta'] = withAclMeta; + } + if (role) { + headers['X-Role'] = role; + } + + return headers; +} diff --git a/scripts/utils.ts b/scripts/utils.ts index c6342dcc1e..1abf08602a 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -58,7 +58,7 @@ const checkServer = async (duration = 1000, max = 60 * 10) => { } axios - .get(`http://localhost:${process.env.APP_PORT}/api/__health_check`) + .get(`${process.env.APP_BASE_URL}/api/__health_check`) .then((response) => { if (response.status === 200) { clearInterval(timer); @@ -86,7 +86,7 @@ const checkUI = async (duration = 1000, max = 60 * 10) => { } axios - .get(`http://localhost:${process.env.APP_PORT}/__umi/api/bundle-status`) + .get(`${process.env.APP_BASE_URL}/__umi/api/bundle-status`) .then((response) => { if (response.data.bundleStatus.done) { clearInterval(timer); @@ -138,6 +138,12 @@ export const runNocoBase = async (options?: CommonOptions) => { return { awaitForNocoBase }; } + if (!process.env.APP_BASE_URL.includes('localhost')) { + return { + awaitForNocoBase: async () => {}, + }; + } + // 加上 -f 会清空数据库 console.log('yarn nocobase install -f'); await runCommand('yarn', ['nocobase', 'install', '-f'], options);