2021-05-14 14:10:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Users::BanService do
|
2021-08-02 11:08:56 -04:00
|
|
|
let(:user) { create(:user) }
|
2021-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
let_it_be(:current_user) { create(:admin) }
|
2021-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
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-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
context 'ban', :aggregate_failures do
|
|
|
|
subject(:ban_user) { described_class.new(current_user).execute(user) }
|
2021-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
context 'when successful', :enable_admin_mode do
|
|
|
|
it 'returns success status' do
|
|
|
|
response = ban_user
|
2021-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
expect(response[:status]).to eq(:success)
|
2021-05-14 14:10:34 -04:00
|
|
|
end
|
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
it 'bans the user' do
|
|
|
|
expect { ban_user }.to change { user.state }.from('active').to('banned')
|
2021-05-14 14:10:34 -04:00
|
|
|
end
|
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
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-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
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}")
|
2021-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
ban_user
|
2021-05-14 14:10:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when failed' do
|
2021-08-02 11:08:56 -04:00
|
|
|
context 'when user is blocked', :enable_admin_mode do
|
|
|
|
before do
|
|
|
|
user.block!
|
|
|
|
end
|
2021-05-14 14:10:34 -04:00
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
it 'returns state error message' do
|
|
|
|
response = ban_user
|
|
|
|
|
|
|
|
expect(response[:status]).to eq(:error)
|
2021-08-26 05:11:15 -04:00
|
|
|
expect(response[:message]).to match('You cannot ban blocked users.')
|
2021-05-14 14:10:34 -04:00
|
|
|
end
|
2021-08-02 11:08:56 -04:00
|
|
|
|
|
|
|
it_behaves_like 'does not modify the BannedUser record or user state'
|
2021-05-14 14:10:34 -04:00
|
|
|
end
|
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
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-05-14 14:10:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|