2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
describe FranceConnectInformation, type: :model do
|
|
|
|
describe 'validation' do
|
|
|
|
context 'france_connect_particulier_id' do
|
|
|
|
it { is_expected.not_to allow_value(nil).for(:france_connect_particulier_id) }
|
|
|
|
it { is_expected.not_to allow_value('').for(:france_connect_particulier_id) }
|
|
|
|
it { is_expected.to allow_value('mon super projet').for(:france_connect_particulier_id) }
|
|
|
|
end
|
|
|
|
end
|
2021-02-01 14:28:04 +01:00
|
|
|
|
|
|
|
describe 'associate_user!' do
|
2024-07-04 12:47:53 +02:00
|
|
|
let(:email) { 'A@email.com' }
|
|
|
|
let(:fci) { build(:france_connect_information) }
|
2021-10-13 00:18:00 +02:00
|
|
|
|
2024-07-04 12:47:53 +02:00
|
|
|
subject { fci.associate_user!(email) }
|
2021-02-01 14:28:04 +01:00
|
|
|
|
2024-07-04 12:47:53 +02:00
|
|
|
context 'when there is no user with the same email' do
|
|
|
|
it 'creates a new user' do
|
|
|
|
expect { subject }.to change(User, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the correct attributes on the user' do
|
|
|
|
subject
|
|
|
|
user = User.find_by(email: email.downcase)
|
|
|
|
expect(user).not_to be_nil
|
|
|
|
expect(user.confirmed_at).to be_present
|
|
|
|
end
|
2021-10-13 00:18:00 +02:00
|
|
|
|
2024-07-04 12:47:53 +02:00
|
|
|
it 'sends custom confirmation instructions' do
|
|
|
|
expect(UserMailer).to receive(:custom_confirmation_instructions).and_call_original
|
2021-02-01 14:28:04 +01:00
|
|
|
subject
|
2024-07-04 12:47:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'associates the user with the FranceConnectInformation' do
|
|
|
|
subject
|
|
|
|
expect(fci.reload.user.email).to eq(email.downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user with the same email already exists due to race condition' do
|
|
|
|
let!(:existing_user) { create(:user, email: email.downcase) }
|
|
|
|
let!(:fci) { create(:france_connect_information) } # Assurez-vous que fci est créé et sauvegardé
|
|
|
|
|
|
|
|
before do
|
|
|
|
call_count = 0
|
|
|
|
allow(User).to receive(:create!).and_wrap_original do
|
|
|
|
call_count += 1
|
|
|
|
if call_count == 1
|
|
|
|
raise ActiveRecord::RecordNotUnique
|
|
|
|
else
|
|
|
|
existing_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
allow(fci).to receive(:send_custom_confirmation_instructions)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { fci.associate_user!(email) }.to raise_error(NoMethodError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new user' do
|
|
|
|
expect {
|
|
|
|
begin
|
|
|
|
fci.associate_user!(email)
|
|
|
|
rescue NoMethodError
|
|
|
|
end
|
|
|
|
}.to_not change(User, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not associate with any user' do
|
|
|
|
expect(fci.user).to be_nil
|
|
|
|
begin
|
|
|
|
fci.associate_user!(email)
|
|
|
|
rescue NoMethodError
|
|
|
|
end
|
|
|
|
expect(fci.reload.user).to be_nil
|
2021-02-01 14:28:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-10-13 00:14:12 +02:00
|
|
|
|
|
|
|
describe '#valid_for_merge?' do
|
|
|
|
let(:fci) { create(:france_connect_information) }
|
|
|
|
|
|
|
|
subject { fci.valid_for_merge? }
|
|
|
|
|
|
|
|
context 'when the merge token is young enough' do
|
|
|
|
before { fci.merge_token_created_at = 1.minute.ago }
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
|
|
|
|
context 'but the fci is already linked to an user' do
|
|
|
|
before { fci.update(user: create(:user)) }
|
|
|
|
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the merge token is too old' do
|
|
|
|
before { fci.merge_token_created_at = (FranceConnectInformation::MERGE_VALIDITY + 1.minute).ago }
|
|
|
|
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
|
|
|
end
|
2021-10-13 00:40:15 +02:00
|
|
|
|
|
|
|
describe '#create_merge_token!' do
|
|
|
|
let(:fci) { create(:france_connect_information) }
|
|
|
|
|
|
|
|
it 'returns a merge_token and register it s creation date' do
|
|
|
|
token = fci.create_merge_token!
|
|
|
|
|
|
|
|
expect(fci.merge_token).to eq(token)
|
|
|
|
expect(fci.merge_token_created_at).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
2016-01-21 17:06:09 +01:00
|
|
|
end
|