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

Refactor BaseContract with specs for Create/Update Contract

Aaron Contrerascab006f68f8c
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index 66d76674d9b..32bee318904 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -42,9 +42,10 @@ module Projects
attribute :parent do
validate_parent_assignable
end
- attribute :status do
+ attribute :status_code do
validate_status_code_included
end
+ attribute :status_explanation
attribute :templated do
validate_templated_set_by_admin
end
@@ -68,7 +69,7 @@ module Projects
delegate :assignable_versions, to: :model
def assignable_status_codes
- Projects::Status.codes.keys
+ Project.status_codes.keys
end
private
@@ -94,7 +95,7 @@ module Projects
end
def validate_status_code_included
- errors.add :status, :inclusion if model.status&.code && !Projects::Status.codes.keys.include?(model.status.code.to_s)
+ errors.add :status, :inclusion if model.status_code && Project.status_codes.keys.exclude?(model.status_code.to_s)
end
def validate_templated_set_by_admin
diff --git a/spec/contracts/projects/create_contract_spec.rb b/spec/contracts/projects/create_contract_spec.rb
index be557e0364b..a8f6e1a7b75 100644
--- a/spec/contracts/projects/create_contract_spec.rb
+++ b/spec/contracts/projects/create_contract_spec.rb
@@ -38,7 +38,8 @@ describe Projects::CreateContract do
active: project_active,
public: project_public,
parent: project_parent,
- status: project_status)
+ status_code: project_status_code,
+ status_explanation: project_status_explanation)
end
let(:permissions) { [:add_project] }
let!(:allowed_to) do
diff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rb
index 6037b08685e..a17685f5855 100644
--- a/spec/contracts/projects/shared_contract_examples.rb
+++ b/spec/contracts/projects/shared_contract_examples.rb
@@ -43,7 +43,8 @@ shared_examples_for 'project contract' do
let(:project_description) { 'Project description' }
let(:project_active) { true }
let(:project_public) { true }
- let(:project_status) { build_stubbed(:project_status) }
+ let(:project_status_code) { 'on_track' }
+ let(:project_status_explanation) { 'some explanation' }
let(:project_parent) do
build_stubbed(:project)
end
@@ -132,17 +133,37 @@ shared_examples_for 'project contract' do
end
end
- context 'if status is nil' do
- let(:project_status) { nil }
+ context 'if status code is nil' do
+ let(:project_status_code) { nil }
+
+ it_behaves_like 'is valid'
+ end
+
+ context 'if status explanation is nil' do
+ let(:project_status_explanation) { nil }
it_behaves_like 'is valid'
end
context 'if status code is invalid' do
before do
- allow(project_status)
- .to receive(:code)
- .and_return('bogus')
+ # Questions for Jens:
+ # I'm unsure if this should even be in a spec. It implies a dependency
+ # between the SetAttributesService and the Contract which I'm not sure
+ # is good. However, it might be warranted as this is only shifting the
+ # behavior of what would've happened either way into the Project itself
+ # instead of an association which is why it didn't pose the need for hack
+ # on this spec.
+
+ # Hack in order to handle setting an Enum value without raising an
+ # ArgumentError and letting the Contract perform the validation.
+ #
+ # This is the behavior that would be expected to be performed by
+ # the SetAttributesService at that layer of the flow.
+ bogus_project_status_code = 'bogus'
+ code_attributes = project.instance_variable_get(:@attributes)['status_code']
+ code_attributes.instance_variable_set(:@value_before_type_cast, bogus_project_status_code)
+ code_attributes.instance_variable_set(:@value, bogus_project_status_code)
end
it 'is invalid' do
diff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rb
index 46c33fc4586..1e9f4890927 100644
--- a/spec/contracts/projects/update_contract_spec.rb
+++ b/spec/contracts/projects/update_contract_spec.rb
@@ -35,7 +35,8 @@ describe Projects::UpdateContract do
build_stubbed(:project,
active: project_active,
public: project_public,
- status: project_status).tap do |p|
+ status_code: project_status_code,
+ status_explanation: project_status_explanation).tap do |p|
# in order to actually have something changed
p.name = project_name
p.parent = project_parent