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

Rebase with dev

Marcello Rochabfd474629e99
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
index 6362b3ebdba..0ed3a579778 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
@@ -33,7 +33,7 @@ module Storages
module StorageInteraction
module OneDrive
class FilesQuery
- FIELDS = "?$select=id,name,size,webUrl,lastModifiedBy,createdBy,fileSystemInfo,file,folder"
+ FIELDS = "?$select=id,name,size,webUrl,lastModifiedBy,createdBy,fileSystemInfo,file,folder,parentReference"
using ServiceResultRefinements
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_query_spec.rb
index a9ea1fe85e8..7fcfd9480c0 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_query_spec.rb
@@ -1,6 +1,31 @@
# 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.
#++
require 'spec_helper'
@@ -13,10 +38,17 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesQuery,
create(:oauth_client_token, user:, oauth_client: storage.oauth_client, origin_user_id: 'darth@vader with spaces')
end
- let(:webdav_success_response) { create(:webdav_data, parent_path: parent, root_path:, origin_user_id:) }
+ let(:origin_user_id) { 'darth@vader with spaces' }
+ let(:webdav_success_response) { create(:webdav_data, parent_path: '', root_path: '', origin_user_id:) }
subject(:files_query) { described_class }
+ before do
+ uri = "#{storage.host}/remote.php/dav/files/darth@vader%20with%20spaces/"
+ allow(Storages::Peripherals::StorageInteraction::Nextcloud::Util).to receive(:token).and_yield(token)
+ stub_request(:propfind, uri).to_return(status: 207, body: webdav_success_response, headers: {})
+ end
+
it '.call requires 3 arguments: storage, user, and folder' do
expect(described_class).to respond_to(:call)
@@ -24,183 +56,111 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesQuery,
expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq user], %i[keyreq folder])
end
- context 'when outbound call is successful' do
- let(:parent) { '' }
- let(:root_path) { '' }
- let(:origin_user_id) { 'darth@vader with spaces' }
- let(:uri) { "#{storage.host}/remote.php/dav/files/darth@vader%20with%20spaces/" }
+ it 'returns a list of files and folders' do
+ storage_files = files_query.call(storage:, folder: nil, user:).result
+ expect(storage_files).to be_a(Storages::StorageFiles)
+
+ expect(storage_files.files.size).to eq(4)
+ expect(storage_files.ancestors.size).to eq(0)
+ expect(storage_files.parent.location).to eq('/')
+
+ mime_types = storage_files.files.map(&:mime_type).uniq!
+
+ expect(mime_types).to include('application/pdf') # file
+ expect(mime_types).to include('application/x-op-directory') # folder
+ end
+
+ it 'returns permissions for each' do
+ storage_files = files_query.call(storage:, folder: nil, user:).result
+
+ writeable_folder = storage_files.files.find { |file| file.mime_type == 'application/x-op-directory' }
+ expect(writeable_folder.permissions).to match_array(%i[readable writeable])
+
+ readonly_file = storage_files.files.find { |file| file.mime_type == 'application/pdf' }
+ expect(readonly_file.permissions).to match_array(%i[readable])
+ end
+
+ context 'when requesting a sub-folder' do
+ let(:parent) { '/Photos/Birds' }
+ let(:webdav_subfolder_success_response) { create(:webdav_data, parent_path: parent, root_path: '', origin_user_id:) }
before do
- allow(Storages::Peripherals::StorageInteraction::Nextcloud::Util).to receive(:token).and_yield(token)
- stub_request(:propfind, uri).to_return(status: 207, body: webdav_success_response, headers: {})
+ uri = "#{storage.host}/remote.php/dav/files/darth@vader%20with%20spaces#{parent}"
+ stub_request(:propfind, uri).to_return(status: 207, body: webdav_subfolder_success_response, headers: {})
+ end
+
+ subject(:query_result) { files_query.call(user:, storage:, folder: parent).result }
+
+ it 'returns 2 ancestors' do
+ ancestors = query_result.ancestors
+
+ expect(ancestors.size).to eq(2)
+ expect(ancestors.map(&:location)).to match_array(%w[/ /Photos])
+ expect(ancestors.map(&:name)).to match_array(%w[/ Photos])
end
- it 'returns a list of files and folders' do
- storage_files = files_query.call(storage:, folder: nil, user:).result
- expect(storage_files).to be_a(Storages::StorageFiles)
-
- expect(storage_files.files.size).to eq(4)
- expect(storage_files.ancestors.size).to eq(0)
- expect(storage_files.parent.location).to eq('/')
- expect(storage_files.files[0]).to have_attributes(
- id: '11',
- name: 'Folder1',
- mime_type: 'application/x-op-directory',
- permissions: include(:readable, :writeable)
- )
- expect(storage_files.files[1]).to have_attributes(mime_type: 'application/x-op-directory',
- permissions: %i[readable])
-
- expect(storage_files.files[2]).to have_attributes(id: '12',
- name: 'README.md',
- mime_type: 'text/markdown',
- permissions: include(
- :readable, :writeable
- ))
-
- expect(storage_files.files[3]).to have_attributes(mime_type: 'application/pdf',
- permissions: %i[readable])
+ it 'returns the parent folder' do
+ expect(query_result.parent.name).to eq('Birds')
+ expect(query_result.parent.location).to eq('/Photos/Birds')
end
- it 'returns permissions for each' do
- storage_files = files_query.call(storage:, folder: nil, user:).result
+ it 'lists the contents of the folder' do
+ expect(query_result.files).to all(be_a(Storages::StorageFile))
+ expect(query_result.files.size).to eq(4)
+ end
- storage_files.files.map(&:permissions)
+ it 'the files "location" include the entire path and the file name' do
+ expect(query_result.files.last.location).to eq("/Photos/Birds/Manual.pdf")
end
end
- # describe '#files_query' do
- # let(:parent) { '' }
- # let(:root_path) { '' }
- # let(:origin_user_id) { 'darth@vader with spaces' }
- # let(:xml) { create(:webdav_data, parent_path: parent, root_path:, origin_user_id:) }
- # let(:url) { "https://example.com#{root_path}" }
- # let(:request_url) do
- # Storages::Peripherals::StorageInteraction::Nextcloud::Util.join_uri_path(
- # url,
- # "/remote.php/dav/files/",
- # CGI.escapeURIComponent(origin_user_id),
- # parent
- # )
- # end
- #
- # context 'when outbound is success' do
- # before do
- # stub_request(:propfind, request_url).to_return(status: 207, body: xml, headers: {})
- # end
- #
- # describe 'with Nextcloud storage type selected' do
- # it 'returns a list files directories with names and permissions' do
- # result = registry.resolve('queries.nextcloud.files').call(storage:, folder: nil, user:)
- # expect(result).to be_success
- #
- # query_result = result.result
- # expect(query_result.files.size).to eq(4)
- # expect(query_result.ancestors.size).to eq(0)
- # expect(query_result.parent).not_to be_nil
- # expect(query_result.files[0]).to have_attributes(id: '11',
- # name: 'Folder1',
- # mime_type: 'application/x-op-directory',
- # permissions: include(:readable, :writeable))
- # expect(query_result.files[1]).to have_attributes(mime_type: 'application/x-op-directory',
- # permissions: %i[readable])
- # expect(query_result.files[2]).to have_attributes(id: '12',
- # name: 'README.md',
- # mime_type: 'text/markdown',
- # permissions: include(:readable, :writeable))
- # expect(query_result.files[3]).to have_attributes(mime_type: 'application/pdf',
- # permissions: %i[readable])
- # end
- #
- # describe 'with origin user id containing whitespaces' do
- # let(:origin_user_id) { 'my user' }
- # let(:xml) { create(:webdav_data, origin_user_id:) }
- #
- # it do
- # result = registry.resolve('queries.nextcloud.files').call(folder: parent, user:, storage:)
- # expect(result.result.files[0].location).to eq('/Folder1')
- #
- # assert_requested(:propfind, request_url)
- # end
- # end
- #
- # describe 'with parent query parameter' do
- # let(:parent) { '/Photos/Birds' }
- #
- # it do
- # result = registry.resolve('queries.nextcloud.files').call(folder: parent, user:, storage:)
- # expect(result.result.files[2].location).to eq('/Photos/Birds/README.md')
- # expect(result.result.ancestors[0].location).to eq('/')
- # expect(result.result.ancestors[1].location).to eq('/Photos')
- #
- # assert_requested(:propfind, request_url)
- # end
- # end
- #
- # describe 'with storage running on a sub path' do
- # let(:root_path) { '/storage' }
- #
- # it do
- # result = registry.resolve('queries.nextcloud.files').call(folder: nil, user:, storage:)
- # expect(result.result.files[2].location).to eq('/README.md')
- # assert_requested(:propfind, request_url)
- # end
- # end
- #
- # describe 'with storage running on a sub path and with parent parameter' do
- # let(:root_path) { '/storage' }
- # let(:parent) { '/Photos/Birds' }
- #
- # it do
- # result = registry.resolve('queries.nextcloud.files').call(folder: parent, user:, storage:)
- #
- # expect(result.result.files[2].location).to eq('/Photos/Birds/README.md')
- # assert_requested(:propfind, request_url)
- # end
- # end
- # end
- #
- # describe 'with not supported storage type selected' do
- # before do
- # allow(storage).to receive(:provider_type).and_return('not_supported_storage_type')
- # end
- #
- # it 'must raise ArgumentError' do
- # expect { registry.resolve('queries.nextcloud.files').call(storage:) }.to raise_error(ArgumentError)
- # end
- # end
- #
- # describe 'with missing OAuth token' do
- # before do
- # instance = instance_double(OAuthClients::ConnectionManager)
- # allow(OAuthClients::ConnectionManager).to receive(:new).and_return(instance)
- # allow(instance).to receive(:get_access_token).and_return(ServiceResult.failure)
- # end
- #
- # it 'must return ":not_authorized" ServiceResult' do
- # result = registry.resolve('queries.nextcloud.files').call(folder: parent, user:, storage:)
- # expect(result).to be_failure
- # expect(result.errors.code).to be(:not_authorized)
- # end
- # end
- # end
- #
- # shared_examples_for 'outbound is failing' do |code = 500, symbol = :error|
- # describe "with outbound request returning #{code}" do
- # before do
- # stub_request(:propfind, request_url).to_return(status: code)
- # end
- #
- # it "must return :#{symbol} ServiceResult" do
- # result = registry.resolve('queries.nextcloud.files').call(folder: parent, user:, storage:)
- # expect(result).to be_failure
- # expect(result.errors.code).to be(symbol)
- # end
- # end
- # end
- #
- # include_examples 'outbound is failing', 404, :not_found
- # include_examples 'outbound is failing', 401, :not_authorized
- # include_examples 'outbound is failing', 500, :error
- # end
+ context 'when the storage runs on a subfolder' do
+ let(:storage) { create(:nextcloud_storage, :with_oauth_client, host: 'https://example.com/death_star_blueprints') }
+
+ it 'just works' do
+ storage_files = files_query.call(storage:, user:, folder: '')
+
+ expect(storage_files).to be_success
+ end
+ end
+
+ describe 'with missing OAuth token' do
+ before do
+ allow(Storages::Peripherals::StorageInteraction::Nextcloud::Util)
+ .to receive(:token)
+ .and_return(ServiceResult.failure(result: :unauthorized,
+ errors: Storages::StorageError.new(code: :unauthorized)))
+ end
+
+ it 'returns an ":unauthorized" ServiceResult' do
+ result = files_query.call(folder: '', user:, storage:)
+ expect(result).to be_failure
+ expect(result.errors.code).to be(:unauthorized)
+ end
+ end
+
+ shared_examples_for 'outbound is failing' do |code = 500, symbol = :error|
+ describe "with outbound request returning #{code}" do
+ before do
+ uri = "#{storage.host}/remote.php/dav/files/darth@vader%20with%20spaces/"
+ stub_request(:propfind, uri).to_return(status: code)
+ end
+
+ it "must return :#{symbol} ServiceResult" do
+ result = files_query.call(folder: '', user:, storage:)
+ expect(result).to be_failure
+ expect(result.errors.code).to be(symbol)
+ end
+ end
+ end
+
+ include_examples 'outbound is failing', 404, :not_found
+ include_examples 'outbound is failing', 401, :unauthorized
+ include_examples 'outbound is failing', 500, :error
end
+
+# describe 'with storage running on a sub path' do
+# let(:root_path) { '/storage' }
+# #
+# end
+# end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb
index a5b0771dd82..4e6d8346853 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb
@@ -50,8 +50,8 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq user], %i[keyreq folder])
end
- it 'returns an array of StorageFile' do
- stub_request(:get, "https://graph.microsoft.com/v1.0/me/drive/root/children#{described_class::FIELDS}")
+ it 'returns a StorageFiles object' do
+ stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root/children#{described_class::FIELDS}")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: json, headers: {})
@@ -76,7 +76,10 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
end
it 'when the argument folder is nil, gets information from that users root folder' do
- stub_request(:get, "https://graph.microsoft.com/v1.0/me/drive/root/children#{described_class::FIELDS}")
+ stub_request(
+ :get,
+ "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root/children#{described_class::FIELDS}"
+ )
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: json, headers: {})
@@ -128,7 +131,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
describe 'error handling' do
it 'returns a notfound error if the API call returns a 404' do
- stub_request(:get, "https://graph.microsoft.com/v1.0/me/drive/root/children#{described_class::FIELDS}")
+ stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root/children#{described_class::FIELDS}")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 404, body: '', headers: {})
@@ -140,7 +143,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
end
it 'retries authentication when it returns a 401' do
- stub_request(:get, "https://graph.microsoft.com/v1.0/me/drive/root/children#{described_class::FIELDS}")
+ stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root/children#{described_class::FIELDS}")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 401, body: '', headers: {})