From 461f79c2b938b57ad280037a8e8f9a73f3fb061f Mon Sep 17 00:00:00 2001
From: Aboobacker MK <akarakath@gitlab.com>
Date: Mon, 4 Dec 2023 07:02:56 +0000
Subject: [PATCH] Redirect audit_log to /-/user_settings/authentication_log

Move /-/profile/audit_log to /-/user_settings/authentication_log and
redirect end_point to new endpoint till next major release (17.0)

Changelog: changed
---
 .../rspec/factory_bot/avoid_create.yml        |  2 +-
 app/controllers/profiles_controller.rb        | 11 -------
 .../user_settings/user_settings_controller.rb | 16 ++++++++++
 app/models/authentication_event.rb            |  2 ++
 .../user_settings/_event_table.haml}          |  0
 .../user_settings/authentication_log.haml}    |  0
 config/routes.rb                              |  1 +
 config/routes/profile.rb                      |  2 +-
 config/routes/user_settings.rb                |  7 +++++
 .../menus/authentication_log_menu.rb          |  4 +--
 spec/controllers/profiles_controller_spec.rb  | 24 --------------
 .../user_sees_active_nav_items_spec.rb        |  2 +-
 .../menus/authentication_log_menu_spec.rb     |  4 +--
 spec/requests/legacy_routes_spec.rb           | 16 ++++++++++
 spec/requests/user_settings_spec.rb           | 31 +++++++++++++++++++
 spec/routing/routing_spec.rb                  | 12 ++++---
 .../authentication_log.html.haml_spec.rb}     |  2 +-
 17 files changed, 89 insertions(+), 47 deletions(-)
 create mode 100644 app/controllers/user_settings/user_settings_controller.rb
 rename app/views/{profiles/_event_table.html.haml => user_settings/user_settings/_event_table.haml} (100%)
 rename app/views/{profiles/audit_log.html.haml => user_settings/user_settings/authentication_log.haml} (100%)
 create mode 100644 config/routes/user_settings.rb
 create mode 100644 spec/requests/legacy_routes_spec.rb
 create mode 100644 spec/requests/user_settings_spec.rb
 rename spec/views/{profiles/audit_log.html.haml_spec.rb => user_settings/user_settings/authentication_log.html.haml_spec.rb} (85%)

diff --git a/.rubocop_todo/rspec/factory_bot/avoid_create.yml b/.rubocop_todo/rspec/factory_bot/avoid_create.yml
index d36df0b01de8..88d2255f6811 100644
--- a/.rubocop_todo/rspec/factory_bot/avoid_create.yml
+++ b/.rubocop_todo/rspec/factory_bot/avoid_create.yml
@@ -583,7 +583,7 @@ RSpec/FactoryBot/AvoidCreate:
     - 'spec/views/notify/import_issues_csv_email.html.haml_spec.rb'
     - 'spec/views/notify/pipeline_failed_email.text.erb_spec.rb'
     - 'spec/views/notify/push_to_merge_request_email.text.haml_spec.rb'
-    - 'spec/views/profiles/audit_log.html.haml_spec.rb'
+    - 'spec/views/user_settings/user_settings/authentication_log.html.haml_spec.rb'
     - 'spec/views/profiles/keys/_key.html.haml_spec.rb'
     - 'spec/views/profiles/keys/_key_details.html.haml_spec.rb'
     - 'spec/views/profiles/notifications/show.html.haml_spec.rb'
diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb
index cb29f0f35392..e427f6071862 100644
--- a/app/controllers/profiles_controller.rb
+++ b/app/controllers/profiles_controller.rb
@@ -14,7 +14,6 @@ class ProfilesController < Profiles::ApplicationController
   feature_category :user_profile, [:show, :update, :reset_incoming_email_token, :reset_feed_token,
                             :reset_static_object_token, :update_username]
 
-  feature_category :system_access, [:audit_log]
   urgency :low, [:show, :update]
 
   def show
@@ -65,16 +64,6 @@ def reset_static_object_token
       notice: s_('Profiles|Static object token was successfully reset')
   end
 
-  # rubocop: disable CodeReuse/ActiveRecord
-  def audit_log
-    @events = AuthenticationEvent.where(user: current_user)
-      .order("created_at DESC")
-      .page(params[:page])
-
-    Gitlab::Tracking.event(self.class.name, 'search_audit_event', user: current_user)
-  end
-  # rubocop: enable CodeReuse/ActiveRecord
-
   def update_username
     result = Users::UpdateService.new(current_user, user: @user, username: username_param).execute
 
