1 | const bcrypt = require('../bcrypt');
|
2 | const promises = require('../promises');
|
3 |
|
4 | test('salt_returns_promise_on_no_args', () => {
|
5 |
|
6 |
|
7 | expect(typeof bcrypt.genSalt().then).toEqual('function')
|
8 | })
|
9 |
|
10 | test('salt_returns_promise_on_null_callback', () => {
|
11 | expect(typeof bcrypt.genSalt(13, null, null).then).toEqual('function')
|
12 | })
|
13 |
|
14 | test('salt_length', () => {
|
15 | return expect(bcrypt.genSalt(10)).resolves.toHaveLength(29);
|
16 | })
|
17 |
|
18 | test('salt_rounds_is_string_number', () => {
|
19 | return expect(bcrypt.genSalt('10')).rejects.toThrow('rounds must be a number');
|
20 | })
|
21 |
|
22 | test('salt_rounds_is_string_non_number', () => {
|
23 | return expect(bcrypt.genSalt('b')).rejects.toThrow('rounds must be a number');
|
24 | })
|
25 |
|
26 | test('hash_returns_promise_on_null_callback', () => {
|
27 | expect(typeof bcrypt.hash('password', 10, null).then).toStrictEqual('function')
|
28 | })
|
29 |
|
30 | test('hash', () => {
|
31 | return expect(bcrypt.genSalt(10)
|
32 | .then(salt => bcrypt.hash('password', salt))).resolves.toBeDefined()
|
33 | })
|
34 |
|
35 | test('hash_rounds', () => {
|
36 | return bcrypt.hash('bacon', 8).then(hash => {
|
37 | expect(bcrypt.getRounds(hash)).toStrictEqual(8)
|
38 | });
|
39 | })
|
40 |
|
41 | test('hash_empty_strings', () => {
|
42 | expect.assertions(2);
|
43 | return Promise.all([
|
44 | expect(bcrypt.genSalt(10)
|
45 | .then(salt => bcrypt.hash('', salt)))
|
46 | .resolves.toBeDefined(),
|
47 | expect(bcrypt.hash('', '')).rejects.toThrow(''),
|
48 | ]);
|
49 | })
|
50 |
|
51 | test('hash_no_params', () => {
|
52 | expect.assertions(1);
|
53 | return expect(bcrypt.hash()).rejects.toThrow('data and salt arguments required');
|
54 | })
|
55 |
|
56 | test('hash_one_param', () => {
|
57 | return expect(bcrypt.hash('password')).rejects.toThrow('data and salt arguments required');
|
58 | })
|
59 |
|
60 | test('hash_salt_validity', () => {
|
61 | expect.assertions(2);
|
62 | return Promise.all(
|
63 | [
|
64 | expect(bcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse')).resolves.toBeDefined(),
|
65 | expect(bcrypt.hash('password', 'some$value')).rejects.toThrow("Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue")
|
66 | ]);
|
67 | })
|
68 |
|
69 | test('verify_salt', () => {
|
70 | expect.assertions(2);
|
71 | return bcrypt.genSalt(10).then(result => {
|
72 | const [_, version, salt] = result.split('$');
|
73 | expect(version).toEqual('2b')
|
74 | expect(salt).toEqual('10')
|
75 | });
|
76 | })
|
77 |
|
78 | test('verify_salt_min_rounds', () => {
|
79 | expect.assertions(2);
|
80 | return bcrypt.genSalt(1).then(value => {
|
81 | const [_, version, rounds] = value.split('$');
|
82 | expect(version).toEqual('2b');
|
83 | expect(rounds).toEqual('04');
|
84 | });
|
85 | })
|
86 |
|
87 | test('verify_salt_max_rounds', () => {
|
88 | expect.assertions(2);
|
89 | return bcrypt.genSalt(100).then(value => {
|
90 | const [_, version, rounds] = value.split('$');
|
91 | expect(version).toEqual('2b');
|
92 | expect(rounds).toEqual('31');
|
93 | });
|
94 | })
|
95 |
|
96 | test('hash_compare_returns_promise_on_null_callback', () => {
|
97 | expect(typeof bcrypt.compare('password', 'something', null).then).toStrictEqual('function')
|
98 | })
|
99 |
|
100 | test('hash_compare', () => {
|
101 | expect.assertions(3);
|
102 | return bcrypt.genSalt(10).then(function (salt) {
|
103 | expect(salt).toHaveLength(29);
|
104 | return bcrypt.hash("test", salt);
|
105 | }).then(hash => Promise.all(
|
106 | [
|
107 | expect(bcrypt.compare("test", hash)).resolves.toEqual(true),
|
108 | expect(bcrypt.compare("blah", hash)).resolves.toEqual(false)
|
109 | ]));
|
110 | })
|
111 |
|
112 | test('hash_compare_empty_strings', () => {
|
113 | expect.assertions(2);
|
114 | const hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
|
115 | return Promise.all([
|
116 | expect(bcrypt.compare("", hash)).resolves.toEqual(false),
|
117 | expect(bcrypt.compare("", "")).resolves.toEqual(false)
|
118 | ]);
|
119 | })
|
120 |
|
121 | test('hash_compare_invalid_strings', () => {
|
122 | const fullString = 'envy1362987212538';
|
123 | const hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
|
124 | const wut = ':';
|
125 | return Promise.all([
|
126 | expect(bcrypt.compare(fullString, hash)).resolves.toEqual(true),
|
127 | expect(bcrypt.compare(fullString, wut)).resolves.toEqual(false),
|
128 | ]);
|
129 | })
|
130 |
|
131 | test('hash_compare_no_params', () => {
|
132 | expect.assertions(1);
|
133 | return expect(bcrypt.compare()).rejects.toThrow('data and hash arguments required')
|
134 | })
|
135 |
|
136 | test('hash_compare_one_param', () => {
|
137 | expect.assertions(1);
|
138 | return expect(bcrypt.compare('password')).rejects.toThrow('data and hash arguments required')
|
139 | })
|
140 |
|
141 | test('change_promise_impl_reject', () => {
|
142 |
|
143 | promises.use({
|
144 | reject: function () {
|
145 | return 'mock';
|
146 | }
|
147 | });
|
148 |
|
149 | expect(promises.reject()).toEqual('mock');
|
150 |
|
151 |
|
152 | promises.use(global.Promise);
|
153 | })
|
154 |
|
155 | test('change_promise_impl_promise', () => {
|
156 |
|
157 | promises.use({
|
158 | reject: function (err) {
|
159 | expect(err.message).toEqual('fn must be a function');
|
160 | return 'mock';
|
161 | }
|
162 | });
|
163 |
|
164 | expect(promises.promise('', '', '')).toEqual('mock');
|
165 |
|
166 |
|
167 | promises.use(global.Promise);
|
168 | })
|