1 | var bcrypt = require('../bcrypt');
|
2 | var promises = require('../promises');
|
3 |
|
4 | var fail = function(assert, error) {
|
5 | assert.ok(false, error);
|
6 | assert.done();
|
7 | };
|
8 |
|
9 |
|
10 | if (typeof Promise !== 'undefined') {
|
11 | module.exports = {
|
12 | test_salt_returns_promise_on_no_args: function(assert) {
|
13 |
|
14 |
|
15 | assert.strictEqual(typeof bcrypt.genSalt().then, 'function', "Should return a promise");
|
16 | assert.done();
|
17 | },
|
18 | test_salt_returns_promise_on_null_callback: function(assert) {
|
19 | assert.strictEqual(typeof bcrypt.genSalt(13, null, null).then,'function', "Should return a promise");
|
20 | assert.done();
|
21 | },
|
22 | test_salt_length: function(assert) {
|
23 | assert.expect(2);
|
24 | bcrypt.genSalt(10).then(function(salt) {
|
25 | assert.ok(salt,'salt must be defined');
|
26 | assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
|
27 | assert.done();
|
28 | });
|
29 | },
|
30 | test_salt_rounds_is_string_number: function(assert) {
|
31 | assert.expect(1);
|
32 | bcrypt.genSalt('10').then(function() {
|
33 | fail(assert, "should not be resolved");
|
34 | }).catch(function(err) {
|
35 | assert.ok((err instanceof Error), "Should be an Error. genSalt requires round to be of type number.");
|
36 | }).then(function() {
|
37 | assert.done();
|
38 | });
|
39 | },
|
40 | test_salt_rounds_is_string_non_number: function(assert) {
|
41 | assert.expect(1);
|
42 | bcrypt.genSalt('b').then(function() {
|
43 | fail(assert, "should not be resolved");
|
44 | }).catch(function(err) {
|
45 | assert.ok((err instanceof Error), "Should be an Error. genSalt requires round to be of type number.");
|
46 | }).then(function() {
|
47 | assert.done();
|
48 | });
|
49 | },
|
50 | test_hash_returns_promise_on_null_callback: function(assert) {
|
51 | assert.strictEqual(typeof bcrypt.hash('password', 10, null).then,'function', "Should return a promise");
|
52 | assert.done();
|
53 | },
|
54 | test_hash: function(assert) {
|
55 | assert.expect(1);
|
56 | bcrypt.genSalt(10).then(function(salt) {
|
57 | return bcrypt.hash('password', salt);
|
58 | }).then(function(res) {
|
59 | assert.ok(res, "Res should be defined.");
|
60 | assert.done();
|
61 | });
|
62 | },
|
63 | test_hash_rounds: function(assert) {
|
64 | assert.expect(1);
|
65 | bcrypt.hash('bacon', 8).then(function(hash) {
|
66 | assert.strictEqual(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
|
67 | assert.done();
|
68 | });
|
69 | },
|
70 | test_hash_empty_strings: function(assert) {
|
71 | assert.expect(2);
|
72 | Promise.all([
|
73 | bcrypt.genSalt(10).then(function(salt) {
|
74 | return bcrypt.hash('', salt);
|
75 | }).then(function(res) {
|
76 | assert.ok(res, "Res should be defined even with an empty pw.");
|
77 | }),
|
78 | bcrypt.hash('', '').then(function() {
|
79 | fail(assert, "should not be resolved")
|
80 | }).catch(function(err) {
|
81 | assert.ok(err);
|
82 | }),
|
83 | ]).then(function() {
|
84 | assert.done();
|
85 | });
|
86 | },
|
87 | test_hash_no_params: function(assert) {
|
88 | assert.expect(1);
|
89 | bcrypt.hash().then(function() {
|
90 | fail(assert, "should not be resolved");
|
91 | }).catch(function(err) {
|
92 | assert.ok(err, "Should be an error. No params.");
|
93 | }).then(function() {
|
94 | assert.done();
|
95 | });
|
96 | },
|
97 | test_hash_one_param: function(assert) {
|
98 | assert.expect(1);
|
99 | bcrypt.hash('password').then(function() {
|
100 | fail(assert, "should not be resolved");
|
101 | }).catch(function(err) {
|
102 | assert.ok(err, "Should be an error. No salt.");
|
103 | }).then(function() {
|
104 | assert.done();
|
105 | });
|
106 | },
|
107 | test_hash_salt_validity: function(assert) {
|
108 | assert.expect(3);
|
109 | Promise.all(
|
110 | [
|
111 | bcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse').then(function(enc) {
|
112 | assert.ok(enc, "should be resolved with a value");
|
113 | }),
|
114 | bcrypt.hash('password', 'some$value').then(function() {
|
115 | fail(assert, "should not resolve");
|
116 | }).catch(function(err) {
|
117 | assert.notEqual(err, undefined);
|
118 | assert.strictEqual(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
|
119 | })
|
120 | ]).then(function() {
|
121 | assert.done();
|
122 | });
|
123 | },
|
124 | test_verify_salt: function(assert) {
|
125 | assert.expect(2);
|
126 | bcrypt.genSalt(10).then(function(salt) {
|
127 | var split_salt = salt.split('$');
|
128 | assert.strictEqual(split_salt[1], '2b');
|
129 | assert.strictEqual(split_salt[2], '10');
|
130 | assert.done();
|
131 | });
|
132 | },
|
133 | test_verify_salt_min_rounds: function(assert) {
|
134 | assert.expect(2);
|
135 | bcrypt.genSalt(1).then(function(salt) {
|
136 | var split_salt = salt.split('$');
|
137 | assert.strictEqual(split_salt[1], '2b');
|
138 | assert.strictEqual(split_salt[2], '04');
|
139 | assert.done();
|
140 | });
|
141 | },
|
142 | test_verify_salt_max_rounds: function(assert) {
|
143 | assert.expect(2);
|
144 | bcrypt.genSalt(100).then(function(salt) {
|
145 | var split_salt = salt.split('$');
|
146 | assert.strictEqual(split_salt[1], '2b');
|
147 | assert.strictEqual(split_salt[2], '31');
|
148 | assert.done();
|
149 | });
|
150 | },
|
151 | test_hash_compare_returns_promise_on_null_callback: function(assert) {
|
152 | assert.strictEqual(typeof bcrypt.compare('password', 'something', null).then, 'function', "Should return a promise");
|
153 | assert.done();
|
154 | },
|
155 | test_hash_compare: function(assert) {
|
156 | assert.expect(3);
|
157 | bcrypt.genSalt(10).then(function(salt) {
|
158 | assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
|
159 | return bcrypt.hash("test", salt);
|
160 | }).then(function(hash) {
|
161 | return Promise.all(
|
162 | [
|
163 | bcrypt.compare("test", hash).then(function(res) {
|
164 | assert.strictEqual(res, true, "These hashes should be equal.");
|
165 | }),
|
166 | bcrypt.compare("blah", hash).then(function(res) {
|
167 | assert.strictEqual(res, false, "These hashes should not be equal.");
|
168 | })
|
169 | ]).then(function() {
|
170 | assert.done();
|
171 | });
|
172 | });
|
173 | },
|
174 | test_hash_compare_empty_strings: function(assert) {
|
175 | assert.expect(2);
|
176 | var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
|
177 | bcrypt.compare("", hash).then(function(res) {
|
178 | assert.strictEqual(res, false, "These hashes should not be equal.");
|
179 | return bcrypt.compare("", "");
|
180 | }).then(function(res) {
|
181 | assert.strictEqual(res, false, "These hashes should not be equal.");
|
182 | assert.done();
|
183 | });
|
184 | },
|
185 | test_hash_compare_invalid_strings: function(assert) {
|
186 | var fullString = 'envy1362987212538';
|
187 | var hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
|
188 | var wut = ':';
|
189 | Promise.all([
|
190 | bcrypt.compare(fullString, hash).then(function(res) {
|
191 | assert.ok(res);
|
192 | }),
|
193 | bcrypt.compare(fullString, wut).then(function(res) {
|
194 | assert.ok(!res);
|
195 | })
|
196 | ]).then(function() {
|
197 | assert.done();
|
198 | });
|
199 | },
|
200 | test_hash_compare_no_params: function(assert) {
|
201 | assert.expect(1);
|
202 | bcrypt.compare().then(function() {
|
203 | fail(assert, 'Should not resolve');
|
204 | }).catch(function(err) {
|
205 | assert.strictEqual(err.message, 'data and hash arguments required', 'Promise should be rejected when no parameters are supplied');
|
206 | }).then(function() {
|
207 | assert.done();
|
208 | });
|
209 | },
|
210 | test_hash_compare_one_param: function(assert) {
|
211 | assert.expect(1);
|
212 | bcrypt.compare('password').then(function() {
|
213 | fail(assert, 'Should not resolve');
|
214 | }).catch(function(err) {
|
215 | assert.strictEqual(err.message, 'data and hash arguments required', 'Promise should be rejected when no parameters are supplied');
|
216 | }).then(function() {
|
217 | assert.done();
|
218 | });
|
219 | },
|
220 | test_change_promise_impl_reject: function(assert) {
|
221 |
|
222 | promises.use({
|
223 | reject: function() {
|
224 | return 'mock';
|
225 | }
|
226 | });
|
227 |
|
228 | assert.equal(promises.reject(), 'mock');
|
229 |
|
230 |
|
231 | promises.use(global.Promise);
|
232 | assert.done();
|
233 |
|
234 | },
|
235 | test_change_promise_impl_promise: function(assert) {
|
236 |
|
237 | promises.use({
|
238 | reject: function(err) {
|
239 | assert.equal(err.message, 'fn must be a function');
|
240 | return 'mock';
|
241 | }
|
242 | });
|
243 |
|
244 | assert.equal(promises.promise('', '', ''), 'mock');
|
245 |
|
246 |
|
247 | promises.use(global.Promise);
|
248 | assert.done();
|
249 |
|
250 | }
|
251 | };
|
252 | }
|