2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-28 07:08:25 +02:00
|
|
|
describe ApplicationController, type: :controller do
|
2021-01-28 14:49:22 +01:00
|
|
|
describe 'before_action: set_sentry_user' do
|
2017-06-28 07:08:25 +02:00
|
|
|
it 'is present' do
|
|
|
|
before_actions = ApplicationController
|
|
|
|
._process_action_callbacks
|
2019-09-12 11:26:22 +02:00
|
|
|
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
|
2017-06-28 07:08:25 +02:00
|
|
|
.map(&:filter)
|
|
|
|
|
2021-01-28 14:49:22 +01:00
|
|
|
expect(before_actions).to include(:set_sentry_user)
|
2019-02-01 17:17:10 +01:00
|
|
|
expect(before_actions).to include(:redirect_if_untrusted)
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-28 14:49:22 +01:00
|
|
|
describe 'set_sentry_user and append_info_to_payload' do
|
2017-06-28 07:08:25 +02:00
|
|
|
let(:current_user) { nil }
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:current_instructeur) { nil }
|
2017-06-28 07:08:25 +02:00
|
|
|
let(:current_administrateur) { nil }
|
2020-11-05 15:09:11 +01:00
|
|
|
let(:current_super_admin) { nil }
|
2018-08-12 10:31:28 +02:00
|
|
|
let(:payload) { {} }
|
2017-06-28 07:08:25 +02:00
|
|
|
|
|
|
|
before do
|
2020-06-29 17:54:15 +02:00
|
|
|
allow(@controller).to receive(:media_type).and_return('text/plain')
|
2019-09-17 11:01:20 +02:00
|
|
|
allow(@controller).to receive(:current_user).and_return(current_user)
|
2019-08-06 11:02:54 +02:00
|
|
|
expect(@controller).to receive(:current_instructeur).and_return(current_instructeur)
|
2022-09-28 12:40:44 +02:00
|
|
|
expect(@controller).to receive(:current_administrateur).at_least(:once).and_return(current_administrateur)
|
2020-11-05 15:09:11 +01:00
|
|
|
expect(@controller).to receive(:current_super_admin).and_return(current_super_admin)
|
2021-01-28 14:49:22 +01:00
|
|
|
allow(Sentry).to receive(:set_user)
|
2017-06-28 07:08:25 +02:00
|
|
|
|
2021-01-28 14:49:22 +01:00
|
|
|
@controller.send(:set_sentry_user)
|
2018-08-12 10:31:28 +02:00
|
|
|
@controller.send(:append_info_to_payload, payload)
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no one is logged in' do
|
|
|
|
it do
|
2021-01-28 14:49:22 +01:00
|
|
|
expect(Sentry).to have_received(:set_user)
|
2019-04-04 14:21:18 +02:00
|
|
|
.with({ id: 'Guest' })
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|
2018-01-17 14:40:31 +01:00
|
|
|
|
2018-08-12 10:31:28 +02:00
|
|
|
it do
|
|
|
|
[:db_runtime, :view_runtime, :variant, :rendered_format].each do |key|
|
|
|
|
payload.delete(key)
|
|
|
|
end
|
2023-11-08 12:19:03 +01:00
|
|
|
expect(payload[:to_log].compact).to eq({
|
2018-08-12 10:31:28 +02:00
|
|
|
user_agent: 'Rails Testing',
|
2018-08-13 16:40:16 +02:00
|
|
|
user_roles: 'Guest'
|
2018-08-12 10:31:28 +02:00
|
|
|
})
|
|
|
|
end
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is logged in' do
|
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
|
|
|
|
it do
|
2021-01-28 14:49:22 +01:00
|
|
|
expect(Sentry).to have_received(:set_user)
|
2019-04-04 14:21:18 +02:00
|
|
|
.with({ id: "User##{current_user.id}" })
|
2018-01-17 14:40:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
2018-08-12 10:31:28 +02:00
|
|
|
[:db_runtime, :view_runtime, :variant, :rendered_format].each do |key|
|
|
|
|
payload.delete(key)
|
|
|
|
end
|
2023-11-08 12:19:03 +01:00
|
|
|
expect(payload[:to_log].compact).to eq({
|
2018-01-17 14:40:31 +01:00
|
|
|
user_agent: 'Rails Testing',
|
2018-08-12 10:31:28 +02:00
|
|
|
user_id: current_user.id,
|
2018-08-13 16:40:16 +02:00
|
|
|
user_roles: 'User'
|
2018-01-17 14:40:31 +01:00
|
|
|
})
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 15:09:11 +01:00
|
|
|
context 'when someone is logged as a user, instructeur, administrateur and super_admin' do
|
2017-06-28 07:08:25 +02:00
|
|
|
let(:current_user) { create(:user) }
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:current_instructeur) { create(:instructeur) }
|
2024-06-01 22:50:48 +02:00
|
|
|
let(:current_administrateur) { administrateurs(:default_admin) }
|
2020-11-05 15:09:11 +01:00
|
|
|
let(:current_super_admin) { create(:super_admin) }
|
2017-06-28 07:08:25 +02:00
|
|
|
|
|
|
|
it do
|
2021-01-28 14:49:22 +01:00
|
|
|
expect(Sentry).to have_received(:set_user)
|
2019-04-04 14:21:18 +02:00
|
|
|
.with({ id: "User##{current_user.id}" })
|
2018-01-17 14:40:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
2018-08-12 10:31:28 +02:00
|
|
|
[:db_runtime, :view_runtime, :variant, :rendered_format].each do |key|
|
|
|
|
payload.delete(key)
|
|
|
|
end
|
2023-11-08 12:19:03 +01:00
|
|
|
expect(payload[:to_log].compact).to eq({
|
2018-01-17 14:40:31 +01:00
|
|
|
user_agent: 'Rails Testing',
|
2018-08-12 10:31:28 +02:00
|
|
|
user_id: current_user.id,
|
2020-11-05 15:09:11 +01:00
|
|
|
user_roles: 'User, Instructeur, Administrateur, SuperAdmin'
|
2018-01-17 14:40:31 +01:00
|
|
|
})
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-26 10:52:41 +02:00
|
|
|
|
|
|
|
describe 'reject before action' do
|
|
|
|
let(:path_info) { '/one_path' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(@controller).to receive(:redirect_to)
|
|
|
|
allow(@controller).to receive(:sign_out)
|
|
|
|
allow(@controller).to receive(:render)
|
|
|
|
@request.path_info = path_info
|
|
|
|
end
|
|
|
|
|
2020-11-05 15:09:11 +01:00
|
|
|
context 'when no super_admin is logged in' do
|
2018-04-26 10:52:41 +02:00
|
|
|
before { @controller.send(:reject) }
|
|
|
|
|
|
|
|
it { expect(@controller).to have_received(:sign_out).with(:user) }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(@controller).to have_received(:sign_out).with(:instructeur) }
|
2018-04-26 10:52:41 +02:00
|
|
|
it { expect(@controller).to have_received(:sign_out).with(:administrateur) }
|
|
|
|
it { expect(flash[:alert]).to eq(ApplicationController::MAINTENANCE_MESSAGE) }
|
|
|
|
it { expect(@controller).to have_received(:redirect_to).with(root_path) }
|
|
|
|
|
|
|
|
context 'when the path is safe' do
|
2020-11-05 15:09:11 +01:00
|
|
|
['/', '/manager', '/super_admins'].each do |path|
|
2018-04-26 10:52:41 +02:00
|
|
|
let(:path_info) { path }
|
|
|
|
|
|
|
|
it { expect(@controller).not_to have_received(:sign_out) }
|
|
|
|
it { expect(@controller).not_to have_received(:redirect_to) }
|
|
|
|
it { expect(flash.alert).to eq(ApplicationController::MAINTENANCE_MESSAGE) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the path is api related' do
|
|
|
|
let(:path_info) { '/api/some-stuff' }
|
|
|
|
let(:json_error) { { error: ApplicationController::MAINTENANCE_MESSAGE }.to_json }
|
|
|
|
it { expect(@controller).not_to have_received(:sign_out) }
|
|
|
|
it { expect(@controller).not_to have_received(:redirect_to) }
|
|
|
|
it { expect(flash.alert).to be_nil }
|
|
|
|
it { expect(@controller).to have_received(:render).with({ json: json_error, status: :service_unavailable }) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 15:09:11 +01:00
|
|
|
context 'when a super_admin is logged in' do
|
|
|
|
let(:current_super_admin) { create(:super_admin) }
|
2018-04-26 10:52:41 +02:00
|
|
|
|
|
|
|
before do
|
2020-11-05 15:09:11 +01:00
|
|
|
sign_in(current_super_admin)
|
2018-04-26 10:52:41 +02:00
|
|
|
@controller.send(:reject)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(@controller).not_to have_received(:sign_out) }
|
|
|
|
it { expect(@controller).not_to have_received(:redirect_to) }
|
|
|
|
it { expect(flash[:alert]).to eq(ApplicationController::MAINTENANCE_MESSAGE) }
|
|
|
|
end
|
|
|
|
end
|
2019-02-01 17:17:10 +01:00
|
|
|
|
|
|
|
describe '#redirect_if_unstrusted' do
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:current_instructeur) { create(:instructeur) }
|
2019-02-01 17:17:10 +01:00
|
|
|
|
|
|
|
before do
|
2019-08-06 11:02:54 +02:00
|
|
|
allow(@controller).to receive(:current_instructeur).and_return(current_instructeur)
|
2019-02-01 17:17:10 +01:00
|
|
|
allow(@controller).to receive(:redirect_to)
|
|
|
|
allow(@controller).to receive(:trusted_device?).and_return(trusted_device)
|
2019-08-06 11:02:54 +02:00
|
|
|
allow(@controller).to receive(:instructeur_signed_in?).and_return(instructeur_signed_in)
|
2019-02-01 17:17:10 +01:00
|
|
|
allow(@controller).to receive(:sensitive_path).and_return(sensitive_path)
|
|
|
|
allow(@controller).to receive(:send_login_token_or_bufferize)
|
2020-03-18 16:29:48 +01:00
|
|
|
allow(@controller).to receive(:get_stored_location_for).and_return(nil)
|
2019-02-06 20:51:04 +01:00
|
|
|
allow(@controller).to receive(:store_location_for)
|
2019-04-03 14:27:28 +02:00
|
|
|
allow(IPService).to receive(:ip_trusted?).and_return(ip_trusted)
|
2019-02-01 17:17:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { @controller.send(:redirect_if_untrusted) }
|
|
|
|
|
|
|
|
context 'when the path is sensitive' do
|
|
|
|
let(:sensitive_path) { true }
|
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
before do
|
2021-11-25 15:37:40 +01:00
|
|
|
current_instructeur.update!(bypass_email_login_token: false)
|
2019-07-31 15:04:49 +02:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when the instructeur is signed_in' do
|
|
|
|
let(:instructeur_signed_in) { true }
|
2019-02-01 17:17:10 +01:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
context 'when the ip is not trusted' do
|
|
|
|
let(:ip_trusted) { false }
|
2019-02-01 17:17:10 +01:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
context 'when the device is trusted' do
|
|
|
|
let(:trusted_device) { true }
|
2019-02-01 17:17:10 +01:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
before { subject }
|
2019-04-03 14:27:28 +02:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
it { expect(@controller).not_to have_received(:redirect_to) }
|
2019-03-06 15:21:10 +01:00
|
|
|
end
|
2019-02-01 17:17:10 +01:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
context 'when the device is not trusted' do
|
|
|
|
let(:trusted_device) { false }
|
2019-04-03 14:27:28 +02:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
before { subject }
|
2019-04-03 14:27:28 +02:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
it { expect(@controller).to have_received(:redirect_to) }
|
|
|
|
it { expect(@controller).to have_received(:send_login_token_or_bufferize) }
|
|
|
|
it { expect(@controller).to have_received(:store_location_for) }
|
2019-04-03 14:27:28 +02:00
|
|
|
end
|
2019-07-31 15:04:49 +02:00
|
|
|
end
|
2019-04-03 14:27:28 +02:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
context 'when the ip is trusted' do
|
|
|
|
let(:ip_trusted) { true }
|
2019-04-03 14:27:28 +02:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
context 'when the device is not trusted' do
|
|
|
|
let(:trusted_device) { false }
|
2019-02-01 17:17:10 +01:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
before { subject }
|
2019-02-01 17:17:10 +01:00
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
it { expect(@controller).not_to have_received(:redirect_to) }
|
2019-02-01 17:17:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|