Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Rewrite duplicated CreateService specs (#8959)

Oliver Günther90d8dec29635
diff --git a/modules/bim/spec/services/bcf/issues/create_service_spec.rb b/modules/bim/spec/services/bcf/issues/create_service_spec.rb
index c5f25a621e5..b187a4323b6 100644
--- a/modules/bim/spec/services/bcf/issues/create_service_spec.rb
+++ b/modules/bim/spec/services/bcf/issues/create_service_spec.rb
@@ -29,145 +29,26 @@
#++
require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
describe Bim::Bcf::Issues::CreateService, type: :model do
- let(:user) { FactoryBot.build_stubbed(:user) }
- let(:contract_class) do
- double('contract_class', '<=': true)
- end
- let(:issue_valid) { true }
- let(:instance) do
- described_class.new(user: user,
- contract_class: contract_class)
- end
- let(:call_attributes) { { subject: 'Some name' } }
- let(:set_attributes_success) do
- true
- end
- let(:set_attributes_errors) do
- double('set_attributes_errors')
- end
- let(:set_attributes_result) do
- ServiceResult.new result: created_issue,
- success: set_attributes_success,
- errors: set_attributes_errors
- end
- let!(:created_work_package) do
- FactoryBot.build_stubbed(:work_package)
- end
- let(:wp_create_errors) do
- double('wp_create_errors')
- end
- let(:wp_create_result) do
- ServiceResult.new result: created_work_package,
- success: true,
- errors: wp_create_errors
- end
- let!(:wp_create_service) do
- wp_service = double('wp create service')
-
- allow(WorkPackages::CreateService)
- .to receive(:new)
- .with(user: user)
- .and_return(wp_service)
-
- allow(wp_service)
- .to receive(:call)
- .and_return(wp_create_result)
-
- wp_service
- end
- let!(:created_issue) do
- issue = FactoryBot.build_stubbed(:bcf_issue)
-
- allow(Bim::Bcf::Issue)
- .to receive(:new)
- .and_return(issue)
-
- allow(issue)
- .to receive(:save)
- .and_return(issue_valid)
-
- issue
- end
- let!(:set_attributes_service) do
- service = double('set_attributes_service_instance')
-
- allow(Bim::Bcf::Issues::SetAttributesService)
- .to receive(:new)
- .with(user: user,
- model: created_issue,
- contract_class: contract_class,
- contract_options: {})
- .and_return(service)
-
- allow(service)
- .to receive(:call)
- .and_return(set_attributes_result)
- end
-
- describe '#call' do
- subject { instance.call(call_attributes) }
-
- it 'is successful' do
- expect(subject.success?).to be_truthy
+ it_behaves_like 'BaseServices create service' do
+ let(:model_class) { ::Bim::Bcf::Issue }
+ let(:factory) { :bcf_issue }
+ let(:work_package) { FactoryBot.build_stubbed :work_package }
+ let(:wp_call) { ServiceResult.new(success: true, result: work_package) }
+
+ before do
+ allow(instance)
+ .to(receive(:create_work_package))
+ .and_return(wp_call)
end
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'persists the issue' do
- expect(created_issue)
- .to receive(:save)
- .and_return(issue_valid)
-
- subject
- end
-
- it 'creates a issue' do
- expect(subject.result)
- .to eql created_issue
- end
-
- context 'if the SetAttributeService is unsuccessful' do
- let(:set_attributes_success) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'does not persist the changes' do
- expect(created_issue)
- .to_not receive(:save)
-
- subject
- end
-
- it "exposes the contract's errors" do
- subject
-
- expect(subject.errors).to eql set_attributes_errors
- end
- end
-
- context 'when the issue is invalid' do
- let(:issue_valid) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it "exposes the issue's errors" do
- subject
+ context 'when WP service call fails' do
+ let(:wp_call) { ServiceResult.new(success: false, result: work_package) }
- expect(subject.errors).to eql created_issue.errors
+ it 'returns with that call immediately' do
+ expect(subject).to eq wp_call
end
end
end
diff --git a/modules/bim/spec/services/ifc_models/create_service_spec.rb b/modules/bim/spec/services/ifc_models/create_service_spec.rb
index c35de82e300..bb934a69d63 100644
--- a/modules/bim/spec/services/ifc_models/create_service_spec.rb
+++ b/modules/bim/spec/services/ifc_models/create_service_spec.rb
@@ -1,93 +1,22 @@
require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
describe Bim::IfcModels::CreateService do
- let(:user) { FactoryBot.build_stubbed(:user) }
- let(:contract_class) do
- double('contract_class', '<=': true)
- end
- let(:model_valid) { true }
- let(:instance) do
- described_class.new(user: user,
- contract_class: contract_class)
- end
- let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
- let(:set_attributes_success) do
- true
- end
- let(:set_attributes_errors) do
- double('set_attributes_errors')
- end
- let(:set_attributes_result) do
- ServiceResult.new result: created_model,
- success: set_attributes_success,
- errors: set_attributes_errors
- end
- let!(:created_model) do
- model = FactoryBot.build_stubbed(:ifc_model)
-
- allow(Bim::IfcModels::IfcModel)
- .to receive(:new)
- .and_return(model)
-
- allow(model)
- .to receive(:save)
- .and_return(model_valid)
-
- model
- end
- let!(:set_attributes_service) do
- service = double('set_attributes_service_instance')
-
- allow(Bim::IfcModels::SetAttributesService)
- .to receive(:new)
- .with(user: user,
- model: created_model,
- contract_class: contract_class,
- contract_options: {})
- .and_return(service)
-
- allow(service)
- .to receive(:call)
- .and_return(set_attributes_result)
- end
- let(:conversion_job) do
- double('ifc_conversion_job').tap do |job|
- allow(job)
- .to receive(:perform_later)
-
- stub_const('Bim::IfcModels::IfcConversionJob', job)
- end
- end
-
- describe 'call' do
- subject { instance.call(call_attributes) }
+ it_behaves_like 'BaseServices create service' do
+ let(:model_class) { ::Bim::IfcModels::IfcModel }
+ let(:factory) { :ifc_model }
+ let(:conversion_job) do
+ double('ifc_conversion_job').tap do |job|
+ allow(job).to receive(:perform_later)
- it 'is successful' do
- expect(subject.success?).to be_truthy
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'persists the model' do
- expect(created_model)
- .to receive(:save)
- .and_return(model_valid)
-
- subject
- end
-
- it 'returns the model' do
- expect(subject.result)
- .to eql created_model
+ stub_const('Bim::IfcModels::IfcConversionJob', job)
+ end
end
it 'queues a conversion job' do
expect(conversion_job)
- .to receive(:perform_later)
- .with(created_model)
+ .to(receive(:perform_later))
+ .with(model_instance)
subject
end
@@ -95,52 +24,18 @@ describe Bim::IfcModels::CreateService do
context 'if the SetAttributeService is unsuccessful' do
let(:set_attributes_success) { false }
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'does not persist the changes' do
- expect(created_model)
- .to_not receive(:save)
-
- subject
- end
-
- it "exposes the contract's errors" do
- subject
-
- expect(subject.errors).to eql set_attributes_errors
- end
-
it 'queues no conversion job' do
- expect(conversion_job)
- .not_to receive(:perform_later)
+ expect(conversion_job).not_to receive(:perform_later)
subject
end
end
context 'when the model is invalid' do
- let(:model_valid) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it "exposes the model's errors" do
- subject
-
- expect(subject.errors).to eql created_model.errors
- end
+ let(:model_save_result) { false }
it 'queues no conversion job' do
- expect(conversion_job)
- .not_to receive(:perform_later)
+ expect(conversion_job).not_to receive(:perform_later)
subject
end
diff --git a/modules/grids/app/services/grids/create_service.rb b/modules/grids/app/services/grids/create_service.rb
index eb785fae4b3..3f52be6c186 100644
--- a/modules/grids/app/services/grids/create_service.rb
+++ b/modules/grids/app/services/grids/create_service.rb
@@ -33,17 +33,8 @@ class Grids::CreateService < ::BaseServices::Create
protected
- def set_attributes(attributes)
- grid = new_grid(attributes.delete(:scope))
-
- Grids::SetAttributesService
- .new(user: user,
- model: grid,
- contract_class: contract_class)
- .call(attributes)
- end
-
- def new_grid(scope)
+ def instance(attributes)
+ scope = attributes.delete(:scope)
::Grids::Factory.build(scope, user)
end
end
diff --git a/modules/grids/spec/services/grids/create_service_spec.rb b/modules/grids/spec/services/grids/create_service_spec.rb
index afa8c497d34..75a2e168439 100644
--- a/modules/grids/spec/services/grids/create_service_spec.rb
+++ b/modules/grids/spec/services/grids/create_service_spec.rb
@@ -29,139 +29,10 @@
#++
require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
describe Grids::CreateService, type: :model do
- let(:user) { FactoryBot.build_stubbed(:user) }
- let(:project) do
- FactoryBot.build_stubbed(:project).tap do |p|
- allow(Project)
- .to receive(:find)
- .with(p.id)
- .and_return(p)
- end
- end
- let(:contract_class) do
- double('contract_class', '<=': true)
- end
- let(:grid_valid) { true }
- let(:instance) do
- described_class.new(user: user,
- contract_class: contract_class)
- end
- let(:scope) { "some/scope/url" }
- let(:call_attributes) { { scope: scope } }
- let(:grid_class) do
- Grids::Grid
- end
- let(:set_attributes_success) do
- true
- end
- let(:set_attributes_errors) do
- double('set_attributes_errors')
- end
- let(:set_attributes_result) do
- ServiceResult.new result: grid,
- success: set_attributes_success,
- errors: set_attributes_errors
- end
- let!(:grid) do
- grid = FactoryBot.build(grid_class.name.demodulize.underscore.to_sym)
-
- allow(Grids::Factory)
- .to receive(:build)
- .with(scope, user)
- .and_return(grid)
-
- allow(grid)
- .to receive(:save)
- .and_return(grid_valid)
-
- grid
- end
- let!(:set_attributes_service) do
- service = double('set_attributes_service_instance')
-
- allow(Grids::SetAttributesService)
- .to receive(:new)
- .with(user: user,
- model: grid,
- contract_class: contract_class)
- .and_return(service)
-
- allow(service)
- .to receive(:call)
- .and_return(set_attributes_result)
- end
- let!(:grid_configuration) do
- allow(Grids::Configuration)
- .to receive(:attributes_from_scope)
- .with(scope)
- .and_return(class: grid_class, project_id: project.id)
- end
-
- describe 'call' do
- subject { instance.call(call_attributes) }
-
- it 'is successful' do
- expect(subject.success?).to be_truthy
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'persists the grid' do
- expect(grid)
- .to receive(:save)
- .and_return(grid_valid)
-
- subject
- end
-
- context 'when the SetAttributeService is unsuccessful' do
- let(:set_attributes_success) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'does not persist the changes' do
- expect(grid)
- .to_not receive(:save)
-
- subject
- end
-
- it "exposes the contract's errors" do
- subject
-
- expect(subject.errors).to eql set_attributes_errors
- end
- end
-
- context 'when the grid is invalid' do
- let(:grid_valid) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it "exposes the grid's errors" do
- subject
-
- expect(subject.errors).to eql grid.errors
- end
- end
-
- it 'creates a Grid' do
- expect(subject.result)
- .to be_a grid_class
- end
+ it_behaves_like 'BaseServices create service' do
+ let(:model_class) { Grids::Grid }
end
end
diff --git a/spec/services/base_services/behaves_like_create_service.rb b/spec/services/base_services/behaves_like_create_service.rb
new file mode 100644
index 00000000000..55801d0f70a
--- /dev/null
+++ b/spec/services/base_services/behaves_like_create_service.rb
@@ -0,0 +1,138 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+shared_examples 'BaseServices create service' do
+ let(:service_class) { described_class }
+ let(:namespace) { service_class.to_s.deconstantize }
+ let(:model_class) { namespace.singularize.constantize }
+ let(:contract_class) { "#{namespace}::CreateContract".constantize }
+ let(:factory) { namespace.singularize.underscore }
+
+ let(:set_attributes_class) { "#{namespace}::SetAttributesService".constantize }
+
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:instance) do
+ described_class.new(user: user, contract_class: contract_class)
+ end
+ let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
+ let(:set_attributes_success) do
+ true
+ end
+ let(:set_attributes_errors) do
+ double('set_attributes_errors')
+ end
+ let(:set_attributes_result) do
+ ServiceResult.new result: model_instance,
+ success: set_attributes_success,
+ errors: set_attributes_errors
+ end
+ let!(:model_instance) { FactoryBot.build_stubbed(factory) }
+ let!(:set_attributes_service) do
+ service = double('set_attributes_service_instance')
+
+ allow(set_attributes_class)
+ .to(receive(:new))
+ .with(user: user,
+ model: model_instance,
+ contract_class: contract_class,
+ contract_options: {})
+ .and_return(service)
+
+ allow(service)
+ .to(receive(:call))
+ .and_return(set_attributes_result)
+ end
+
+ let(:model_save_result) { true }
+ let(:contract_validate_result) { true }
+
+ before do
+ allow(model_instance).to receive(:save).and_return(model_save_result)
+ allow(instance).to receive(:instance).and_return(model_instance)
+ end
+
+ subject { instance.call(call_attributes) }
+
+ describe '#user' do
+ it 'exposes a user which is available as a getter' do
+ expect(instance.user).to eql user
+ end
+ end
+
+ describe '#contract' do
+ it 'uses the CreateContract contract' do
+ expect(instance.contract_class).to eql contract_class
+ end
+ end
+
+ describe '#call' do
+ context 'if contract validates and the user saves' do
+ it 'is successful' do
+ expect(subject).to be_success
+ end
+
+ it 'matches the set attributes errors' do
+ expect(subject.errors).to eq(set_attributes_errors)
+ end
+
+ it 'returns the user as a result' do
+ result = subject.result
+ expect(result).to be_a model_class
+ end
+ end
+
+ context 'if contract does not validate' do
+ let(:set_attributes_success) { false }
+
+ it 'is unsuccessful' do
+ expect(subject).to_not be_success
+ end
+ end
+
+ context 'if model does not save' do
+ let(:model_save_result) { false }
+ let(:errors) { double('errors') }
+
+ it 'is unsuccessful' do
+ expect(subject).to_not be_success
+ end
+
+ it "returns the user's errors" do
+ allow(model_instance)
+ .to(receive(:errors))
+ .and_return errors
+
+ expect(subject.errors).to eql errors
+ end
+ end
+ end
+end
diff --git a/spec/services/custom_fields/create_service_spec.rb b/spec/services/custom_fields/create_service_spec.rb
index 64f0dd0cf36..9b62b509865 100644
--- a/spec/services/custom_fields/create_service_spec.rb
+++ b/spec/services/custom_fields/create_service_spec.rb
@@ -29,126 +29,18 @@
#++
require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
-describe CustomFields::CreateService, type: :model do
- let(:user) { FactoryBot.build_stubbed(:user) }
- let(:contract_class) do
- double('contract_class', '<=': true)
- end
- let(:cf_valid) { true }
- let(:instance) do
- described_class.new(user: user, contract_class: contract_class)
- end
- let(:call_attributes) { { type: 'ProjectCustomField', name: 'Some name', field_format: 'string' } }
- let(:set_attributes_success) do
- true
- end
- let(:set_attributes_errors) do
- double('set_attributes_errors')
- end
- let(:set_attributes_result) do
- ServiceResult.new result: created_cf,
- success: set_attributes_success,
- errors: set_attributes_errors
- end
- let!(:created_cf) do
- cf = FactoryBot.build_stubbed(:project_custom_field)
-
- allow(ProjectCustomField)
- .to receive(:new)
- .and_return(cf)
-
- allow(cf)
- .to receive(:save)
- .and_return(cf_valid)
-
- cf
- end
-
- let!(:set_attributes_service) do
- service = double('set_attributes_service_instance')
-
- allow(CustomFields::SetAttributesService)
- .to receive(:new)
- .with(user: user,
- model: created_cf,
- contract_class: contract_class,
- contract_options: {})
- .and_return(service)
-
- allow(service)
- .to receive(:call)
- .and_return(set_attributes_result)
- end
- let(:new_project_role) { FactoryBot.build_stubbed(:role) }
-
- describe 'call' do
- subject { instance.call(call_attributes) }
-
- it 'is successful' do
- expect(subject.success?).to be_truthy
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'persists the custom_field' do
- expect(created_cf)
- .to receive(:save)
- .and_return(cf_valid)
-
- subject
- end
- it 'modifies the settings' do
- subject
- expect(Setting.enabled_projects_columns).to include "cf_#{created_cf.id}"
- end
-
- it 'creates the custom field' do
- expect(subject.result).to eql created_cf
- expect(subject.result).to be_kind_of(ProjectCustomField)
- end
-
- context 'if the SetAttributeService is unsuccessful' do
- let(:set_attributes_success) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'does not persist the changes' do
- expect(created_cf)
- .to_not receive(:save)
-
- subject
- end
-
- it "exposes the contract's errors" do
- subject
-
- expect(subject.errors).to eql set_attributes_errors
- end
- end
+describe CustomFields::CreateService, type: :model do
+ it_behaves_like 'BaseServices create service' do
- context 'when the cf is invalid' do
- let(:cf_valid) { false }
+ context 'when creating a project cf' do
+ let(:model_instance) { FactoryBot.build_stubbed :project_custom_field }
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it "exposes the message's errors" do
+ it 'modifies the settings on successful call' do
subject
-
- expect(subject.errors).to eql created_cf.errors
+ expect(Setting.enabled_projects_columns).to include "cf_#{model_instance.id}"
end
end
end
diff --git a/spec/services/messages/create_service_spec.rb b/spec/services/messages/create_service_spec.rb
index a079b0d4c08..c5b06442cdc 100644
--- a/spec/services/messages/create_service_spec.rb
+++ b/spec/services/messages/create_service_spec.rb
@@ -29,122 +29,9 @@
#++
require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
-describe Messages::CreateService, type: :model do
- let(:user) { FactoryBot.build_stubbed(:user) }
- let(:contract_class) do
- double('contract_class', '<=': true)
- end
- let(:message_valid) { true }
- let(:instance) do
- described_class.new(user: user,
- contract_class: contract_class)
- end
- let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
- let(:set_attributes_success) do
- true
- end
- let(:set_attributes_errors) do
- double('set_attributes_errors')
- end
- let(:set_attributes_result) do
- ServiceResult.new result: created_message,
- success: set_attributes_success,
- errors: set_attributes_errors
- end
- let!(:created_message) do
- message = FactoryBot.build_stubbed(:message)
-
- allow(Message)
- .to receive(:new)
- .and_return(message)
-
- allow(message)
- .to receive(:save)
- .and_return(message_valid)
-
- message
- end
- let!(:set_attributes_service) do
- service = double('set_attributes_service_instance')
-
- allow(Messages::SetAttributesService)
- .to receive(:new)
- .with(user: user,
- model: created_message,
- contract_class: contract_class,
- contract_options: {})
- .and_return(service)
-
- allow(service)
- .to receive(:call)
- .and_return(set_attributes_result)
- end
- let(:new_project_role) { FactoryBot.build_stubbed(:role) }
-
- describe 'call' do
- subject { instance.call(call_attributes) }
-
- it 'is successful' do
- expect(subject.success?).to be_truthy
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'persists the message' do
- expect(created_message)
- .to receive(:save)
- .and_return(message_valid)
-
- subject
- end
-
- it 'creates a message' do
- expect(subject.result)
- .to eql created_message
- end
- context 'if the SetAttributeService is unsuccessful' do
- let(:set_attributes_success) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'does not persist the changes' do
- expect(created_message)
- .to_not receive(:save)
-
- subject
- end
-
- it "exposes the contract's errors" do
- subject
-
- expect(subject.errors).to eql set_attributes_errors
- end
- end
-
- context 'when the message is invalid' do
- let(:message_valid) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it "exposes the message's errors" do
- subject
-
- expect(subject.errors).to eql created_message.errors
- end
- end
- end
+describe Messages::CreateService, type: :model do
+ it_behaves_like 'BaseServices create service'
end
diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb
index 43b1ff453fd..60b21700efc 100644
--- a/spec/services/projects/create_service_spec.rb
+++ b/spec/services/projects/create_service_spec.rb
@@ -29,100 +29,28 @@
#++
require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
describe Projects::CreateService, type: :model do
- let(:user) { FactoryBot.build_stubbed(:user) }
- let(:contract_class) do
- double('contract_class', '<=': true)
- end
- let(:project_valid) { true }
- let(:instance) do
- described_class.new(user: user,
- contract_class: contract_class)
- end
- let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
- let(:set_attributes_success) do
- true
- end
- let(:set_attributes_errors) do
- double('set_attributes_errors')
- end
- let(:set_attributes_result) do
- ServiceResult.new result: created_project,
- success: set_attributes_success,
- errors: set_attributes_errors
- end
- let!(:created_project) do
- project = FactoryBot.build_stubbed(:project)
-
- allow(Project)
- .to receive(:new)
- .and_return(project)
-
- allow(project)
- .to receive(:save)
- .and_return(project_valid)
-
- project
- end
- let!(:set_attributes_service) do
- service = double('set_attributes_service_instance')
-
- allow(Projects::SetAttributesService)
- .to receive(:new)
- .with(user: user,
- model: created_project,
- contract_class: contract_class,
- contract_options: {})
- .and_return(service)
-
- allow(service)
- .to receive(:call)
- .and_return(set_attributes_result)
- end
- let(:new_project_role) { FactoryBot.build_stubbed(:role) }
-
- describe 'call' do
- subject { instance.call(call_attributes) }
-
- it 'is successful' do
- expect(subject.success?).to be_truthy
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'persists the project' do
- expect(created_project)
- .to receive(:save)
- .and_return(project_valid)
-
- subject
- end
-
- it 'creates a project' do
- expect(subject.result)
- .to eql created_project
- end
+ it_behaves_like 'BaseServices create service' do
+ let(:new_project_role) { FactoryBot.build_stubbed(:role) }
it 'adds the current user to the project' do
allow(Role)
- .to receive(:in_new_project)
+ .to(receive(:in_new_project))
.and_return(new_project_role)
create_member_instance = double('Members::CreateService instance')
expect(Members::CreateService)
- .to receive(:new)
+ .to(receive(:new))
.with(user: user, contract_class: EmptyContract)
.and_return(create_member_instance)
expect(create_member_instance)
- .to receive(:call)
+ .to(receive(:call))
.with(principal: user,
- project: instance_of(Project),
+ project: model_instance,
roles: [new_project_role])
subject
@@ -131,54 +59,14 @@ describe Projects::CreateService, type: :model do
context 'current user is admin' do
it 'does not add the user to the project' do
allow(user)
- .to receive(:admin?)
+ .to(receive(:admin?))
.and_return(true)
- expect(created_project)
- .not_to receive(:add_member!)
+ expect(model_instance)
+ .not_to(receive(:add_member!))
subject
end
end
-
- context 'if the SetAttributeService is unsuccessful' do
- let(:set_attributes_success) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'returns the result of the SetAttributesService' do
- expect(subject)
- .to eql set_attributes_result
- end
-
- it 'does not persist the changes' do
- expect(created_project)
- .to_not receive(:save)
-
- subject
- end
-
- it "exposes the contract's errors" do
- subject
-
- expect(subject.errors).to eql set_attributes_errors
- end
- end
-
- context 'when the project is invalid' do
- let(:project_valid) { false }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it "exposes the project's errors" do
- subject
-
- expect(subject.errors).to eql created_project.errors
- end
- end
end
end
diff --git a/spec/services/users/create_user_service_spec.rb b/spec/services/users/create_user_service_spec.rb
index 0cfc6ed8fe4..cc20f6d8fda 100644
--- a/spec/services/users/create_user_service_spec.rb
+++ b/spec/services/users/create_user_service_spec.rb
@@ -27,80 +27,28 @@
# See docs/COPYRIGHT.rdoc for more details.
require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
describe Users::CreateService do
- let(:current_user) { FactoryBot.build_stubbed(:user) }
- let(:new_user) { FactoryBot.build_stubbed(:user) }
- let(:instance) { described_class.new(user: current_user) }
- let(:params) do
- {
- firstname: 'Bob',
- lastname: 'Bobby',
- login: 'bob1',
- }
- end
-
- describe '.contract' do
- it 'uses the CreateContract contract' do
- expect(instance.contract_class).to eql Users::CreateContract
- end
- end
-
- describe '.new' do
- it 'takes a user which is available as a getter' do
- expect(instance.user).to eql current_user
- end
- end
-
- describe '#call' do
- subject { instance.call(params) }
- let(:validates) { true }
- let(:saves) { true }
-
- before do
- allow(instance).to receive(:instance).and_return(new_user)
- allow(new_user).to receive(:save).and_return(saves)
- allow_any_instance_of(Users::CreateContract).to receive(:validate).and_return(validates)
- end
-
- context 'if contract validates and the user saves' do
- it 'is successful' do
- expect(subject).to be_success
- end
-
- it 'has no errors' do
- expect(subject.errors).to be_empty
- end
-
- it 'returns the user as a result' do
- result = subject.result
- expect(result).to be_a User
- end
- end
-
- context 'if contract does not validate' do
- let(:validates) { false }
-
- it 'is unsuccessful' do
- expect(subject).to_not be_success
- end
- end
-
- context 'if user does not save' do
- let(:saves) { false }
- let(:errors) { double('errors') }
-
- it 'is unsuccessful' do
- expect(subject).to_not be_success
+ it_behaves_like 'BaseServices create service' do
+ context 'when the user being invited' do
+ let(:model_instance) { FactoryBot.build :invited_user }
+
+ context 'and the mail is present' do
+ let(:model_instance) { FactoryBot.build :invited_user, mail: '[email protected]' }
+ it 'will call UserInvitation' do
+ expect(::UserInvitation).to receive(:invite_user!).with(model_instance).and_return(model_instance)
+ expect(subject).to be_success
+ end
end
- it "returns the user's errors" do
- allow(new_user)
- .to receive(:errors)
- .and_return errors
-
- expect(subject.errors).to eql errors
+ context 'and the mail is empty' do
+ let(:model_instance) { FactoryBot.build :invited_user, mail: nil }
+ it 'will call not call UserInvitation' do
+ expect(::UserInvitation).not_to receive(:invite_user!)
+ expect(subject).not_to be_success
+ expect(subject.errors.details[:mail]).to eq [{error: :blank}]
+ end
end
end
end