summaryrefslogtreecommitdiff
path: root/spec/services/customer_relations/contacts/create_service_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/services/customer_relations/contacts/create_service_spec.rb')
-rw-r--r--spec/services/customer_relations/contacts/create_service_spec.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/spec/services/customer_relations/contacts/create_service_spec.rb b/spec/services/customer_relations/contacts/create_service_spec.rb
new file mode 100644
index 00000000000..71eb447055e
--- /dev/null
+++ b/spec/services/customer_relations/contacts/create_service_spec.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe CustomerRelations::Contacts::CreateService do
+ describe '#execute' do
+ let_it_be(:user) { create(:user) }
+ let_it_be(:not_found_or_does_not_belong) { 'The specified organization was not found or does not belong to this group' }
+
+ let(:params) { attributes_for(:contact, group: group) }
+
+ subject(:response) { described_class.new(group: group, current_user: user, params: params).execute }
+
+ context 'when user does not have permission' do
+ let_it_be(:group) { create(:group) }
+
+ before_all do
+ group.add_reporter(user)
+ end
+
+ it 'returns an error' do
+ expect(response).to be_error
+ expect(response.message).to match_array(['You have insufficient permissions to create a contact for this group'])
+ end
+ end
+
+ context 'when user has permission' do
+ let_it_be(:group) { create(:group) }
+
+ before_all do
+ group.add_developer(user)
+ end
+
+ it 'creates a contact' do
+ expect(response).to be_success
+ end
+
+ it 'returns an error when the contact is not persisted' do
+ params[:last_name] = nil
+
+ expect(response).to be_error
+ expect(response.message).to match_array(["Last name can't be blank"])
+ end
+
+ it 'returns an error when the organization_id is invalid' do
+ params[:organization_id] = non_existing_record_id
+
+ expect(response).to be_error
+ expect(response.message).to match_array([not_found_or_does_not_belong])
+ end
+
+ it 'returns an error when the organization belongs to a different group' do
+ organization = create(:organization)
+ params[:organization_id] = organization.id
+
+ expect(response).to be_error
+ expect(response.message).to match_array([not_found_or_does_not_belong])
+ end
+ end
+ end
+end