Skip to content

Commit 7adc496

Browse files
authored
#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 d7a2022. * fix: mock port variable * Revert "fix: mock port variable" This reverts commit 017ac27. * fix: pr reviews
1 parent 9c8c385 commit 7adc496

File tree

16 files changed

+51
-46
lines changed

16 files changed

+51
-46
lines changed

extension/chrome/dev/ci_unit_test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ for (const lib of libs) {
5555
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
5656
(window as any)[(lib as any).name] = lib;
5757
}
58+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
59+
(window as any).MOCK_PORT = '[TEST_REPLACEABLE_MOCK_PORT]';
5860
/* eslint-enable @typescript-eslint/no-explicit-any */

extension/chrome/elements/compose-modules/formatters/encrypted-mail-msg-formatter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class EncryptedMsgMailFormatter extends BaseMailFormatter {
124124
const pubkeyMsgData = {
125125
...newMsg,
126126
recipients: pubkeyRecipients,
127-
// brackets are required for test emails like '@test:8001'
127+
// brackets are required for test emails like '@test'
128128
replyTo: replyToForMessageSentToPubkeyRecipients.length
129129
? Str.formatEmailList([newMsg.from, ...replyToForMessageSentToPubkeyRecipients], true)
130130
: undefined,

extension/js/common/core/common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { base64decode, base64encode } from '../platform/util.js';
66
import { Xss } from '../platform/xss.js';
77
import { Buf } from './buf.js';
8+
import { MOCK_PORT } from './const.js';
89

910
export type Dict<T> = { [key: string]: T };
1011
export type UrlParam = string | number | null | undefined | boolean | string[];
@@ -159,7 +160,7 @@ export class Str {
159160
if (email.includes(' ')) {
160161
return false;
161162
}
162-
email = email.replace(/\:8001$/, ''); // for MOCK tests, todo: remove from production
163+
email = email.replace(new RegExp(`:${MOCK_PORT}$`), ''); // for MOCK tests, todo: remove from production
163164
// `localhost` is a valid top-level domain for an email address, otherwise we require a second-level domain to be present
164165
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(
165166
email

extension/js/common/core/const.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const BACKEND_API_HOST = '[BUILD_REPLACEABLE_BACKEND_API_HOST]';
1212
export const ATTESTER_API_HOST = '[BUILD_REPLACEABLE_ATTESTER_API_HOST]';
1313
export const SHARED_TENANT_API_HOST = '[BUILD_REPLACEABLE_SHARED_TENANT_API_HOST]';
1414
export const KEYS_OPENPGP_ORG_API_HOST = '[BUILD_REPLACEABLE_KEYS_OPENPGP_ORG_API_HOST]';
15+
export const MOCK_PORT = '[TEST_REPLACEABLE_MOCK_PORT]';
1516
export const WKD_API_HOST = ''; // empty means choose host per recipient domain
1617

1718
/**

scripts/config-mock-build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
BUILD_PATH=./build/test/mock-builds/port-$2-$(openssl rand -hex 12)
33
mkdir -p $BUILD_PATH
44
cp -r ./$1/* $BUILD_PATH
5-
grep ":8001" $BUILD_PATH -lr | xargs sed -i.bak -e "s/\:8001/\:$2/g"
5+
grep "\[TEST_REPLACEABLE_MOCK_PORT\]" $BUILD_PATH -lr | xargs sed -i.bak -e "s/\[TEST_REPLACEABLE_MOCK_PORT\]/$2/g"
66
echo $BUILD_PATH

test/source/mock/fes/customer-url-fes-endpoints.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
8080
'/api/v1/message/FES-MOCK-EXTERNAL-ID/gateway': async ({ body }, req) => {
8181
const port = parsePort(req);
8282
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
83-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
83+
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
8484
authenticate(req, isCustomIDPUsed);
8585
expect(body).to.match(messageIdRegex(port));
8686
return {};
@@ -90,7 +90,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
9090
'/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
9191
const port = parsePort(req);
9292
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
93-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
93+
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
9494
authenticate(req, isCustomIDPUsed);
9595
expect(body).to.match(messageIdRegex(port));
9696
return {};
@@ -100,10 +100,10 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
100100
'/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
101101
const port = parsePort(req);
102102
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
103-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
104-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
105-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc`
106-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
103+
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
104+
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
105+
// test: `compose - [email protected] - PWD encrypted message with FES web portal - pubkey recipient in bcc`
106+
// test: `compose - [email protected] - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
107107
authenticate(req, isCustomIDPUsed);
108108
expect(body).to.match(messageIdRegex(port));
109109
return {};
@@ -113,15 +113,15 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
113113
'/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
114114
const port = parsePort(req);
115115
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
116-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
116+
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
117117
authenticate(req, isCustomIDPUsed);
118118
expect(body).to.match(messageIdRegex(port));
119119
return {};
120120
}
121121
throw new HttpClientErr('Not Found', 404);
122122
},
123123
'/api/v1/message/[email protected]/gateway': async () => {
124-
// test: `[email protected]:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error`
124+
// test: `[email protected] - PWD encrypted message with FES web portal - a send fails with gateway update error`
125125
throw new HttpClientErr(`Test error`, Status.BAD_REQUEST);
126126
},
127127
};

test/source/mock/fes/shared-tenant-fes-endpoints.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined):
159159
},
160160
'/shared-tenant-fes/api/v1/message/FES-MOCK-EXTERNAL-ID/gateway': async ({ body }, req) => {
161161
if (req.method === 'POST') {
162-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
162+
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
163163
authenticate(req, 'oidc');
164164
expect(body).to.match(messageIdRegexForRequest(req));
165165
return {};
@@ -168,7 +168,7 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined):
168168
},
169169
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
170170
if (req.method === 'POST') {
171-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
171+
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
172172
authenticate(req, 'oidc');
173173
expect(body).to.match(messageIdRegexForRequest(req));
174174
return {};
@@ -177,10 +177,10 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined):
177177
},
178178
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
179179
if (req.method === 'POST') {
180-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
181-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES - Reply rendering`
182-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - pubkey recipient in bcc`
183-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
180+
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
181+
// test: `compose - [email protected] - PWD encrypted message with FES - Reply rendering`
182+
// test: `compose - [email protected] - PWD encrypted message with FES web portal - pubkey recipient in bcc`
183+
// test: `compose - [email protected] - PWD encrypted message with FES web portal - some sends fail with BadRequest error`
184184
authenticate(req, 'oidc');
185185
expect(body).to.match(messageIdRegexForRequest(req));
186186
return {};
@@ -189,15 +189,15 @@ export const getMockSharedTenantFesEndpoints = (config: FesConfig | undefined):
189189
},
190190
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async ({ body }, req) => {
191191
if (req.method === 'POST') {
192-
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
192+
// test: `compose - [email protected] - PWD encrypted message with FES web portal`
193193
authenticate(req, 'oidc');
194194
expect(body).to.match(messageIdRegexForRequest(req));
195195
return {};
196196
}
197197
throw new HttpClientErr('Not Found', 404);
198198
},
199199
'/shared-tenant-fes/api/v1/message/[email protected]/gateway': async () => {
200-
// test: `[email protected]:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error`
200+
// test: `[email protected] - PWD encrypted message with FES web portal - a send fails with gateway update error`
201201
throw new HttpClientErr(`Test error`, Status.BAD_REQUEST);
202202
},
203203
};

test/source/mock/google/exported-messages/message-export-1803be3182d1937b.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"acctEmail": "[email protected]:8001",
2+
"acctEmail": "[email protected]",
33
"full": {
44
"id": "1803be3182d1937b",
55
"threadId": "1803be2e506153d2",

test/source/mock/lib/oauth.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { HttpClientErr, Status } from './api';
55
import { Buf } from '../../core/buf';
66
import { Str } from '../../core/common';
77
import { issuedCustomIDPIdTokens, issuedGoogleIDPIdTokens } from '../fes/customer-url-fes-endpoints';
8+
import { MOCK_PORT } from '../../core/const';
89

9-
const authURL = 'https://localhost:8001';
10+
const authURL = `https://localhost:${MOCK_PORT}`;
1011

1112
export class OauthMock {
1213
public static customIDPClientId = 'custom-test-idp-client-id';

test/source/test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import { BrowserHandle, BrowserPool } from './browser';
88
import { AvaContext, TestContext, getDebugHtmlAtts, minutes, standaloneTestTimeout } from './tests/tooling';
99
import { Util, getParsedCliParams } from './util';
1010

11-
import { mkdirSync, realpathSync, writeFileSync } from 'fs';
11+
import { mkdirSync, realpathSync } from 'fs';
12+
import { writeFile } from 'fs/promises';
1213
import { TestUrls } from './browser/test-urls';
1314
import { startAllApisMock } from './mock/all-apis-mock';
15+
import { reportedErrors } from './mock/fes/shared-tenant-fes-endpoints';
1416
import { defineComposeTests } from './tests/compose';
1517
import { defineContentScriptTests } from './tests/content-script';
1618
import { defineDecryptTests } from './tests/decrypt';
@@ -21,7 +23,6 @@ import { defineSettingsTests } from './tests/settings';
2123
import { defineSetupTests } from './tests/setup';
2224
import { defineUnitBrowserTests } from './tests/unit-browser';
2325
import { defineUnitNodeTests } from './tests/unit-node';
24-
import { reportedErrors } from './mock/fes/shared-tenant-fes-endpoints';
2526

2627
export const { testVariant, testGroup, oneIfNotPooled, buildDir, isMock } = getParsedCliParams();
2728
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) => {
106107
const address = mockApi.server.address();
107108
if (typeof address === 'object' && address) {
108109
const result = await asyncExec(`sh ./scripts/config-mock-build.sh ${buildDir} ${address.port}`);
109-
110110
t.context.extensionDir = result.stdout;
111111
t.context.urls = new TestUrls(await browserPool.getExtensionId(t), address.port);
112112
} else {
@@ -128,7 +128,7 @@ const saveBrowserLog = async (t: AvaContext, browser: BrowserHandle) => {
128128
const input = JSON.stringify(item.input);
129129
const output = JSON.stringify(item.output, undefined, 2);
130130
const file = `./test/tmp/${t.title}-${i}.txt`;
131-
writeFileSync(file, `in: ${input}\n\nout: ${output}`);
131+
await writeFile(file, `in: ${input}\n\nout: ${output}`);
132132
t.log(`browser debug written to file: ${file}`);
133133
}
134134
} catch (e) {
@@ -219,7 +219,7 @@ test.afterEach.always('finalize', async t => {
219219
const fileName = `debugHtmlAttachment-${testVariant}-${failRnd}-${i}.html`;
220220
const filePath = `${debugArtifactDir}/${fileName}`;
221221
console.info(`Writing debug file ${fileName}`);
222-
writeFileSync(filePath, debugHtmlAttachments[i]);
222+
await writeFile(filePath, debugHtmlAttachments[i]);
223223
try {
224224
await asyncExec(`artifact push job ${filePath}`);
225225
} catch {

test/source/tests/browser-unit-tests/unit-Sks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - trailing slash`);
2424
(async () => {
2525
const email = '[email protected]';
26-
const sks = new Sks('https://localhost:8001/');
26+
const sks = new Sks(`https://localhost:${MOCK_PORT}/`);
2727
const { pubkey } = await sks.lookupEmail(email);
2828
const key = await KeyUtil.parse(pubkey);
2929
if (key.id !== '094C3CBA696FA009F6015C473B635D858A1DB5E0') {
@@ -35,7 +35,7 @@ BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - trailing slash`);
3535
BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - no trailing slash`);
3636
(async () => {
3737
const email = '[email protected]';
38-
const sks = new Sks('https://localhost:8001');
38+
const sks = new Sks(`https://localhost:${MOCK_PORT}`);
3939
const { pubkey } = await sks.lookupEmail(email);
4040
const key = await KeyUtil.parse(pubkey);
4141
if (key.id !== '094C3CBA696FA009F6015C473B635D858A1DB5E0') {
@@ -62,7 +62,7 @@ BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - server down`);
6262
BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - not found`);
6363
(async () => {
6464
const email = '[email protected]';
65-
const sks = new Sks('https://localhost:8001/');
65+
const sks = new Sks(`https://localhost:${MOCK_PORT}/`);
6666
const { pubkey } = await sks.lookupEmail(email);
6767
if (pubkey !== null) {
6868
throw Error(`expected pubkey=null but got ${pubkey}`);

test/source/tests/compose.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,7 @@ export const defineComposeTests = (testVariant: TestVariant, testWithBrowser: Te
30623062
);
30633063

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

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

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

32573257
test(
3258-
'[email protected]:8001 - PWD encrypted message with FES web portal - a send fails with gateway update error',
3258+
'[email protected] - PWD encrypted message with FES web portal - a send fails with gateway update error',
32593259
testWithBrowser(async (t, browser) => {
32603260
t.context.mockApi!.configProvider = new ConfigurationProvider({
32613261
attester: {

test/source/tests/flaky.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const defineFlakyTests = (testVariant: TestVariant, testWithBrowser: Test
272272
);
273273

274274
test(
275-
'[email protected]:8001 - PWD encrypted message with FES web portal - some sends fail with BadRequest error',
275+
'[email protected] - PWD encrypted message with FES web portal - some sends fail with BadRequest error',
276276
testWithBrowser(
277277
async (t, browser) => {
278278
t.context.mockApi!.configProvider = new ConfigurationProvider({

test/source/tests/setup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ AN8G3r5Htj8olot+jm9mIa5XLXWzMNUZgg==
22692269
);
22702270

22712271
test(
2272-
'[email protected]:8001 - uses FES on standard domain',
2272+
'[email protected] - uses FES on standard domain',
22732273
testWithBrowser(async (t, browser) => {
22742274
t.context.mockApi!.configProvider = new ConfigurationProvider({
22752275
attester: {

test/source/tests/unit-browser.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ export const defineUnitBrowserTests = (testVariant: TestVariant, testWithBrowser
7373
(window as any).testConstants = object;
7474
}, testConstants);
7575
// prepare code to run
76-
const testCodeWithMockPort = testCode.replace(/\:8001/g, ':' + t.context.urls?.port);
7776
const runThisCodeInBrowser = `
7877
(async () => {
7978
try {
80-
return await ${testCodeWithMockPort}
79+
return await ${testCode}
8180
} catch (e) {
8281
return "unit test threw something:" + String(e) + "\\n\\n" + e.stack;
8382
}

0 commit comments

Comments
 (0)