2015-11-04 17:27:01 +01:00
|
|
|
require 'spec_helper'
|
2015-10-23 16:19:55 +02:00
|
|
|
|
2015-11-04 17:27:01 +01:00
|
|
|
describe Administrateur, type: :model do
|
2018-05-31 18:07:19 +02:00
|
|
|
let(:administration) { create(:administration) }
|
|
|
|
|
2015-11-10 10:23:15 +01:00
|
|
|
describe 'assocations' do
|
2016-05-20 15:39:17 +02:00
|
|
|
it { is_expected.to have_and_belong_to_many(:gestionnaires) }
|
2015-11-10 10:23:15 +01:00
|
|
|
it { is_expected.to have_many(:procedures) }
|
|
|
|
end
|
|
|
|
|
2016-12-07 17:24:01 +01:00
|
|
|
context 'unified login' do
|
|
|
|
it 'syncs credentials to associated user' do
|
|
|
|
administrateur = create(:administrateur)
|
|
|
|
user = create(:user, email: administrateur.email)
|
|
|
|
|
2018-01-25 10:50:06 +01:00
|
|
|
administrateur.update(email: 'whoami@plop.com', password: 'voilà un super mdp')
|
2016-12-07 17:24:01 +01:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email).to eq('whoami@plop.com')
|
2018-01-25 10:50:06 +01:00
|
|
|
expect(user.valid_password?('voilà un super mdp')).to be(true)
|
2016-12-07 17:24:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'syncs credentials to associated administrateur' do
|
|
|
|
administrateur = create(:administrateur)
|
|
|
|
gestionnaire = create(:gestionnaire, email: administrateur.email)
|
|
|
|
|
2018-01-25 10:50:06 +01:00
|
|
|
administrateur.update(email: 'whoami@plop.com', password: 'et encore un autre mdp')
|
2016-12-07 17:24:01 +01:00
|
|
|
|
|
|
|
gestionnaire.reload
|
|
|
|
expect(gestionnaire.email).to eq('whoami@plop.com')
|
2018-01-25 10:50:06 +01:00
|
|
|
expect(gestionnaire.valid_password?('et encore un autre mdp')).to be(true)
|
2016-12-07 17:24:01 +01:00
|
|
|
end
|
|
|
|
end
|
2018-01-11 14:17:50 +01:00
|
|
|
|
2018-08-24 16:45:43 +02:00
|
|
|
describe "#renew_api_token" do
|
2018-09-26 17:22:36 +02:00
|
|
|
let!(:administrateur) { create(:administrateur) }
|
|
|
|
let!(:token) { administrateur.renew_api_token }
|
2018-08-24 16:45:43 +02:00
|
|
|
|
2018-09-26 17:22:36 +02:00
|
|
|
it { expect(BCrypt::Password.new(administrateur.encrypted_token)).to eq(token) }
|
2018-08-24 16:45:43 +02:00
|
|
|
|
|
|
|
context 'when it s called twice' do
|
2018-09-26 17:22:36 +02:00
|
|
|
let!(:new_token) { administrateur.renew_api_token }
|
2018-08-24 16:45:43 +02:00
|
|
|
|
2018-09-26 17:22:36 +02:00
|
|
|
it { expect(new_token).not_to eq(token) }
|
2018-08-24 16:45:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
describe '#find_inactive_by_token' do
|
|
|
|
let(:administrateur) { create(:administration).invite_admin('paul@tps.fr') }
|
2018-05-31 18:07:19 +02:00
|
|
|
let(:reset_password_token) { administrateur.invite!(administration.id) }
|
2018-01-11 14:17:50 +01:00
|
|
|
|
|
|
|
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') }
|
2018-05-31 18:07:19 +02:00
|
|
|
let(:reset_password_token) { administrateur.invite!(administration.id) }
|
2018-01-11 14:17:50 +01:00
|
|
|
|
2018-01-25 10:50:06 +01:00
|
|
|
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 }
|
2018-01-11 14:17:50 +01:00
|
|
|
it { expect(Administrateur.reset_password(reset_password_token, '').errors).not_to be_empty }
|
|
|
|
end
|
2018-03-27 15:43:59 +02:00
|
|
|
|
|
|
|
describe '#feature_enabled?' do
|
|
|
|
let(:administrateur) { create(:administrateur) }
|
|
|
|
|
|
|
|
before do
|
2018-04-18 12:16:25 +02:00
|
|
|
administrateur.enable_feature(:champ_pj)
|
2018-03-27 15:43:59 +02:00
|
|
|
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 }
|
2018-03-27 15:43:59 +02:00
|
|
|
end
|
2018-01-25 10:50:06 +01:00
|
|
|
|
|
|
|
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
|
2015-10-23 16:19:55 +02:00
|
|
|
end
|