diff --git a/app/controllers/user_settings/user_settings_controller.rb b/app/controllers/user_settings/user_settings_controller.rb
new file mode 100644
index 000000000000..3d69a9c2fd6a
--- /dev/null
+++ b/app/controllers/user_settings/user_settings_controller.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+module UserSettings
+  class UserSettingsController < ApplicationController
+    layout 'profile'
+    feature_category :system_access
+
+    def authentication_log
+      @events = AuthenticationEvent.for_user(current_user)
+          .order_by_created_at_desc
+          .page(params[:page])
+
+      Gitlab::Tracking.event(self.class.name, 'search_audit_event', user: current_user)
+    end
+  end
+end
diff --git a/app/models/authentication_event.rb b/app/models/authentication_event.rb
index e9fe49f980d6..e3a5922efd19 100644
--- a/app/models/authentication_event.rb
+++ b/app/models/authentication_event.rb
@@ -21,6 +21,8 @@ class AuthenticationEvent < MainClusterwide::ApplicationRecord
 
   scope :for_provider, ->(provider) { where(provider: provider) }
   scope :ldap, -> { where('provider LIKE ?', 'ldap%') }
+  scope :for_user, ->(user) { where(user: user) }
+  scope :order_by_created_at_desc, -> { reorder(created_at: :desc) }
 
   def self.providers
     STATIC_PROVIDERS | Devise.omniauth_providers.map(&:to_s)
diff --git a/app/views/profiles/_event_table.html.haml b/app/views/user_settings/user_settings/_event_table.haml
similarity index 100%
rename from app/views/profiles/_event_table.html.haml
rename to app/views/user_settings/user_settings/_event_table.haml
diff --git a/app/views/profiles/audit_log.html.haml b/app/views/user_settings/user_settings/authentication_log.haml
similarity index 100%
rename from app/views/profiles/audit_log.html.haml
rename to app/views/user_settings/user_settings/authentication_log.haml
diff --git a/config/routes.rb b/config/routes.rb
index cd0bd686cb28..babc1ffa3d3e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -210,6 +210,7 @@
 
       draw :snippets
       draw :profile
+      draw :user_settings
 
       post '/mailgun/webhooks' => 'mailgun/webhooks#process_webhook'
 
diff --git a/config/routes/profile.rb b/config/routes/profile.rb
index 73c8d63b8ec1..b4f00fa4ad82 100644
--- a/config/routes/profile.rb
+++ b/config/routes/profile.rb
@@ -5,7 +5,7 @@
 
 resource :profile, only: [:show, :update] do
   member do
-    get :audit_log
+    get :audit_log, to: redirect('-/user_settings/authentication_log')
     get :applications, to: 'oauth/applications#index'
 
     put :reset_incoming_email_token
diff --git a/config/routes/user_settings.rb b/config/routes/user_settings.rb
new file mode 100644
index 000000000000..478d807c8b58
--- /dev/null
+++ b/config/routes/user_settings.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+scope module: 'user_settings' do
+  namespace :user_settings do
+    get :authentication_log
+  end
+end
diff --git a/lib/sidebars/user_settings/menus/authentication_log_menu.rb b/lib/sidebars/user_settings/menus/authentication_log_menu.rb
index c5a27acf1fd8..fc4b0bba9c38 100644
--- a/lib/sidebars/user_settings/menus/authentication_log_menu.rb
+++ b/lib/sidebars/user_settings/menus/authentication_log_menu.rb
@@ -8,7 +8,7 @@ class AuthenticationLogMenu < ::Sidebars::Menu
 
         override :link
         def link
-          audit_log_profile_path
+          user_settings_authentication_log_path
         end
 
         override :title
@@ -23,7 +23,7 @@ def sprite_icon
 
         override :active_routes
         def active_routes
-          { path: 'profiles#audit_log' }
+          { path: 'user_settings#authentication_log' }
         end
       end
     end
diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb
index 4f350ddf1ef4..26144edb6704 100644
--- a/spec/controllers/profiles_controller_spec.rb
+++ b/spec/controllers/profiles_controller_spec.rb
@@ -140,30 +140,6 @@
     end
   end
 
-  describe 'GET audit_log' do
-    let(:auth_event) { create(:authentication_event, user: user) }
-
-    it 'tracks search event', :snowplow do
-      sign_in(user)
-
-      get :audit_log
-
-      expect_snowplow_event(
-        category: 'ProfilesController',
-        action: 'search_audit_event',
-        user: user
-      )
-    end
-
-    it 'loads page correctly' do
-      sign_in(user)
-
-      get :audit_log
-
-      expect(response).to have_gitlab_http_status(:success)
-    end
-  end
-
   describe 'PUT update_username' do
     let(:namespace) { user.namespace }
     let(:gitlab_shell) { Gitlab::Shell.new }
diff --git a/spec/features/user_sees_active_nav_items_spec.rb b/spec/features/user_sees_active_nav_items_spec.rb
index 966b84913743..1e6b2b8f189c 100644
--- a/spec/features/user_sees_active_nav_items_spec.rb
+++ b/spec/features/user_sees_active_nav_items_spec.rb
@@ -32,7 +32,7 @@
 
     context 'when visiting authentication logs' do
       before do
