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
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.rbindex aa0422df0d1..76085ccc1f3 100644--- a/app/services/oauth_clients/connection_manager.rb+++ b/app/services/oauth_clients/connection_manager.rb@@ -168,6 +168,13 @@ module OAuthClientsyield_service_resultend+ 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.rbindex 834d99eb3f9..bf69ff54a9b 100644--- a/app/services/service_result.rb+++ b/app/services/service_result.rb@@ -169,6 +169,14 @@ class ServiceResultselfend+ def match(on_success:, on_failure:)+ if success?+ on_success.call(result)+ else+ on_failure.call+ end+ end+def eachyield result if success?selfdiff --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.rbindex 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::StorageInteractionclass 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}/"enddef fileshttp = Net::HTTP.new(@uri.host, @uri.port)http.use_ssl = @uri.scheme == 'https'- result = @token_refresh.call do+ result = @with_refreshed_token.call doresponse = 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.rbindex 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::StorageInteractioncase @provider_typewhen ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUDconnection_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)elseraise ArgumentErrordiff --git a/modules/storages/lib/api/v3/utilities/storage_requests.rb b/modules/storages/lib/api/v3/utilities/storage_requests.rbindex 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_clientend+ # The download_command is actually a query and should be refactoreddef download_command->(access_token:, file_id:) dorequest_url = File.join(@oauth_client.integration.host, '/ocs/v2.php/apps/dav/api/v1/direct')@@ -55,17 +56,15 @@ class API::V3::Utilities::StorageRequestsenddef 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_procendenddiff --git a/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb b/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rbnew file mode 100644index 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