Skip to content

Commit ca7003e

Browse files
author
Tom J
committed
updated mock tests to stop using real backend
1 parent 6f77632 commit ca7003e

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

test/source/mock/backend/backend-endpoints.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com */
22

3-
import * as request from 'fc-node-requests';
4-
53
import { HttpAuthErr, HttpClientErr } from '../lib/api';
64

75
import { BackendData } from './backend-data';
@@ -14,30 +12,6 @@ import { expect } from 'chai';
1412

1513
export const mockBackendData = new BackendData(oauth);
1614

17-
const fwdToRealBackend = async (parsed: any, req: IncomingMessage): Promise<string> => {
18-
// we are forwarding this request to backend, but we are not properly authenticated with real backend
19-
// better remove authentication: requests that we currently forward during tests don't actually require it
20-
delete req.headers.host;
21-
delete req.headers['content-length'];
22-
const forwarding: any = { headers: req.headers, url: `https://flowcrypt.com${req.url}` };
23-
if (req.url!.includes('message/upload')) {
24-
// Removing mock auth when forwarding request to real backend at ${req.url}
25-
// here a bit more difficult, because the request was already encoded as form-data
26-
parsed.body = (parsed.body as string).replace(/(-----END PGP MESSAGE-----\r\n\r\n------[A-Za-z0-9]+)(.|\r\n)*$/gm, (_, found) => `${found}--\r\n`);
27-
forwarding.body = parsed.body; // FORM-DATA
28-
const r = await request.post(forwarding);
29-
return r.body; // already json-stringified for this call, maybe because backend doesn't return proper content-type
30-
}
31-
if (parsed.body && typeof parsed.body === 'object' && parsed.body.account && parsed.body.uuid) {
32-
// Removing mock auth when forwarding request to real backend at ${req.url}
33-
delete parsed.body.account;
34-
delete parsed.body.uuid;
35-
}
36-
forwarding.json = parsed.body; // JSON
37-
const r = await request.post(forwarding);
38-
return JSON.stringify(r.body);
39-
};
40-
4115
export const mockBackendEndpoints: HandlersDefinition = {
4216
'/api/account/login': async ({ body }, req) => {
4317
const parsed = throwIfNotPostWithAuth(body, req);
@@ -78,11 +52,15 @@ export const mockBackendEndpoints: HandlersDefinition = {
7852
expect((body as any).email).to.equal('flowcrypt.compatibility@gmail.com');
7953
return { sent: true, text: 'Feedback sent' };
8054
},
81-
'/api/message/presign_files': fwdToRealBackend,
82-
'/api/message/confirm_files': fwdToRealBackend,
83-
'/api/message/upload': fwdToRealBackend,
84-
'/api/link/message': fwdToRealBackend,
85-
'/api/link/me': fwdToRealBackend,
55+
'/api/message/upload': async ({ }) => {
56+
return { short: '0000000000', url: 'https://example.com/msg-123', admin_code: 'mocked-admin-code' };
57+
},
58+
'/api/link/message': async ({ }) => {
59+
return { "url": "https://example.com/msg-123", "repliable": "False", "expire": "2100-05-18 16:31:28", "expired": "False", "deleted": "False" };
60+
},
61+
'/api/link/me': async ({ }, req) => {
62+
throw new Error(`${req.url} mock not implemented`);
63+
},
8664
};
8765

8866
const throwIfNotPostWithAuth = (body: unknown, req: IncomingMessage) => {

test/source/test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { TestUrls } from './browser/test-urls';
2424
export const { testVariant, testGroup, oneIfNotPooled, buildDir, isMock } = getParsedCliParams();
2525
export const internalTestState = { expectIntentionalErrReport: false }; // updated when a particular test that causes an error is run
2626
const DEBUG_BROWSER_LOG = false; // set to true to print / export information from browser
27+
const DEBUG_MOCK_LOG = false; // se to true to print mock server logs
2728

2829
process.setMaxListeners(60);
2930

@@ -52,7 +53,12 @@ ava.before('set config and mock api', async t => {
5253
Config.extensionId = await browserPool.getExtensionId(t);
5354
console.info(`Extension url: chrome-extension://${Config.extensionId}`);
5455
if (isMock) {
55-
const mockApi = await mock(line => mockApiLogs.push(line));
56+
const mockApi = await mock(line => {
57+
if (DEBUG_MOCK_LOG) {
58+
console.log(line);
59+
}
60+
mockApiLogs.push(line);
61+
});
5662
closeMockApi = mockApi.close;
5763
}
5864
t.pass();

test/source/tests/flaky.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,7 @@ export const defineFlakyTests = (testVariant: TestVariant, testWithBrowser: Test
108108
await ComposePageRecipe.sendAndClose(composePage, { password: msgPwd });
109109
const msg = (await GoogleData.withInitializedData('flowcrypt.compatibility@gmail.com')).getMessageBySubject(subject)!;
110110
const webDecryptUrl = msg.payload!.body!.data!.replace(/&#x2F;/g, '/').match(/https:\/\/flowcrypt.com\/[a-z0-9A-Z]+/g)![0];
111-
// while this test runs on a mock, it forwards the message/upload call to real backend - see `fwdToRealBackend`
112-
// that's why we are able to test the message on real flowcrypt.com/api and web.
113-
const webDecryptPage = await browser.newPage(t, webDecryptUrl);
114-
await webDecryptPage.waitAndType('@input-msg-pwd', msgPwd);
115-
await webDecryptPage.waitAndClick('@action-decrypt');
116-
await webDecryptPage.waitForContent('@container-pgp-decrypted-content', subject);
117-
await webDecryptPage.waitForContent('@container-pgp-decrypted-content', 'flowcrypt.compatibility test footer with an img');
118-
await webDecryptPage.waitAll('@container-att-name(small.txt)');
119-
const fileText = await webDecryptPage.awaitDownloadTriggeredByClicking('@container-att-name(small.txt)');
120-
expect(fileText.toString()).to.equal(`small text file\nnot much here\nthis worked\n`);
111+
expect(webDecryptUrl).to.not.be.empty('missing webDecryptUrl');
121112
}));
122113

123114
ava.default(`[unit][Stream.readToEnd] efficiently handles multiple chunks`, async t => {

0 commit comments

Comments
 (0)