2015-12-24 10:12:23 +01:00
|
|
|
describe FranceConnect::ParticulierController, type: :controller do
|
|
|
|
let(:birthdate) { '20150821' }
|
2021-10-13 00:45:20 +02:00
|
|
|
let(:email) { 'EMAIL_from_fc@test.com' }
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 15:29:37 +01:00
|
|
|
let(:user_info) do
|
|
|
|
{
|
|
|
|
france_connect_particulier_id: 'blablabla',
|
|
|
|
given_name: 'titi',
|
|
|
|
family_name: 'toto',
|
|
|
|
birthdate: birthdate,
|
|
|
|
birthplace: '1234',
|
|
|
|
gender: 'M',
|
|
|
|
email_france_connect: email
|
|
|
|
}
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 13:37:38 +01:00
|
|
|
describe '#auth' do
|
|
|
|
subject { get :login }
|
|
|
|
|
|
|
|
it { is_expected.to have_http_status(:redirect) }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2018-01-16 13:45:12 +01:00
|
|
|
describe '#callback' do
|
2018-01-16 15:29:37 +01:00
|
|
|
let(:code) { 'plop' }
|
|
|
|
|
2018-01-16 13:34:24 +01:00
|
|
|
subject { get :callback, params: { code: code } }
|
2018-01-16 13:45:12 +01:00
|
|
|
|
2019-05-09 13:54:50 +02:00
|
|
|
context 'when params are missing' do
|
|
|
|
subject { get :callback }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to(new_user_session_path) }
|
|
|
|
end
|
|
|
|
|
2018-01-16 14:11:17 +01:00
|
|
|
context 'when param code is missing' do
|
2018-01-16 13:45:12 +01:00
|
|
|
let(:code) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to(new_user_session_path) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when param code is empty' do
|
|
|
|
let(:code) { '' }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to(new_user_session_path) }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
context 'when code is correct' do
|
|
|
|
before do
|
|
|
|
allow(FranceConnectService).to receive(:retrieve_user_informations_particulier)
|
|
|
|
.and_return(FranceConnectInformation.new(user_info))
|
|
|
|
end
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
context 'when france_connect_particulier_id exists in database' do
|
2021-10-13 00:45:20 +02:00
|
|
|
let!(:fci) { FranceConnectInformation.create!(user_info.merge(user_id: fc_user&.id)) }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
context 'and is linked to an user' do
|
|
|
|
let(:fc_user) { create(:user, email: 'associated_user@a.com') }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
it { expect { subject }.not_to change { FranceConnectInformation.count } }
|
|
|
|
|
|
|
|
it 'signs in with the fci associated user' do
|
|
|
|
subject
|
|
|
|
expect(controller.current_user).to eq(fc_user)
|
|
|
|
expect(fc_user.reload.loged_in_with_france_connect).to eq(User.loged_in_with_france_connects.fetch(:particulier))
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
context 'and the user has a stored location' do
|
|
|
|
let(:stored_location) { '/plip/plop' }
|
|
|
|
before { controller.store_location_for(:user, stored_location) }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
it { is_expected.to redirect_to(stored_location) }
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
context 'and is linked an instructeur' do
|
|
|
|
let(:fc_user) { create(:instructeur, email: 'another_email@a.com').user }
|
2021-04-26 16:27:28 +02:00
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
before { subject }
|
2021-04-26 16:27:28 +02:00
|
|
|
|
2021-10-11 11:39:14 +02:00
|
|
|
it do
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
expect(flash[:alert]).to be_present
|
|
|
|
end
|
2018-01-16 13:52:44 +01:00
|
|
|
end
|
2021-10-13 00:45:20 +02:00
|
|
|
|
|
|
|
context 'and is not linked to an user' do
|
|
|
|
let(:fc_user) { nil }
|
|
|
|
|
|
|
|
context 'and no user with the same email exists' do
|
|
|
|
it 'creates an user with the same email and log in' do
|
|
|
|
expect { subject }.to change { User.count }.by(1)
|
|
|
|
|
|
|
|
user = User.last
|
|
|
|
|
|
|
|
expect(user.email).to eq(email.downcase)
|
|
|
|
expect(controller.current_user).to eq(user)
|
|
|
|
expect(response).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and an user with the same email exists' do
|
|
|
|
let!(:preexisting_user) { create(:user, email: email) }
|
|
|
|
|
|
|
|
it 'redirects to the merge process' do
|
|
|
|
expect { subject }.not_to change { User.count }
|
|
|
|
|
|
|
|
expect(response).to redirect_to(france_connect_particulier_merge_path(fci.reload.merge_token))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-01-16 13:52:44 +01:00
|
|
|
end
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
context 'when france_connect_particulier_id does not exist in database' do
|
|
|
|
it { expect { subject }.to change { FranceConnectInformation.count }.by(1) }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
describe 'FranceConnectInformation attributs' do
|
2018-01-16 15:16:51 +01:00
|
|
|
let(:stored_fci) { FranceConnectInformation.last }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 15:16:51 +01:00
|
|
|
before { subject }
|
2018-01-16 13:52:44 +01:00
|
|
|
|
2019-11-25 14:39:42 +01:00
|
|
|
it { expect(stored_fci).to have_attributes(user_info.merge(birthdate: Time.zone.parse(birthdate).to_datetime)) }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
2018-01-16 13:52:44 +01:00
|
|
|
|
2018-01-16 15:16:51 +01:00
|
|
|
it { is_expected.to redirect_to(root_path) }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
2018-01-16 13:52:44 +01:00
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
context 'when code is not correct' do
|
|
|
|
before do
|
|
|
|
allow(FranceConnectService).to receive(:retrieve_user_informations_particulier) { raise Rack::OAuth2::Client::Error.new(500, error: 'Unknown') }
|
2018-01-16 15:16:51 +01:00
|
|
|
subject
|
2018-01-16 13:52:44 +01:00
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 15:16:51 +01:00
|
|
|
it { expect(response).to redirect_to(new_user_session_path) }
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 15:16:51 +01:00
|
|
|
it { expect(flash[:alert]).to be_present }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|