summaryrefslogtreecommitdiff
path: root/spec/services/customer_relations/contacts/update_service_spec.rb
blob: 729fdc2058b3f581d20058dee385ec1ea83a1dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CustomerRelations::Contacts::UpdateService do
  let_it_be(:user) { create(:user) }

  let(:contact) { create(:contact, first_name: 'Mark', group: group, state: 'active') }

  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
      let_it_be(:group) { create(:group, :crm_enabled) }

      let(:params) { { first_name: 'Gary' } }

      it 'returns an error' do
        response = update

        expect(response).to be_error
        expect(response.message).to match_array(['You have insufficient permissions to manage contacts for this group'])
      end
    end

    context 'when user has permission' do
      let_it_be(:group) { create(:group, :crm_enabled) }

      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

      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

      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