Commit 924829f0 authored by Imre Farkas's avatar Imre Farkas

Merge branch 'chore/conditional-mocking-admin-mode-specs' into 'master'

Conditional mocking of admin mode in specs by directory

See merge request gitlab-org/gitlab!28420
parents a09380da 06e78f6d
---
title: Conditional mocking of admin mode in specs by directory
merge_request: 28420
author: Diego Louzán
type: other
...@@ -214,15 +214,62 @@ RSpec.configure do |config| ...@@ -214,15 +214,62 @@ RSpec.configure do |config|
# modifying a significant number of specs to test both states for admin # modifying a significant number of specs to test both states for admin
# mode enabled / disabled. # mode enabled / disabled.
# #
# See https://gitlab.com/gitlab-org/gitlab/issues/31511 # This will only be applied to specs below dirs in `admin_mode_mock_dirs`
# See gitlab/spec/support/helpers/admin_mode_helpers.rb
# #
# If it is required to have the real behaviour that an admin is signed in # See ongoing migration: https://gitlab.com/gitlab-org/gitlab/-/issues/31511
#
# Until the migration is finished, if it is required to have the real
# behaviour in any of the mocked dirs specs that an admin is signed in
# with normal user mode and needs to switch to admin mode, it is possible to # with normal user mode and needs to switch to admin mode, it is possible to
# mark such tests with the `do_not_mock_admin_mode` metadata tag, e.g: # mark such tests with the `do_not_mock_admin_mode` metadata tag, e.g:
# #
# context 'some test with normal user mode', :do_not_mock_admin_mode do ... end # context 'some test in mocked dir', :do_not_mock_admin_mode do ... end
unless example.metadata[:do_not_mock_admin_mode] admin_mode_mock_dirs = %w(
./ee/spec/controllers
./ee/spec/elastic_integration
./ee/spec/features
./ee/spec/finders
./ee/spec/lib
./ee/spec/models
./ee/spec/policies
./ee/spec/requests/admin
./ee/spec/serializers
./ee/spec/services
./ee/spec/support/protected_tags
./ee/spec/support/shared_examples
./spec/controllers
./spec/features
./spec/finders
./spec/frontend
./spec/helpers
./spec/lib
./spec/models
./spec/policies
./spec/requests
./spec/serializers
./spec/services
./spec/support/cycle_analytics_helpers
./spec/support/protected_tags
./spec/support/shared_examples
./spec/views
./spec/workers
)
if !example.metadata[:do_not_mock_admin_mode] && example.metadata[:file_path].start_with?(*admin_mode_mock_dirs)
allow_any_instance_of(Gitlab::Auth::CurrentUserMode).to receive(:admin_mode?) do |current_user_mode|
current_user_mode.send(:user)&.admin?
end
end
# Administrators have to re-authenticate in order to access administrative
# functionality when feature flag :user_mode_in_session is active. Any spec
# that requires administrative access can use the tag :enable_admin_mode
# to avoid the second auth step (provided the user is already an admin):
#
# context 'some test that requires admin mode', :enable_admin_mode do ... end
#
# See also spec/support/helpers/admin_mode_helpers.rb
if example.metadata[:enable_admin_mode]
allow_any_instance_of(Gitlab::Auth::CurrentUserMode).to receive(:admin_mode?) do |current_user_mode| allow_any_instance_of(Gitlab::Auth::CurrentUserMode).to receive(:admin_mode?) do |current_user_mode|
current_user_mode.send(:user)&.admin? current_user_mode.send(:user)&.admin?
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