2015-10-07 14:18:55 +02:00
|
|
|
describe Users::SessionsController, type: :controller do
|
2019-01-09 13:47:52 +01:00
|
|
|
let(:email) { 'unique@plop.com' }
|
|
|
|
let(:password) { 'un super mot de passe' }
|
2018-08-28 11:41:37 +02:00
|
|
|
let(:loged_in_with_france_connect) { User.loged_in_with_france_connects.fetch(:particulier) }
|
2019-01-09 13:47:52 +01:00
|
|
|
let!(:user) { create(:user, email: email, password: password, loged_in_with_france_connect: loged_in_with_france_connect) }
|
2015-10-07 16:38:29 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
@request.env["devise.mapping"] = Devise.mappings[:user]
|
|
|
|
end
|
2015-10-07 14:18:55 +02:00
|
|
|
|
2018-03-20 16:00:30 +01:00
|
|
|
describe '#create' do
|
2019-01-09 13:47:52 +01:00
|
|
|
context "when the user is also a gestionnaire and an administrateur" do
|
2019-01-07 15:11:55 +01:00
|
|
|
let!(:administrateur) { create(:administrateur, :with_admin_trusted_device, email: email, password: password) }
|
|
|
|
let(:gestionnaire) { administrateur.gestionnaire }
|
2019-01-09 13:47:52 +01:00
|
|
|
let(:trusted_device) { true }
|
|
|
|
let(:send_password) { password }
|
2016-12-07 17:03:36 +01:00
|
|
|
|
2019-01-09 13:47:52 +01:00
|
|
|
before do
|
|
|
|
allow(controller).to receive(:trusted_device?).and_return(trusted_device)
|
2019-01-10 10:41:03 +01:00
|
|
|
allow(GestionnaireMailer).to receive(:send_login_token).and_return(double(deliver_later: true))
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
2019-01-09 13:47:52 +01:00
|
|
|
post :create, params: { user: { email: email, password: send_password } }
|
|
|
|
user.reload
|
|
|
|
end
|
2016-10-11 11:12:45 +02:00
|
|
|
|
2019-01-09 13:47:52 +01:00
|
|
|
context 'when the device is not trusted' do
|
|
|
|
let(:trusted_device) { false }
|
2019-01-07 15:11:55 +01:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
it 'redirects to the root path' do
|
2019-01-10 10:41:03 +01:00
|
|
|
subject
|
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
expect(controller).to redirect_to(root_path)
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-01-10 10:41:03 +01:00
|
|
|
expect(controller.current_user).to eq(user)
|
2019-02-01 17:17:10 +01:00
|
|
|
expect(controller.current_gestionnaire).to eq(gestionnaire)
|
|
|
|
expect(controller.current_administrateur).to eq(administrateur)
|
|
|
|
expect(user.loged_in_with_france_connect).to eq(nil)
|
2018-10-30 18:24:29 +01:00
|
|
|
end
|
2019-01-09 13:47:52 +01:00
|
|
|
end
|
2018-10-30 18:24:29 +01:00
|
|
|
|
2019-01-09 13:47:52 +01:00
|
|
|
context 'when the device is trusted' do
|
|
|
|
it 'signs in as user, gestionnaire and adminstrateur' do
|
2019-01-10 10:41:03 +01:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response.redirect?).to be(true)
|
|
|
|
expect(controller).not_to redirect_to link_sent_path(email: email)
|
2018-11-16 15:07:04 +01:00
|
|
|
# TODO when signing in as non-administrateur, and not starting a demarche, log in to gestionnaire path
|
2019-01-10 10:41:03 +01:00
|
|
|
# expect(controller).to redirect_to gestionnaire_procedures_path
|
2019-01-09 13:47:52 +01:00
|
|
|
|
2019-01-10 10:41:03 +01:00
|
|
|
expect(controller.current_user).to eq(user)
|
|
|
|
expect(controller.current_gestionnaire).to eq(gestionnaire)
|
|
|
|
expect(controller.current_administrateur).to eq(administrateur)
|
2019-01-09 13:47:52 +01:00
|
|
|
expect(user.loged_in_with_france_connect).to be(nil)
|
2018-10-30 18:24:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-09 13:47:52 +01:00
|
|
|
context 'when the credentials are wrong' do
|
|
|
|
let(:send_password) { 'wrong_password' }
|
|
|
|
|
|
|
|
it 'fails to sign in with bad credentials' do
|
2019-01-10 10:41:03 +01:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response.unauthorized?).to be(true)
|
|
|
|
expect(controller.current_user).to be(nil)
|
|
|
|
expect(controller.current_gestionnaire).to be(nil)
|
|
|
|
expect(controller.current_administrateur).to be(nil)
|
2019-01-09 13:47:52 +01:00
|
|
|
end
|
2016-10-11 11:12:45 +02:00
|
|
|
end
|
|
|
|
end
|
2015-10-07 16:38:29 +02:00
|
|
|
end
|
2015-10-07 14:18:55 +02:00
|
|
|
|
2018-03-20 16:00:30 +01:00
|
|
|
describe '#destroy' do
|
2015-10-07 14:18:55 +02:00
|
|
|
before do
|
2015-10-07 16:38:29 +02:00
|
|
|
sign_in user
|
|
|
|
delete :destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'user is sign out' do
|
|
|
|
expect(subject.current_user).to be_nil
|
2015-10-07 14:18:55 +02:00
|
|
|
end
|
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
it 'loged_in_with_france_connect current_user attribut is nil' do
|
2015-10-07 14:18:55 +02:00
|
|
|
user.reload
|
2018-12-18 22:48:56 +01:00
|
|
|
expect(user.loged_in_with_france_connect.present?).to be_falsey
|
2015-10-07 14:18:55 +02:00
|
|
|
end
|
2015-10-07 16:38:29 +02:00
|
|
|
|
2016-02-11 16:12:59 +01:00
|
|
|
context 'when user is connect with france connect particulier' do
|
2018-08-28 11:41:37 +02:00
|
|
|
let(:loged_in_with_france_connect) { User.loged_in_with_france_connects.fetch(:particulier) }
|
2015-12-24 10:12:23 +01:00
|
|
|
|
|
|
|
it 'redirect to france connect logout page' do
|
2018-01-11 14:04:24 +01:00
|
|
|
expect(response).to redirect_to(FRANCE_CONNECT[:particulier][:logout_endpoint])
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-07 16:38:29 +02:00
|
|
|
context 'when user is not connect with france connect' do
|
2015-12-24 10:12:23 +01:00
|
|
|
let(:loged_in_with_france_connect) { '' }
|
2015-12-09 15:10:11 +01:00
|
|
|
|
2015-10-07 16:38:29 +02:00
|
|
|
it 'redirect to root page' do
|
|
|
|
expect(response).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 11:12:45 +02:00
|
|
|
|
|
|
|
context "when associated gestionnaire" do
|
|
|
|
let(:user) { create(:user, email: 'unique@plop.com', password: 'password') }
|
|
|
|
let(:gestionnaire) { create(:gestionnaire, email: 'unique@plop.com', password: 'password') }
|
|
|
|
|
|
|
|
it 'signs user out' do
|
|
|
|
sign_in user
|
|
|
|
delete :destroy
|
|
|
|
expect(@response.redirect?).to be(true)
|
|
|
|
expect(subject.current_user).to be(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'signs gestionnaire out' do
|
|
|
|
sign_in gestionnaire
|
|
|
|
delete :destroy
|
|
|
|
expect(@response.redirect?).to be(true)
|
|
|
|
expect(subject.current_gestionnaire).to be(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'signs user + gestionnaire out' do
|
|
|
|
sign_in user
|
|
|
|
sign_in gestionnaire
|
|
|
|
delete :destroy
|
|
|
|
expect(@response.redirect?).to be(true)
|
|
|
|
expect(subject.current_user).to be(nil)
|
|
|
|
expect(subject.current_gestionnaire).to be(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'signs user out from france connect' do
|
2018-08-28 11:41:37 +02:00
|
|
|
user.update(loged_in_with_france_connect: User.loged_in_with_france_connects.fetch(:particulier))
|
2016-10-11 11:12:45 +02:00
|
|
|
sign_in user
|
|
|
|
delete :destroy
|
2018-01-11 14:04:24 +01:00
|
|
|
expect(@response.headers["Location"]).to eq(FRANCE_CONNECT[:particulier][:logout_endpoint])
|
2016-10-11 11:12:45 +02:00
|
|
|
end
|
2019-01-07 15:11:55 +01:00
|
|
|
end
|
2016-12-07 17:03:36 +01:00
|
|
|
|
2019-01-07 15:11:55 +01:00
|
|
|
context "when associated administrateur" do
|
|
|
|
let(:administrateur) { create(:administrateur, email: 'unique@plop.com') }
|
2016-12-07 17:03:36 +01:00
|
|
|
|
2019-01-07 15:11:55 +01:00
|
|
|
it 'signs user + gestionnaire + administrateur out' do
|
|
|
|
sign_in user
|
|
|
|
sign_in administrateur.gestionnaire
|
|
|
|
sign_in administrateur
|
|
|
|
delete :destroy
|
|
|
|
expect(@response.redirect?).to be(true)
|
|
|
|
expect(subject.current_user).to be(nil)
|
|
|
|
expect(subject.current_gestionnaire).to be(nil)
|
|
|
|
expect(subject.current_administrateur).to be(nil)
|
2016-12-07 17:03:36 +01:00
|
|
|
end
|
2016-10-11 11:12:45 +02:00
|
|
|
end
|
2015-10-07 14:18:55 +02:00
|
|
|
end
|
2016-05-26 15:59:50 +02:00
|
|
|
|
2018-03-20 16:00:30 +01:00
|
|
|
describe '#new' do
|
2016-05-26 15:59:50 +02:00
|
|
|
subject { get :new }
|
|
|
|
|
2019-01-14 16:25:48 +01:00
|
|
|
it { expect(subject.status).to eq 200 }
|
2016-05-26 15:59:50 +02:00
|
|
|
|
2019-01-14 16:25:48 +01:00
|
|
|
context 'when a procedure location has been stored' do
|
|
|
|
let(:procedure) { create :procedure, :published }
|
2016-06-09 17:49:38 +02:00
|
|
|
|
2019-01-14 16:25:48 +01:00
|
|
|
before do
|
2019-01-16 16:16:15 +01:00
|
|
|
controller.store_location_for(:user, commencer_path(path: procedure.path))
|
2016-06-09 17:49:38 +02:00
|
|
|
end
|
|
|
|
|
2019-01-14 16:25:48 +01:00
|
|
|
it 'makes the saved procedure available' do
|
|
|
|
expect(subject.status).to eq 200
|
|
|
|
expect(assigns(:procedure)).to eq procedure
|
2016-05-26 15:59:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-03 11:11:02 +02:00
|
|
|
|
|
|
|
describe '#sign_in_by_link' do
|
|
|
|
context 'when the gestionnaire has non other account' do
|
|
|
|
let(:gestionnaire) { create(:gestionnaire) }
|
2019-02-18 17:05:30 +01:00
|
|
|
let!(:good_jeton) { gestionnaire.create_trusted_device_token }
|
2019-02-01 17:17:10 +01:00
|
|
|
let(:logged) { false }
|
2019-01-03 16:00:58 +01:00
|
|
|
|
2018-10-03 11:11:02 +02:00
|
|
|
before do
|
2019-02-01 17:17:10 +01:00
|
|
|
if logged
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
2018-10-30 18:24:29 +01:00
|
|
|
allow(controller).to receive(:trust_device)
|
2019-02-01 17:17:10 +01:00
|
|
|
allow(controller).to receive(:send_login_token_or_bufferize)
|
2018-12-26 17:35:28 +01:00
|
|
|
post :sign_in_by_link, params: { id: gestionnaire.id, jeton: jeton }
|
2018-10-03 11:11:02 +02:00
|
|
|
end
|
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
context 'when the gestionnaire is not logged in' do
|
|
|
|
context 'when the token is valid' do
|
|
|
|
let(:jeton) { good_jeton }
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
it { is_expected.to redirect_to new_user_session_path }
|
|
|
|
it { expect(controller.current_gestionnaire).to be_nil }
|
|
|
|
it { expect(controller).to have_received(:trust_device) }
|
|
|
|
end
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
context 'when the token is invalid' do
|
|
|
|
let(:jeton) { 'invalid_token' }
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
it { is_expected.to redirect_to link_sent_path(email: gestionnaire.email) }
|
|
|
|
it { expect(controller.current_gestionnaire).to be_nil }
|
|
|
|
it { expect(controller).not_to have_received(:trust_device) }
|
|
|
|
it { expect(controller).to have_received(:send_login_token_or_bufferize) }
|
|
|
|
end
|
2018-10-03 11:11:02 +02:00
|
|
|
end
|
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
context 'when the gestionnaire is logged in' do
|
|
|
|
let(:logged) { true }
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
context 'when the token is valid' do
|
|
|
|
let(:jeton) { good_jeton }
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
# redirect to root_path, then redirect to gestionnaire_procedures_path (see root_controller)
|
|
|
|
it { is_expected.to redirect_to root_path }
|
|
|
|
it { expect(controller.current_gestionnaire).to eq(gestionnaire) }
|
|
|
|
it { expect(controller).to have_received(:trust_device) }
|
|
|
|
end
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
context 'when the token is invalid' do
|
|
|
|
let(:jeton) { 'invalid_token' }
|
2018-10-03 11:11:02 +02:00
|
|
|
|
2019-02-01 17:17:10 +01:00
|
|
|
it { is_expected.to redirect_to link_sent_path(email: gestionnaire.email) }
|
|
|
|
it { expect(controller.current_gestionnaire).to eq(gestionnaire) }
|
|
|
|
it { expect(controller).not_to have_received(:trust_device) }
|
|
|
|
it { expect(controller).to have_received(:send_login_token_or_bufferize) }
|
|
|
|
end
|
2018-10-03 11:11:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-30 18:24:29 +01:00
|
|
|
|
|
|
|
describe '#trust_device and #trusted_device?' do
|
|
|
|
subject { controller.trusted_device? }
|
|
|
|
|
|
|
|
context 'when the trusted cookie is not present' do
|
|
|
|
it { is_expected.to be false }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cookie is outdated' do
|
|
|
|
before do
|
2019-02-04 11:57:50 +01:00
|
|
|
emission_date = Time.zone.now - TrustedDeviceConcern::TRUSTED_DEVICE_PERIOD - 1.minute
|
|
|
|
controller.trust_device(emission_date)
|
2018-10-30 18:24:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be false }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cookie is ok' do
|
2019-02-01 17:17:10 +01:00
|
|
|
before { controller.trust_device(Time.zone.now) }
|
2018-10-30 18:24:29 +01:00
|
|
|
|
|
|
|
it { is_expected.to be true }
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 11:12:45 +02:00
|
|
|
end
|