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
show and index actions for views
ulferts60dc2750b1f0
diff --git a/app/models/queries/queries/query_query.rb b/app/models/queries/queries/query_query.rbindex 8286cb58cae..51cbc9cb4e4 100644--- a/app/models/queries/queries/query_query.rb+++ b/app/models/queries/queries/query_query.rb@@ -32,6 +32,6 @@ class Queries::Queries::QueryQuery < Queries::BaseQueryenddef default_scope- Query.visible(to: user)+ Query.visible(user)endenddiff --git a/app/models/queries/scopes/visible.rb b/app/models/queries/scopes/visible.rbnew file mode 100644index 00000000000..285901fdee2--- /dev/null+++ b/app/models/queries/scopes/visible.rb@@ -0,0 +1,55 @@+#-- 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 Queries::Scopes+ module Visible+ extend ActiveSupport::Concern++ class_methods do+ # Return queries visible to a user:+ # * the user is the query user and the user has the :view_work_packages permission in the project OR+ # * the user is the query user and the user has the :view_work_packages permission in any project+ # and the query is global (no project) OR+ # * the user is not the query user and the user has the :view_work_packages permission in the project+ # and the query is public OR+ # * the user is not the query user and the user has the :view_work_packages permission in any project+ # and the query is public and the query is global (no project)+ def visible(user)+ scope = where(user_id: user.id)+ .or(where(is_public: true))+ .where(project: Project.allowed_to(user, :view_work_packages))++ if user.allowed_to_globally?(:view_work_packages)+ scope.or(where(project: nil))+ else+ scope+ end+ end+ end+ end+enddiff --git a/app/models/queries/views.rb b/app/models/queries/views.rbnew file mode 100644index 00000000000..e4deeab74fd--- /dev/null+++ b/app/models/queries/views.rb@@ -0,0 +1,39 @@+#-- 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 Queries::Views+ [Queries::Views::Filters::ProjectFilter].each do |filter|+ Queries::Register.filter Queries::Views::ViewQuery,+ filter+ end++ [Queries::Views::Orders::DefaultOrder].each do |order|+ Queries::Register.order Queries::Views::ViewQuery,+ order+ end+enddiff --git a/app/models/queries/views/filters/project_filter.rb b/app/models/queries/views/filters/project_filter.rbnew file mode 100644index 00000000000..447d7ed9d2d--- /dev/null+++ b/app/models/queries/views/filters/project_filter.rb@@ -0,0 +1,40 @@+#-- 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.+#++++# Using the associated queries' project relation to filter+class Queries::Views::Filters::ProjectFilter < Queries::Views::Filters::ViewFilter+ include Queries::Filters::Shared::ProjectFilter++ def left_outer_joins+ [:query]+ end++ def where+ operator_strategy.sql_for_field(values, Query.table_name, self.class.key)+ end+enddiff --git a/app/models/queries/views/filters/view_filter.rb b/app/models/queries/views/filters/view_filter.rbnew file mode 100644index 00000000000..5118d7d8a57--- /dev/null+++ b/app/models/queries/views/filters/view_filter.rb@@ -0,0 +1,35 @@+#-- 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 Queries::Views::Filters::ViewFilter < Queries::Filters::Base+ self.model = View++ def human_name+ View.human_attribute_name(name)+ end+enddiff --git a/app/models/queries/views/orders/default_order.rb b/app/models/queries/views/orders/default_order.rbnew file mode 100644index 00000000000..324f0fc403f--- /dev/null+++ b/app/models/queries/views/orders/default_order.rb@@ -0,0 +1,35 @@+#-- encoding: UTF-8++#-- 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 Queries::Views::Orders::DefaultOrder < Queries::Orders::Base+ def self.key+ /\A(id)\z/+ end+enddiff --git a/app/models/queries/views/view_query.rb b/app/models/queries/views/view_query.rbnew file mode 100644index 00000000000..ad29bc5d033--- /dev/null+++ b/app/models/queries/views/view_query.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.+#++++class Queries::Views::ViewQuery < Queries::BaseQuery+ def default_scope+ super.where(query_id: Query.visible(User.current))+ end+enddiff --git a/app/models/query.rb b/app/models/query.rbindex 750af1c5a3b..5b53916a072 100644--- a/app/models/query.rb+++ b/app/models/query.rb@@ -39,6 +39,9 @@ class Query < ApplicationRecordhas_one :query_menu_item, -> { order('name') },class_name: 'MenuItems::QueryMenuItem',dependent: :delete, foreign_key: 'navigatable_id'+ has_many :views,+ dependent: :destroy+serialize :filters, Queries::WorkPackages::FilterSerializerserialize :column_names, Arrayserialize :sort_criteria, Array@@ -52,16 +55,8 @@ class Query < ApplicationRecordvalidate :validate_group_byvalidate :validate_show_hierarchies- scope(:visible, ->(to:) do- # User can see public queries and his own queries- scope = where(is_public: true)-- if to.logged?- scope.or(where(user_id: to.id))- else- scope- end- end)+ include Scopes::Scoped+ scopes :visiblescope(:global, -> { where(project_id: nil) })diff --git a/app/models/view.rb b/app/models/view.rbnew file mode 100644index 00000000000..b14ad079a25--- /dev/null+++ b/app/models/view.rb@@ -0,0 +1,34 @@+#-- 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 View < ApplicationRecord+ # disable STI+ self.inheritance_column = :_type_disabled++ belongs_to :query+enddiff --git a/db/migrate/20211116155545_add_column_projections_to_queries.rb b/db/migrate/20211116155545_add_column_projections_to_queries.rbdeleted file mode 100644index b5f58639476..00000000000--- a/db/migrate/20211116155545_add_column_projections_to_queries.rb+++ /dev/null@@ -1,5 +0,0 @@-class AddColumnProjectionsToQueries < ActiveRecord::Migration[6.1]- def change- add_column :queries, :projections, :jsonb, default: {}- end-enddiff --git a/db/migrate/20211130161501_add_views.rb b/db/migrate/20211130161501_add_views.rbnew file mode 100644index 00000000000..d8e9759056d--- /dev/null+++ b/db/migrate/20211130161501_add_views.rb@@ -0,0 +1,11 @@+class AddViews < ActiveRecord::Migration[6.1]+ def change+ create_table :views do |t|+ t.references :query, null: false, foreign_key: true, index: true+ t.jsonb :options, default: {}+ t.string :type++ t.timestamps+ end+ end+enddiff --git a/lib/api/v3/queries/query_representer.rb b/lib/api/v3/queries/query_representer.rbindex c71e32b730d..bdd1db69cab 100644--- a/lib/api/v3/queries/query_representer.rb+++ b/lib/api/v3/queries/query_representer.rb@@ -307,15 +307,6 @@ module APIproperty :highlighting_mode,render_nil: false- # Projections- property :projections,- getter: ->(*) {- represented.projections.map do |k, v|- ::API::V3::Queries::Projections::QueryProjectionRepresenter.new(v.merge(type: k), current_user: current_user)- end- },- exec_context: :decorator-attr_accessor :results,:paramsdiff --git a/lib/api/v3/root.rb b/lib/api/v3/root.rbindex e32a4524129..e0f9df5f167 100644--- a/lib/api/v3/root.rb+++ b/lib/api/v3/root.rb@@ -73,6 +73,7 @@ module APImount ::API::V3::UserPreferences::UserPreferencesAPImount ::API::V3::Groups::GroupsAPImount ::API::V3::Versions::VersionsAPI+ mount ::API::V3::Views::ViewsAPImount ::API::V3::WorkPackages::WorkPackagesAPImount ::API::V3::WikiPages::WikiPagesAPImount ::API::V3::Grids::GridsAPIdiff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex 89a8f3cef0c..65544f57687 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -437,6 +437,9 @@ module APIresources :version+ index :view+ show :view+def self.versions_available_projects"#{versions}/available_projects"enddiff --git a/lib/api/v3/views/view_collection_representer.rb b/lib/api/v3/views/view_collection_representer.rbnew file mode 100644index 00000000000..ebe7c27d468--- /dev/null+++ b/lib/api/v3/views/view_collection_representer.rb@@ -0,0 +1,35 @@+#-- 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 ViewCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection; end+ end+ end+enddiff --git a/lib/api/v3/views/view_representer.rb b/lib/api/v3/views/view_representer.rbnew file mode 100644index 00000000000..deae26c888e--- /dev/null+++ b/lib/api/v3/views/view_representer.rb@@ -0,0 +1,91 @@+#-- 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 ViewRepresenter < ::API::Decorators::Single+ include API::Decorators::DateProperty+ include API::Decorators::LinkedResource++ self_link path: :view, title_getter: ->(*) {}++ associated_resource :query+ resource :project,+ getter: ->(*) {+ next unless represented.query.project++ ::API::V3::Projects::ProjectRepresenter+ .create(represented.query.project, current_user: current_user, embed_links: embed_links)+ },+ link: ->(*) {+ if represented.query.project+ {+ href: api_v3_paths.project(represented.query.project.id),+ title: represented.query.project.name+ }+ else+ {+ href: nil,+ title: nil+ }+ end+ },+ setter: ->(fragment:, represented:, **) {+ link = ::API::Decorators::LinkObject.new(represented,+ path: :project,+ property_name: :project,+ getter: :project_id,+ setter: :'project_id=')++ link.from_hash(fragment)+ }++ property :id++ property :public,+ getter: ->(*) {+ query.is_public+ }++ property :starred,+ getter: ->(*) {+ query.starred+ }++ date_time_property :created_at++ date_time_property :updated_at++ def _type+ 'Views::Table'+ end+ end+ end+ end+enddiff --git a/lib/api/v3/views/views_api.rb b/lib/api/v3/views/views_api.rbnew file mode 100644index 00000000000..0eaba97f79e--- /dev/null+++ b/lib/api/v3/views/views_api.rb@@ -0,0 +1,54 @@+#-- 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 ViewsAPI < ::API::OpenProjectAPI+ resources :views do+ get &::API::V3::Utilities::Endpoints::Index+ .new(model: View)+ .mount++ route_param :id, type: Integer, desc: 'View ID' do+ after_validation do+ @view = ::Queries::Views::ViewQuery+ .new(user: current_user)+ .results+ .find(params['id'])+ end++ get &::API::V3::Utilities::Endpoints::Show+ .new(model: View)+ .mount+ end+ end+ end+ end+ end+enddiff --git a/spec/factories/view_factory.rb b/spec/factories/view_factory.rbnew file mode 100644index 00000000000..21072180da1--- /dev/null+++ b/spec/factories/view_factory.rb@@ -0,0 +1,36 @@+#-- 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.+#++++FactoryBot.define do+ factory :view do+ type { 'table' }+ query+ end++ factory :view_table, parent: :view+enddiff --git a/spec/lib/api/v3/queries/query_representer_generation_spec.rb b/spec/lib/api/v3/queries/query_representer_rendering_spec.rbsimilarity index 97%rename from spec/lib/api/v3/queries/query_representer_generation_spec.rbrename to spec/lib/api/v3/queries/query_representer_rendering_spec.rbindex 607db22a371..30576c4bc91 100644--- a/spec/lib/api/v3/queries/query_representer_generation_spec.rb+++ b/spec/lib/api/v3/queries/query_representer_rendering_spec.rb@@ -31,13 +31,7 @@ require 'spec_helper'describe ::API::V3::Queries::QueryRepresenter doinclude ::API::V3::Utilities::PathHelper- let(:query) { FactoryBot.build_stubbed(:query, project: project, projections: projections) }- let(:projections) do- {- calendar: {},- team_calendar: {}- }- end+ let(:query) { FactoryBot.build_stubbed(:query, project: project) }let(:unpersisted_query) { FactoryBot.build(:query, project: project, user: other_user) }let(:project) { FactoryBot.build_stubbed(:project) }let(:user) { double('current_user', allowed_to_globally?: true, allowed_to?: true, admin: true, admin?: true, active?: true) }@@ -609,31 +603,6 @@ describe ::API::V3::Queries::QueryRepresenter doendend- describe 'projections' do- it_behaves_like 'property', :projections do- let(:value) do- [- {- "_type": "QueryProjections::Calendar",- "_links": {- "schema": {- "href": api_v3_paths.query_projection_calendar_schema- }- }- },- {- "_type": "QueryProjections::TeamCalendar",- "_links": {- "schema": {- "href": api_v3_paths.query_projection_team_calendar_schema- }- }- },- ]- end- end- end-describe 'highlighting' docontext 'with EE', with_ee: %i[conditional_highlighting] doit 'renders when the value is set' dodiff --git a/spec/lib/api/v3/utilities/path_helper_spec.rb b/spec/lib/api/v3/utilities/path_helper_spec.rbindex ccfed5d258c..4f43225a9d1 100644--- a/spec/lib/api/v3/utilities/path_helper_spec.rb+++ b/spec/lib/api/v3/utilities/path_helper_spec.rb@@ -529,6 +529,11 @@ describe ::API::V3::Utilities::PathHelper doendend+ describe 'view paths' do+ it_behaves_like 'show', :view+ it_behaves_like 'index', :view+ end+describe 'wiki pages paths' doit_behaves_like 'show', :wiki_pageenddiff --git a/spec/lib/api/v3/views/view_representer_rendering_spec.rb b/spec/lib/api/v3/views/view_representer_rendering_spec.rbnew file mode 100644index 00000000000..275f924ac94--- /dev/null+++ b/spec/lib/api/v3/views/view_representer_rendering_spec.rb@@ -0,0 +1,131 @@+#-- 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'++describe ::API::V3::Views::ViewRepresenter, 'rendering' do+ include ::API::V3::Utilities::PathHelper++ subject(: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(:current_user) { FactoryBot.build_stubbed(:user) }+ let(:query_public) { true }+ # aka starred+ let(:query_menu_item) { FactoryBot.build_stubbed(:query_menu_item) }++ let(:embed_links) { false }++ let(:representer) do+ described_class.create view,+ current_user: current_user,+ embed_links: embed_links+ end++ describe 'properties' do+ describe '_type' do+ it_behaves_like 'property', :_type do+ let(:value) { 'Views::Table' }+ end+ end++ describe 'id' do+ it_behaves_like 'property', :id do+ let(:value) { view.id }+ end+ end++ describe 'public' do+ context 'with the query being public' do+ it_behaves_like 'property', :public do+ let(:value) { true }+ end+ end++ context 'with the query being private' do+ let(:query_public) { false }++ it_behaves_like 'property', :public do+ let(:value) { false }+ end+ end+ end++ describe 'starred' do+ context 'with the query being starred' do+ it_behaves_like 'property', :starred do+ let(:value) { true }+ end+ end++ context 'without the query being starred' do+ let(:query_menu_item) { nil }++ it_behaves_like 'property', :starred do+ let(:value) { false }+ end+ end+ end++ describe 'timestamps' do+ it_behaves_like 'datetime property', :createdAt do+ let(:value) { view.created_at }+ end++ it_behaves_like 'datetime property', :updatedAt do+ let(:value) { view.updated_at }+ end+ end+ end++ describe '_links' do+ describe 'self' do+ it_behaves_like 'has an untitled link' do+ let(:link) { 'self' }+ let(:href) { api_v3_paths.view view.id }+ end+ end++ describe 'query' do+ it_behaves_like 'has a titled link' do+ let(:link) { 'query' }+ let(:href) { api_v3_paths.query query.id }+ let(:title) { query.name }+ end+ end++ describe 'project' do+ it_behaves_like 'has a titled link' do+ let(:link) { 'project' }+ let(:href) { api_v3_paths.project query.project_id }+ let(:title) { query.project.name }+ end+ end+ end+enddiff --git a/spec/models/queries/queries/query_query_spec.rb b/spec/models/queries/queries/query_query_spec.rbindex 2cbed4decbc..c3933b3da57 100644--- a/spec/models/queries/queries/query_query_spec.rb+++ b/spec/models/queries/queries/query_query_spec.rb@@ -31,7 +31,7 @@ require 'spec_helper'describe Queries::Queries::QueryQuery, type: :model dolet(:user) { FactoryBot.build_stubbed(:user) }let(:instance) { described_class.new(user: user) }- let(:base_scope) { Query.visible(to: user).order(id: :desc) }+ let(:base_scope) { Query.visible(user).order(id: :desc) }context 'without a filter' dodescribe '#results' dodiff --git a/spec/models/query/scopes/visible_spec.rb b/spec/models/query/scopes/visible_spec.rbnew file mode 100644index 00000000000..de3d2f16760--- /dev/null+++ b/spec/models/query/scopes/visible_spec.rb@@ -0,0 +1,95 @@+#-- 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'++describe Queries::Scopes::Visible, type: :model do+ describe '.visible' do+ subject(:scope) { ::Query.visible(user) }++ let(:user) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_with_permissions: permissions)+ end+ let(:permissions) { %i[view_work_packages] }+ let!(:private_user_query) do+ FactoryBot.create(:query,+ project: project,+ user: user)+ end+ let!(:private_other_user_query) do+ FactoryBot.create(:query,+ project: project)+ end+ let!(:private_user_query_lacking_permissions) do+ FactoryBot.create(:query,+ project: FactoryBot.create(:project,+ members: { user => FactoryBot.create(:role, permissions: []) }),+ user: user)+ end+ let!(:public_query) do+ FactoryBot.create(:query,+ project: project,+ is_public: true)+ end+ let!(:public_query_lacking_permissions) do+ FactoryBot.create(:query,+ project: FactoryBot.create(:project,+ members: { user => FactoryBot.create(:role, permissions: []) }),+ is_public: true)+ end+ let!(:global_user_query) do+ FactoryBot.create(:query,+ project: nil,+ user: user)+ end+ let!(:global_other_user_query) do+ FactoryBot.create(:query,+ project: nil)+ end+ let(:project) { FactoryBot.create(:project) }+ let(:public_project) { FactoryBot.create(:public_project) }++ context 'with the user having the :view_work_packages permission' do+ it 'returns the queries that are public or that are the user`s' do+ expect(scope)+ .to match_array([private_user_query, public_query, global_user_query, global_other_user_query])+ end+ end++ context 'without the user having the :view_work_packages permission' do+ let(:permissions) { [] }++ it 'is empty' do+ expect(scope)+ .to be_empty+ end+ end+ end+enddiff --git a/spec/requests/api/v3/views/index_resource_spec.rb b/spec/requests/api/v3/views/index_resource_spec.rbnew file mode 100644index 00000000000..e8d2092e830--- /dev/null+++ b/spec/requests/api/v3/views/index_resource_spec.rb@@ -0,0 +1,150 @@+#-- 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'+require 'rack/test'++describe ::API::V3::Views::ViewsAPI,+ 'index',+ type: :request,+ content_type: :json do++ include API::V3::Utilities::PathHelper++ shared_let(:permitted_user) { FactoryBot.create(:user) }+ shared_let(:role) do+ FactoryBot.create(:role, permissions: [:view_work_packages])+ end+ shared_let(:project) do+ FactoryBot.create(:project,+ members: { permitted_user => role })+ end+ shared_let(:user_private_project_query) do+ FactoryBot.create(:query,+ user: permitted_user,+ project: project,+ is_public: false)+ end+ shared_let(:user_private_project_view) do+ FactoryBot.create(:view_table,+ query: user_private_project_query)+ end+ shared_let(:other_user_private_project_query) do+ FactoryBot.create(:query,+ project: project,+ is_public: false)+ end+ shared_let(:other_user_private_project_view) do+ FactoryBot.create(:view_table,+ query: other_user_private_project_query)+ end+ shared_let(:user_public_project_query) do+ FactoryBot.create(:query,+ project: project,+ is_public: true)+ end+ shared_let(:user_public_project_view) do+ FactoryBot.create(:view_table,+ query: user_public_project_query)+ end+ shared_let(:other_project) do+ FactoryBot.create(:project,+ members: { permitted_user => role })+ end+ shared_let(:user_private_other_project_query) do+ FactoryBot.create(:query,+ user: permitted_user,+ project: other_project)+ end+ shared_let(:user_private_other_project_view) do+ FactoryBot.create(:view_table,+ query: user_private_other_project_query)+ end++ let(:views) do+ [user_private_project_view,+ user_private_other_project_view,+ user_public_project_view,+ other_user_private_project_view]+ end++ let(:filters) { nil }++ let(:send_request) do+ get api_v3_paths.path_for :views, filters: filters+ end++ let(:parsed_response) { JSON.parse(last_response.body) }++ current_user { permitted_user }++ before do+ views++ send_request+ end++ context 'without any filter' do+ it_behaves_like 'API V3 collection response', 3, 3, 'Views::Table' do+ let(:elements) do+ [+ user_private_other_project_view,+ user_public_project_view,+ user_private_project_view+ ]+ end+ end+ end++ context 'with a project filter' do+ let(:filters) do+ [+ {+ 'project' => {+ 'operator' => '=',+ 'values' => [project.id.to_s]+ }+ }+ ]+ end++ it_behaves_like 'API V3 collection response', 2, 2, 'Views::Table' do+ let(:elements) do+ [+ user_public_project_view,+ user_private_project_view+ ]+ end+ end+ end++ context 'for a user without any visible queries' do+ current_user { FactoryBot.create(:user) }++ it_behaves_like 'API V3 collection response', 0, 0, 'Views::Table'+ end+enddiff --git a/spec/requests/api/v3/views/show_resource_spec.rb b/spec/requests/api/v3/views/show_resource_spec.rbnew file mode 100644index 00000000000..573daee1ab2--- /dev/null+++ b/spec/requests/api/v3/views/show_resource_spec.rb@@ -0,0 +1,95 @@+#-- encoding: UTF-8++#-- 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,+ 'show',+ 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]) }+ 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+ shared_let(:view) do+ FactoryBot.create :view_table,+ query: private_user_query+ end++ let(:send_request) do+ get api_v3_paths.view(view.id)+ end++ current_user { permitted_user }++ subject(:response) { last_response }++ before do+ send_request+ end++ context 'with a user allowed to see the query' do+ it 'returns 200 OK' do+ expect(response.status)+ .to eq(200)+ end++ it 'returns the view' do+ expect(response.body)+ .to be_json_eql('Views::Table'.to_json)+ .at_path('_type')++ expect(response.body)+ .to be_json_eql(view.id.to_json)+ .at_path('id')+ end+ end++ context 'with a user not allowed to see the query' do+ current_user do+ FactoryBot.create(:user,+ member_in_project: project,+ member_through_role: role)+ end++ it 'returns a 404 response' do+ expect(last_response.status).to eq(404)+ end+ end+end