From 55802242ce65dcea36b74fffcb3474e8254192f7 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 11 Jan 2024 05:36:27 -0500 Subject: [PATCH] Refactor login activity partial (remove inline ruby) (#28687) --- app/helpers/admin/settings_helper.rb | 56 +++++++++++++++++++ .../_login_activity.html.haml | 10 +--- .../login_activities_controller_spec.rb | 5 ++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/app/helpers/admin/settings_helper.rb b/app/helpers/admin/settings_helper.rb index 552a3ee5a86..6937331e1a6 100644 --- a/app/helpers/admin/settings_helper.rb +++ b/app/helpers/admin/settings_helper.rb @@ -4,4 +4,60 @@ module Admin::SettingsHelper def captcha_available? ENV['HCAPTCHA_SECRET_KEY'].present? && ENV['HCAPTCHA_SITE_KEY'].present? end + + def login_activity_title(activity) + t( + "login_activities.#{login_activity_key(activity)}", + method: login_activity_method(activity), + ip: login_activity_ip(activity), + browser: login_activity_browser(activity) + ) + end + + private + + def login_activity_key(activity) + activity.success? ? 'successful_sign_in_html' : 'failed_sign_in_html' + end + + def login_activity_method(activity) + content_tag( + :span, + login_activity_method_string(activity), + class: 'target' + ) + end + + def login_activity_ip(activity) + content_tag( + :span, + activity.ip, + class: 'target' + ) + end + + def login_activity_browser(activity) + content_tag( + :span, + login_activity_browser_description(activity), + class: 'target', + title: activity.user_agent + ) + end + + def login_activity_method_string(activity) + if activity.omniauth? + t("auth.providers.#{activity.provider}") + else + t("login_activities.authentication_methods.#{activity.authentication_method}") + end + end + + def login_activity_browser_description(activity) + t( + 'sessions.description', + browser: t(activity.browser, scope: 'sessions.browsers', default: activity.browser.to_s), + platform: t(activity.platform, scope: 'sessions.platforms', default: activity.platform.to_s) + ) + end end diff --git a/app/views/settings/login_activities/_login_activity.html.haml b/app/views/settings/login_activities/_login_activity.html.haml index 2e001cdceff..36314926a3d 100644 --- a/app/views/settings/login_activities/_login_activity.html.haml +++ b/app/views/settings/login_activities/_login_activity.html.haml @@ -1,8 +1,3 @@ -:ruby - method_str = content_tag(:span, login_activity.omniauth? ? t(login_activity.provider, scope: 'auth.providers') : t(login_activity.authentication_method, scope: 'login_activities.authentication_methods'), class: 'target') - ip_str = content_tag(:span, login_activity.ip, class: 'target') - browser_str = content_tag(:span, t('sessions.description', browser: t("sessions.browsers.#{login_activity.browser}", default: login_activity.browser.to_s), platform: t("sessions.platforms.#{login_activity.platform}", default: login_activity.platform.to_s)), class: 'target', title: login_activity.user_agent) - .log-entry .log-entry__header .log-entry__avatar @@ -10,9 +5,6 @@ = fa_icon login_activity.success? ? 'check' : 'times' .log-entry__content .log-entry__title - - if login_activity.success? - = t('login_activities.successful_sign_in_html', method: method_str, ip: ip_str, browser: browser_str) - - else - = t('login_activities.failed_sign_in_html', method: method_str, ip: ip_str, browser: browser_str) + = login_activity_title(login_activity) .log-entry__timestamp %time.formatted{ datetime: login_activity.created_at.iso8601 }= l(login_activity.created_at) diff --git a/spec/controllers/settings/login_activities_controller_spec.rb b/spec/controllers/settings/login_activities_controller_spec.rb index 4f266e03ddf..294bf85c97a 100644 --- a/spec/controllers/settings/login_activities_controller_spec.rb +++ b/spec/controllers/settings/login_activities_controller_spec.rb @@ -6,6 +6,7 @@ describe Settings::LoginActivitiesController do render_views let!(:user) { Fabricate(:user) } + let!(:login_activity) { Fabricate :login_activity, user: user } before do sign_in user, scope: :user @@ -19,6 +20,10 @@ describe Settings::LoginActivitiesController do it 'returns http success with private cache control headers', :aggregate_failures do expect(response).to have_http_status(200) expect(response.headers['Cache-Control']).to include('private, no-store') + expect(response.body) + .to include(login_activity.user_agent) + .and include(login_activity.authentication_method) + .and include(login_activity.ip.to_s) end end end