Skip to content

Commit

Permalink
DEV: Use waiters instead of sleep.
Browse files Browse the repository at this point in the history
  • Loading branch information
udan11 committed Oct 18, 2019
1 parent 00868e5 commit 326f4b7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default {

// Decode composer on reply reload. This usually occurs when a post is
// edited or a draft is loaded.
const appEvents = container.lookup("app-events:main");
const appEvents = container.lookup("service:app-events");
appEvents.on("composer:reply-reloaded", this, "composerReplyReloaded");
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default {
return;
}

const appEvents = container.lookup("app-events:main");
const appEvents = container.lookup("service:app-events");
appEvents.on("encrypt:status-changed", this, "decryptTitles");
appEvents.on("page:changed", this, "decryptDocTitle");

Expand Down
62 changes: 39 additions & 23 deletions test/javascripts/acceptance/encrypt-test.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
import EncryptLibDiscourse, {
ENCRYPT_ACTIVE,
ENCRYPT_DISABLED,
ENCRYPT_ENABLED
ENCRYPT_ENABLED,
getEncryptionStatus
} from "discourse/plugins/discourse-encrypt/lib/discourse";
import {
exportIdentity,
Expand All @@ -33,27 +34,34 @@ QUnit.assert.notContains = function notContains(haystack, needle, message) {
};

/**
* @var Secret passphrase used for testing purposes.
* @var PASSPHRASE Secret passphrase used for testing purposes.
*/
const PASSPHRASE = "curren7U$er.pa$$Phr4se";

/**
* @var Constant string that is used to check for plaintext leakage.
* @var PLAINTEXT Constant string that is used to check for plaintext leakage.
*/
const PLAINTEXT = "!PL41N73X7!";

/**
* @var User keys.
* @var keys User keys.
*/
const keys = {};

/**
* @var Global assert instance used to report plaintext leakage.
* @var globalAssert Global assert instance used to report plaintext leakage.
*/
let globalAssert;

/**
* @var requestsCount Number of requests intercepted by the leak checker.
*/
let requestsCount = 0;

/**
* Sets up encryption.
*
* @param status
*/
async function setEncryptionStatus(status) {
const user = Discourse.User.current();
Expand Down Expand Up @@ -99,9 +107,22 @@ async function setEncryptionStatus(status) {
return (keys[user.username] = identity);
}

// TODO: Figure out why `await` is not enough.
function sleep(time) {
return new Ember.RSVP.Promise(resolve => setTimeout(resolve, time));
/**
* Executes the given function and waits until current encryption status
* changes or given waiter becomes true.
*
* @param statusOrWaiter
* @param func
*/
async function wait(statusOrWaiter, func) {
const waiter =
typeof statusOrWaiter === "function"
? statusOrWaiter
: () => getEncryptionStatus(Discourse.User.current()) === statusOrWaiter;

Ember.Test.registerWaiter(waiter);
await func();
Ember.Test.unregisterWaiter(waiter);
}

acceptance("Encrypt", {
Expand All @@ -115,6 +136,7 @@ acceptance("Encrypt", {
XMLHttpRequest.prototype.send_ = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(body) {
if (body && globalAssert) {
requestsCount += 1;
globalAssert.notContains(body, PLAINTEXT, "does not leak plaintext");
globalAssert.notContains(body, PASSPHRASE, "does not leak passphrase");
}
Expand Down Expand Up @@ -142,18 +164,16 @@ acceptance("Encrypt", {
});

test("meta: leak checker works", async assert => {
globalAssert = {
checked: false,
notContains: () => (globalAssert.checked = true)
};
globalAssert = { notContains: () => {} };

await visit("/");
await click("#create-topic");
await fillIn("#reply-title", `Some hidden message ${PLAINTEXT}`);
await fillIn(".d-editor-input", `Hello, world! ${PLAINTEXT}`.repeat(42));
await click("button.create");

assert.ok(globalAssert.checked);
requestsCount = 0;
await click("button.create");
assert.ok(requestsCount > 0);

globalAssert = null;
});
Expand All @@ -173,8 +193,8 @@ test("posting does not leak plaintext", async assert => {
await fillIn("#reply-title", `Some hidden message ${PLAINTEXT}`);
await fillIn(".d-editor-input", `Hello, world! ${PLAINTEXT}`.repeat(42));

await click("button.create");
await sleep(1500);
requestsCount = 0;
await wait(() => requestsCount > 0, () => click("button.create"));

globalAssert = null;
});
Expand All @@ -190,9 +210,7 @@ test("enabling works", async assert => {
});

await visit("/u/eviltrout/preferences");
await click(".encrypt button.btn-primary");
await sleep(3000);

await wait(ENCRYPT_ACTIVE, () => click(".encrypt button.btn-primary"));
assert.ok(ajaxRequested, "AJAX request to save keys was made");

const identity = await loadDbIdentity();
Expand All @@ -207,8 +225,7 @@ test("activation works", async assert => {

await visit("/u/eviltrout/preferences");
await fillIn(".encrypt #passphrase", PASSPHRASE);
await click(".encrypt button.btn-primary");
await sleep(3000);
await wait(ENCRYPT_ACTIVE, () => click(".encrypt button.btn-primary"));

const identity = await loadDbIdentity();
assert.ok(identity.encryptPublic instanceof CryptoKey);
Expand All @@ -221,8 +238,7 @@ test("deactivation works", async assert => {
await setEncryptionStatus(ENCRYPT_ACTIVE);

await visit("/u/eviltrout/preferences");
await click(".encrypt button#deactivate");
await sleep(1500);
await wait(ENCRYPT_ENABLED, () => click(".encrypt button#deactivate"));

const identity = await loadDbIdentity();
assert.equal(identity, null);
Expand Down

0 comments on commit 326f4b7

Please sign in to comment.