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

Added first spec for storage file query

Andreas Pfohl2082bee84f18
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index aa0422df0d1..76085ccc1f3 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -168,6 +168,13 @@ module OAuthClients
yield_service_result
end
+ def with_refreshed_token(&)
+ token = get_existing_token
+ return ServiceResult.failure(result: :not_authorized) if token.blank?
+
+ request_with_token_refresh(token, &)
+ end
+
private
# Check if a OAuthClientToken already exists and return nil otherwise.
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index 834d99eb3f9..bf69ff54a9b 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -169,6 +169,14 @@ class ServiceResult
self
end
+ def match(on_success:, on_failure:)
+ if success?
+ on_success.call(result)
+ else
+ on_failure.call
+ end
+ end
+
def each
yield result if success?
self
diff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
index fecf679a745..7863a1bfb5f 100644
--- a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
+++ b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
@@ -1,23 +1,24 @@
module API::V3::Utilities::StorageInteraction
class NextcloudStorageQuery
- def initialize(base_uri:, token:, token_refresh:)
+ def initialize(base_uri:, origin_user_id:, token:, with_refreshed_token:)
@uri = base_uri
+ @origin_user_id = origin_user_id
@token = token
- @token_refresh = token_refresh
- @base_path = "/remote.php/dav/files/#{@token.send('origin_user_id')}/"
+ @with_refreshed_token = with_refreshed_token
+ @base_path = "/remote.php/dav/files/#{@origin_user_id}/"
end
def files
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = @uri.scheme == 'https'
- result = @token_refresh.call do
+ result = @with_refreshed_token.call do
response = http.propfind(
@base_path,
requested_properties,
{
'Depth' => '1',
- 'Authorization' => "Bearer #{@token.access_token}"
+ 'Authorization' => "Bearer #{@token}"
}
)
diff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb
index 4b0554dc0c0..d72f693124f 100644
--- a/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb
+++ b/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb
@@ -11,16 +11,18 @@ module API::V3::Utilities::StorageInteraction
case @provider_type
when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
connection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client: @oauth_client)
- token = connection_manager.get_access_token.result
-
- ::API::V3::Utilities::StorageInteraction::NextcloudStorageQuery.new(
- base_uri: @uri,
- token:,
- token_refresh: ->(&block) {
- connection_manager.request_with_token_refresh(token) do
- block.call
- end
- }
+ connection_manager.get_access_token.match(
+ on_success: ->(token) do
+ ::API::V3::Utilities::StorageInteraction::NextcloudStorageQuery.new(
+ base_uri: @uri,
+ origin_user_id: token.send(:origin_user_id),
+ token: token.send(:access_token),
+ with_refreshed_token: connection_manager.method(:with_refreshed_token).to_proc
+ )
+ end,
+ on_failure: -> do
+ ServiceResult.failure(result: :not_authorized)
+ end
)
else
raise ArgumentError
diff --git a/modules/storages/lib/api/v3/utilities/storage_requests.rb b/modules/storages/lib/api/v3/utilities/storage_requests.rb
index 8c1275f0ba9..36b25213c64 100644
--- a/modules/storages/lib/api/v3/utilities/storage_requests.rb
+++ b/modules/storages/lib/api/v3/utilities/storage_requests.rb
@@ -32,6 +32,7 @@ class API::V3::Utilities::StorageRequests
@oauth_client = storage.oauth_client
end
+ # The download_command is actually a query and should be refactored
def download_command
->(access_token:, file_id:) do
request_url = File.join(@oauth_client.integration.host, '/ocs/v2.php/apps/dav/api/v1/direct')
@@ -55,17 +56,15 @@ class API::V3::Utilities::StorageRequests
end
def files_query(user:)
- query = ::API::V3::Utilities::StorageInteraction::StorageQueries
- .new(
- uri: URI(@storage.host),
- provider_type: @storage.provider_type,
- user:,
- oauth_client: @oauth_client
- )
- .files_query
-
- -> do
- query.files
- end
+ ::API::V3::Utilities::StorageInteraction::StorageQueries
+ .new(
+ uri: URI(@storage.host),
+ provider_type: @storage.provider_type,
+ user:,
+ oauth_client: @oauth_client
+ )
+ .files_query
+ .method(:files)
+ .to_proc
end
end
diff --git a/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb b/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb
new file mode 100644
index 00000000000..3badd03cda6
--- /dev/null
+++ b/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb
@@ -0,0 +1,126 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe API::V3::Utilities::StorageRequests, webmock: true do
+ let(:user) { build_stubbed(:user) }
+
+ let(:url) { 'https://example.com' }
+ let(:origin_user_id) { 'admin' }
+ let(:xml) do
+ '<?xml version="1.0"?>' \
+ '<d:multistatus xmlns:d="DAV:" ' \
+ 'xmlns:s="http://sabredav.org/ns" ' \
+ 'xmlns:oc="http://owncloud.org/ns" ' \
+ 'xmlns:nc="http://nextcloud.org/ns">' \
+ '<d:response>' \
+ '<d:href>/remote.php/dav/files/admin/</d:href>' \
+ '<d:propstat>' \
+ '<d:prop>' \
+ '<d:getlastmodified>Tue, 06 Sep 2022 06:43:56 GMT</d:getlastmodified>' \
+ '<oc:fileid>6</oc:fileid>' \
+ '<oc:owner-display-name>admin</oc:owner-display-name>' \
+ '<oc:size>24049442</oc:size>' \
+ '</d:prop>' \
+ '<d:status>HTTP/1.1 200 OK</d:status>' \
+ '</d:propstat>' \
+ '<d:propstat>' \
+ '<d:prop>' \
+ '<d:getcontenttype/>' \
+ '</d:prop>' \
+ '<d:status>HTTP/1.1 404 Not Found</d:status>' \
+ '</d:propstat>' \
+ '</d:response>' \
+ '<d:response>' \
+ '<d:href>/remote.php/dav/files/admin/Nextcloud%20Manual.pdf</d:href>' \
+ '<d:propstat>' \
+ '<d:prop>' \
+ '<d:getcontenttype>application/pdf</d:getcontenttype>' \
+ '<d:getlastmodified>Tue, 06 Sep 2022 06:43:56 GMT</d:getlastmodified>' \
+ '<oc:fileid>7</oc:fileid>' \
+ '<oc:owner-display-name>admin</oc:owner-display-name>' \
+ '<oc:size>12764917</oc:size>' \
+ '</d:prop>' \
+ '<d:status>HTTP/1.1 200 OK</d:status>' \
+ '</d:propstat>' \
+ '</d:response>' \
+ '<d:response>' \
+ '<d:href>/remote.php/dav/files/admin/Nextcloud%20intro.mp4</d:href>' \
+ '<d:propstat>' \
+ '<d:prop>' \
+ '<d:getcontenttype>video/mp4</d:getcontenttype>' \
+ '<d:getlastmodified>Tue, 06 Sep 2022 06:43:56 GMT</d:getlastmodified>' \
+ '<oc:fileid>13</oc:fileid>' \
+ '<oc:owner-display-name>admin</oc:owner-display-name>' \
+ '<oc:size>3963036</oc:size>' \
+ '</d:prop>' \
+ '<d:status>HTTP/1.1 200 OK</d:status>' \
+ '</d:propstat>' \
+ '</d:response>' \
+ '</d:multistatus>'
+ end
+
+ let(:storage) do
+ storage = instance_double(::Storages::Storage)
+ allow(storage).to receive(:oauth_client).and_return(instance_double(OAuthClient))
+ allow(storage).to receive(:host).and_return(url)
+ allow(storage).to receive(:provider_type).and_return(::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD)
+ storage
+ end
+
+ let(:connection_manager) do
+ connection_manager = instance_double(::OAuthClients::ConnectionManager)
+ allow(connection_manager).to receive(:get_access_token).and_return(ServiceResult.success(result: access_token))
+ allow(connection_manager).to receive(:with_refreshed_token).and_yield
+ connection_manager
+ end
+
+ let(:access_token) do
+ token = instance_double(OAuthClientToken)
+ allow(token).to receive(:origin_user_id).and_return(origin_user_id)
+ allow(token).to receive(:access_token).and_return('xyz')
+ token
+ end
+
+ subject { described_class.new(storage:).files_query(user:) }
+
+ before do
+ allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
+ stub_request(:propfind, "#{url}/remote.php/dav/files/#{origin_user_id}/")
+ .to_return(status: 207, body: xml, headers: {})
+ end
+
+ describe 'files_query' do
+ it 'must return a list of files when called' do
+ result = subject.call
+ expect(result.success).to be_truthy
+ expect(result.result.size).to eq(2)
+ end
+ end
+end