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
static create endpoint for Views::WorkPackagesTable
ulferts3f9cfdf732b8
diff --git a/app/contracts/views/base_contract.rb b/app/contracts/views/base_contract.rbnew file mode 100644index 00000000000..93442749a3a--- /dev/null+++ b/app/contracts/views/base_contract.rb@@ -0,0 +1,77 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++module Views+ class BaseContract < ::ModelContract+ attribute :query++ validate :type_allowed+ validate :query_present,+ :query_manageable++ private++ def type_allowed+ # TODO: fetch from registry+ unless ['work_packages_table'].include?(model.type)+ errors.add(:type, :inclusion)+ end+ end++ def query_present+ unless model.query+ errors.add(:query, :blank)+ end+ end++ def query_manageable+ return unless model.query++ if query_visible?+ errors.add(:query, :error_unauthorized) unless query_permissions?+ else+ errors.add(:query, :does_not_exist)+ end+ end++ def query_visible?+ Query.visible(user).exists?(id: model.query.id)+ end++ def query_permissions?+ # The visibility i.e. whether a private query belongs to the user is checked via the+ # query_visible? method.+ (model.query.is_public && user_allowed_on_query?(:manage_public_queries)) ||+ (!model.query.is_public && user_allowed_on_query?(:save_queries))+ end++ def user_allowed_on_query?(permission)+ user.allowed_to?(permission, model.query.project, global: model.query.project.nil?)+ end+ end+enddiff --git a/app/contracts/views/create_contract.rb b/app/contracts/views/create_contract.rbnew file mode 100644index 00000000000..4a10d934e4b--- /dev/null+++ b/app/contracts/views/create_contract.rb@@ -0,0 +1,33 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++module Views+ class CreateContract < BaseContract+ attribute :type+ end+enddiff --git a/app/contracts/views/update_contract.rb b/app/contracts/views/update_contract.rbnew file mode 100644index 00000000000..cbd25d22300--- /dev/null+++ b/app/contracts/views/update_contract.rb@@ -0,0 +1,31 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++module Views+ class UpdateContract < BaseContract; end+enddiff --git a/app/services/views/create_service.rb b/app/services/views/create_service.rbnew file mode 100644index 00000000000..9bb25df40fe--- /dev/null+++ b/app/services/views/create_service.rb@@ -0,0 +1,29 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++class Views::CreateService < ::BaseServices::Create; enddiff --git a/app/services/views/set_attributes_service.rb b/app/services/views/set_attributes_service.rbnew file mode 100644index 00000000000..8706aed9e2c--- /dev/null+++ b/app/services/views/set_attributes_service.rb@@ -0,0 +1,31 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++module Views+ class SetAttributesService < ::BaseServices::SetAttributes; end+enddiff --git a/db/migrate/20211130161501_add_views.rb b/db/migrate/20211130161501_add_views.rbindex d8e9759056d..1c0e87a5868 100644--- a/db/migrate/20211130161501_add_views.rb+++ b/db/migrate/20211130161501_add_views.rb@@ -2,8 +2,8 @@ class AddViews < ActiveRecord::Migration[6.1]def changecreate_table :views do |t|t.references :query, null: false, foreign_key: true, index: true- t.jsonb :options, default: {}- t.string :type+ t.jsonb :options, default: {}, null: false+ t.string :type, null: falset.timestampsenddiff --git a/docs/api/apiv3/components/examples/view_table.yml b/docs/api/apiv3/components/examples/view_work_packages_table.ymlsimilarity index 91%rename from docs/api/apiv3/components/examples/view_table.ymlrename to docs/api/apiv3/components/examples/view_work_packages_table.ymlindex c0a5b222199..e2eac5d71f5 100644--- a/docs/api/apiv3/components/examples/view_table.yml+++ b/docs/api/apiv3/components/examples/view_work_packages_table.yml@@ -1,7 +1,7 @@-# Example: ViewTable+# Example: ViewWorkPackagesTable---value:- _type: Views::Table+ _type: Views::WorkPackagesTablename: Current work packagestimelineVisible: trueid: 9diff --git a/docs/api/apiv3/components/examples/view_table_schema.yml b/docs/api/apiv3/components/examples/view_work_packages_table_schema.ymlsimilarity index 79%rename from docs/api/apiv3/components/examples/view_table_schema.ymlrename to docs/api/apiv3/components/examples/view_work_packages_table_schema.ymlindex d92c91de5ed..00b1326b340 100644--- a/docs/api/apiv3/components/examples/view_table_schema.yml+++ b/docs/api/apiv3/components/examples/view_work_packages_table_schema.yml@@ -1,11 +1,6 @@-# Example: Views::TableSchema+# Example: Views::WorkPackagesTableSchema---value:- _dependencies: []- _embedded:- _links:- self:- href: "/api/v3/views/schemas/table"_type: Schemacolumns:_links: {}@@ -15,3 +10,6 @@ value:required: falsetype: "[]QueryColumn"writable: true+ _links:+ self:+ href: "/api/v3/views/schemas/table"diff --git a/docs/api/apiv3/components/examples/views.yml b/docs/api/apiv3/components/examples/views.ymlindex e0b22b90c6f..fcfaf83c743 100644--- a/docs/api/apiv3/components/examples/views.yml+++ b/docs/api/apiv3/components/examples/views.yml@@ -15,7 +15,7 @@ value:_type: Collection_embedded:elements:- - _type: Views::Table+ - _type: Views::WorkPackagesTablename: Current work packagestimelineVisible: trueid: 9diff --git a/docs/api/apiv3/openapi-spec.yml b/docs/api/apiv3/openapi-spec.ymlindex 17c6bd4b64b..4707ea1b833 100644--- a/docs/api/apiv3/openapi-spec.yml+++ b/docs/api/apiv3/openapi-spec.yml@@ -353,6 +353,8 @@ paths:"$ref": "./paths/version_projects.yml""/api/v3/views":"$ref": "./paths/views.yml"+ "/api/v3/views/{identifier}":+ "$ref": "./paths/views_{identifier}.yml""/api/v3/views/schemas":"$ref": "./paths/views_schemas.yml""/api/v3/views/schemas/{identifier}":@@ -409,10 +411,10 @@ components:"$ref": "./components/examples/views.yml"ViewBCF:"$ref": "./components/examples/view_bcf.yml"- ViewTable:- "$ref": "./components/examples/view_table.yml"- ViewTableSchema:- "$ref": "./components/examples/view_table_schema.yml"+ ViewWorkPackagesTable:+ "$ref": "./components/examples/view_work_packages_table.yml"+ ViewWorkPackagesTableSchema:+ "$ref": "./components/examples/view_work_packages_table_schema.yml"schemas:ActivityModel:"$ref": "./components/schemas/activity_model.yml"diff --git a/docs/api/apiv3/paths/view.yml b/docs/api/apiv3/paths/view.ymlindex a30b033ea54..a2de5f67e83 100644--- a/docs/api/apiv3/paths/view.yml+++ b/docs/api/apiv3/paths/view.yml@@ -14,8 +14,8 @@ get:content:application/hal+json:examples:- ViewTable:- "$ref": "../components/examples/view_table.yml"+ ViewWorkPackagesTable:+ "$ref": "../components/examples/view_work_packages_table.yml"ViewBcf:"$ref": "../components/examples/view_bcf.yml"'400':@@ -128,8 +128,8 @@ patch:content:application/hal+json:examples:- ViewTable:- "$ref": "../components/examples/view_table.yml"+ ViewWorkPackagesTable:+ "$ref": "../components/examples/view_work_packages_table.yml"description: OKheaders: {}'400':diff --git a/docs/api/apiv3/paths/views.yml b/docs/api/apiv3/paths/views.ymlindex f28320a0d89..ed899becb28 100644--- a/docs/api/apiv3/paths/views.yml+++ b/docs/api/apiv3/paths/views.yml@@ -32,55 +32,3 @@ get:query parameters similar to how work packages are filtered.operationId: List_viewssummary: List views-post:- responses:- '201':- content:- application/hal+json:- examples:- Query:- "$ref": "../components/examples/view_table.yml"- description: Created- headers: {}- '400':- content:- application/hal+json:- examples:- response:- value:- _type: Error- errorIdentifier: urn:openproject-org:api:v3:errors:InvalidRequestBody- message: The request body was not a single JSON object.- description: Occurs when the client did not send a valid JSON object in the- request body.- headers: {}- '422':- content:- application/hal+json:- examples:- response:- value:- _embedded:- details:- attribute: query- _type: Error- errorIdentifier: urn:openproject-org:api:v3:errors:PropertyConstraintViolation- message: Query not found- description: |-- Returned if:-- * the client tries to modify a read-only property (`PropertyIsReadOnly`)-- * a constraint for a property was violated (`PropertyConstraintViolation`)-- * the client provides a link to an invalid resource (`ResourceTypeMismatch`),- e.g. a query not found- headers: {}- tags:- - Views- description: |-- When calling this endpoint the client provides a single object, containing at least the properties and links that are required, in the body.- The required fields of a View can be found in its schema, which is embedded in the respective form.- Note that it is only allowed to provide properties or links supporting the write operation.- operationId: Create_views- summary: Create viewdiff --git a/docs/api/apiv3/paths/views_schemas_{identifier}.yml b/docs/api/apiv3/paths/views_schemas_{identifier}.ymlindex 6735ede5a38..7db31e90136 100644--- a/docs/api/apiv3/paths/views_schemas_{identifier}.yml+++ b/docs/api/apiv3/paths/views_schemas_{identifier}.yml@@ -16,8 +16,8 @@ get:content:application/hal+json:examples:- TableSchema:- "$refs": '../components/examples/view_table_schema.yml'+ ViewWorkPackagesTableSchema:+ "$ref": '../components/examples/view_work_packages_table_schema.yml''404':content:application/hal+json:diff --git a/docs/api/apiv3/paths/views_{identifier}.yml b/docs/api/apiv3/paths/views_{identifier}.ymlnew file mode 100644index 00000000000..d59e20693fa--- /dev/null+++ b/docs/api/apiv3/paths/views_{identifier}.yml@@ -0,0 +1,68 @@+# /api/v3/views+---++post:+ responses:+ '201':+ content:+ application/hal+json:+ examples:+ Views:+ "$ref": "../components/examples/view_work_packages_table.yml"+ description: Created+ headers: {}+ '400':+ content:+ application/hal+json:+ examples:+ response:+ value:+ _type: Error+ errorIdentifier: urn:openproject-org:api:v3:errors:InvalidRequestBody+ message: The request body was not a single JSON object.+ description: Occurs when the client did not send a valid JSON object in the+ request body.+ headers: {}+ '422':+ content:+ application/hal+json:+ examples:+ response:+ value:+ _embedded:+ details:+ attribute: query+ _type: Error+ errorIdentifier: urn:openproject-org:api:v3:errors:PropertyConstraintViolation+ message: "Query does not exist."+ description: |-+ Returned if:++ * the client tries to modify a read-only property (`PropertyIsReadOnly`)++ * a constraint for a property was violated (`PropertyConstraintViolation`)++ * the client provides a link to an invalid resource (`ResourceTypeMismatch`),+ e.g. a query not found+ headers: {}+ tags:+ - Views+ description: |-+ When calling this endpoint the client provides a single object, containing at least the properties and links that are required, in the body.+ The required fields of a View can be found in its schema, which is embedded in the respective form.+ Note that it is only allowed to provide properties or links supporting the write operation.++ There are different subtypes of Views (e.g. `Views::WorkPackagesTable`) with each having its own+ endpoint for creating that subtype e.g.+ ```+ /api/v3/views/work_packages_table+ ```+ for `Views::WorkPackagesTable`.++ To get the list of available subtypes and by that the endpoints for creating a subtype, use the+ ```+ /api/v3/views/schemas+ ```+ endpoint.+ operationId: Create_views+ summary: Create viewdiff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex 65544f57687..cb38a6ad3e6 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -440,6 +440,10 @@ module APIindex :viewshow :view+ def self.views_work_packages_table+ "#{views}/work_packages_table"+ end+def self.versions_available_projects"#{versions}/available_projects"enddiff --git a/lib/api/v3/views/view_payload_representer.rb b/lib/api/v3/views/view_payload_representer.rbnew file mode 100644index 00000000000..6d311e4d23b--- /dev/null+++ b/lib/api/v3/views/view_payload_representer.rb@@ -0,0 +1,37 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++module API+ module V3+ module Views+ class ViewPayloadRepresenter < ViewRepresenter+ include ::API::Utilities::PayloadRepresenter+ end+ end+ end+enddiff --git a/lib/api/v3/views/view_representer.rb b/lib/api/v3/views/view_representer.rbindex deae26c888e..44e5c26b6ee 100644--- a/lib/api/v3/views/view_representer.rb+++ b/lib/api/v3/views/view_representer.rb@@ -83,7 +83,7 @@ module APIdate_time_property :updated_atdef _type- 'Views::Table'+ 'Views::WorkPackagesTable'endendenddiff --git a/lib/api/v3/views/views_api.rb b/lib/api/v3/views/views_api.rbindex 0eaba97f79e..3c7a1ef2e8a 100644--- a/lib/api/v3/views/views_api.rb+++ b/lib/api/v3/views/views_api.rb@@ -35,6 +35,15 @@ module API.new(model: View).mount+ resources :work_packages_table do+ post &::API::V3::Utilities::Endpoints::Create+ .new(model: View,+ params_modifier: ->(params) {+ params.merge(type: 'work_packages_table')+ })+ .mount+ end+route_param :id, type: Integer, desc: 'View ID' doafter_validation do@view = ::Queries::Views::ViewQuerydiff --git a/spec/contracts/views/create_contract_spec.rb b/spec/contracts/views/create_contract_spec.rbnew file mode 100644index 00000000000..dd2d113b720--- /dev/null+++ b/spec/contracts/views/create_contract_spec.rb@@ -0,0 +1,60 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'+require_relative './shared_contract_examples'++describe Views::CreateContract do+ it_behaves_like 'view contract' do+ let(:view) do+ View.new(query: view_query,+ type: view_type)+ end+ let(:view_type) do+ 'work_packages_table'+ end++ subject(:contract) do+ described_class.new(view, current_user)+ end++ describe 'validation' do+ context 'with the type being nil' do+ let(:view_type) { nil }++ it_behaves_like 'contract is invalid', type: :inclusion+ end++ context 'with the type not being one of the configured' do+ let(:view_type) { 'blubs' }++ it_behaves_like 'contract is invalid', type: :inclusion+ end+ end+ end+enddiff --git a/spec/contracts/views/shared_contract_examples.rb b/spec/contracts/views/shared_contract_examples.rbnew file mode 100644index 00000000000..2e61081551c--- /dev/null+++ b/spec/contracts/views/shared_contract_examples.rb@@ -0,0 +1,109 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'+require 'contracts/shared/model_contract_shared_context'++shared_examples_for 'view contract' do+ include_context 'ModelContract shared context'++ let(:current_user) do+ FactoryBot.build_stubbed(:user) do |user|+ allow(user)+ .to receive(:allowed_to?) do |permission, permission_project, **_args|+ permissions.include?(permission) && query_project == permission_project+ end+ end+ end+ let(:view_query) do+ FactoryBot.build_stubbed(:query,+ user: query_user,+ is_public: query_public,+ project: query_project)+ end+ let(:permissions) { %i[view_work_packages save_queries] }+ let(:query_public) { false }+ let(:query_user) { current_user }+ let(:query_visible) { true }+ let(:query_project) { FactoryBot.build_stubbed(:project) }++ before do+ next unless view_query++ visible_scope = instance_double('ActiveRecord::Relation')++ allow(Query)+ .to receive(:visible)+ .with(query_user)+ .and_return(visible_scope)++ allow(visible_scope)+ .to receive(:exists?)+ .with(id: view_query.id)+ .and_return(query_visible)+ end++ describe 'validation' do+ it_behaves_like 'contract is valid'++ context 'with the query being nil' do+ let(:view_query) { nil }++ it_behaves_like 'contract is invalid', query: :blank+ end++ context 'with the query being invisible to the user' do+ let(:query_visible) { false }++ it_behaves_like 'contract is invalid', query: :does_not_exist+ end++ context 'with the query being private, the user being the query user and having the :save_queries permission' do+ it_behaves_like 'contract is valid'+ end++ context 'with the query being private, the user being the query user and not having the :save_queries permission' do+ let(:permissions) { %i[view_work_packages] }++ it_behaves_like 'contract is invalid', query: :error_unauthorized+ end++ context 'with the query being public, the user being the query user but only having the :save_queries permission' do+ let(:query_public) { true }++ it_behaves_like 'contract is invalid', query: :error_unauthorized+ end++ context 'with the query being public, the user not being the query user but having the :manage_public_queries permission' do+ let(:query_public) { true }+ let(:permissions) { %i[view_work_packages manage_public_queries] }++ it_behaves_like 'contract is valid'+ end+ end+enddiff --git a/spec/contracts/views/update_contract_spec.rb b/spec/contracts/views/update_contract_spec.rbnew file mode 100644index 00000000000..39dacb62899--- /dev/null+++ b/spec/contracts/views/update_contract_spec.rb@@ -0,0 +1,56 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'+require_relative './shared_contract_examples'++describe Views::UpdateContract do+ # TODO: this is just a stub to ensure that the type is not altered+ it_behaves_like 'view contract' do+ let(:view) do+ FactoryBot+ .build_stubbed(:view_work_packages_table).tap do |view|+ view.type = view_type if defined?(view_type)+ view.query = view_query+ end+ end++ subject(:contract) do+ described_class.new(view, current_user)+ end++ describe 'validation' do+ context 'with the type being changed' do+ # TODO: once a second type is defined, use that here+ let(:view_type) { 'another_type' }++ it_behaves_like 'contract is invalid', type: [:error_readonly, :inclusion]+ end+ end+ end+enddiff --git a/spec/factories/view_factory.rb b/spec/factories/view_factory.rbindex 21072180da1..84358e271d0 100644--- a/spec/factories/view_factory.rb+++ b/spec/factories/view_factory.rb@@ -28,9 +28,9 @@FactoryBot.define dofactory :view do- type { 'table' }+ type { 'work_packages_table' }queryend- factory :view_table, parent: :view+ factory :view_work_packages_table, parent: :viewenddiff --git a/spec/lib/api/v3/utilities/path_helper_spec.rb b/spec/lib/api/v3/utilities/path_helper_spec.rbindex 4f43225a9d1..2ff1bd42565 100644--- a/spec/lib/api/v3/utilities/path_helper_spec.rb+++ b/spec/lib/api/v3/utilities/path_helper_spec.rb@@ -532,6 +532,12 @@ describe ::API::V3::Utilities::PathHelper dodescribe 'view paths' doit_behaves_like 'show', :viewit_behaves_like 'index', :view++ describe '#views_work_packages_table' do+ subject { helper.views_work_packages_table }++ it_behaves_like 'api v3 path', '/views/work_packages_table'+ endenddescribe 'wiki pages paths' dodiff --git a/spec/lib/api/v3/views/view_representer_rendering_spec.rb b/spec/lib/api/v3/views/view_representer_rendering_spec.rbindex 275f924ac94..d2b79861c54 100644--- a/spec/lib/api/v3/views/view_representer_rendering_spec.rb+++ b/spec/lib/api/v3/views/view_representer_rendering_spec.rb@@ -34,7 +34,7 @@ describe ::API::V3::Views::ViewRepresenter, 'rendering' dosubject(:generated) { representer.to_json }let(:query) { FactoryBot.build_stubbed(:query, is_public: query_public, query_menu_item: query_menu_item) }- let(:view) { FactoryBot.build_stubbed(:view_table, query: query) }+ let(:view) { FactoryBot.build_stubbed(:view_work_packages_table, query: query) }let(:current_user) { FactoryBot.build_stubbed(:user) }let(:query_public) { true }# aka starred@@ -51,7 +51,7 @@ describe ::API::V3::Views::ViewRepresenter, 'rendering' dodescribe 'properties' dodescribe '_type' doit_behaves_like 'property', :_type do- let(:value) { 'Views::Table' }+ let(:value) { 'Views::WorkPackagesTable' }endenddiff --git a/spec/requests/api/v3/views/create_resource_spec.rb b/spec/requests/api/v3/views/create_resource_spec.rbnew file mode 100644index 00000000000..4303a27e712--- /dev/null+++ b/spec/requests/api/v3/views/create_resource_spec.rb@@ -0,0 +1,109 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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'++describe ::API::V3::Views::ViewsAPI,+ 'create',+ content_type: :json,+ type: :request do+ include API::V3::Utilities::PathHelper++ shared_let(:permitted_user) { FactoryBot.create(:user) }+ shared_let(:role) { FactoryBot.create(:role, permissions: %w[view_work_packages save_queries]) }+ shared_let(:project) do+ FactoryBot.create(:project,+ members: { permitted_user => role })+ end+ shared_let(:private_user_query) do+ FactoryBot.create(:query,+ project: project,+ is_public: false,+ user: permitted_user)+ end++ let(:additional_setup) do+ # to be overwritten by some specs+ end++ let(:body) do+ {+ _links: {+ query: {+ href: api_v3_paths.query(private_user_query.id)+ }+ }+ }.to_json+ end++ let(:send_request) do+ post api_v3_paths.views_work_packages_table, body+ end++ current_user { permitted_user }++ subject(:response) { last_response }++ before do+ additional_setup++ send_request+ end++ describe 'POST /api/v3/views/work_packages_table' do+ context 'with a user allowed to save the query' do+ it 'returns 201 CREATED' do+ expect(response.status)+ .to eq(201)+ end++ it 'returns the view' do+ expect(response.body)+ .to be_json_eql('Views::WorkPackagesTable'.to_json)+ .at_path('_type')++ expect(response.body)+ .to be_json_eql(View.last.id.to_json)+ .at_path('id')+ end+ end++ context 'with a user not allowed to see the query' do+ let(:additional_setup) do+ role.update_attribute(:permissions, [])+ end++ it 'responds with 422 and explains the error' do+ expect(last_response.status).to eq(422)++ expect(last_response.body)+ .to be_json_eql("Query does not exist.".to_json)+ .at_path('message')+ end+ end+ end+enddiff --git a/spec/requests/api/v3/views/index_resource_spec.rb b/spec/requests/api/v3/views/index_resource_spec.rbindex e8d2092e830..7f0fde09b94 100644--- a/spec/requests/api/v3/views/index_resource_spec.rb+++ b/spec/requests/api/v3/views/index_resource_spec.rb@@ -50,7 +50,7 @@ describe ::API::V3::Views::ViewsAPI,is_public: false)endshared_let(:user_private_project_view) do- FactoryBot.create(:view_table,+ FactoryBot.create(:view_work_packages_table,query: user_private_project_query)endshared_let(:other_user_private_project_query) do@@ -59,7 +59,7 @@ describe ::API::V3::Views::ViewsAPI,is_public: false)endshared_let(:other_user_private_project_view) do- FactoryBot.create(:view_table,+ FactoryBot.create(:view_work_packages_table,query: other_user_private_project_query)endshared_let(:user_public_project_query) do@@ -68,7 +68,7 @@ describe ::API::V3::Views::ViewsAPI,is_public: true)endshared_let(:user_public_project_view) do- FactoryBot.create(:view_table,+ FactoryBot.create(:view_work_packages_table,query: user_public_project_query)endshared_let(:other_project) do@@ -81,7 +81,7 @@ describe ::API::V3::Views::ViewsAPI,project: other_project)endshared_let(:user_private_other_project_view) do- FactoryBot.create(:view_table,+ FactoryBot.create(:view_work_packages_table,query: user_private_other_project_query)end@@ -109,7 +109,7 @@ describe ::API::V3::Views::ViewsAPI,endcontext 'without any filter' do- it_behaves_like 'API V3 collection response', 3, 3, 'Views::Table' do+ it_behaves_like 'API V3 collection response', 3, 3, 'Views::WorkPackagesTable' dolet(:elements) do[user_private_other_project_view,@@ -132,7 +132,7 @@ describe ::API::V3::Views::ViewsAPI,]end- it_behaves_like 'API V3 collection response', 2, 2, 'Views::Table' do+ it_behaves_like 'API V3 collection response', 2, 2, 'Views::WorkPackagesTable' dolet(:elements) do[user_public_project_view,@@ -145,6 +145,6 @@ describe ::API::V3::Views::ViewsAPI,context 'for a user without any visible queries' docurrent_user { FactoryBot.create(:user) }- it_behaves_like 'API V3 collection response', 0, 0, 'Views::Table'+ it_behaves_like 'API V3 collection response', 0, 0, 'Views::WorkPackagesTable'endenddiff --git a/spec/requests/api/v3/views/show_resource_spec.rb b/spec/requests/api/v3/views/show_resource_spec.rbindex 573daee1ab2..97ef97f7626 100644--- a/spec/requests/api/v3/views/show_resource_spec.rb+++ b/spec/requests/api/v3/views/show_resource_spec.rb@@ -48,7 +48,7 @@ describe ::API::V3::Views::ViewsAPI,user: permitted_user)endshared_let(:view) do- FactoryBot.create :view_table,+ FactoryBot.create :view_work_packages_table,query: private_user_queryend@@ -72,7 +72,7 @@ describe ::API::V3::Views::ViewsAPI,it 'returns the view' doexpect(response.body)- .to be_json_eql('Views::Table'.to_json)+ .to be_json_eql('Views::WorkPackagesTable'.to_json).at_path('_type')expect(response.body)diff --git a/spec/services/services/create_service_spec.rb b/spec/services/services/create_service_spec.rbnew file mode 100644index 00000000000..3b5a6fdba35--- /dev/null+++ b/spec/services/services/create_service_spec.rb@@ -0,0 +1,41 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'+require 'services/base_services/behaves_like_create_service'++describe Views::CreateService, type: :model do+ it_behaves_like 'BaseServices create service' do+ let(:call_attributes) do+ {+ type: "work_packages_table",+ query_id: 5+ }+ end+ end+end