|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Authentication::PasswordHasher do |
| 6 | + let(:password) { 'Test123!@#' } |
| 7 | + |
| 8 | + describe '.hash' do |
| 9 | + it 'returns a string with argon2id prefix' do |
| 10 | + expect(described_class.hash(password)).to start_with('$argon2id$') |
| 11 | + end |
| 12 | + |
| 13 | + it 'produces a different digest on each call due to random salt' do |
| 14 | + expect(described_class.hash(password)).not_to eq(described_class.hash(password)) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + describe '.verify' do |
| 19 | + context 'with an argon2id digest' do |
| 20 | + let(:digest) { described_class.hash(password) } |
| 21 | + |
| 22 | + it 'returns true for the correct password' do |
| 23 | + expect(described_class.verify(password, digest)).to be true |
| 24 | + end |
| 25 | + |
| 26 | + it 'returns false for a wrong password' do |
| 27 | + expect(described_class.verify('WrongPass1', digest)).to be false |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + context 'with a bcrypt digest (legacy retrocompatibility)' do |
| 32 | + let(:digest) { BCrypt::Password.create(password, cost: BCrypt::Engine::MIN_COST) } |
| 33 | + |
| 34 | + it 'returns true for the correct password' do |
| 35 | + expect(described_class.verify(password, digest)).to be true |
| 36 | + end |
| 37 | + |
| 38 | + it 'returns false for a wrong password' do |
| 39 | + expect(described_class.verify('WrongPass1', digest)).to be false |
| 40 | + end |
| 41 | + |
| 42 | + it 'returns false for a malformed bcrypt string' do |
| 43 | + expect(described_class.verify(password, '$2a$not_a_valid_hash')).to be false |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context 'with blank inputs' do |
| 48 | + let(:digest) { described_class.hash(password) } |
| 49 | + |
| 50 | + it 'returns false when password is blank' do |
| 51 | + expect(described_class.verify('', digest)).to be false |
| 52 | + end |
| 53 | + |
| 54 | + it 'returns false when digest is blank' do |
| 55 | + expect(described_class.verify(password, '')).to be false |
| 56 | + end |
| 57 | + |
| 58 | + it 'returns false when both are blank' do |
| 59 | + expect(described_class.verify('', '')).to be false |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + describe '.needs_upgrade?' do |
| 65 | + it 'returns true for a bcrypt digest' do |
| 66 | + digest = BCrypt::Password.create(password, cost: BCrypt::Engine::MIN_COST) |
| 67 | + expect(described_class.needs_upgrade?(digest)).to be true |
| 68 | + end |
| 69 | + |
| 70 | + it 'returns false for an argon2id digest' do |
| 71 | + expect(described_class.needs_upgrade?(described_class.hash(password))).to be false |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + describe '.bcrypt?' do |
| 76 | + it 'returns true for $2a$ prefix (standard bcrypt)' do |
| 77 | + expect(described_class.bcrypt?('$2a$12$somehashvalue')).to be true |
| 78 | + end |
| 79 | + |
| 80 | + it 'returns true for $2b$ prefix (canonical bcrypt)' do |
| 81 | + expect(described_class.bcrypt?('$2b$12$somehashvalue')).to be true |
| 82 | + end |
| 83 | + |
| 84 | + it 'returns false for an argon2id digest' do |
| 85 | + expect(described_class.bcrypt?('$argon2id$v=19$m=65536,t=3,p=2$salt$hash')).to be false |
| 86 | + end |
| 87 | + |
| 88 | + it 'returns false for a blank string' do |
| 89 | + expect(described_class.bcrypt?('')).to be false |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments