summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/customer_relations/contacts/create_spec.rb
blob: dafc7b4c367c2a2738ba77d0a2f9a1b5f6f3a14f (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::CustomerRelations::Contacts::Create do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }

  let(:group) { create(:group, :crm_enabled) }
  let(:not_found_or_does_not_belong) { 'The specified organization was not found or does not belong to this group' }
  let(:valid_params) do
    attributes_for(:contact,
      group: group,
      description: 'Managing Director'
    )
  end

  describe '#resolve' do
    subject(:resolve_mutation) do
      described_class.new(object: nil, context: { current_user: user }, field: nil).resolve(
        **valid_params,
        group_id: group.to_global_id
      )
    end

    context 'when the user does not have permission' do
      before do
        group.add_reporter(user)
      end

      it 'raises an error' do
        expect { resolve_mutation }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
          .with_message(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
      end
    end

    context 'when the user has permission' do
      before do
        group.add_developer(user)
      end

      context 'when the feature flag is disabled' do
        before do
          stub_feature_flags(customer_relations: false)
        end

        it 'raises an error' do
          expect { resolve_mutation }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
            .with_message("The resource that you are attempting to access does not exist or you don't have permission to perform this action")
        end
      end

      context 'when crm_enabled is false' do
        let(:group) { create(:group) }

        it 'raises an error' do
          expect { resolve_mutation }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
            .with_message("The resource that you are attempting to access does not exist or you don't have permission to perform this action")
        end
      end

      context 'when the params are invalid' do
        it 'returns the validation error' do
          valid_params[:first_name] = nil

          expect(resolve_mutation[:errors]).to match_array(["First name can't be blank"])
        end
      end

      context 'when attaching to an organization' do
        context 'when all ok' do
          before do
            organization = create(:organization, group: group)
            valid_params[:organization_id] = organization.to_global_id
          end

          it 'creates contact with correct values' do
            expect(resolve_mutation[:contact].organization).to be_present
          end
        end

        context 'when organization does not exist' do
          before do
            valid_params[:organization_id] = global_id_of(model_name: 'CustomerRelations::Organization', id: non_existing_record_id)
          end

          it 'returns the relevant error' do
            expect(resolve_mutation[:errors]).to match_array([not_found_or_does_not_belong])
          end
        end

        context 'when organzation belongs to a different group' do
          before do
            organization = create(:organization)
            valid_params[:organization_id] = organization.to_global_id
          end

          it 'returns the relevant error' do
            expect(resolve_mutation[:errors]).to match_array([not_found_or_does_not_belong])
          end
        end
      end

      it 'creates contact with correct values' do
        expect(resolve_mutation[:contact]).to have_attributes(valid_params)
      end
    end
  end

  specify { expect(described_class).to require_graphql_authorizations(:admin_crm_contact) }
end