Skip to content

Commit

Permalink
DEV: Add tests for JavaScript code.
Browse files Browse the repository at this point in the history
  • Loading branch information
udan11 committed Dec 4, 2018
1 parent a122de8 commit ef2f8f6
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 59 deletions.
2 changes: 1 addition & 1 deletion assets/javascripts/lib/keys.js.es6
Expand Up @@ -129,7 +129,7 @@ export function rsaEncrypt(key, plaintext) {
* @return Promise<String>
*/
export function rsaDecrypt(key, ciphertext) {
const encrypted = stringToBuffer(ciphertext);
const encrypted = base64ToBuffer(ciphertext);

return window.crypto.subtle
.decrypt({ name: "RSA-OAEP", hash: { name: "SHA-256" } }, key, encrypted)
Expand Down
16 changes: 16 additions & 0 deletions test/javascripts/acceptance/encrypt-test.js.es6
Expand Up @@ -8,6 +8,22 @@ import {
} from "discourse/plugins/discourse-encrypt/lib/keys";
import { saveKeyPairToIndexedDb } from "discourse/plugins/discourse-encrypt/lib/keys_db";

/*
* Checks if a string is not contained in a string.
*
* @param haystack
* @param needle
* @param message
*/
QUnit.assert.notContains = function notContains(haystack, needle, message) {
this.pushResult({
result: haystack.indexOf(needle) === -1,
actual: haystack,
expected: "not to contain " + needle,
message
});
};

/**
* @var Secret passphrase used for testing purposes.
*/
Expand Down
58 changes: 0 additions & 58 deletions test/javascripts/helpers/helpers.js.es6

This file was deleted.

43 changes: 43 additions & 0 deletions test/javascripts/lib/base64-test.js.es6
Expand Up @@ -3,6 +3,49 @@ import {
bufferToBase64
} from "discourse/plugins/discourse-encrypt/lib/base64";

/*
* Checks if two array-like objects are equal.
*
* @param haystack
* @param needle
* @param message
*/
QUnit.assert.arrayEqual = function(actual, expected) {
if (actual.length !== expected.length) {
this.pushResult({
result: false,
actual: actual.length,
expected: expected.length,
message: "array lengths are equal"
});

return;
}

let result = true;

for (let i = 0; i < actual.length; ++i) {
if (actual[i] !== expected[i]) {
result = false;
this.pushResult({
result,
actual: actual[i],
expected: expected[i],
message: `index ${i} matches`
});
}
}

if (result) {
this.pushResult({
result,
actual: actual,
expected: expected,
message: "arrays match"
});
}
};

QUnit.module("discourse-encrypt:lib:base64");

test("base64 to buffer", assert => {
Expand Down
43 changes: 43 additions & 0 deletions test/javascripts/lib/buffers-test.js.es6
Expand Up @@ -3,6 +3,49 @@ import {
bufferToString
} from "discourse/plugins/discourse-encrypt/lib/buffers";

/*
* Checks if two array-like objects are equal.
*
* @param haystack
* @param needle
* @param message
*/
QUnit.assert.arrayEqual = function(actual, expected) {
if (actual.length !== expected.length) {
this.pushResult({
result: false,
actual: actual.length,
expected: expected.length,
message: "array lengths are equal"
});

return;
}

let result = true;

for (let i = 0; i < actual.length; ++i) {
if (actual[i] !== expected[i]) {
result = false;
this.pushResult({
result,
actual: actual[i],
expected: expected[i],
message: `index ${i} matches`
});
}
}

if (result) {
this.pushResult({
result,
actual: actual,
expected: expected,
message: "arrays match"
});
}
};

QUnit.module("discourse-encrypt:lib:buffers");

test("string to buffer", assert => {
Expand Down
74 changes: 74 additions & 0 deletions test/javascripts/lib/keys-test.js.es6
@@ -0,0 +1,74 @@
import {
generateKeyPair,
exportPublicKey,
importPublicKey,
exportPrivateKey,
importPrivateKey,
rsaEncrypt,
rsaDecrypt,
generateSalt,
generatePassphraseKey,
generateKey,
exportKey,
importKey,
encrypt,
decrypt
} from "discourse/plugins/discourse-encrypt/lib/keys";

QUnit.module("discourse-encrypt:lib:keys");

test("generateKeyPair", async assert => {
const [publicKey, privateKey] = await generateKeyPair();
assert.ok(publicKey instanceof CryptoKey);
assert.ok(privateKey instanceof CryptoKey);
});

test("exportPublicKey & importPublicKey", async assert => {
const publicKey = (await generateKeyPair())[0];
const exported = await exportPublicKey(publicKey);
assert.ok((await importPublicKey(exported)) instanceof CryptoKey);
});

test("exportPrivateKey & importPrivateKey", async assert => {
const key = await generatePassphraseKey("passphrase", generateSalt());
const privateKey = (await generateKeyPair())[1];
const exported = await exportPrivateKey(privateKey, key);
assert.ok((await importPrivateKey(exported, key)) instanceof CryptoKey);
});

test("rsaEncrypt & rsaDecrypt", async assert => {
const [publicKey, privateKey] = await generateKeyPair();
const plaintext = "this is a message";
const ciphertext = await rsaEncrypt(publicKey, plaintext);
const plaintext2 = await rsaDecrypt(privateKey, ciphertext);
assert.equal(plaintext, plaintext2);
});

test("generateSalt", async assert => {
assert.equal(24, generateSalt().length);
});

test("generatePassphraseKey", async assert => {
const key = await generatePassphraseKey("passphrase", generateSalt());
assert.ok(key instanceof CryptoKey);
});

test("generateKey", async assert => {
const key = await generateKey();
assert.ok(key instanceof CryptoKey);
});

test("exportKey & importKey", async assert => {
const [publicKey, privateKey] = await generateKeyPair();
const key = await generateKey();
const exported = await exportKey(key, publicKey);
assert.ok((await importKey(exported, privateKey)) instanceof CryptoKey);
});

test("encrypt & decrypt", async assert => {
const key = await generateKey();
const plaintext = "this is a message";
const ciphertext = await encrypt(key, plaintext);
const plaintext2 = await decrypt(key, ciphertext);
assert.equal(plaintext, plaintext2);
});
32 changes: 32 additions & 0 deletions test/javascripts/lib/keys_db-test.js.es6
@@ -0,0 +1,32 @@
import { generateKeyPair } from "discourse/plugins/discourse-encrypt/lib/keys";
import {
saveKeyPairToIndexedDb,
loadKeyPairFromIndexedDb,
deleteIndexedDb
} from "discourse/plugins/discourse-encrypt/lib/keys_db";

QUnit.module("discourse-encrypt:lib:keys_db");

test("Indexed Database API", async assert => {
try{
await deleteIndexedDb();
} catch (e) {}

let publicKey, privateKey;

[publicKey, privateKey] = await loadKeyPairFromIndexedDb();

This comment has been minimized.

Copy link
@SamSaffron

SamSaffron Dec 6, 2018

Member

destructuring from an await is so cool. makes the code very easy to follow.


assert.equal(null, publicKey);
assert.equal(null, privateKey);

[publicKey, privateKey] = await generateKeyPair();
await saveKeyPairToIndexedDb(publicKey, privateKey);

[publicKey, privateKey] = await loadKeyPairFromIndexedDb();
assert.ok(publicKey instanceof CryptoKey);
assert.ok(privateKey instanceof CryptoKey);

try{
await deleteIndexedDb();
} catch (e) {}
});

0 comments on commit ef2f8f6

Please sign in to comment.