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

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

79 lines
2.5 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe Users::RejectService, feature_category: :user_management do
2021-02-22 17:27:13 +05:30
let_it_be(:current_user) { create(:admin) }
2021-09-30 23:02:18 +05:30
2021-02-22 17:27:13 +05:30
let(:user) { create(:user, :blocked_pending_approval) }
subject(:execute) { described_class.new(current_user).execute(user) }
describe '#execute' do
context 'failures' do
context 'when the executor user is not allowed to reject users' do
let(:current_user) { create(:user) }
it 'returns error result' do
expect(subject[:status]).to eq(:error)
expect(subject[:message]).to match(/You are not allowed to reject a user/)
end
end
context 'when the executor user is an admin in admin mode', :enable_admin_mode do
context 'when user is not in pending approval state' do
let(:user) { create(:user, state: 'active') }
it 'returns error result' do
expect(subject[:status]).to eq(:error)
expect(subject[:message])
2021-11-11 11:23:49 +05:30
.to match(/User does not have a pending request/)
2021-02-22 17:27:13 +05:30
end
end
end
end
context 'success' do
context 'when the executor user is an admin in admin mode', :enable_admin_mode do
2023-01-13 00:05:48 +05:30
it 'initiates user removal', :sidekiq_inline do
subject
2022-10-11 01:57:18 +05:30
2023-01-13 00:05:48 +05:30
expect(subject[:status]).to eq(:success)
expect(
Users::GhostUserMigration.where(user: user,
initiator_user: current_user)
).to be_exists
2021-02-22 17:27:13 +05:30
end
it 'emails the user on rejection' do
expect_next_instance_of(NotificationService) do |notification|
2021-11-11 11:23:49 +05:30
allow(notification).to receive(:user_admin_rejection).with(user.name, user.notification_email_or_default)
2021-02-22 17:27:13 +05:30
end
subject
end
2021-03-11 19:13:27 +05:30
it 'logs rejection in application logs' do
allow(Gitlab::AppLogger).to receive(:info)
subject
2023-01-13 00:05:48 +05:30
expect(Gitlab::AppLogger).to have_received(:info).with(message: "User instance access request rejected", user: user.username.to_s, email: user.email.to_s, rejected_by: current_user.username.to_s, ip_address: current_user.current_sign_in_ip.to_s)
2021-03-11 19:13:27 +05:30
end
end
end
context 'audit events' do
context 'when not licensed' do
before do
stub_licensed_features(admin_audit_log: false)
end
it 'does not log any audit event' do
expect { subject }.not_to change(AuditEvent, :count)
end
2021-02-22 17:27:13 +05:30
end
end
end
end