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

123 lines
4.2 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Users::ApproveService do
let_it_be(:current_user) { create(:admin) }
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 approve 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 approve a user/)
end
end
2021-01-29 00:20:46 +05:30
context 'when the executor user is an admin not in admin mode' do
2021-01-03 14:25:43 +05:30
it 'returns error result' do
expect(subject[:status]).to eq(:error)
2021-01-29 00:20:46 +05:30
expect(subject[:message]).to match(/You are not allowed to approve a user/)
2021-01-03 14:25:43 +05:30
end
end
2021-01-29 00:20:46 +05:30
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') }
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
it 'returns error result' do
expect(subject[:status]).to eq(:error)
expect(subject[:message])
.to match(/The user you are trying to approve is not pending an approval/)
end
2021-01-03 14:25:43 +05:30
end
2021-01-29 00:20:46 +05:30
context 'when user cannot be activated' do
let(:user) do
build(:user, state: 'blocked_pending_approval', email: 'invalid email')
end
it 'returns error result' do
expect(subject[:status]).to eq(:error)
expect(subject[:message]).to match(/Email is invalid/)
end
it 'does not change the state of the user' do
expect { subject }.not_to change { user.state }
end
2021-01-03 14:25:43 +05:30
end
end
end
context 'success' do
2021-01-29 00:20:46 +05:30
context 'when the executor user is an admin in admin mode', :enable_admin_mode do
it 'activates the user' do
expect(subject[:status]).to eq(:success)
expect(user.reload).to be_active
end
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
it 'emails the user on approval' do
expect(DeviseMailer).to receive(:user_admin_approval).with(user).and_call_original
expect { subject }.to have_enqueued_mail(DeviseMailer, :user_admin_approval)
end
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
context 'email confirmation status' do
context 'user is unconfirmed' do
let(:user) { create(:user, :blocked_pending_approval, :unconfirmed) }
it 'sends confirmation instructions' do
expect { subject }
.to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
end
2021-01-03 14:25:43 +05:30
end
2021-01-29 00:20:46 +05:30
context 'user is confirmed' do
it 'does not send a confirmation email' do
expect { subject }
.not_to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
end
2021-01-03 14:25:43 +05:30
end
end
2021-01-29 00:20:46 +05:30
context 'pending invitations' do
let!(:project_member_invite) { create(:project_member, :invited, invite_email: user.email) }
let!(:group_member_invite) { create(:group_member, :invited, invite_email: user.email) }
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
context 'user is unconfirmed' do
let(:user) { create(:user, :blocked_pending_approval, :unconfirmed) }
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
it 'does not accept pending invites of the user' do
expect(subject[:status]).to eq(:success)
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
group_member_invite.reload
project_member_invite.reload
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
expect(group_member_invite).to be_invite
expect(project_member_invite).to be_invite
end
2021-01-03 14:25:43 +05:30
end
2021-01-29 00:20:46 +05:30
context 'user is confirmed' do
it 'accepts pending invites of the user' do
expect(subject[:status]).to eq(:success)
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
group_member_invite.reload
project_member_invite.reload
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
expect(group_member_invite).not_to be_invite
expect(project_member_invite).not_to be_invite
expect(group_member_invite.user).to eq(user)
expect(project_member_invite.user).to eq(user)
end
2021-01-03 14:25:43 +05:30
end
end
end
end
end
end