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
[chore] Fixed response for errors in project storage collection filters
Eric Schubertfb0841dd7deb
diff --git a/modules/storages/app/models/queries/storages/project_storages/filter/project_id_filter.rb b/modules/storages/app/models/queries/storages/project_storages/filter/project_id_filter.rbindex dc68fe2e7e4..61ff2b1393d 100644--- a/modules/storages/app/models/queries/storages/project_storages/filter/project_id_filter.rb+++ b/modules/storages/app/models/queries/storages/project_storages/filter/project_id_filter.rb@@ -34,6 +34,10 @@ module Queries::Storages::ProjectStorages::Filter:listend+ def human_name+ ::Storages::ProjectStorage.human_attribute_name(name)+ end+def allowed_valuesStorages::ProjectStorage.pluck(:project_id).map { |id| [id, id.to_s] }enddiff --git a/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb b/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rbindex 0f0955631c8..ffd63899341 100644--- a/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb+++ b/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb@@ -34,6 +34,10 @@ module Queries::Storages::ProjectStorages::Filter:listend+ def human_name+ ::Storages::ProjectStorage.human_attribute_name(name)+ end+def allowed_valuesStorages::ProjectStorage.pluck(:storage_id).map { |id| [id, id.to_s] }enddiff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rbindex 9ec106c6ef9..91c6827378c 100644--- a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb@@ -43,7 +43,7 @@ class API::V3::FileLinks::WorkPackagesFileLinksAPI < API::OpenProjectAPI.call(params)unless query.valid?- message = I18n.t('api_v3.errors.missing_or_malformed_parameter')+ message = I18n.t('api_v3.errors.missing_or_malformed_parameter', parameter: 'filters')raise ::API::Errors::InvalidQuery.new(message)enddiff --git a/modules/storages/lib/api/v3/project_storages/project_storages_api.rb b/modules/storages/lib/api/v3/project_storages/project_storages_api.rbindex 1c48de45e87..a96c2e814f8 100644--- a/modules/storages/lib/api/v3/project_storages/project_storages_api.rb+++ b/modules/storages/lib/api/v3/project_storages/project_storages_api.rb@@ -39,7 +39,7 @@ module API::V3::ProjectStorages.call(params)unless query.valid?- message = I18n.t('api_v3.errors.missing_or_malformed_parameter')+ message = I18n.t('api_v3.errors.missing_or_malformed_parameter', parameter: 'filters')raise ::API::Errors::InvalidQuery.new(message)end@@ -55,6 +55,10 @@ module API::V3::ProjectStoragesroute_param :id, type: Integer, desc: 'ProjectStorage id' doafter_validation do@project_storage = Storages::ProjectStorage.find(params[:id])++ unless current_user.allowed_to?(:view_file_links, @project_storage.project)+ raise ::API::Errors::NotFound.new+ endendget &API::V3::Utilities::Endpoints::Show.new(model: Storages::ProjectStorage).mountdiff --git a/modules/storages/spec/requests/api/v3/project_storages/project_storages_spec.rb b/modules/storages/spec/requests/api/v3/project_storages/project_storages_spec.rbnew file mode 100644index 00000000000..65f49bd54e7--- /dev/null+++ b/modules/storages/spec/requests/api/v3/project_storages/project_storages_spec.rb@@ -0,0 +1,191 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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_module_spec_helper++describe 'API v3 project storages resource', content_type: :json, webmock: true do+ include API::V3::Utilities::PathHelper++ let(:view_permissions) { %i(view_work_packages view_file_links) }++ # rubocop:disable RSpec/IndexedLet+ let(:project1) { create(:project) }+ let(:project2) { create(:project) }+ let(:project3) { create(:project) }+ let(:storage1) { create(:storage) }+ let(:storage2) { create(:storage) }+ let(:storage3) { create(:storage) }+ let!(:project_storage11) { create(:project_storage, project: project1, storage: storage1) }+ let!(:project_storage12) { create(:project_storage, project: project1, storage: storage2) }+ let!(:project_storage13) { create(:project_storage, project: project1, storage: storage3) }+ let!(:project_storage23) { create(:project_storage, project: project2, storage: storage3) }+ let!(:project_storage21) { create(:project_storage, project: project2, storage: storage1) }+ let!(:project_storage31) { create(:project_storage, project: project3, storage: storage1) }+ # rubocop:enable RSpec/IndexedLet++ subject(:last_response) do+ get path+ end++ before do+ login_as current_user+ end++ describe 'GET /api/v3/project_storages' do+ let(:path) { api_v3_paths.project_storages }++ context 'as admin' do+ let(:current_user) { create(:admin) }++ subject { last_response.body }++ describe 'gets full project storage collection' do+ it_behaves_like 'API V3 collection response', 6, 6, 'ProjectStorage', 'Collection' do+ let(:elements) do+ [+ project_storage31,+ project_storage21,+ project_storage23,+ project_storage13,+ project_storage12,+ project_storage11+ ]+ end+ end+ end++ context 'with project filter' do+ let(:filters) { [{ projectId: { operator: "=", values: [project_id] } }] }+ let(:path) { api_v3_paths.path_for(:project_storages, filters:) }++ describe 'gets all project storages of the filtered project' do+ let(:project_id) { project2.id }++ it_behaves_like 'API V3 collection response', 2, 2, 'ProjectStorage', 'Collection' do+ let(:elements) { [project_storage21, project_storage23] }+ end+ end++ context 'with invalid project id' do+ let(:project_id) { '1337' }++ it_behaves_like 'invalid filters'+ end+ end++ context 'with storage filter' do+ let(:filters) { [{ storageId: { operator: "=", values: [storage_id] } }] }+ let(:path) { api_v3_paths.path_for(:project_storages, filters:) }++ describe 'gets all project storages of the filtered project' do+ let(:storage_id) { storage3.id }++ it_behaves_like 'API V3 collection response', 2, 2, 'ProjectStorage', 'Collection' do+ let(:elements) { [project_storage23, project_storage13] }+ end+ end++ context 'with invalid storage id' do+ let(:storage_id) { '1337' }++ it_behaves_like 'invalid filters'+ end+ end+ end++ context 'as user with permissions' do+ let(:current_user) do+ create(:user, member_in_projects: [project1, project3], member_with_permissions: view_permissions)+ end++ it_behaves_like 'API V3 collection response', 4, 4, 'ProjectStorage', 'Collection' do+ let(:elements) do+ [+ project_storage31,+ project_storage13,+ project_storage12,+ project_storage11+ ]+ end+ end+ end++ context 'as user without permissions' do+ let(:current_user) do+ create(:user, member_in_projects: [project1, project2, project3], member_with_permissions: [])+ end++ it_behaves_like 'API V3 collection response', 0, 0, 'ProjectStorage', 'Collection' do+ let(:elements) { [] }+ end+ end+ end++ describe 'GET /api/v3/project_storages/:id' do+ let(:project_storage) do+ create(:project_storage,+ project: project3,+ storage: storage3,+ project_folder_id: '1337',+ project_folder_mode: 'manual')+ end+ let(:project_storage_id) { project_storage.id }+ let(:path) { api_v3_paths.project_storage(project_storage_id) }+ let(:current_user) do+ create(:user, member_in_project: project3, member_with_permissions: view_permissions)+ end++ subject { last_response.body }++ it_behaves_like 'successful response'++ it { is_expected.to be_json_eql('1337'.to_json).at_path('projectFolderId') }+ it { is_expected.to be_json_eql('manual'.to_json).at_path('projectFolderMode') }++ context 'if user has permission to see file storages in project' do+ let(:current_user) do+ create(:user, member_in_project: project3, member_with_permissions: [])+ end++ it_behaves_like 'not found'+ end++ context 'if user is not member in related project' do+ let(:project_storage_id) { project_storage11.id }++ it_behaves_like 'not found'+ end++ context 'if project storage does not exists' do+ let(:project_storage_id) { '1337' }++ it_behaves_like 'not found'+ end+ end+enddiff --git a/spec/requests/api/v3/support/response_examples.rb b/spec/requests/api/v3/support/response_examples.rbindex 975d8a9160f..5fb1d01d9fa 100644--- a/spec/requests/api/v3/support/response_examples.rb+++ b/spec/requests/api/v3/support/response_examples.rb@@ -118,6 +118,13 @@ shared_examples_for 'parse error' do |details|endend+shared_examples_for 'invalid filters' do+ it_behaves_like 'error response',+ 400,+ 'InvalidQuery',+ I18n.t('api_v3.errors.missing_or_malformed_parameter', parameter: 'filters')+end+shared_examples_for 'unauthenticated access' doit_behaves_like 'error response',401,