promise.test.js 5.1 KB

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