summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/customer_relations/organizations/update_spec.rb
blob: 0bc6f184fe3b393c89adafac351af0c86123bf63 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::CustomerRelations::Organizations::Update do
  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group) }

  let(:name) { 'GitLab' }
  let(:default_rate) { 1000.to_f }
  let(:description) { 'VIP' }
  let(:does_not_exist_or_no_permission) { "The resource that you are attempting to access does not exist or you don't have permission to perform this action" }
  let(:organization) { create(:organization, group: group) }
  let(:attributes) do
    {
      id: organization.to_global_id,
      name: name,
      default_rate: default_rate,
      description: description
    }
  end

  describe '#resolve' do
    subject(:resolve_mutation) do
      described_class.new(object: nil, context: { current_user: user }, field: nil).resolve(
        attributes
      )
    end

    context 'when the user does not have permission to update an organization' 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(does_not_exist_or_no_permission)
      end
    end

    context 'when the organization does not exist' do
      it 'raises an error' do
        attributes[:id] = "gid://gitlab/CustomerRelations::Organization/#{non_existing_record_id}"

        expect { resolve_mutation }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
          .with_message(does_not_exist_or_no_permission)
      end
    end

    context 'when the user has permission to update an organization' do
      before_all do
        group.add_developer(user)
      end

      it 'updates the organization with correct values' do
        expect(resolve_mutation[:organization]).to have_attributes(attributes)
      end

      context 'when the feature 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('Feature disabled')
        end
      end
    end
  end

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