-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.spec.js
116 lines (96 loc) · 2.54 KB
/
index.spec.js
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
'use strict'
/* eslint-env mocha */
// ===================================================================
require('native-promise-only')
var expect = require('chai').expect
var semver = require('semver')
var hashy = require('./')
// ===================================================================
var data = [
{
value: 'password',
hash: '$2y$04$bCdlo4cUGt5.DpaorjzbN.XUX46/YNj4iKsdTvSQ3UE0pleNR2rjS',
info: {
algorithm: 'bcrypt',
id: '2y',
options: {
cost: 4
}
},
needsRehash: true
},
{
value: 'password',
hash: '$2y$05$P2ZY1eZ3oex3LZJ9bGuRnugsVeq6AXy2wlasiKmYamgDEl6w2dRMG',
info: {
algorithm: 'bcrypt',
id: '2y',
options: {
cost: 5
}
},
needsRehash: false
}
]
if (semver.satisfies(process.version, '>=4')) {
data.push({
value: 'password',
hash: '$argon2i$m=4096,t=3,p=1$tbagT6b1YH33niCo9lVzuA$htv/k+OqWk1V9zD9k5DOBi2kcfcZ6Xu3tWmwEPV3/nc',
info: {
algorithm: 'argon2',
id: 'argon2i',
options: {
memoryCost: 12,
parallelism: 1,
timeCost: 3
}
},
needsRehash: false
})
}
// ===================================================================
// Sets a small cost for Bcrypt to speed up the tests.
hashy.options.bcrypt.cost = 5
describe('hash()', function () {
var hash = hashy.hash
it('can return a promise', function () {
return hash('test')
})
it('can work with callback', function (done) {
hash('test', done)
})
it('does not creates the same hash twice', function () {
return Promise.all([
hash('test'),
hash('test')
]).then(function (hashes) {
expect(hashes[0]).to.not.equal(hashes[1])
})
})
})
describe('getInfo()', function () {
var getInfo = hashy.getInfo
it('returns the algorithm and options', function () {
data.forEach(function (datum) {
expect(getInfo(datum.hash)).to.deep.equal(datum.info)
})
})
})
describe('needsRehash()', function () {
var needsRehash = hashy.needsRehash
it('returns true if the algorithm or the options differs', function () {
data.forEach(function (datum) {
expect(needsRehash(datum.hash, datum.info.algorithm)).to.equal(datum.needsRehash)
})
})
})
describe('verify()', function () {
var verify = hashy.verify
it('returns whether the password matches the hash', function () {
return Promise.all(data.map(function (datum) {
return verify(datum.value, datum.hash).then(function (success) {
expect(success).to.be.true
})
}))
})
})