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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add create contract spec for queries
Oliver Günther58b64686d835
diff --git a/spec/contracts/queries/create_contract_spec.rb b/spec/contracts/queries/create_contract_spec.rbnew file mode 100644index 00000000000..df49504a043--- /dev/null+++ b/spec/contracts/queries/create_contract_spec.rb@@ -0,0 +1,62 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'+require 'contracts/shared/model_contract_shared_context'+require_relative 'shared_contract_examples'++describe Queries::CreateContract do+ include_context 'ModelContract shared context'+ include_context 'with queries contract'++ describe 'include subprojects' do+ let(:query) do+ Query.new name: 'foo',+ include_subprojects: include_subprojects,+ project: project+ end++ context 'when true' do+ let(:include_subprojects) { true }++ it_behaves_like 'contract is valid'+ end++ context 'when falsea' do+ let(:include_subprojects) { false }++ it_behaves_like 'contract is valid'+ end++ context 'when nil' do+ let(:include_subprojects) { nil }++ it_behaves_like 'contract is invalid', include_subprojects: %i[inclusion]+ end+ end+enddiff --git a/spec/contracts/queries/shared_contract_examples.rb b/spec/contracts/queries/shared_contract_examples.rbnew file mode 100644index 00000000000..90d0d20938b--- /dev/null+++ b/spec/contracts/queries/shared_contract_examples.rb@@ -0,0 +1,52 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'+require 'contracts/shared/model_contract_shared_context'++shared_context 'with queries contract' do+ let(:project) { build_stubbed :project }+ let(:query) do+ build_stubbed(:query, project: project, public: public, user: user)+ end++ let(:current_user) do+ build_stubbed(:user) do |user|+ allow(user)+ .to receive(:allowed_to?) do |permission, permission_project|+ permissions.include?(permission) && project == permission_project+ end+ end+ end+ let(:contract) { described_class.new(query, current_user) }++ before do+ # Assume project is always visible+ allow(contract).to receive(:project_visible?).and_return true+ end+enddiff --git a/spec/contracts/queries/update_contract_spec.rb b/spec/contracts/queries/update_contract_spec.rbindex 37c2a8a34e9..30735721b61 100644--- a/spec/contracts/queries/update_contract_spec.rb+++ b/spec/contracts/queries/update_contract_spec.rb@@ -28,29 +28,11 @@require 'spec_helper'require 'contracts/shared/model_contract_shared_context'+require_relative 'shared_contract_examples'describe Queries::UpdateContract doinclude_context 'ModelContract shared context'-- let(:project) { build_stubbed :project }- let(:query) do- build_stubbed(:query, project: project, public: public, user: user)- end-- let(:current_user) do- build_stubbed(:user) do |user|- allow(user)- .to receive(:allowed_to?) do |permission, permission_project|- permissions.include?(permission) && project == permission_project- end- end- end- let(:contract) { described_class.new(query, current_user) }-- before do- # Assume project is always visible- allow(contract).to receive(:project_visible?).and_return true- end+ include_context 'with queries contract'describe 'private query' dolet(:public) { false }@@ -58,13 +40,13 @@ describe Queries::UpdateContract docontext 'when user is author' dolet(:user) { current_user }- context 'user has no permission to save' do+ context 'when user has no permission to save' dolet(:permissions) { %i(edit_work_packages) }it_behaves_like 'contract user is unauthorized'end- context 'user has permission to save' do+ context 'when user has permission to save' dolet(:permissions) { %i(save_queries) }it_behaves_like 'contract is valid'@@ -83,19 +65,19 @@ describe Queries::UpdateContract dolet(:public) { true }let(:user) { nil }- context 'user has no permission to save' do+ context 'when user has no permission to save' dolet(:permissions) { %i(invalid_permission) }it_behaves_like 'contract user is unauthorized'end- context 'user has no permission to manage public' do+ context 'when user has no permission to manage public' dolet(:permissions) { %i(manage_public_queries) }it_behaves_like 'contract is valid'end- context 'user has permission to save only own' do+ context 'when user has permission to save only own' dolet(:permissions) { %i(save_queries) }it_behaves_like 'contract user is unauthorized'diff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rbindex 15604a5a2e7..c90fb1583a7 100644--- a/spec/models/query_spec.rb+++ b/spec/models/query_spec.rb@@ -63,19 +63,19 @@ describe Query, type: :model docontext 'with global subprojects include', with_settings: { display_subprojects_work_packages: true } doit 'sets the include subprojects' do- expect(query.include_subprojects).to eq true+ expect(query.include_subprojects).to be trueendendcontext 'with global subprojects include', with_settings: { display_subprojects_work_packages: false } doit 'sets the include subprojects' do- expect(query.include_subprojects).to eq false+ expect(query.include_subprojects).to be falseendendenddescribe 'include_subprojects' do- let(:query) { Query.new name: 'foo' }+ let(:query) { described_class.new name: 'foo' }it 'is required' doexpect(query).not_to be_valid