Skip to content
代码片段 群组 项目
提交 460c393d 编辑于 作者: Alper Akgun's avatar Alper Akgun
浏览文件

Merge branch 'codyw-fix-dormantuser-cron' into 'master'

Fix Admin Mode bug in DeactivateDormantUsersWorker

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/136907



Merged-by: default avatarAlper Akgun <aakgun@gitlab.com>
Approved-by: default avatarJosianne Hyson <jhyson@gitlab.com>
Approved-by: default avatarSmriti Garg <sgarg@gitlab.com>
Reviewed-by: default avatarJosianne Hyson <jhyson@gitlab.com>
Reviewed-by: default avatarHuzaifa Iftikhar <hiftikhar@gitlab.com>
Co-authored-by: default avatarCody West <cwest@gitlab.com>
Co-authored-by: default avatarJosianne Hyson <jhyson@gitlab.com>
No related branches found
No related tags found
无相关合并请求
...@@ -18,8 +18,10 @@ def perform ...@@ -18,8 +18,10 @@ def perform
admin_bot = Users::Internal.admin_bot admin_bot = Users::Internal.admin_bot
return unless admin_bot return unless admin_bot
deactivate_users(User.dormant, admin_bot) Gitlab::Auth::CurrentUserMode.bypass_session!(admin_bot.id) do
deactivate_users(User.with_no_activity, admin_bot) deactivate_users(User.dormant, admin_bot)
deactivate_users(User.with_no_activity, admin_bot)
end
end end
private private
......
...@@ -10,34 +10,27 @@ ...@@ -10,34 +10,27 @@
let_it_be(:inactive) { create(:user, last_activity_on: nil, created_at: User::MINIMUM_DAYS_CREATED.days.ago.to_date) } let_it_be(:inactive) { create(:user, last_activity_on: nil, created_at: User::MINIMUM_DAYS_CREATED.days.ago.to_date) }
let_it_be(:inactive_recently_created) { create(:user, last_activity_on: nil, created_at: (User::MINIMUM_DAYS_CREATED - 1).days.ago.to_date) } let_it_be(:inactive_recently_created) { create(:user, last_activity_on: nil, created_at: (User::MINIMUM_DAYS_CREATED - 1).days.ago.to_date) }
let(:admin_bot) { create(:user, :admin_bot) }
let(:deactivation_service) { instance_spy(Users::DeactivateService) }
before do
allow(Users::DeactivateService).to receive(:new).and_return(deactivation_service)
end
subject(:worker) { described_class.new } subject(:worker) { described_class.new }
it 'does not run for SaaS', :saas do it 'does not run for SaaS', :saas do
# Now makes a call to current settings to determine period of dormancy
worker.perform worker.perform
expect(deactivation_service).not_to have_received(:execute) expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
end expect(deactivation_service).not_to have_received(:execute)
context 'when automatic deactivation of dormant users is enabled' do
before do
stub_application_setting(deactivate_dormant_users: true)
end end
end
it 'deactivates dormant users' do shared_examples 'deactivates dormant users' do
worker.perform specify do
expect { worker.perform }
expect(deactivation_service).to have_received(:execute).twice .to change { dormant.reload.state }
.to('deactivated')
.and change { inactive.reload.state }
.to('deactivated')
end end
end
shared_examples 'deactivates certain user types' do
where(:user_type, :expected_state) do where(:user_type, :expected_state) do
:human | 'deactivated' :human | 'deactivated'
:support_bot | 'active' :support_bot | 'active'
...@@ -52,33 +45,69 @@ ...@@ -52,33 +45,69 @@
end end
with_them do with_them do
it 'deactivates certain user types' do specify do
user = create(:user, user_type: user_type, state: :active, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date) user = create(:user, user_type: user_type, state: :active, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date)
worker.perform worker.perform
if expected_state == 'deactivated' expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
expect(deactivation_service).to have_received(:execute).with(user) if expected_state == 'deactivated'
else expect(deactivation_service).to receive(:execute).with(user).and_call_original
expect(deactivation_service).not_to have_received(:execute).with(user) else
expect(deactivation_service).not_to have_received(:execute).with(user)
end
end end
expect(user.reload.state).to eq expected_state
end end
end end
end
it 'does not deactivate non-active users' do shared_examples 'does not deactivate non-active users' do
specify do
human_user = create(:user, user_type: :human, state: :blocked, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date) human_user = create(:user, user_type: :human, state: :blocked, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date)
service_user = create(:user, user_type: :service_user, state: :blocked, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date) service_user = create(:user, user_type: :service_user, state: :blocked, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date)
worker.perform worker.perform
expect(deactivation_service).not_to have_received(:execute).with(human_user) expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
expect(deactivation_service).not_to have_received(:execute).with(service_user) expect(deactivation_service).not_to have_received(:execute).with(human_user)
expect(deactivation_service).not_to have_received(:execute).with(service_user)
end
end end
end
it 'does not deactivate recently created users' do shared_examples 'does not deactivate recently created users' do
specify do
worker.perform worker.perform
expect(deactivation_service).not_to have_received(:execute).with(inactive_recently_created) expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
expect(deactivation_service).not_to have_received(:execute).with(inactive_recently_created)
end
end
end
context 'when automatic deactivation of dormant users is enabled' do
before do
stub_application_setting(deactivate_dormant_users: true)
end
context 'when admin mode is not enabled', :do_not_mock_admin_mode_setting do
include_examples 'deactivates dormant users'
include_examples 'deactivates certain user types'
include_examples 'does not deactivate non-active users'
include_examples 'does not deactivate recently created users'
end
context 'when admin mode is enabled', :request_store do
before do
stub_application_setting(admin_mode: true)
end
include_examples 'deactivates dormant users'
include_examples 'deactivates certain user types'
include_examples 'does not deactivate non-active users'
include_examples 'does not deactivate recently created users'
end end
end end
...@@ -90,7 +119,9 @@ ...@@ -90,7 +119,9 @@
it 'does nothing' do it 'does nothing' do
worker.perform worker.perform
expect(deactivation_service).not_to have_received(:execute) expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
expect(deactivation_service).not_to have_received(:execute)
end
end end
end end
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册