Sha256: f05b27fa2a919ef29de51829d5e682a1e7e5a567678e5dbf71474089e6668562

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'spec_helper'

describe Clearance::PasswordStrategies::BCrypt do
  subject do
    fake_model_with_password_strategy(Clearance::PasswordStrategies::BCrypt)
  end

  describe '#password=' do
    let(:password) { 'password' }
    let(:encrypted_password) { double("encrypted password") }

    before do
      allow(BCrypt::Password).to receive(:create).and_return(encrypted_password)
    end

    it 'encrypts the password into encrypted_password' do
      subject.password = password

      expect(subject.encrypted_password).to eq encrypted_password
    end

    it 'encrypts with BCrypt using default cost in non test environments' do
      allow(Rails).to receive(:env).
        and_return(ActiveSupport::StringInquirer.new("production"))

      subject.password = password

      expect(BCrypt::Password).to have_received(:create).with(
        password,
        cost: ::BCrypt::Engine::DEFAULT_COST
      )
    end

    it 'encrypts with BCrypt using minimum cost in test environment' do
      subject.password = password

      expect(BCrypt::Password).to have_received(:create).with(
        password,
        cost: ::BCrypt::Engine::MIN_COST
      )
    end
  end

  describe '#authenticated?' do

    before do
      subject.password = password
    end

    context 'given a password' do
      let(:password) { 'password' }

      it 'is authenticated with BCrypt' do
        expect(subject).to be_authenticated(password)
      end
    end

    context 'given no password' do
      let(:password) { nil }

      it 'is not authenticated' do
        expect(subject).not_to be_authenticated(password)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clearance-1.7.0 spec/models/bcrypt_spec.rb
clearance-1.6.1 spec/models/bcrypt_spec.rb
clearance-1.6.0 spec/models/bcrypt_spec.rb