Files
parcel/tests/encryption.test.ts
2021-04-11 23:27:13 +02:00

77 lines
2.1 KiB
TypeScript

import puppeteer, { Browser, Page } from 'puppeteer';
import path from 'path';
import fs from 'fs-extra';
import { nanoid } from 'nanoid';
import openpgp, { key, message } from 'openpgp';
const sleep = (time: number) => new Promise((resolve) => setTimeout(resolve, time));
describe('encryption', () => {
let browser: Browser;
let page: Page;
let tmpDir: string;
let keys: key.KeyResult;
const getText = async (elm: any) => {
const text = await page.evaluate(el => el.textContent, elm);
return text;
}
beforeAll(async () => {
tmpDir = path.resolve(`.tmp/${nanoid}`);
await fs.mkdirp(tmpDir);
const data = await fs.readFile(
path.join(__dirname, '..', 'test-assets', 'key'),
'utf-8',
);
keys = await key.readArmored(data);
});
beforeEach(async () => {
browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
]
});
page = await browser.newPage();
(page as any)._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: tmpDir,
});
await page.goto(testUrl, {
waitUntil: 'networkidle2',
});
});
afterEach(async () => {
await browser.close();
});
it('should be able to encrypt a text', async () => {
await page.click('.send-btn');
await page.click('.add-text-tab');
await page.type('.msg-title', 'Foo');
await page.type('.msg-body', 'Bar');
await page.click('.msg-add');
await sleep(300);
const items = await page.$$('.msg-item');
expect(items.length).toBe(1);
const [item] = items;
const title = await item.$('.ant-list-item-meta-title');
const titleText = await getText(title);
expect(titleText).toBe('Foo.txt.asc');
await page.click('.msg-download');
await sleep(300);
const downloadPath = path.join(tmpDir, 'Foo.txt.asc');
expect(fs.existsSync(downloadPath)).toBe(true);
const data = await fs.readFile(downloadPath, 'utf-8');
const decrypted = await openpgp.decrypt({
message: await message.readArmored(data),
privateKeys: keys.keys[0],
});
expect(decrypted.data).toBe('Bar');
});
});