2015-12-24 10:12:23 +01:00
|
|
|
describe FranceConnect::ParticulierController, type: :controller do
|
|
|
|
let(:code) { 'plop' }
|
|
|
|
let(:given_name) { 'titi' }
|
|
|
|
let(:family_name) { 'toto' }
|
|
|
|
let(:birthdate) { '20150821' }
|
|
|
|
let(:gender) { 'M' }
|
|
|
|
let(:birthplace) { '1234' }
|
|
|
|
let(:france_connect_particulier_id) { 'blabla' }
|
2016-01-21 17:06:09 +01:00
|
|
|
let(:email) { 'test@test.com' }
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-11 18:16:38 +01:00
|
|
|
let(:user_info) { { france_connect_particulier_id: france_connect_particulier_id, given_name: given_name, family_name: family_name, birthdate: birthdate, birthplace: birthplace, gender: gender, email_france_connect: email } }
|
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
|
2015-12-24 10:12:23 +01:00
|
|
|
context 'when param code is missing' do
|
2018-01-16 13:45:12 +01:00
|
|
|
subject { get :callback, params: { code: code } }
|
|
|
|
|
|
|
|
let(:code) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to(new_user_session_path) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when param code is empty' do
|
|
|
|
subject { get :callback, params: { code: code } }
|
|
|
|
|
|
|
|
let(:code) { '' }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to(new_user_session_path) }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params code is present' do
|
|
|
|
context 'when code is correct' do
|
|
|
|
before do
|
2018-01-11 18:16:38 +01:00
|
|
|
allow(FranceConnectService).to receive(:retrieve_user_informations_particulier)
|
|
|
|
.and_return(FranceConnectInformation.new(user_info))
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when france_connect_particulier_id exist in database' do
|
2016-01-21 17:06:09 +01:00
|
|
|
let!(:france_connect_information) { create(:france_connect_information, france_connect_particulier_id: france_connect_particulier_id, given_name: given_name, family_name: family_name, birthdate: birthdate, gender: gender, birthplace: birthplace) }
|
|
|
|
|
|
|
|
context {
|
2016-11-15 04:42:32 +01:00
|
|
|
subject { get :callback, params: {code: code} }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
it 'does not create a new france_connect_information in database' do
|
|
|
|
expect { subject }.not_to change { FranceConnectInformation.count }
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
context 'when france_connect_particulier_id have an associate user' do
|
|
|
|
before do
|
|
|
|
create(:user, email: email, france_connect_information: france_connect_information)
|
|
|
|
|
2016-11-15 04:42:32 +01:00
|
|
|
get :callback, params: {code: code}
|
2016-01-21 17:06:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:email) { 'plop@plop.com' }
|
|
|
|
let(:current_user) { User.find_by_email(email) }
|
|
|
|
let(:stored_location) { '/plip/plop' }
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
it 'current user have attribut loged_in_with_france_connect? at true' do
|
|
|
|
expect(current_user.loged_in_with_france_connect?).to be_truthy
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
it 'redirect to stored location' do
|
|
|
|
subject.store_location_for(:user, stored_location)
|
|
|
|
|
2016-11-15 04:42:32 +01:00
|
|
|
get :callback, params: {code: code}
|
2016-01-21 17:06:09 +01:00
|
|
|
expect(response).to redirect_to(stored_location)
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
context 'when france_connect_particulier_id does not have an associate user' do
|
|
|
|
before do
|
2016-11-15 04:42:32 +01:00
|
|
|
get :callback, params: {code: code}
|
2016-01-21 17:06:09 +01:00
|
|
|
end
|
|
|
|
|
2018-01-16 12:08:50 +01:00
|
|
|
it {expect(response).to redirect_to(root_path)}
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when france_connect_particulier_id does not exist in database' do
|
2016-01-21 17:06:09 +01:00
|
|
|
let(:last_france_connect_information) { FranceConnectInformation.last }
|
2016-11-15 04:42:32 +01:00
|
|
|
subject { get :callback, params: {code: code} }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
it { expect { subject }.to change { FranceConnectInformation.count }.by(1) }
|
|
|
|
|
|
|
|
describe 'FranceConnectInformation attributs' do
|
|
|
|
before do
|
2016-11-15 04:42:32 +01:00
|
|
|
get :callback, params: {code: code}
|
2016-01-21 17:06:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { last_france_connect_information }
|
|
|
|
|
|
|
|
it { expect(subject.gender).to eq gender }
|
|
|
|
it { expect(subject.given_name).to eq given_name }
|
|
|
|
it { expect(subject.family_name).to eq family_name }
|
|
|
|
it { expect(subject.email_france_connect).to eq email }
|
|
|
|
it { expect(subject.birthdate.to_time.to_i).to eq birthdate.to_time.to_i }
|
|
|
|
it { expect(subject.birthplace).to eq birthplace }
|
|
|
|
it { expect(subject.france_connect_particulier_id).to eq france_connect_particulier_id }
|
|
|
|
end
|
|
|
|
|
2018-01-16 12:08:50 +01:00
|
|
|
it { expect(subject).to redirect_to(root_path) }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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') }
|
2016-11-15 04:42:32 +01:00
|
|
|
get :callback, params: {code: code}
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirect to login page' do
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'display error message' do
|
|
|
|
expect(flash[:alert]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|