Files
zulip/frontend_tests/node_tests/password.js
Steve Howell 30c7108955 zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.

For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.

We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.

We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.

The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js.  This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code.  It's often the case that you can simply
use override(), as well.

In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 11:10:57 -05:00

83 lines
2.4 KiB
JavaScript

"use strict";
const {strict: assert} = require("assert");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
set_global("zxcvbn", require("zxcvbn"));
const common = zrequire("common");
run_test("basics", () => {
let accepted;
let password;
let warning;
const bar = (function () {
const self = {};
self.width = (width) => {
self.w = width;
return self;
};
self.removeClass = (arg) => {
assert.equal(arg, "bar-success bar-danger");
return self;
};
self.addClass = (arg) => {
self.added_class = arg;
return self;
};
return self;
})();
function password_field(min_length, min_guesses) {
const self = {};
self.data = (field) => {
if (field === "minLength") {
return min_length;
} else if (field === "minGuesses") {
return min_guesses;
}
throw new Error(`Unknown field ${field}`);
};
return self;
}
password = "z!X4@S_&";
accepted = common.password_quality(password, bar, password_field(10, 80000));
assert(!accepted);
assert.equal(bar.w, "39.7%");
assert.equal(bar.added_class, "bar-danger");
warning = common.password_warning(password, password_field(10));
assert.equal(warning, "translated: Password should be at least 10 characters long");
password = "foo";
accepted = common.password_quality(password, bar, password_field(2, 200));
assert(accepted);
assert.equal(bar.w, "10.390277164940581%");
assert.equal(bar.added_class, "bar-success");
warning = common.password_warning(password, password_field(2));
assert.equal(warning, "translated: Password is too weak");
password = "aaaaaaaa";
accepted = common.password_quality(password, bar, password_field(6, 1e100));
assert(!accepted);
assert.equal(bar.added_class, "bar-danger");
warning = common.password_warning(password, password_field(6));
assert.equal(warning, 'Repeats like "aaa" are easy to guess');
set_global("zxcvbn", undefined);
password = "aaaaaaaa";
accepted = common.password_quality(password, bar, password_field(6, 1e100));
assert(accepted === undefined);
warning = common.password_warning(password, password_field(6));
assert(warning === undefined);
});