Skip to content

Commit 7adc496

Browse files
author
Ioan Moldovan
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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 {

0 commit comments

Comments
 (0)