UNPKG

1.61 kBJavaScriptView Raw
1const bcrypt = require('../bcrypt');
2
3const EXPECTED = 2500; //number of times to iterate these tests.)
4
5test('salt_length', () => {
6 expect.assertions(EXPECTED);
7
8 return Promise.all(Array.from({length: EXPECTED},
9 () => bcrypt.genSalt(10)
10 .then(salt => expect(salt).toHaveLength(29))));
11})
12
13test('test_hash_length', () => {
14 expect.assertions(EXPECTED);
15 const SALT = '$2a$04$TnjywYklQbbZjdjBgBoA4e';
16 return Promise.all(Array.from({length: EXPECTED},
17 () => bcrypt.hash('test', SALT)
18 .then(hash => expect(hash).toHaveLength(60))));
19})
20
21test('test_compare', () => {
22 expect.assertions(EXPECTED);
23 const HASH = '$2a$04$TnjywYklQbbZjdjBgBoA4e9G7RJt9blgMgsCvUvus4Iv4TENB5nHy';
24 return Promise.all(Array.from({length: EXPECTED},
25 () => bcrypt.compare('test', HASH)
26 .then(match => expect(match).toEqual(true))));
27})
28
29test('test_hash_and_compare', () => {
30 expect.assertions(EXPECTED * 3);
31 const salt = bcrypt.genSaltSync(4)
32
33 return Promise.all(Array.from({length: EXPECTED},
34 () => {
35 const password = 'secret' + Math.random();
36 return bcrypt.hash(password, salt)
37 .then(hash => {
38 expect(hash).toHaveLength(60);
39 const goodCompare = bcrypt.compare(password, hash).then(res => expect(res).toEqual(true));
40 const badCompare = bcrypt.compare('bad' + password, hash).then(res => expect(res).toEqual(false));
41
42 return Promise.all([goodCompare, badCompare]);
43 });
44 }));
45}, 10000);
46