2020-10-15 08:09:06 -04:00
|
|
|
# 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
|
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
context 'when the executor user is an admin not in admin mode' do
|
2020-10-15 08:09:06 -04:00
|
|
|
it 'returns error result' do
|
|
|
|
expect(subject[:status]).to eq(:error)
|
2020-11-11 07:09:06 -05:00
|
|
|
expect(subject[:message]).to match(/You are not allowed to approve a user/)
|
2020-10-15 08:09:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
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') }
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
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
|
2020-10-15 08:09:06 -04:00
|
|
|
end
|
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
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
|
2020-10-15 08:09:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'success' do
|
2020-11-11 07:09:06 -05:00
|
|
|
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
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-12 16:09:19 -05:00
|
|
|
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
|
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
context 'email confirmation status' do
|
|
|
|
context 'user is unconfirmed' do
|
|
|
|
let(:user) { create(:user, :blocked_pending_approval, :unconfirmed) }
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
it 'sends confirmation instructions' do
|
|
|
|
expect { subject }
|
|
|
|
.to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
|
|
|
|
end
|
2020-10-15 08:09:06 -04:00
|
|
|
end
|
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
context 'user is confirmed' do
|
|
|
|
it 'does not send a confirmation email' do
|
|
|
|
expect { subject }
|
|
|
|
.not_to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
|
|
|
|
end
|
2020-10-15 08:09:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
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) }
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
context 'user is unconfirmed' do
|
|
|
|
let(:user) { create(:user, :blocked_pending_approval, :unconfirmed) }
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
it 'does not accept pending invites of the user' do
|
|
|
|
expect(subject[:status]).to eq(:success)
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
group_member_invite.reload
|
|
|
|
project_member_invite.reload
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
expect(group_member_invite).to be_invite
|
|
|
|
expect(project_member_invite).to be_invite
|
|
|
|
end
|
2020-10-15 08:09:06 -04:00
|
|
|
end
|
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
context 'user is confirmed' do
|
|
|
|
it 'accepts pending invites of the user' do
|
|
|
|
expect(subject[:status]).to eq(:success)
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
group_member_invite.reload
|
|
|
|
project_member_invite.reload
|
2020-10-15 08:09:06 -04:00
|
|
|
|
2020-11-11 07:09:06 -05:00
|
|
|
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
|
2020-10-15 08:09:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|