2021-10-12 05:09:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe CustomerRelations::Contacts::UpdateService do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2022-04-29 14:08:18 -04:00
|
|
|
let(:contact) { create(:contact, first_name: 'Mark', group: group, state: 'active') }
|
2021-10-12 05:09:35 -04:00
|
|
|
|
|
|
|
subject(:update) { described_class.new(group: group, current_user: user, params: params).execute(contact) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
context 'when the user has no permission' do
|
2022-01-07 19:14:32 -05:00
|
|
|
let_it_be(:group) { create(:group, :crm_enabled) }
|
2021-10-12 05:09:35 -04:00
|
|
|
|
|
|
|
let(:params) { { first_name: 'Gary' } }
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
response = update
|
|
|
|
|
|
|
|
expect(response).to be_error
|
2022-05-11 08:09:03 -04:00
|
|
|
expect(response.message).to match_array(['You have insufficient permissions to manage contacts for this group'])
|
2021-10-12 05:09:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has permission' do
|
2022-01-07 19:14:32 -05:00
|
|
|
let_it_be(:group) { create(:group, :crm_enabled) }
|
2021-10-12 05:09:35 -04:00
|
|
|
|
|
|
|
before_all do
|
|
|
|
group.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when first_name is changed' do
|
|
|
|
let(:params) { { first_name: 'Gary' } }
|
|
|
|
|
|
|
|
it 'updates the contact' do
|
|
|
|
response = update
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.payload.first_name).to eq('Gary')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-29 14:08:18 -04:00
|
|
|
context 'when activating' do
|
|
|
|
let(:contact) { create(:contact, state: 'inactive') }
|
|
|
|
let(:params) { { active: true } }
|
|
|
|
|
|
|
|
it 'updates the contact' do
|
|
|
|
response = update
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.payload.active?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deactivating' do
|
|
|
|
let(:params) { { active: false } }
|
|
|
|
|
|
|
|
it 'updates the contact' do
|
|
|
|
response = update
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.payload.active?).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 05:09:35 -04:00
|
|
|
context 'when the contact is invalid' do
|
|
|
|
let(:params) { { first_name: nil } }
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
response = update
|
|
|
|
|
|
|
|
expect(response).to be_error
|
|
|
|
expect(response.message).to match_array(["First name can't be blank"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|