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

use default contract contexts

ulferts28deaaa9d9a6
diff --git a/spec/contracts/projects/create_contract_spec.rb b/spec/contracts/projects/create_contract_spec.rb
index fb96b3251b6..29a09192b78 100644
--- a/spec/contracts/projects/create_contract_spec.rb
+++ b/spec/contracts/projects/create_contract_spec.rb
@@ -53,9 +53,7 @@ RSpec.describe Projects::CreateContract do
context "if the identifier is nil" do
let(:project_identifier) { nil }
- it "is replaced for new project" do
- expect_valid(true)
- end
+ it_behaves_like "contract is valid"
end
describe "permissions" do
diff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rb
index 79bf902a03f..36055f4cd81 100644
--- a/spec/contracts/projects/shared_contract_examples.rb
+++ b/spec/contracts/projects/shared_contract_examples.rb
@@ -29,8 +29,11 @@
#++
require "spec_helper"
+require "contracts/shared/model_contract_shared_context"
RSpec.shared_examples_for "project contract" do
+ include_context "ModelContract shared context"
+
let(:current_user) { build_stubbed(:user) }
before do
@@ -89,68 +92,48 @@ RSpec.shared_examples_for "project contract" do
assignable_parents
end
- def expect_valid(valid, symbols = {})
- expect(contract.validate).to eq(valid)
-
- symbols.each do |key, arr|
- expect(contract.errors.symbols_for(key)).to match_array arr
- end
- end
-
- shared_examples "is valid" do
- it "is valid" do
- expect_valid(true)
- end
- end
-
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
context "if the name is nil" do
let(:project_name) { nil }
- it "is invalid" do
- expect_valid(false, name: %i(blank))
- end
+ it_behaves_like "contract is invalid", name: %i(blank)
end
context "if the description is nil" do
let(:project_description) { nil }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "if the parent is nil" do
let(:project_parent) { nil }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "if the parent is not in the set of assignable_parents" do
let(:parent_assignable) { false }
- it "is invalid" do
- expect_valid(false, parent: %i(does_not_exist))
- end
+ it_behaves_like "contract is invalid", parent: %i(does_not_exist)
end
context "if active is nil" do
let(:project_active) { nil }
- it "is invalid" do
- expect_valid(false, active: %i(blank))
- end
+ it_behaves_like "contract is invalid", active: %i(blank)
end
context "if status code is nil" do
let(:project_status_code) { nil }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "if status explanation is nil" do
let(:project_status_explanation) { nil }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "if status code is invalid" do
@@ -166,70 +149,62 @@ RSpec.shared_examples_for "project contract" do
code_attributes.instance_variable_set(:@value, bogus_project_status_code)
end
- it "is invalid" do
- expect_valid(false, status: %i(inclusion))
- end
+ it_behaves_like "contract is invalid", status: %i(inclusion)
end
context "when the identifier consists of only letters" do
let(:project_identifier) { "abc" }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "when the identifier consists of letters followed by numbers" do
let(:project_identifier) { "abc12" }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "when the identifier consists of letters followed by numbers with a hyphen in between" do
let(:project_identifier) { "abc-12" }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "when the identifier consists of letters followed by numbers with an underscore in between" do
let(:project_identifier) { "abc_12" }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "when the identifier consists of numbers followed by letters with a hyphen in between" do
let(:project_identifier) { "12-abc" }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "when the identifier consists of numbers followed by letters with an underscore in between" do
let(:project_identifier) { "12_abc" }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "when the identifier consists of only numbers" do
let(:project_identifier) { "12" }
- it "is invalid" do
- expect_valid(false, identifier: %i(invalid))
- end
+ it_behaves_like "contract is invalid", identifier: %i(invalid)
end
context "when the identifier consists of a reserved word" do
let(:project_identifier) { "new" }
- it "is invalid" do
- expect_valid(false, identifier: %i(exclusion))
- end
+ it_behaves_like "contract is invalid", identifier: %i(exclusion)
end
context "if the user lacks permission" do
let(:global_permissions) { [] }
let(:project_permissions) { [] }
- it "is invalid" do
- expect_valid(false, base: %i(error_unauthorized))
- end
+ it_behaves_like "contract is invalid", base: %i(error_unauthorized)
end
describe "assignable_values" do
diff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rb
index e61a8921c8b..31e36a4743a 100644
--- a/spec/contracts/projects/update_contract_spec.rb
+++ b/spec/contracts/projects/update_contract_spec.rb
@@ -66,9 +66,7 @@ RSpec.describe Projects::UpdateContract do
context "if the identifier is nil" do
let(:project_identifier) { nil }
- it "is replaced for new project" do
- expect_valid(false, identifier: %i(blank))
- end
+ it_behaves_like "contract is invalid", identifier: %i(blank)
end
describe "permissions" do
@@ -85,26 +83,23 @@ RSpec.describe Projects::UpdateContract do
context "and only project_custom_fields are changed" do
let(:project_changed) { false }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "and other project attributes are changed too" do
let(:project_changed) { true }
- it "is invalid" do
- expect_valid(false, { name: %i(error_readonly),
- parent_id: %i(error_readonly),
- identifier: %i(error_readonly) })
- end
+ it_behaves_like "contract is invalid",
+ name: %i(error_readonly),
+ parent_id: %i(error_readonly),
+ identifier: %i(error_readonly)
end
end
context "when project_attributes_only flag is false" do
let(:options) { { project_attributes_only: false } }
- it "is invalid" do
- expect_valid(false, base: %i(error_unauthorized))
- end
+ it_behaves_like "contract is invalid", name: %i(error_readonly)
end
end
@@ -112,9 +107,7 @@ RSpec.describe Projects::UpdateContract do
context "when project_attributes_only flag is true" do
let(:options) { { project_attributes_only: true } }
- it "is invalid" do
- expect_valid(false, base: %i(error_unauthorized))
- end
+ it_behaves_like "contract user is unauthorized"
end
context "when project_attributes_only flag is false" do
@@ -123,7 +116,7 @@ RSpec.describe Projects::UpdateContract do
context "and only project attributes are changed" do
let(:project_changed) { true }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "and project_custom_fields are changed too" do
@@ -134,7 +127,7 @@ RSpec.describe Projects::UpdateContract do
end
it "is invalid" do
- expect_valid(false, "custom_field_#{custom_field.id}": %i(error_readonly))
+ expect_contract_invalid("custom_field_#{custom_field.id}": %i(error_readonly))
end
end
end
@@ -149,11 +142,10 @@ RSpec.describe Projects::UpdateContract do
context "and only project attributes are changed" do
let(:project_changed) { true }
- it "is invalid" do
- expect_valid(false, { name: %i(error_readonly),
- parent_id: %i(error_readonly),
- identifier: %i(error_readonly) })
- end
+ it_behaves_like "contract is invalid",
+ name: %i(error_readonly),
+ parent_id: %i(error_readonly),
+ identifier: %i(error_readonly)
end
context "and only project_custom_fields are changed" do
@@ -163,7 +155,7 @@ RSpec.describe Projects::UpdateContract do
project.custom_field_values = { custom_field.id => "1" }
end
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "when both project attributes and project custom_fields are changed" do
@@ -173,11 +165,10 @@ RSpec.describe Projects::UpdateContract do
project.custom_field_values = { custom_field.id => "1" }
end
- it "is invalid" do
- expect_valid(false, { name: %i(error_readonly),
- parent_id: %i(error_readonly),
- identifier: %i(error_readonly) })
- end
+ it_behaves_like "contract is invalid",
+ name: %i(error_readonly),
+ parent_id: %i(error_readonly),
+ identifier: %i(error_readonly)
end
end
@@ -187,7 +178,7 @@ RSpec.describe Projects::UpdateContract do
context "and only project attributes are changed" do
let(:project_changed) { true }
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
context "and project_custom_fields are changed too" do
@@ -197,7 +188,7 @@ RSpec.describe Projects::UpdateContract do
project.custom_field_values = { custom_field.id => "1" }
end
- it_behaves_like "is valid"
+ it_behaves_like "contract is valid"
end
end
end
@@ -206,9 +197,7 @@ RSpec.describe Projects::UpdateContract do
let(:project_permissions) { [] }
let(:options) { { project_attributes_only: true } }
- it "is invalid" do
- expect_valid(false, base: %i(error_unauthorized))
- end
+ it_behaves_like "contract user is unauthorized"
end
end