zulip/web/tests/password.test.cjs

77 lines
2.2 KiB
JavaScript
Raw Normal View History

"use strict";
const assert = require("node:assert/strict");
const {zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
const {password_quality, password_warning} = zrequire("password_quality");
2017-01-09 21:51:22 +01:00
function password_field(min_length, min_guesses) {
const self = {};
self.attr = (name) => {
switch (name) {
case "data-min-length":
return min_length;
case "data-min-guesses":
return min_guesses;
/* istanbul ignore next */
default:
throw new Error(`Unknown attribute ${name}`);
}
};
return self;
}
run_test("basics w/progress bar", () => {
let accepted;
let password;
let warning;
2017-01-09 21:51:22 +01:00
const $bar = (function () {
const $self = {};
2017-01-09 21:51:22 +01:00
$self.width = (width) => {
$self.w = width;
return $self;
2017-01-09 21:51:22 +01:00
};
$self.removeClass = (arg) => {
assert.equal(arg, "bar-success bar-danger");
return $self;
2017-01-09 21:51:22 +01:00
};
$self.addClass = (arg) => {
$self.added_class = arg;
return $self;
2017-01-09 21:51:22 +01:00
};
return $self;
})();
2017-01-09 21:51:22 +01:00
password = "z!X4@S_&";
accepted = password_quality(password, $bar, password_field(10, 80000));
assert.ok(!accepted);
assert.equal($bar.w, "39.7%");
assert.equal($bar.added_class, "bar-danger");
warning = password_warning(password, password_field(10));
assert.equal(warning, "translated: Password should be at least 10 characters long");
2017-01-09 21:51:22 +01:00
password = "foo";
accepted = password_quality(password, $bar, password_field(2, 200));
assert.ok(accepted);
assert.equal($bar.w, "10.390277164940581%");
assert.equal($bar.added_class, "bar-success");
warning = password_warning(password, password_field(2));
assert.equal(warning, "translated: Password is too weak");
password = "aaaaaaaa";
accepted = password_quality(password, $bar, password_field(6, 1e100));
assert.ok(!accepted);
assert.equal($bar.added_class, "bar-danger");
warning = password_warning(password, password_field(6));
assert.equal(warning, 'Repeated characters like "aaa" are easy to guess.');
});