From 7adc496827e9b6e3e2d5aef59c267f250583b59b Mon Sep 17 00:00:00 2001 From: Ioan Moldovan Date: Tue, 7 Jan 2025 04:47:03 -0600 Subject: [PATCH] #4953 Use test replaceable port (#5887) * feat: use test replaceable port * fix: pr reviews * fix: unit test * fix: ui test * fix: writeFile * fix: ppr reviews * fix: remove port check * Revert "fix: remove port check" This reverts commit d7a20225eb139176638c22e05e9d8286012dd903. * fix: mock port variable * Revert "fix: mock port variable" This reverts commit 017ac27bc3054cd007f8372735f79915e5afa9bf. * fix: pr reviews --- extension/chrome/dev/ci_unit_test.ts | 2 ++ .../encrypted-mail-msg-formatter.ts | 2 +- extension/js/common/core/common.ts | 3 ++- extension/js/common/core/const.ts | 1 + scripts/config-mock-build.sh | 2 +- .../mock/fes/customer-url-fes-endpoints.ts | 16 ++++++++-------- .../mock/fes/shared-tenant-fes-endpoints.ts | 16 ++++++++-------- .../message-export-1803be3182d1937b.json | 2 +- test/source/mock/lib/oauth.ts | 3 ++- test/source/test.ts | 10 +++++----- .../tests/browser-unit-tests/unit-Sks.js | 6 +++--- test/source/tests/compose.ts | 8 ++++---- test/source/tests/flaky.ts | 2 +- test/source/tests/setup.ts | 2 +- test/source/tests/unit-browser.ts | 3 +-- tooling/build-types-and-manifests.ts | 19 ++++++++++--------- 16 files changed, 51 insertions(+), 46 deletions(-) diff --git a/extension/chrome/dev/ci_unit_test.ts b/extension/chrome/dev/ci_unit_test.ts index d23bcdf1115..af34562867e 100644 --- a/extension/chrome/dev/ci_unit_test.ts +++ b/extension/chrome/dev/ci_unit_test.ts @@ -55,4 +55,6 @@ for (const lib of libs) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access (window as any)[(lib as any).name] = lib; } +// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access +(window as any).MOCK_PORT = '[TEST_REPLACEABLE_MOCK_PORT]'; /* eslint-enable @typescript-eslint/no-explicit-any */ diff --git a/extension/chrome/elements/compose-modules/formatters/encrypted-mail-msg-formatter.ts b/extension/chrome/elements/compose-modules/formatters/encrypted-mail-msg-formatter.ts index 1e4b354e632..0606f400308 100644 --- a/extension/chrome/elements/compose-modules/formatters/encrypted-mail-msg-formatter.ts +++ b/extension/chrome/elements/compose-modules/formatters/encrypted-mail-msg-formatter.ts @@ -124,7 +124,7 @@ export class EncryptedMsgMailFormatter extends BaseMailFormatter { const pubkeyMsgData = { ...newMsg, recipients: pubkeyRecipients, - // brackets are required for test emails like '@test:8001' + // brackets are required for test emails like '@test' replyTo: replyToForMessageSentToPubkeyRecipients.length ? Str.formatEmailList([newMsg.from, ...replyToForMessageSentToPubkeyRecipients], true) : undefined, diff --git a/extension/js/common/core/common.ts b/extension/js/common/core/common.ts index d2b554e069b..6aac74e249f 100644 --- a/extension/js/common/core/common.ts +++ b/extension/js/common/core/common.ts @@ -5,6 +5,7 @@ import { base64decode, base64encode } from '../platform/util.js'; import { Xss } from '../platform/xss.js'; import { Buf } from './buf.js'; +import { MOCK_PORT } from './const.js'; export type Dict = { [key: string]: T }; export type UrlParam = string | number | null | undefined | boolean | string[]; @@ -159,7 +160,7 @@ export class Str { if (email.includes(' ')) { return false; } - email = email.replace(/\:8001$/, ''); // for MOCK tests, todo: remove from production + email = email.replace(new RegExp(`:${MOCK_PORT}$`), ''); // for MOCK tests, todo: remove from production // `localhost` is a valid top-level domain for an email address, otherwise we require a second-level domain to be present return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|localhost|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i.test( email diff --git a/extension/js/common/core/const.ts b/extension/js/common/core/const.ts index 018fb0dd6ed..6808ce45543 100644 --- a/extension/js/common/core/const.ts +++ b/extension/js/common/core/const.ts @@ -12,6 +12,7 @@ export const BACKEND_API_HOST = '[BUILD_REPLACEABLE_BACKEND_API_HOST]'; export const ATTESTER_API_HOST = '[BUILD_REPLACEABLE_ATTESTER_API_HOST]'; export const SHARED_TENANT_API_HOST = '[BUILD_REPLACEABLE_SHARED_TENANT_API_HOST]'; export const KEYS_OPENPGP_ORG_API_HOST = '[BUILD_REPLACEABLE_KEYS_OPENPGP_ORG_API_HOST]'; +export const MOCK_PORT = '[TEST_REPLACEABLE_MOCK_PORT]'; export const WKD_API_HOST = ''; // empty means choose host per recipient domain /** diff --git a/scripts/config-mock-build.sh b/scripts/config-mock-build.sh index d03193f241f..625c1c11a7f 100644 --- a/scripts/config-mock-build.sh +++ b/scripts/config-mock-build.sh @@ -2,5 +2,5 @@ BUILD_PATH=./build/test/mock-builds/port-$2-$(openssl rand -hex 12) mkdir -p $BUILD_PATH cp -r ./$1/* $BUILD_PATH -grep ":8001" $BUILD_PATH -lr | xargs sed -i.bak -e "s/\:8001/\:$2/g" +grep "\[TEST_REPLACEABLE_MOCK_PORT\]" $BUILD_PATH -lr | xargs sed -i.bak -e "s/\[TEST_REPLACEABLE_MOCK_PORT\]/$2/g" echo $BUILD_PATH \ No newline at end of file diff --git a/test/source/mock/fes/customer-url-fes-endpoints.ts b/test/source/mock/fes/customer-url-fes-endpoints.ts index cf69c0f3ebb..a81ce15006c 100644 --- a/test/source/mock/fes/customer-url-fes-endpoints.ts +++ b/test/source/mock/fes/customer-url-fes-endpoints.ts @@ -80,7 +80,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H '/api/v1/message/FES-MOCK-EXTERNAL-ID/gateway': async ({ body }, req) => { const port = parsePort(req); if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') { - // test: `compose - user@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal` + // test: `compose - user@standardsubdomainfes.localhost - PWD encrypted message with FES web portal` authenticate(req, isCustomIDPUsed); expect(body).to.match(messageIdRegex(port)); return {}; @@ -90,7 +90,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H '/api/v1/message/FES-MOCK-EXTERNAL-FOR-SENDER@DOMAIN.COM-ID/gateway': async ({ body }, req) => { const port = parsePort(req); if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') { - // test: `compose - user2@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES - Reply rendering` + // test: `compose - user2@standardsubdomainfes.localhost - PWD encrypted message with FES - Reply rendering` authenticate(req, isCustomIDPUsed); expect(body).to.match(messageIdRegex(port)); return {}; @@ -100,10 +100,10 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H '/api/v1/message/FES-MOCK-EXTERNAL-FOR-TO@EXAMPLE.COM-ID/gateway': async ({ body }, req) => { const port = parsePort(req); if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') { - // test: `compose - user@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal` - // test: `compose - user2@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES - Reply rendering` - // test: `compose - user3@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc` - // test: `compose - user4@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error` + // test: `compose - user@standardsubdomainfes.localhost - PWD encrypted message with FES web portal` + // test: `compose - user2@standardsubdomainfes.localhost - PWD encrypted message with FES - Reply rendering` + // test: `compose - user3@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - pubkey recipient in bcc` + // test: `compose - user4@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - some sends fail with BadRequest error` authenticate(req, isCustomIDPUsed); expect(body).to.match(messageIdRegex(port)); return {}; @@ -113,7 +113,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H '/api/v1/message/FES-MOCK-EXTERNAL-FOR-BCC@EXAMPLE.COM-ID/gateway': async ({ body }, req) => { const port = parsePort(req); if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') { - // test: `compose - user@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal` + // test: `compose - user@standardsubdomainfes.localhost - PWD encrypted message with FES web portal` authenticate(req, isCustomIDPUsed); expect(body).to.match(messageIdRegex(port)); return {}; @@ -121,7 +121,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H throw new HttpClientErr('Not Found', 404); }, '/api/v1/message/FES-MOCK-EXTERNAL-FOR-GATEWAYFAILURE@EXAMPLE.COM-ID/gateway': async () => { - // test: `user4@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error` + // test: `user4@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - a send fails with gateway update error` throw new HttpClientErr(`Test error`, Status.BAD_REQUEST); }, }; diff --git a/test/source/mock/fes/shared-tenant-fes-endpoints.ts b/test/source/mock/fes/shared-tenant-fes-endpoints.ts index 7280aecf4b4..0d41016adf6 100644 --- a/test/source/mock/fes/shared-tenant-fes-endpoints.ts +++ b/test/source/mock/fes/shared-tenant-fes-endpoints.ts @@ -159,7 +159,7 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined): }, '/shared-tenant-fes/api/v1/message/FES-MOCK-EXTERNAL-ID/gateway': async ({ body }, req) => { if (req.method === 'POST') { - // test: `compose - user@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal` + // test: `compose - user@standardsubdomainfes.localhost - PWD encrypted message with FES web portal` authenticate(req, 'oidc'); expect(body).to.match(messageIdRegexForRequest(req)); return {}; @@ -168,7 +168,7 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined): }, '/shared-tenant-fes/api/v1/message/FES-MOCK-EXTERNAL-FOR-SENDER@DOMAIN.COM-ID/gateway': async ({ body }, req) => { if (req.method === 'POST') { - // test: `compose - user2@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES - Reply rendering` + // test: `compose - user2@standardsubdomainfes.localhost - PWD encrypted message with FES - Reply rendering` authenticate(req, 'oidc'); expect(body).to.match(messageIdRegexForRequest(req)); return {}; @@ -177,10 +177,10 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined): }, '/shared-tenant-fes/api/v1/message/FES-MOCK-EXTERNAL-FOR-TO@EXAMPLE.COM-ID/gateway': async ({ body }, req) => { if (req.method === 'POST') { - // test: `compose - user@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal` - // test: `compose - user2@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES - Reply rendering` - // test: `compose - user3@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc` - // test: `compose - user4@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error` + // test: `compose - user@standardsubdomainfes.localhost - PWD encrypted message with FES web portal` + // test: `compose - user2@standardsubdomainfes.localhost - PWD encrypted message with FES - Reply rendering` + // test: `compose - user3@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - pubkey recipient in bcc` + // test: `compose - user4@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - some sends fail with BadRequest error` authenticate(req, 'oidc'); expect(body).to.match(messageIdRegexForRequest(req)); return {}; @@ -189,7 +189,7 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined): }, '/shared-tenant-fes/api/v1/message/FES-MOCK-EXTERNAL-FOR-BCC@EXAMPLE.COM-ID/gateway': async ({ body }, req) => { if (req.method === 'POST') { - // test: `compose - user@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal` + // test: `compose - user@standardsubdomainfes.localhost - PWD encrypted message with FES web portal` authenticate(req, 'oidc'); expect(body).to.match(messageIdRegexForRequest(req)); return {}; @@ -197,7 +197,7 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined): throw new HttpClientErr('Not Found', 404); }, '/shared-tenant-fes/api/v1/message/FES-MOCK-EXTERNAL-FOR-GATEWAYFAILURE@EXAMPLE.COM-ID/gateway': async () => { - // test: `user4@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error` + // test: `user4@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - a send fails with gateway update error` throw new HttpClientErr(`Test error`, Status.BAD_REQUEST); }, }; diff --git a/test/source/mock/google/exported-messages/message-export-1803be3182d1937b.json b/test/source/mock/google/exported-messages/message-export-1803be3182d1937b.json index ed7d985373a..c4716ecc9cc 100644 --- a/test/source/mock/google/exported-messages/message-export-1803be3182d1937b.json +++ b/test/source/mock/google/exported-messages/message-export-1803be3182d1937b.json @@ -1,5 +1,5 @@ { - "acctEmail": "user2@standardsubdomainfes.localhost:8001", + "acctEmail": "user2@standardsubdomainfes.localhost", "full": { "id": "1803be3182d1937b", "threadId": "1803be2e506153d2", diff --git a/test/source/mock/lib/oauth.ts b/test/source/mock/lib/oauth.ts index a47b8d71a66..491c5aef05d 100644 --- a/test/source/mock/lib/oauth.ts +++ b/test/source/mock/lib/oauth.ts @@ -5,8 +5,9 @@ import { HttpClientErr, Status } from './api'; import { Buf } from '../../core/buf'; import { Str } from '../../core/common'; import { issuedCustomIDPIdTokens, issuedGoogleIDPIdTokens } from '../fes/customer-url-fes-endpoints'; +import { MOCK_PORT } from '../../core/const'; -const authURL = 'https://localhost:8001'; +const authURL = `https://localhost:${MOCK_PORT}`; export class OauthMock { public static customIDPClientId = 'custom-test-idp-client-id'; diff --git a/test/source/test.ts b/test/source/test.ts index 2b58d275049..881997bf7e3 100644 --- a/test/source/test.ts +++ b/test/source/test.ts @@ -8,9 +8,11 @@ import { BrowserHandle, BrowserPool } from './browser'; import { AvaContext, TestContext, getDebugHtmlAtts, minutes, standaloneTestTimeout } from './tests/tooling'; import { Util, getParsedCliParams } from './util'; -import { mkdirSync, realpathSync, writeFileSync } from 'fs'; +import { mkdirSync, realpathSync } from 'fs'; +import { writeFile } from 'fs/promises'; import { TestUrls } from './browser/test-urls'; import { startAllApisMock } from './mock/all-apis-mock'; +import { reportedErrors } from './mock/fes/shared-tenant-fes-endpoints'; import { defineComposeTests } from './tests/compose'; import { defineContentScriptTests } from './tests/content-script'; import { defineDecryptTests } from './tests/decrypt'; @@ -21,7 +23,6 @@ import { defineSettingsTests } from './tests/settings'; import { defineSetupTests } from './tests/setup'; import { defineUnitBrowserTests } from './tests/unit-browser'; import { defineUnitNodeTests } from './tests/unit-node'; -import { reportedErrors } from './mock/fes/shared-tenant-fes-endpoints'; export const { testVariant, testGroup, oneIfNotPooled, buildDir, isMock } = getParsedCliParams(); export const internalTestState = { expectIntentionalErrReport: false }; // updated when a particular test that causes an error is run @@ -106,7 +107,6 @@ const startMockApiAndCopyBuild = async (t: AvaContext) => { const address = mockApi.server.address(); if (typeof address === 'object' && address) { const result = await asyncExec(`sh ./scripts/config-mock-build.sh ${buildDir} ${address.port}`); - t.context.extensionDir = result.stdout; t.context.urls = new TestUrls(await browserPool.getExtensionId(t), address.port); } else { @@ -128,7 +128,7 @@ const saveBrowserLog = async (t: AvaContext, browser: BrowserHandle) => { const input = JSON.stringify(item.input); const output = JSON.stringify(item.output, undefined, 2); const file = `./test/tmp/${t.title}-${i}.txt`; - writeFileSync(file, `in: ${input}\n\nout: ${output}`); + await writeFile(file, `in: ${input}\n\nout: ${output}`); t.log(`browser debug written to file: ${file}`); } } catch (e) { @@ -219,7 +219,7 @@ test.afterEach.always('finalize', async t => { const fileName = `debugHtmlAttachment-${testVariant}-${failRnd}-${i}.html`; const filePath = `${debugArtifactDir}/${fileName}`; console.info(`Writing debug file ${fileName}`); - writeFileSync(filePath, debugHtmlAttachments[i]); + await writeFile(filePath, debugHtmlAttachments[i]); try { await asyncExec(`artifact push job ${filePath}`); } catch { diff --git a/test/source/tests/browser-unit-tests/unit-Sks.js b/test/source/tests/browser-unit-tests/unit-Sks.js index 6195b4d661e..3074f315189 100644 --- a/test/source/tests/browser-unit-tests/unit-Sks.js +++ b/test/source/tests/browser-unit-tests/unit-Sks.js @@ -23,7 +23,7 @@ BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - trailing slash`); (async () => { const email = 'john.doe@example.com'; - const sks = new Sks('https://localhost:8001/'); + const sks = new Sks(`https://localhost:${MOCK_PORT}/`); const { pubkey } = await sks.lookupEmail(email); const key = await KeyUtil.parse(pubkey); if (key.id !== '094C3CBA696FA009F6015C473B635D858A1DB5E0') { @@ -35,7 +35,7 @@ BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - trailing slash`); BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - no trailing slash`); (async () => { const email = 'john.doe@example.com'; - const sks = new Sks('https://localhost:8001'); + const sks = new Sks(`https://localhost:${MOCK_PORT}`); const { pubkey } = await sks.lookupEmail(email); const key = await KeyUtil.parse(pubkey); if (key.id !== '094C3CBA696FA009F6015C473B635D858A1DB5E0') { @@ -62,7 +62,7 @@ BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - server down`); BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - not found`); (async () => { const email = 'nobody@example.com'; - const sks = new Sks('https://localhost:8001/'); + const sks = new Sks(`https://localhost:${MOCK_PORT}/`); const { pubkey } = await sks.lookupEmail(email); if (pubkey !== null) { throw Error(`expected pubkey=null but got ${pubkey}`); diff --git a/test/source/tests/compose.ts b/test/source/tests/compose.ts index f5c0ad46bf0..369033df536 100644 --- a/test/source/tests/compose.ts +++ b/test/source/tests/compose.ts @@ -3062,7 +3062,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te ); test( - 'user@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal', + 'user@standardsubdomainfes.localhost - PWD encrypted message with FES web portal', testWithBrowser(async (t, browser) => { const port = t.context.urls?.port; t.context.mockApi!.configProvider = new ConfigurationProvider({ @@ -3122,7 +3122,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te ); test( - 'user2@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES - Reply rendering', + 'user2@standardsubdomainfes.localhost - PWD encrypted message with FES - Reply rendering', testWithBrowser(async (t, browser) => { t.context.mockApi!.configProvider = new ConfigurationProvider({ attester: { @@ -3218,7 +3218,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te ); test( - 'user3@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc', + 'user3@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - pubkey recipient in bcc', testWithBrowser(async (t, browser) => { t.context.mockApi!.configProvider = new ConfigurationProvider({ attester: { @@ -3255,7 +3255,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te ); test( - 'user4@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error', + 'user4@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - a send fails with gateway update error', testWithBrowser(async (t, browser) => { t.context.mockApi!.configProvider = new ConfigurationProvider({ attester: { diff --git a/test/source/tests/flaky.ts b/test/source/tests/flaky.ts index 49c04459aaf..35ba336439b 100644 --- a/test/source/tests/flaky.ts +++ b/test/source/tests/flaky.ts @@ -272,7 +272,7 @@ export const defineFlakyTests = (testVariant: TestVariant, testWithBrowser: Test ); test( - 'user4@standardsubdomainfes.localhost:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error', + 'user4@standardsubdomainfes.localhost - PWD encrypted message with FES web portal - some sends fail with BadRequest error', testWithBrowser( async (t, browser) => { t.context.mockApi!.configProvider = new ConfigurationProvider({ diff --git a/test/source/tests/setup.ts b/test/source/tests/setup.ts index 212a2250959..d14f0c55fc4 100644 --- a/test/source/tests/setup.ts +++ b/test/source/tests/setup.ts @@ -2269,7 +2269,7 @@ AN8G3r5Htj8olot+jm9mIa5XLXWzMNUZgg== ); test( - 'user@standardsubdomainfes.localhost:8001 - uses FES on standard domain', + 'user@standardsubdomainfes.localhost - uses FES on standard domain', testWithBrowser(async (t, browser) => { t.context.mockApi!.configProvider = new ConfigurationProvider({ attester: { diff --git a/test/source/tests/unit-browser.ts b/test/source/tests/unit-browser.ts index 2a3abc75ee4..ffd9980fdb6 100644 --- a/test/source/tests/unit-browser.ts +++ b/test/source/tests/unit-browser.ts @@ -73,11 +73,10 @@ export const defineUnitBrowserTests = (testVariant: TestVariant, testWithBrowser (window as any).testConstants = object; }, testConstants); // prepare code to run - const testCodeWithMockPort = testCode.replace(/\:8001/g, ':' + t.context.urls?.port); const runThisCodeInBrowser = ` (async () => { try { - return await ${testCodeWithMockPort} + return await ${testCode} } catch (e) { return "unit test threw something:" + String(e) + "\\n\\n" + e.stack; } diff --git a/tooling/build-types-and-manifests.ts b/tooling/build-types-and-manifests.ts index 78adcecec48..d8e4d1461be 100644 --- a/tooling/build-types-and-manifests.ts +++ b/tooling/build-types-and-manifests.ts @@ -1,6 +1,7 @@ /* ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com */ import { readFileSync, writeFileSync } from 'fs'; import { execSync as exec } from 'child_process'; +const MOCK_PORT = '[TEST_REPLACEABLE_MOCK_PORT]'; /** * This file was originally two files: one that edited manifests, and one that copied build folders and edited mock versions @@ -114,8 +115,8 @@ addManifest('chrome-enterprise', manifest => { const CHROME_CONSUMER = 'chrome-consumer'; const CHROME_ENTERPRISE = 'chrome-enterprise'; const MOCK_HOST: { [buildType: string]: string } = { - 'chrome-consumer': 'https://localhost:8001', - 'chrome-enterprise': 'https://google.mock.localhost:8001', + 'chrome-consumer': `https://localhost:${MOCK_PORT}`, + 'chrome-enterprise': `https://google.mock.localhost:${MOCK_PORT}`, }; const buildDir = (buildType: string) => `./build/${buildType}`; @@ -153,7 +154,7 @@ const updateEnterpriseBuild = () => { const makeMockBuild = (sourceBuildType: string) => { const mockBuildType = `${sourceBuildType}-mock`; - const mockGmailPageHost = 'gmail.localhost:8001'; + const mockGmailPageHost = `gmail.localhost:${MOCK_PORT}`; const mockGmailPage = `https://${mockGmailPageHost}`; exec(`cp -r ${buildDir(sourceBuildType)} ${buildDir(mockBuildType)}`); const editor = (code: string) => { @@ -162,11 +163,11 @@ const makeMockBuild = (sourceBuildType: string) => { /const (OAUTH_GOOGLE_API_HOST|GMAIL_GOOGLE_API_HOST|PEOPLE_GOOGLE_API_HOST|GOOGLE_OAUTH_SCREEN_HOST) = [^;]+;/g, `const $1 = '${MOCK_HOST[sourceBuildType]}';` ) - .replace(/const (BACKEND_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:8001/api/';`) - .replace(/const (ATTESTER_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:8001/attester/';`) - .replace(/const (KEYS_OPENPGP_ORG_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:8001/keys-openpgp-org/';`) - .replace(/const (SHARED_TENANT_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:8001/shared-tenant-fes';`) - .replace(/const (WKD_API_HOST) = '';/g, `const $1 = 'https://localhost:8001';`); + .replace(/const (BACKEND_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:${MOCK_PORT}/api/';`) + .replace(/const (ATTESTER_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:${MOCK_PORT}/attester/';`) + .replace(/const (KEYS_OPENPGP_ORG_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:${MOCK_PORT}/keys-openpgp-org/';`) + .replace(/const (SHARED_TENANT_API_HOST) = [^;]+;/g, `const $1 = 'https://localhost:${MOCK_PORT}/shared-tenant-fes';`) + .replace(/const (WKD_API_HOST) = '';/g, `const $1 = 'https://localhost:${MOCK_PORT}';`); }; edit(`${buildDir(mockBuildType)}/js/common/core/const.js`, editor); edit(`${buildDir(mockBuildType)}/js/common/platform/catch.js`, editor); @@ -174,7 +175,7 @@ const makeMockBuild = (sourceBuildType: string) => { edit(`${buildDir(mockBuildType)}/manifest.json`, code => code .replace(/https:\/\/mail\.google\.com/g, mockGmailPage) - .replace(/https:\/\/www\.google\.com/g, 'https://google.localhost:8001') + .replace(/https:\/\/www\.google\.com/g, `https://google.localhost:${MOCK_PORT}`) .replace(/https:\/\/\*\.google.com\/\*/, 'https://google.localhost/*') ); };