debian-mirror-gitlab/spec/services/users/ban_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.1 KiB
Ruby
Raw Normal View History

2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe Users::BanService, feature_category: :user_management do
2021-10-27 15:23:28 +05:30
let(:user) { create(:user) }
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
let_it_be(:current_user) { create(:admin) }
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
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
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
context 'ban', :aggregate_failures do
subject(:ban_user) { described_class.new(current_user).execute(user) }
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
context 'when successful', :enable_admin_mode do
it 'returns success status' do
response = ban_user
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
expect(response[:status]).to eq(:success)
2021-06-08 01:23:25 +05:30
end
2021-10-27 15:23:28 +05:30
it 'bans the user' do
expect { ban_user }.to change { user.state }.from('active').to('banned')
2021-06-08 01:23:25 +05:30
end
2021-10-27 15:23:28 +05:30
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
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
it 'logs ban in application logs' do
2023-01-13 00:05:48 +05:30
expect(Gitlab::AppLogger).to receive(:info).with(message: "User ban", user: user.username.to_s, email: user.email.to_s, ban_by: current_user.username.to_s, ip_address: current_user.current_sign_in_ip.to_s)
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
ban_user
2021-06-08 01:23:25 +05:30
end
end
context 'when failed' do
2021-10-27 15:23:28 +05:30
context 'when user is blocked', :enable_admin_mode do
before do
user.block!
end
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
it 'returns state error message' do
response = ban_user
expect(response[:status]).to eq(:error)
2021-11-11 11:23:49 +05:30
expect(response[:message]).to match('You cannot ban blocked users.')
2021-06-08 01:23:25 +05:30
end
2021-10-27 15:23:28 +05:30
it_behaves_like 'does not modify the BannedUser record or user state'
2021-06-08 01:23:25 +05:30
end
2021-10-27 15:23:28 +05:30
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'
2021-06-08 01:23:25 +05:30
end
end
end
end