From 12c12953d6ee156a742494ce5e9e39f8d8515dab Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Wed, 28 Jun 2017 07:08:25 +0200 Subject: [PATCH] [FIX #433] Add Raven Metadata on the current user/gestionnaire/admin ... --- app/controllers/application_controller.rb | 22 +++++++ .../application_controller_spec.rb | 59 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 spec/controllers/application_controller_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index afcbf7916..191d2bcf8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,6 +4,7 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :check_browser before_action :load_navbar_left_pannel_partial_url + before_action :set_raven_context def default_url_options return {protocol: 'https'} if Rails.env.staging? || Rails.env.production? @@ -40,4 +41,25 @@ class ApplicationController < ActionController::Base redirect_to new_user_session_path end end + + private + + def set_raven_context + context = { ip_address: request.ip } + + logged_models = [ + current_user, + current_gestionnaire, + current_administrateur, + current_administration + ].compact + + context[:email] = logged_models.first&.email + context[:id] = logged_models.first&.id + + class_names = logged_models.map { |model| model.class.name } + context[:classes] = class_names.any? ? class_names.join(', ') : 'Guest' + + Raven.user_context(context) + end end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 000000000..9a05618a2 --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe ApplicationController, type: :controller do + describe 'before_action: set_raven_context' do + it 'is present' do + before_actions = ApplicationController + ._process_action_callbacks + .find_all{|process_action_callbacks| process_action_callbacks.kind == :before} + .map(&:filter) + + expect(before_actions).to include(:set_raven_context) + end + end + + describe 'set_raven_context' do + let(:current_user) { nil } + let(:current_gestionnaire) { nil } + let(:current_administrateur) { nil } + let(:current_administration) { nil } + + before do + expect(@controller).to receive(:current_user).and_return(current_user) + expect(@controller).to receive(:current_gestionnaire).and_return(current_gestionnaire) + expect(@controller).to receive(:current_administrateur).and_return(current_administrateur) + expect(@controller).to receive(:current_administration).and_return(current_administration) + allow(Raven).to receive(:user_context) + + @controller.send(:set_raven_context) + end + + context 'when no one is logged in' do + it do + expect(Raven).to have_received(:user_context) + .with({ ip_address: '0.0.0.0', email: nil, id: nil, classes: 'Guest' }) + end + end + + context 'when a user is logged in' do + let(:current_user) { create(:user) } + + it do + expect(Raven).to have_received(:user_context) + .with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, classes: 'User' }) + end + end + + context 'when someone is logged as a user, gestionnaire, administrateur and administration' do + let(:current_user) { create(:user) } + let(:current_gestionnaire) { create(:gestionnaire) } + let(:current_administrateur) { create(:administrateur) } + let(:current_administration) { create(:administration) } + + it do + expect(Raven).to have_received(:user_context) + .with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, classes: 'User, Gestionnaire, Administrateur, Administration' }) + end + end + end +end