DEV: Add tests for JavaScript code.

DEV: Add tests for JavaScript code.

From ef2f8f6070c6e650bc108bb1002c28980104dfbc Mon Sep 17 00:00:00 2001
From: Dan Ungureanu <dan@ungureanu.me>
Date: Wed, 5 Dec 2018 01:41:41 +0200
Subject: [PATCH] DEV: Add tests for JavaScript code.


diff --git a/assets/javascripts/lib/keys.js.es6 b/assets/javascripts/lib/keys.js.es6
index acb4a95..dbd067e 100644
--- a/assets/javascripts/lib/keys.js.es6
+++ b/assets/javascripts/lib/keys.js.es6
@@ -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)
diff --git a/test/javascripts/acceptance/encrypt-test.js.es6 b/test/javascripts/acceptance/encrypt-test.js.es6
index 623f80b..e5de678 100644
--- a/test/javascripts/acceptance/encrypt-test.js.es6
+++ b/test/javascripts/acceptance/encrypt-test.js.es6
@@ -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.
  */
diff --git a/test/javascripts/helpers/helpers.js.es6 b/test/javascripts/helpers/helpers.js.es6
deleted file mode 100644
index 32588bf..0000000
--- a/test/javascripts/helpers/helpers.js.es6
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Checks if a string is not contained in a string.
- *
- * @param haystack
- * @param needle
- * @param message
- */
-QUnit.assert.notContains = function(haystack, needle, message) {
-  this.pushResult({
-    result: haystack.indexOf(needle) === -1,
-    actual: haystack,
-    expected: "not to contain " + needle,
-    message
-  });
-};
-
-/*
- * 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"
-    });
-  }
-};
diff --git a/test/javascripts/lib/base64-test.js.es6 b/test/javascripts/lib/base64-test.js.es6
index 5dea5b3..9f7cea1 100644
--- a/test/javascripts/lib/base64-test.js.es6
+++ b/test/javascripts/lib/base64-test.js.es6
@@ -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 => {
diff --git a/test/javascripts/lib/buffers-test.js.es6 b/test/javascripts/lib/buffers-test.js.es6
index a7ebda7..d636f98 100644
--- a/test/javascripts/lib/buffers-test.js.es6
+++ b/test/javascripts/lib/buffers-test.js.es6
@@ -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 => {
diff --git a/test/javascripts/lib/keys-test.js.es6 b/test/javascripts/lib/keys-test.js.es6
new file mode 100644
index 0000000..249bf80
--- /dev/null
+++ b/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 messag

GitHub

2 Likes

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

3 Likes