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

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.rb
new file mode 100644
index 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
+end
diff --git a/spec/contracts/queries/shared_contract_examples.rb b/spec/contracts/queries/shared_contract_examples.rb
new file mode 100644
index 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
+end
diff --git a/spec/contracts/queries/update_contract_spec.rb b/spec/contracts/queries/update_contract_spec.rb
index 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 do
include_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' do
let(:public) { false }
@@ -58,13 +40,13 @@ describe Queries::UpdateContract do
context 'when user is author' do
let(:user) { current_user }
- context 'user has no permission to save' do
+ context 'when user has no permission to save' do
let(: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' do
let(:permissions) { %i(save_queries) }
it_behaves_like 'contract is valid'
@@ -83,19 +65,19 @@ describe Queries::UpdateContract do
let(:public) { true }
let(:user) { nil }
- context 'user has no permission to save' do
+ context 'when user has no permission to save' do
let(: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' do
let(: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' do
let(:permissions) { %i(save_queries) }
it_behaves_like 'contract user is unauthorized'
diff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rb
index 15604a5a2e7..c90fb1583a7 100644
--- a/spec/models/query_spec.rb
+++ b/spec/models/query_spec.rb
@@ -63,19 +63,19 @@ describe Query, type: :model do
context 'with global subprojects include', with_settings: { display_subprojects_work_packages: true } do
it 'sets the include subprojects' do
- expect(query.include_subprojects).to eq true
+ expect(query.include_subprojects).to be true
end
end
context 'with global subprojects include', with_settings: { display_subprojects_work_packages: false } do
it 'sets the include subprojects' do
- expect(query.include_subprojects).to eq false
+ expect(query.include_subprojects).to be false
end
end
end
describe 'include_subprojects' do
- let(:query) { Query.new name: 'foo' }
+ let(:query) { described_class.new name: 'foo' }
it 'is required' do
expect(query).not_to be_valid