debian-mirror-gitlab/spec/graphql/mutations/customer_relations/contacts/create_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.2 KiB
Ruby
Raw Normal View History

2021-11-18 22:05:49 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Mutations::CustomerRelations::Contacts::Create do
2022-07-16 23:28:13 +05:30
include GraphqlHelpers
2021-11-18 22:05:49 +05:30
let_it_be(:user) { create(:user) }
2022-03-02 08:16:31 +05:30
let(:group) { create(:group, :crm_enabled) }
2021-11-18 22:05:49 +05:30
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)
2021-12-11 22:18:48 +05:30
.with_message(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
2021-11-18 22:05:49 +05:30
end
end
context 'when the user has permission' do
2022-03-02 08:16:31 +05:30
before do
2021-11-18 22:05:49 +05:30
group.add_developer(user)
end
2022-03-02 08:16:31 +05:30
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
2021-11-18 22:05:49 +05:30
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
2022-07-16 23:28:13 +05:30
context 'when organization does not exist' do
2021-11-18 22:05:49 +05:30
before do
2022-07-16 23:28:13 +05:30
valid_params[:organization_id] = global_id_of(model_name: 'CustomerRelations::Organization', id: non_existing_record_id)
2021-11-18 22:05:49 +05:30
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
2021-12-11 22:18:48 +05:30
specify { expect(described_class).to require_graphql_authorizations(:admin_crm_contact) }
2021-11-18 22:05:49 +05:30
end