-        visit audit_log_profile_path
+        visit user_settings_authentication_log_path
       end
 
       it 'renders the side navigation with the correct submenu set as active' do
diff --git a/spec/lib/sidebars/user_settings/menus/authentication_log_menu_spec.rb b/spec/lib/sidebars/user_settings/menus/authentication_log_menu_spec.rb
index 33be5050c377..5a154d7dafb2 100644
--- a/spec/lib/sidebars/user_settings/menus/authentication_log_menu_spec.rb
+++ b/spec/lib/sidebars/user_settings/menus/authentication_log_menu_spec.rb
@@ -4,10 +4,10 @@
 
 RSpec.describe Sidebars::UserSettings::Menus::AuthenticationLogMenu, feature_category: :navigation do
   it_behaves_like 'User settings menu',
-    link: '/-/profile/audit_log',
+    link: '/-/user_settings/authentication_log',
     title: _('Authentication Log'),
     icon: 'log',
-    active_routes: { path: 'profiles#audit_log' }
+    active_routes: { path: 'user_settings#authentication_log' }
 
   it_behaves_like 'User settings menu #render? method'
 end
diff --git a/spec/requests/legacy_routes_spec.rb b/spec/requests/legacy_routes_spec.rb
new file mode 100644
index 000000000000..65af3c78fd72
--- /dev/null
+++ b/spec/requests/legacy_routes_spec.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe "Legacy routes", type: :request, feature_category: :system_access do
+  let(:user) { create(:user) }
+
+  before do
+    login_as(user)
+  end
+
+  it "/-/profile/audit_log" do
+    get "/-/profile/audit_log"
+    expect(response).to redirect_to('/-/user_settings/authentication_log')
+  end
+end
diff --git a/spec/requests/user_settings_spec.rb b/spec/requests/user_settings_spec.rb
new file mode 100644
index 000000000000..8298edc9ad00
--- /dev/null
+++ b/spec/requests/user_settings_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe "UserSettings", type: :request, feature_category: :system_access do
+  let(:user) { create(:user) }
+
+  describe 'GET authentication_log' do
+    let(:auth_event) { create(:authentication_event, user: user) }
+
+    it 'tracks search event', :snowplow do
+      sign_in(user)
+
+      get '/-/user_settings/authentication_log'
+
+      expect_snowplow_event(
+        category: 'UserSettings::UserSettingsController',
+        action: 'search_audit_event',
+        user: user
+      )
+    end
+
+    it 'loads page correctly' do
+      sign_in(user)
+
+      get '/-/user_settings/authentication_log'
+
+      expect(response).to have_gitlab_http_status(:success)
+    end
+  end
+end
diff --git a/spec/routing/routing_spec.rb b/spec/routing/routing_spec.rb
index 7c4f040266ed..1bd138ea1488 100644
--- a/spec/routing/routing_spec.rb
+++ b/spec/routing/routing_spec.rb
@@ -131,10 +131,6 @@
     expect(get("/-/profile/account")).to route_to('profiles/accounts#show')
   end
 
-  it "to #audit_log" do
-    expect(get("/-/profile/audit_log")).to route_to('profiles#audit_log')
-  end
-
   it "to #reset_feed_token" do
     expect(put("/-/profile/reset_feed_token")).to route_to('profiles#reset_feed_token')
   end
@@ -397,3 +393,11 @@
     expect(get('/-/jwks')).to route_to('jwks#index')
   end
 end
+
+# user_settings_authentication_log GET  /-/user_settings/authentication_log(.:format) system_access/user_settings#authentication_log
+
+RSpec.describe UserSettings::UserSettingsController, 'routing', feature_category: :system_access do
+  it 'to #authentication_log' do
+    expect(get('/-/user_settings/authentication_log')).to route_to('user_settings/user_settings#authentication_log')
+  end
+end
diff --git a/spec/views/profiles/audit_log.html.haml_spec.rb b/spec/views/user_settings/user_settings/authentication_log.html.haml_spec.rb
similarity index 85%
rename from spec/views/profiles/audit_log.html.haml_spec.rb
rename to spec/views/user_settings/user_settings/authentication_log.html.haml_spec.rb
index d5f6a2d64e78..4188bdc587f4 100644
--- a/spec/views/profiles/audit_log.html.haml_spec.rb
+++ b/spec/views/user_settings/user_settings/authentication_log.html.haml_spec.rb
@@ -2,7 +2,7 @@
 
 require 'spec_helper'
 
-RSpec.describe 'profiles/audit_log' do
+RSpec.describe 'user_settings/user_settings/authentication_log', feature_category: :system_access do
   let(:user) { create(:user) }
 
   before do
-- 
GitLab