Skip to content

Commit

Permalink
#4953 Use test replaceable port (#5887)
Browse files Browse the repository at this point in the history
* 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 d7a2022.

* fix: mock port variable

* Revert "fix: mock port variable"

This reverts commit 017ac27.

* fix: pr reviews
  • Loading branch information
ioanmo226 authored Jan 7, 2025
1 parent 9c8c385 commit 7adc496
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 46 deletions.
2 changes: 2 additions & 0 deletions extension/chrome/dev/ci_unit_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion extension/js/common/core/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = { [key: string]: T };
export type UrlParam = string | number | null | undefined | boolean | string[];
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions extension/js/common/core/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
2 changes: 1 addition & 1 deletion scripts/config-mock-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 8 additions & 8 deletions test/source/mock/fes/customer-url-fes-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 - [email protected]:8001 - PWD encrypted message with FES web portal`
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
authenticate(req, isCustomIDPUsed);
expect(body).to.match(messageIdRegex(port));
return {};
Expand All @@ -90,7 +90,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
'/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
const port = parsePort(req);
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
authenticate(req, isCustomIDPUsed);
expect(body).to.match(messageIdRegex(port));
return {};
Expand All @@ -100,10 +100,10 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
'/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
const port = parsePort(req);
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc`
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
// test: `compose - [email protected] - PWD encrypted message with FES web portal - pubkey recipient in bcc`
// test: `compose - [email protected] - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
authenticate(req, isCustomIDPUsed);
expect(body).to.match(messageIdRegex(port));
return {};
Expand All @@ -113,15 +113,15 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
'/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
const port = parsePort(req);
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
authenticate(req, isCustomIDPUsed);
expect(body).to.match(messageIdRegex(port));
return {};
}
throw new HttpClientErr('Not Found', 404);
},
'/api/v1/message/[email protected]/gateway': async () => {
// test: `[email protected]:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error`
// test: `[email protected] - PWD encrypted message with FES web portal - a send fails with gateway update error`
throw new HttpClientErr(`Test error`, Status.BAD_REQUEST);
},
};
Expand Down
16 changes: 8 additions & 8 deletions test/source/mock/fes/shared-tenant-fes-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 - [email protected]:8001 - PWD encrypted message with FES web portal`
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
authenticate(req, 'oidc');
expect(body).to.match(messageIdRegexForRequest(req));
return {};
Expand All @@ -168,7 +168,7 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined):
},
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
if (req.method === 'POST') {
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
authenticate(req, 'oidc');
expect(body).to.match(messageIdRegexForRequest(req));
return {};
Expand All @@ -177,10 +177,10 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined):
},
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
if (req.method === 'POST') {
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc`
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
// test: `compose - [email protected] - PWD encrypted message with FES web portal - pubkey recipient in bcc`
// test: `compose - [email protected] - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
authenticate(req, 'oidc');
expect(body).to.match(messageIdRegexForRequest(req));
return {};
Expand All @@ -189,15 +189,15 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined):
},
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
if (req.method === 'POST') {
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
authenticate(req, 'oidc');
expect(body).to.match(messageIdRegexForRequest(req));
return {};
}
throw new HttpClientErr('Not Found', 404);
},
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async () => {
// test: `[email protected]:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error`
// test: `[email protected] - PWD encrypted message with FES web portal - a send fails with gateway update error`
throw new HttpClientErr(`Test error`, Status.BAD_REQUEST);
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"acctEmail": "[email protected]:8001",
"acctEmail": "[email protected]",
"full": {
"id": "1803be3182d1937b",
"threadId": "1803be2e506153d2",
Expand Down
3 changes: 2 additions & 1 deletion test/source/mock/lib/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 5 additions & 5 deletions test/source/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions test/source/tests/browser-unit-tests/unit-Sks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - trailing slash`);
(async () => {
const email = '[email protected]';
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') {
Expand All @@ -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 = '[email protected]';
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') {
Expand All @@ -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 = '[email protected]';
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}`);
Expand Down
8 changes: 4 additions & 4 deletions test/source/tests/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3062,7 +3062,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te
);

test(
'[email protected]:8001 - PWD encrypted message with FES web portal',
'[email protected] - PWD encrypted message with FES web portal',
testWithBrowser(async (t, browser) => {
const port = t.context.urls?.port;
t.context.mockApi!.configProvider = new ConfigurationProvider({
Expand Down Expand Up @@ -3122,7 +3122,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te
);

test(
'[email protected]:8001 - PWD encrypted message with FES - Reply rendering',
'[email protected] - PWD encrypted message with FES - Reply rendering',
testWithBrowser(async (t, browser) => {
t.context.mockApi!.configProvider = new ConfigurationProvider({
attester: {
Expand Down Expand Up @@ -3218,7 +3218,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te
);

test(
'[email protected]:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc',
'[email protected] - PWD encrypted message with FES web portal - pubkey recipient in bcc',
testWithBrowser(async (t, browser) => {
t.context.mockApi!.configProvider = new ConfigurationProvider({
attester: {
Expand Down Expand Up @@ -3255,7 +3255,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te
);

test(
'[email protected]:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error',
'[email protected] - 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: {
Expand Down
2 changes: 1 addition & 1 deletion test/source/tests/flaky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export const defineFlakyTests = (testVariant: TestVariant, testWithBrowser: Test
);

test(
'[email protected]:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error',
'[email protected] - PWD encrypted message with FES web portal - some sends fail with BadRequest error',
testWithBrowser(
async (t, browser) => {
t.context.mockApi!.configProvider = new ConfigurationProvider({
Expand Down
2 changes: 1 addition & 1 deletion test/source/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,7 @@ AN8G3r5Htj8olot+jm9mIa5XLXWzMNUZgg==
);

test(
'[email protected]:8001 - uses FES on standard domain',
'[email protected] - uses FES on standard domain',
testWithBrowser(async (t, browser) => {
t.context.mockApi!.configProvider = new ConfigurationProvider({
attester: {
Expand Down
3 changes: 1 addition & 2 deletions test/source/tests/unit-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 7adc496

Please sign in to comment.