-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.test.js
More file actions
122 lines (105 loc) · 3.43 KB
/
Copy pathcli.test.js
File metadata and controls
122 lines (105 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use strict";
const { describe, it } = require("node:test");
const assert = require("assert");
const cli = require("./cli.js");
const hashy = require("./index.js");
// ===================================================================
// Sets a small cost for Bcrypt to speed up the tests.
hashy.options.bcrypt.cost = 4;
// `cli(["-a", "algo", "<secret>"])` logs the hash instead of resolving
// with it, so tests that need the produced hash capture stdout.
function captureLog(fn) {
const original = console.log;
let output;
console.log = function (value) {
output = value;
};
return Promise.resolve()
.then(fn)
.then(
function () {
console.log = original;
return output;
},
function (error) {
console.log = original;
throw error;
},
);
}
// ===================================================================
describe("cli", function () {
it("hashes a password", function () {
return captureLog(() => cli(["secret"])).then(function (output) {
assert.match(output, /^\$argon2id\$/);
});
});
it("supports -a to select the algorithm", function () {
return captureLog(() => cli(["-a", "bcrypt", "secret"])).then(
function (output) {
assert.match(output, /^\$2[aby]\$04\$/);
},
);
});
it("supports -c to set the Bcrypt cost", function () {
return captureLog(() => cli(["-a", "bcrypt", "-c", "5", "secret"])).then(
function (output) {
assert.match(output, /^\$2[aby]\$05\$/);
},
);
});
it("verifies a matching password/hash pair", function () {
return captureLog(() => cli(["-a", "bcrypt", "secret"]))
.then((hash) => cli(["-a", "bcrypt", "secret", hash]))
.then(function (result) {
assert.strictEqual(result, "ok");
});
});
it("reports when the hash should be rehashed", function () {
return captureLog(() => cli(["-a", "bcrypt", "secret"]))
.then((hash) => cli(["secret", hash]))
.then(function (result) {
assert.strictEqual(result, "ok but password should be rehashed");
});
});
it("rejects on a wrong password", function () {
return captureLog(() => cli(["-a", "bcrypt", "secret"]))
.then((hash) => cli(["wrong", hash]))
.then(
function () {
throw new Error("expected verification to fail");
},
function (error) {
assert.strictEqual(error.message, "not ok");
},
);
});
it("rejects on an incorrect number of arguments", function () {
return cli([]).then(
function () {
throw new Error("expected the call to be rejected");
},
function (error) {
assert.match(error.message, /incorrect number of arguments/);
},
);
});
it("displays the help message", function () {
return captureLog(() => cli(["--help"])).then(function (output) {
assert.match(output, /Usage: hashy/);
});
});
it("displays the version number", function () {
return captureLog(() => cli(["--version"])).then(function (output) {
assert.strictEqual(output, require("./package.json").version);
});
});
it("does not accumulate aliases across repeated calls", function () {
return captureLog(() => cli(["secret"]))
.then(() => captureLog(() => cli(["secret"])))
.then(() => captureLog(() => cli(["--help"])))
.then(function (output) {
assert.strictEqual(output.match(/--help/g).length, 1);
});
});
});