2015-12-24 10:12:23 +01:00
|
|
|
describe FranceConnect::ParticulierController, type: :controller do
|
|
|
|
let(:birthdate) { '20150821' }
|
2016-01-21 17:06:09 +01:00
|
|
|
let(:email) { 'test@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
|
|
|
|
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
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
context 'when france_connect_particulier_id exist in database' do
|
2018-01-16 14:52:29 +01:00
|
|
|
let!(:france_connect_information) { create(:france_connect_information, user_info) }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 14:09:32 +01:00
|
|
|
it { expect { subject }.not_to change { FranceConnectInformation.count } }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
context 'when france_connect_particulier_id have an associate user' do
|
2018-01-16 14:27:40 +01:00
|
|
|
let!(:user) { create(:user, email: 'plop@plop.com', france_connect_information: france_connect_information) }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 14:27:40 +01:00
|
|
|
it do
|
|
|
|
subject
|
2018-08-28 11:41:37 +02:00
|
|
|
expect(user.reload.loged_in_with_france_connect).to eq(User.loged_in_with_france_connects.fetch(:particulier))
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2018-01-16 14:27:40 +01: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
|
|
|
|
2018-01-16 14:27:40 +01:00
|
|
|
it { is_expected.to redirect_to(stored_location) }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
context 'when france_connect_particulier_id does not have an associate user' do
|
2018-01-16 14:30:37 +01:00
|
|
|
it { is_expected.to redirect_to(root_path) }
|
2018-01-16 14:52:29 +01:00
|
|
|
|
|
|
|
it do
|
|
|
|
subject
|
|
|
|
expect(User.find_by(email: email)).not_to be_nil
|
|
|
|
end
|
2018-01-16 13:52:44 +01:00
|
|
|
end
|
|
|
|
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
|
|
|
|
2018-01-16 15:16:51 +01:00
|
|
|
it { expect(stored_fci).to have_attributes(user_info.merge(birthdate: DateTime.parse(birthdate))) }
|
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
|