demarches-normaliennes/spec/models/administrateur_spec.rb

107 lines
3.6 KiB
Ruby
Raw Normal View History

2015-11-04 17:27:01 +01:00
require 'spec_helper'
2015-11-04 17:27:01 +01:00
describe Administrateur, type: :model do
let(:administration) { create(:administration) }
describe 'assocations' do
it { is_expected.to have_and_belong_to_many(:gestionnaires) }
it { is_expected.to have_many(:procedures) }
end
context 'unified login' do
it 'syncs credentials to associated user' do
administrateur = create(:administrateur)
user = create(:user, email: administrateur.email)
administrateur.update(email: 'whoami@plop.com', password: 'voilà un super mdp')
user.reload
expect(user.email).to eq('whoami@plop.com')
expect(user.valid_password?('voilà un super mdp')).to be(true)
end
it 'syncs credentials to associated administrateur' do
administrateur = create(:administrateur)
gestionnaire = create(:gestionnaire, email: administrateur.email)
administrateur.update(email: 'whoami@plop.com', password: 'et encore un autre mdp')
gestionnaire.reload
expect(gestionnaire.email).to eq('whoami@plop.com')
expect(gestionnaire.valid_password?('et encore un autre mdp')).to be(true)
end
end
describe "#renew_api_token" do
let(:administrateur) { create(:administrateur) }
before do
administrateur.renew_api_token
administrateur.reload
end
it { expect(administrateur.api_token).to be_present }
it { expect(administrateur.api_token).not_to eq(administrateur.encrypted_token) }
it { expect(BCrypt::Password.new(administrateur.encrypted_token)).to eq(administrateur.api_token) }
context 'when it s called twice' do
let!(:previous_token) { administrateur.api_token }
it { expect(previous_token).not_to eq(administrateur.renew_api_token) }
end
end
describe '#find_inactive_by_token' do
let(:administrateur) { create(:administration).invite_admin('paul@tps.fr') }
let(:reset_password_token) { administrateur.invite!(administration.id) }
it { expect(Administrateur.find_inactive_by_token(reset_password_token)).not_to be_nil }
end
describe '#reset_password' do
let(:administrateur) { create(:administration).invite_admin('paul@tps.fr') }
let(:reset_password_token) { administrateur.invite!(administration.id) }
it { expect(Administrateur.reset_password(reset_password_token, "j'aime manger des radis").errors).to be_empty }
it { expect(Administrateur.reset_password('123', "j'aime manger des radis").errors).not_to be_empty }
it { expect(Administrateur.reset_password(reset_password_token, '').errors).not_to be_empty }
end
describe '#feature_enabled?' do
let(:administrateur) { create(:administrateur) }
before do
2018-04-18 12:16:25 +02:00
administrateur.enable_feature(:champ_pj)
end
2018-04-18 12:16:25 +02:00
it { expect(administrateur.feature_enabled?(:champ_siret)).to be_falsey }
it { expect(administrateur.feature_enabled?(:champ_pj)).to be_truthy }
end
describe "#password_complexity" do
let(:administrateur) { build(:administrateur, password: password) }
subject do
administrateur.save
administrateur.errors[:password]
end
context "with a strong password" do
let(:password) { "la démat c'est simple" }
it { expect(subject).to eq([]) }
end
context "with a weak password" do
let(:password) { "12345678" }
it { expect(subject).to include "n'est pas assez complexe" }
it { expect(subject).not_to include "est trop court" }
end
context "with a short password" do
let(:password) { "1" }
it { expect(subject).to include "est trop court" }
it { expect(subject).not_to include "n'est pas assez complexe" }
end
end
end