diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /spec/services/users | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) | |
download | gitlab-ce-b76ae638462ab0f673e5915986070518dd3f9ad3.tar.gz |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'spec/services/users')
-rw-r--r-- | spec/services/users/activity_service_spec.rb | 9 | ||||
-rw-r--r-- | spec/services/users/ban_service_spec.rb | 65 | ||||
-rw-r--r-- | spec/services/users/banned_user_base_service_spec.rb | 14 | ||||
-rw-r--r-- | spec/services/users/set_status_service_spec.rb | 40 | ||||
-rw-r--r-- | spec/services/users/unban_service_spec.rb | 75 |
5 files changed, 166 insertions, 37 deletions
diff --git a/spec/services/users/activity_service_spec.rb b/spec/services/users/activity_service_spec.rb index cfafa9eff45..6c1df5c745f 100644 --- a/spec/services/users/activity_service_spec.rb +++ b/spec/services/users/activity_service_spec.rb @@ -85,19 +85,14 @@ RSpec.describe Users::ActivityService do end end - context 'with DB Load Balancing', :request_store, :redis, :clean_gitlab_redis_shared_state do - include_context 'clear DB Load Balancing configuration' - + context 'with DB Load Balancing' do let(:user) { create(:user, last_activity_on: last_activity_on) } context 'when last activity is in the past' do let(:user) { create(:user, last_activity_on: Date.today - 1.week) } - context 'database load balancing is configured' do + context 'database load balancing is configured', :db_load_balancing do before do - # Do not pollute AR for other tests, but rather simulate effect of configure_proxy. - allow(ActiveRecord::Base.singleton_class).to receive(:prepend) - ::Gitlab::Database::LoadBalancing.configure_proxy allow(ActiveRecord::Base).to receive(:connection).and_return(::Gitlab::Database::LoadBalancing.proxy) end diff --git a/spec/services/users/ban_service_spec.rb b/spec/services/users/ban_service_spec.rb index 0e6ac615da5..6f49ee08782 100644 --- a/spec/services/users/ban_service_spec.rb +++ b/spec/services/users/ban_service_spec.rb @@ -3,47 +3,68 @@ require 'spec_helper' RSpec.describe Users::BanService do - let(:current_user) { create(:admin) } + let(:user) { create(:user) } - subject(:service) { described_class.new(current_user) } + let_it_be(:current_user) { create(:admin) } - describe '#execute' do - subject(:operation) { service.execute(user) } + shared_examples 'does not modify the BannedUser record or user state' do + it 'does not modify the BannedUser record or user state' do + expect { ban_user }.not_to change { Users::BannedUser.count } + expect { ban_user }.not_to change { user.state } + end + end - context 'when successful' do - let(:user) { create(:user) } + context 'ban', :aggregate_failures do + subject(:ban_user) { described_class.new(current_user).execute(user) } - it { is_expected.to eq(status: :success) } + context 'when successful', :enable_admin_mode do + it 'returns success status' do + response = ban_user - it "bans the user" do - expect { operation }.to change { user.state }.to('banned') + expect(response[:status]).to eq(:success) end - it "blocks the user" do - expect { operation }.to change { user.blocked? }.from(false).to(true) + it 'bans the user' do + expect { ban_user }.to change { user.state }.from('active').to('banned') end - it 'logs ban in application logs' do - allow(Gitlab::AppLogger).to receive(:info) + it 'creates a BannedUser' do + expect { ban_user }.to change { Users::BannedUser.count }.by(1) + expect(Users::BannedUser.last.user_id).to eq(user.id) + end - operation + it 'logs ban in application logs' do + expect(Gitlab::AppLogger).to receive(:info).with(message: "User ban", user: "#{user.username}", email: "#{user.email}", ban_by: "#{current_user.username}", ip_address: "#{current_user.current_sign_in_ip}") - expect(Gitlab::AppLogger).to have_received(:info).with(message: "User banned", user: "#{user.username}", email: "#{user.email}", banned_by: "#{current_user.username}", ip_address: "#{current_user.current_sign_in_ip}") + ban_user end end context 'when failed' do - let(:user) { create(:user, :blocked) } + context 'when user is blocked', :enable_admin_mode do + before do + user.block! + end - it 'returns error result' do - aggregate_failures 'error result' do - expect(operation[:status]).to eq(:error) - expect(operation[:message]).to match(/State cannot transition/) + it 'returns state error message' do + response = ban_user + + expect(response[:status]).to eq(:error) + expect(response[:message]).to match(/State cannot transition/) end + + it_behaves_like 'does not modify the BannedUser record or user state' end - it "does not change the user's state" do - expect { operation }.not_to change { user.state } + context 'when user is not an admin' do + it 'returns permissions error message' do + response = ban_user + + expect(response[:status]).to eq(:error) + expect(response[:message]).to match(/You are not allowed to ban a user/) + end + + it_behaves_like 'does not modify the BannedUser record or user state' end end end diff --git a/spec/services/users/banned_user_base_service_spec.rb b/spec/services/users/banned_user_base_service_spec.rb new file mode 100644 index 00000000000..29a549f0f49 --- /dev/null +++ b/spec/services/users/banned_user_base_service_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Users::BannedUserBaseService do + let(:admin) { create(:admin) } + let(:base_service) { described_class.new(admin) } + + describe '#initialize' do + it 'sets the current_user instance value' do + expect(base_service.instance_values["current_user"]).to eq(admin) + end + end +end diff --git a/spec/services/users/set_status_service_spec.rb b/spec/services/users/set_status_service_spec.rb index 2c776a0eeb4..76e86506d94 100644 --- a/spec/services/users/set_status_service_spec.rb +++ b/spec/services/users/set_status_service_spec.rb @@ -8,6 +8,18 @@ RSpec.describe Users::SetStatusService do subject(:service) { described_class.new(current_user, params) } describe '#execute' do + shared_examples_for 'bumps user' do + it 'bumps User#updated_at' do + expect { service.execute }.to change { current_user.updated_at } + end + end + + shared_examples_for 'does not bump user' do + it 'does not bump User#updated_at' do + expect { service.execute }.not_to change { current_user.updated_at } + end + end + context 'when params are set' do let(:params) { { emoji: 'taurus', message: 'a random status', availability: 'busy' } } @@ -31,6 +43,8 @@ RSpec.describe Users::SetStatusService do expect(service.execute).to be(true) end + it_behaves_like 'bumps user' + context 'when setting availability to not_set' do before do params[:availability] = 'not_set' @@ -72,6 +86,8 @@ RSpec.describe Users::SetStatusService do it 'does not update the status if the current user is not allowed' do expect { service.execute }.not_to change { target_user.status } end + + it_behaves_like 'does not bump user' end end @@ -79,20 +95,28 @@ RSpec.describe Users::SetStatusService do let(:params) { {} } shared_examples 'removes user status record' do - it 'deletes the status' do - status = create(:user_status, user: current_user) - + it 'deletes the user status record' do expect { service.execute } - .to change { current_user.reload.status }.from(status).to(nil) + .to change { current_user.reload.status }.from(user_status).to(nil) end - end - it_behaves_like 'removes user status record' + it_behaves_like 'bumps user' + end - context 'when not_set is given for availability' do - let(:params) { { availability: 'not_set' } } + context 'when user has existing user status record' do + let!(:user_status) { create(:user_status, user: current_user) } it_behaves_like 'removes user status record' + + context 'when not_set is given for availability' do + let(:params) { { availability: 'not_set' } } + + it_behaves_like 'removes user status record' + end + end + + context 'when user has no existing user status record' do + it_behaves_like 'does not bump user' end end end diff --git a/spec/services/users/unban_service_spec.rb b/spec/services/users/unban_service_spec.rb new file mode 100644 index 00000000000..b2b3140ccb3 --- /dev/null +++ b/spec/services/users/unban_service_spec.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Users::UnbanService do + let(:user) { create(:user) } + + let_it_be(:current_user) { create(:admin) } + + shared_examples 'does not modify the BannedUser record or user state' do + it 'does not modify the BannedUser record or user state' do + expect { unban_user }.not_to change { Users::BannedUser.count } + expect { unban_user }.not_to change { user.state } + end + end + + context 'unban', :aggregate_failures do + subject(:unban_user) { described_class.new(current_user).execute(user) } + + context 'when successful', :enable_admin_mode do + before do + user.ban! + end + + it 'returns success status' do + response = unban_user + + expect(response[:status]).to eq(:success) + end + + it 'unbans the user' do + expect { unban_user }.to change { user.state }.from('banned').to('active') + end + + it 'removes the BannedUser' do + expect { unban_user }.to change { Users::BannedUser.count }.by(-1) + expect(user.reload.banned_user).to be_nil + end + + it 'logs unban in application logs' do + expect(Gitlab::AppLogger).to receive(:info).with(message: "User unban", user: "#{user.username}", email: "#{user.email}", unban_by: "#{current_user.username}", ip_address: "#{current_user.current_sign_in_ip}") + + unban_user + end + end + + context 'when failed' do + context 'when user is already active', :enable_admin_mode do + it 'returns state error message' do + response = unban_user + + expect(response[:status]).to eq(:error) + expect(response[:message]).to match(/State cannot transition/) + end + + it_behaves_like 'does not modify the BannedUser record or user state' + end + + context 'when user is not an admin' do + before do + user.ban! + end + + it 'returns permissions error message' do + response = unban_user + + expect(response[:status]).to eq(:error) + expect(response[:message]).to match(/You are not allowed to unban a user/) + end + + it_behaves_like 'does not modify the BannedUser record or user state' + end + end + end +end |