2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-04-23 21:27:13 +02:00
|
|
|
const {password_quality, password_warning} = zrequire("password_quality");
|
2017-01-09 21:51:22 +01:00
|
|
|
|
2021-03-11 17:15:31 +01:00
|
|
|
function password_field(min_length, min_guesses) {
|
|
|
|
const self = {};
|
|
|
|
|
|
|
|
self.data = (field) => {
|
2022-04-09 23:44:38 +02:00
|
|
|
switch (field) {
|
|
|
|
case "minLength":
|
|
|
|
return min_length;
|
|
|
|
case "minGuesses":
|
|
|
|
return min_guesses;
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown field ${field}`);
|
2021-03-11 17:15:31 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
run_test("basics w/progress bar", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let accepted;
|
|
|
|
let password;
|
|
|
|
let warning;
|
2017-01-09 21:51:22 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $bar = (function () {
|
|
|
|
const $self = {};
|
2017-01-09 21:51:22 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.width = (width) => {
|
|
|
|
$self.w = width;
|
|
|
|
return $self;
|
2017-01-09 21:51:22 +01:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.removeClass = (arg) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(arg, "bar-success bar-danger");
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2017-01-09 21:51:22 +01:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$self.addClass = (arg) => {
|
|
|
|
$self.added_class = arg;
|
|
|
|
return $self;
|
2017-01-09 21:51:22 +01:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $self;
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-01-09 21:51:22 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
password = "z!X4@S_&";
|
2022-01-25 11:36:19 +01:00
|
|
|
accepted = password_quality(password, $bar, password_field(10, 80000));
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!accepted);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($bar.w, "39.7%");
|
|
|
|
assert.equal($bar.added_class, "bar-danger");
|
2021-04-23 21:27:13 +02:00
|
|
|
warning = password_warning(password, password_field(10));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(warning, "translated: Password should be at least 10 characters long");
|
2017-01-09 21:51:22 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
password = "foo";
|
2022-01-25 11:36:19 +01:00
|
|
|
accepted = password_quality(password, $bar, password_field(2, 200));
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(accepted);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($bar.w, "10.390277164940581%");
|
|
|
|
assert.equal($bar.added_class, "bar-success");
|
2021-04-23 21:27:13 +02:00
|
|
|
warning = password_warning(password, password_field(2));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(warning, "translated: Password is too weak");
|
2017-05-23 00:53:53 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
password = "aaaaaaaa";
|
2022-01-25 11:36:19 +01:00
|
|
|
accepted = password_quality(password, $bar, password_field(6, 1e100));
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!accepted);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($bar.added_class, "bar-danger");
|
2021-04-23 21:27:13 +02:00
|
|
|
warning = password_warning(password, password_field(6));
|
2022-03-17 08:11:46 +01:00
|
|
|
assert.equal(warning, 'Repeated characters like "aaa" are easy to guess.');
|
2021-03-11 17:15:31 +01:00
|
|
|
});
|