Commit 92005e56 authored by Sean McGivern's avatar Sean McGivern

Add context to SessionsController

This doesn't inherit from ApplicationController and so doesn't get a
context by default.
parent f9ed8e1d
......@@ -26,6 +26,8 @@ class SessionsController < Devise::SessionsController
before_action :load_recaptcha
before_action :frontend_tracking_data, only: [:new]
around_action :set_current_context
after_action :log_failed_login, if: :action_new_and_failed_login?
helper_method :captcha_enabled?, :captcha_on_login_required?
......@@ -305,6 +307,13 @@ class SessionsController < Devise::SessionsController
# We want tracking data pushed to the frontend when the user is _in_ the control group
frontend_experimentation_tracking_data(:signup_flow, 'start') unless experiment_enabled?(:signup_flow)
end
def set_current_context(&block)
Gitlab::ApplicationContext.with_context(
user: -> { current_user },
caller_id: "#{self.class.name}##{action_name}",
&block)
end
end
SessionsController.prepend_if_ee('EE::SessionsController')
......@@ -495,4 +495,45 @@ describe SessionsController do
expect(session[:failed_login_attempts]).to eq(1)
end
end
describe '#set_current_context' do
before do
set_devise_mapping(context: @request)
end
context 'when signed in' do
let_it_be(:user) { create(:user) }
before do
sign_in(user)
end
it 'sets the username and caller_id in the context' do
expect(controller).to receive(:destroy).and_wrap_original do |m, *args|
expect(Labkit::Context.current.to_h)
.to include('meta.user' => user.username,
'meta.caller_id' => 'SessionsController#destroy')
m.call(*args)
end
delete :destroy
end
end
context 'when not signed in' do
it 'sets the caller_id in the context' do
expect(controller).to receive(:new).and_wrap_original do |m, *args|
expect(Labkit::Context.current.to_h)
.to include('meta.caller_id' => 'SessionsController#new')
expect(Labkit::Context.current.to_h)
.not_to include('meta.user')
m.call(*args)
end
get :new
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment