Skip to content

Commit

Permalink
Add tests for new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwing committed Oct 31, 2022
1 parent 981f03b commit 0300a86
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ export async function encrypt<R>(
*
* @param {string} password - password to use for encryption
* @param {R} dataObj - data to encrypt
* @param {R} salt - salt used to encrypt
* @returns {Promise<DetailedEncryptionResult>} object with vault and exportedKeyString
*/
export async function encryptWithDetail<R>(
password: string,
dataObj: R,
salt = generateSalt(),
): Promise<DetailedEncryptionResult> {
const salt = generateSalt();
const key = await keyFromPassword(password, salt);
const exportedKeyString = await exportKey(key);
const vault = await encrypt(password, dataObj, key, salt);
Expand Down Expand Up @@ -200,7 +201,7 @@ export async function createKeyFromString(
* @param {CryptoKey} key - key to export
* @returns {string}
*/
async function exportKey(key: CryptoKey): Promise<string> {
export async function exportKey(key: CryptoKey): Promise<string> {
const exportedKey = await window.crypto.subtle.exportKey(EXPORT_FORMAT, key);
return JSON.stringify(exportedKey);
}
Expand Down
99 changes: 99 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ declare global {

const testPagePath = path.resolve(__dirname, 'index.html');

const SAMPLE_EXPORTED_KEY =
'{"alg":"A256GCM","ext":true,"k":"leW0IR00ACQp3SoWuITXQComCte7lwKLR9ztPlGkFeM","key_ops":["encrypt","decrypt"],"kty":"oct"}';

test.beforeEach(async ({ page }) => {
await page.goto(`file://${testPagePath}`);
});
Expand Down Expand Up @@ -94,6 +97,18 @@ test('encryptor:encrypt & decrypt', async ({ page }) => {
expect(decryptedObj).toStrictEqual(data);
});

test('encryptor:encryptWithDetail returns vault', async ({ page }) => {
const password = 'a sample passw0rd';
const data = { foo: 'data to encrypt' };

const encryptedDetail = await page.evaluate(
async (args) =>
await window.encryptor.encryptWithDetail(args.password, args.data),
{ data, password },
);
expect(typeof encryptedDetail.vault).toBe('string');
});

test('encryptor:encrypt & decrypt with wrong password', async ({ page }) => {
const password = 'a sample passw0rd';
const wrongPassword = 'a wrong password';
Expand Down Expand Up @@ -161,6 +176,37 @@ test('encryptor:decrypt encrypted data using wrong password', async ({
).rejects.toThrow('Incorrect password');
});

test('encryptor:decryptWithDetail returns same vault as decrypt', async ({
page,
}) => {
const password = 'a sample passw0rd';

const decryptResult = await page.evaluate(
async (args) => {
return await window.encryptor.decrypt(
args.password,
JSON.stringify(args.sampleEncryptedData),
);
},
{ password, sampleEncryptedData },
);

const decryptWithDetailResult = await page.evaluate(
async (args) => {
return await window.encryptor.decryptWithDetail(
args.password,
JSON.stringify(args.sampleEncryptedData),
);
},
{ password, sampleEncryptedData },
);

expect(JSON.stringify(decryptResult)).toStrictEqual(
JSON.stringify(decryptWithDetailResult.vault),
);
expect(Object.keys(decryptWithDetailResult).length).toBe(3);
});

test('encryptor:encrypt using key then decrypt', async ({ page }) => {
const password = 'a sample passw0rd';
const data = { foo: 'data to encrypt' };
Expand Down Expand Up @@ -334,3 +380,56 @@ test('encryptor:decrypt encrypted data using key derived from wrong password', a
),
).rejects.toThrow('Incorrect password');
});

test('encryptor:createKeyFromString generates valid CryptoKey', async ({
page,
}) => {
const isKey = await page.evaluate(
async (args) => {
const key = await window.encryptor.createKeyFromString(
args.SAMPLE_EXPORTED_KEY,
);
return key instanceof CryptoKey;
},
{ SAMPLE_EXPORTED_KEY },
);
expect(isKey).toBe(true);
});

test('encryptor:exportKey generates valid CryptoKey string', async ({
page,
}) => {
const keyString = await page.evaluate(
async (args) => {
const key = await window.encryptor.createKeyFromString(
args.SAMPLE_EXPORTED_KEY,
);
return await window.encryptor.exportKey(key);
},
{ SAMPLE_EXPORTED_KEY },
);
expect(keyString).toStrictEqual(SAMPLE_EXPORTED_KEY);
});

test('encryptor:encryptWithDetail and decryptWithDetail provide same data ', async ({
page,
}) => {
const password = 'a sample passw0rd';
const data = { foo: 'data to encrypt' };

const encryptedDetail = await page.evaluate(
async (args) =>
await window.encryptor.encryptWithDetail(args.password, args.data),
{ data, password },
);

const decryptedDetail = await page.evaluate(
async (args) =>
await window.encryptor.decryptWithDetail(args.password, args.data),
{ data: encryptedDetail.vault, password },
);

expect(JSON.stringify(decryptedDetail.vault)).toStrictEqual(
JSON.stringify(data),
);
});

0 comments on commit 0300a86

Please sign in to comment.