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
|
|
|
|
|
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
|
|
|
|
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) }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 14:03:05 +01:00
|
|
|
subject { get :callback, params: {code: code} }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 14:03:05 +01:00
|
|
|
it 'does not create a new france_connect_information in database' do
|
|
|
|
expect { subject }.not_to change { FranceConnectInformation.count }
|
|
|
|
end
|
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
|
|
|
|
before do
|
|
|
|
create(:user, email: email, france_connect_information: france_connect_information)
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
get :callback, params: {code: code}
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
let(:email) { 'plop@plop.com' }
|
|
|
|
let(:current_user) { User.find_by_email(email) }
|
|
|
|
let(:stored_location) { '/plip/plop' }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 13:52:44 +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
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
it 'redirect to stored location' do
|
2018-01-16 14:01:26 +01:00
|
|
|
controller.store_location_for(:user, stored_location)
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
get :callback, params: {code: code}
|
|
|
|
expect(response).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
|
|
|
|
before do
|
|
|
|
get :callback, params: {code: code}
|
|
|
|
end
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
it {expect(response).to redirect_to(root_path)}
|
|
|
|
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
|
|
|
|
let(:last_france_connect_information) { FranceConnectInformation.last }
|
|
|
|
subject { get :callback, params: {code: code} }
|
2016-01-21 17:06:09 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
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
|
|
|
|
before do
|
|
|
|
get :callback, params: {code: code}
|
2016-01-21 17:06:09 +01:00
|
|
|
end
|
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
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 }
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
2018-01-16 13:52:44 +01:00
|
|
|
|
|
|
|
it { expect(subject).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') }
|
|
|
|
get :callback, params: {code: code}
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
it 'redirect to login page' do
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-16 13:52:44 +01:00
|
|
|
it 'display error message' do
|
|
|
|
expect(flash[:alert]).to be_present
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|