password_hash_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe PasswordHash do
  4. let(:pw_plain) { 'zammad' }
  5. context 'stable API' do
  6. it 'responds to crypt' do
  7. expect(described_class).to respond_to(:crypt)
  8. end
  9. it 'responds to verified?' do
  10. expect(described_class).to respond_to(:verified?)
  11. end
  12. it 'responds to crypted?' do
  13. expect(described_class).to respond_to(:crypted?)
  14. end
  15. it 'responds to legacy?' do
  16. expect(described_class).to respond_to(:legacy?)
  17. end
  18. it 'responds to sha2' do
  19. expect(described_class).to respond_to(:sha2)
  20. end
  21. it 'responds to hashed_sha2?' do
  22. expect(described_class).to respond_to(:hashed_sha2?)
  23. end
  24. it 'responds to hashed_argon2?' do
  25. expect(described_class).to respond_to(:hashed_argon2?)
  26. end
  27. end
  28. context 'encryption' do
  29. it 'crypts passwords' do
  30. pw_crypted = described_class.crypt(pw_plain)
  31. expect(pw_crypted).not_to eq(pw_plain)
  32. end
  33. it 'verifies crypted passwords' do
  34. pw_crypted = described_class.crypt(pw_plain)
  35. expect(described_class.verified?(pw_crypted, pw_plain)).to be true
  36. end
  37. it 'detects crypted passwords' do
  38. pw_crypted = described_class.crypt(pw_plain)
  39. expect(described_class.crypted?(pw_crypted)).to be true
  40. end
  41. end
  42. context 'legacy' do
  43. let(:zammad_sha2) { '{sha2}dd9c764fa7ea18cd992c8600006d3dc3ac983d1ba22e9ba2d71f6207456be0ba' }
  44. it 'requires hash to be not blank' do
  45. expect(described_class).not_to be_legacy(nil, pw_plain)
  46. expect(described_class).not_to be_legacy('', pw_plain)
  47. end
  48. it 'requires password to be not nil' do
  49. expect(described_class).not_to be_legacy(zammad_sha2, nil)
  50. end
  51. it 'detects sha2 hashes' do
  52. expect(described_class.legacy?(zammad_sha2, pw_plain)).to be true
  53. end
  54. it 'detects crypted passwords' do
  55. expect(described_class.crypted?(zammad_sha2)).to be true
  56. end
  57. describe '::sha2' do
  58. it 'creates sha2 hashes' do
  59. hashed = described_class.sha2(pw_plain)
  60. expect(hashed).to eq zammad_sha2
  61. end
  62. end
  63. describe '::hashed_sha2?' do
  64. it 'detects sha2 hashes' do
  65. expect(described_class.hashed_sha2?(zammad_sha2)).to be true
  66. end
  67. end
  68. end
  69. end