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

[#50234] added GET /api/v3/storages collection endpoint

Eric Schubert65cc9f8c761f
diff --git a/modules/storages/app/models/queries/storages/storages/storage_query.rb b/modules/storages/app/models/queries/storages/storages/storage_query.rb
new file mode 100644
index 00000000000..6a81e0466ac
--- /dev/null
+++ b/modules/storages/app/models/queries/storages/storages/storage_query.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+module Queries::Storages::Storages
+ class StorageQuery < Queries::BaseQuery
+ class << self
+ def model
+ @model ||= ::Storages::Storage
+ end
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/storages/storage_collection_representer.rb b/modules/storages/lib/api/v3/storages/storage_collection_representer.rb
new file mode 100644
index 00000000000..99d75b39cd4
--- /dev/null
+++ b/modules/storages/lib/api/v3/storages/storage_collection_representer.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+module API
+ module V3
+ module Storages
+ class StorageCollectionRepresenter < ::API::Decorators::UnpaginatedCollection
+ end
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rb
index 2812686937a..f4877fc26b2 100644
--- a/modules/storages/lib/api/v3/storages/storage_representer.rb
+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb
@@ -182,13 +182,15 @@ module API::V3::Storages
associated_resource :oauth_application,
skip_render: ->(*) { !represent_oauth_application? },
getter: ->(*) {
- next unless represent_oauth_application?
+ next unless represent_oauth_application? && represented.oauth_application.present?
::API::V3::OAuth::OAuthApplicationsRepresenter.create(represented.oauth_application, current_user:)
},
link: ->(*) {
next unless represent_oauth_application?
+ return { href: nil } if represented.oauth_application.blank?
+
{
href: api_v3_paths.oauth_application(represented.oauth_application.id),
title: represented.oauth_application.name
diff --git a/modules/storages/lib/api/v3/storages/storages_api.rb b/modules/storages/lib/api/v3/storages/storages_api.rb
index 9bb78f58909..80e67610096 100644
--- a/modules/storages/lib/api/v3/storages/storages_api.rb
+++ b/modules/storages/lib/api/v3/storages/storages_api.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
@@ -32,6 +34,8 @@ class API::V3::Storages::StoragesAPI < API::OpenProjectAPI
resources :storages do
post &API::V3::Utilities::Endpoints::Create.new(model: Storages::Storage).mount
+ get &API::V3::Utilities::Endpoints::Index.new(model: Storages::Storage, scope: -> { visible_storages }).mount
+
route_param :storage_id, type: Integer, desc: 'Storage id' do
after_validation do
@storage = visible_storages.find(params[:storage_id])
diff --git a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb
index 980cbfc80b6..3c4bf50f900 100644
--- a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb
@@ -143,6 +143,15 @@ RSpec.describe API::V3::Storages::StorageRepresenter, 'rendering' do
let(:href) { "/api/v3/oauth_applications/#{oauth_application.id}" }
let(:title) { oauth_application.name }
end
+
+ context 'with invalid configured storage with missing oauth application' do
+ let(:oauth_application) { nil }
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'oauthApplication' }
+ let(:href) { nil }
+ end
+ end
end
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb
index 0342ef64292..ac1e7406340 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb
@@ -29,7 +29,7 @@
require 'spec_helper'
require_module_spec_helper
-RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true do
+RSpec.describe 'API v3 storages resource', :webmock, content_type: :json do
include API::V3::Utilities::PathHelper
include StorageServerHelpers
@@ -75,6 +75,37 @@ RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true do
end
end
+ describe 'GET /api/v3/storages' do
+ let(:path) { api_v3_paths.storages }
+ let!(:another_storage) { create(:nextcloud_storage) }
+
+ subject(:last_response) do
+ get path
+ end
+
+ context 'as admin' do
+ let(:current_user) { create(:admin) }
+
+ describe 'gets the storage collection and returns it' do
+ subject { last_response.body }
+
+ it_behaves_like 'API V3 collection response', 2, 2, 'Storage', 'Collection' do
+ let(:elements) { [another_storage, storage] }
+ end
+ end
+ end
+
+ context 'as non-admin' do
+ describe 'gets the storage collection of storages linked to visible projects with correct permissions' do
+ subject { last_response.body }
+
+ it_behaves_like 'API V3 collection response', 1, 1, 'Storage', 'Collection' do
+ let(:elements) { [storage] }
+ end
+ end
+ end
+ end
+
describe 'POST /api/v3/storages' do
let(:path) { api_v3_paths.storages }
let(:host) { 'https://example.nextcloud.local' }