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

[#44937] Refactor DownloadCommand into DownloadLinkQuery

Andreas Pfohlbe7f1510284b
diff --git a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
index db7f14a06b4..3b93462a005 100644
--- a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
+++ b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
@@ -36,6 +36,12 @@ module Storages::Peripherals
on_failure.call(result)
end
end
+
+ def bind
+ return self if failure?
+
+ yield result
+ end
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
new file mode 100644
index 00000000000..40b45e05d99
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
@@ -0,0 +1,109 @@
+#-- 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.
+#++
+
+module Storages::Peripherals::StorageInteraction::Nextcloud
+ class DownloadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
+ using Storages::Peripherals::ServiceResultRefinements
+
+ def initialize(base_uri:, token:, with_refreshed_token:)
+ super()
+
+ @base_uri = base_uri
+ @uri = URI::join(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')
+ @token = token
+ @with_refreshed_token = with_refreshed_token
+ end
+
+ def query(file_link)
+ outbound_response(file_link)
+ .bind { |response_body| direct_download_token(body: response_body) }
+ .map { |download_token| download_link(download_token, file_link.origin_name) }
+ end
+
+ private
+
+ def outbound_response(file_link)
+ @with_refreshed_token.call(@token) do |token|
+ begin
+ response = ServiceResult.success(
+ result: RestClient.post(
+ @uri.to_s,
+ { fileId: file_link.origin_id },
+ {
+ 'Authorization' => "Bearer #{token.access_token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ }
+ )
+ )
+ rescue RestClient::Unauthorized
+ response = ServiceResult.failure(result: :not_authorized)
+ rescue RestClient::NotFound
+ response = ServiceResult.failure(result: :not_found)
+ rescue StandardError
+ response = ServiceResult.failure(result: :error)
+ end
+
+ response.bind do |r|
+ # The nextcloud API returns a successful response with empty body if the authorization is missing or expired
+ r.body.blank? ? ServiceResult.failure(result: :not_authorized) : ServiceResult.success(result: r)
+ end
+ end
+ end
+
+ def download_link(token, origin_name)
+ URI::join(@base_uri, "/index.php/apps/integration_openproject/direct/#{token}/#{CGI.escape(origin_name)}")
+ end
+
+ def direct_download_token(body:)
+ token = parse_direct_download_token(body:)
+ if token.blank?
+ Rails.logger.error("Received unexpected json response: #{body}")
+ return ServiceResult.failure(result: :error)
+ end
+
+ ServiceResult.success(result: token)
+ end
+
+ def parse_direct_download_token(body:)
+ begin
+ json = JSON.parse(body)
+ rescue JSON::ParserError
+ return nil
+ end
+
+ direct_download_url = json.dig('ocs', 'data', 'url')
+ return nil if direct_download_url.blank?
+
+ path = URI.parse(direct_download_url).path
+ return nil if path.blank?
+
+ path.split('/').last
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud_storage_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
similarity index 95%
rename from modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud_storage_query.rb
rename to modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index 000619d04e2..5e00df01721 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud_storage_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -26,22 +26,23 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module Storages::Peripherals::StorageInteraction
- class NextcloudStorageQuery
+module Storages::Peripherals::StorageInteraction::Nextcloud
+ class FilesQuery < Storages::Peripherals::StorageInteraction::StorageQuery
def initialize(base_uri:, token:, with_refreshed_token:)
+ super()
@uri = base_uri
@token = token
@with_refreshed_token = with_refreshed_token
@base_path = "/remote.php/dav/files/#{token.origin_user_id}"
end
- def files(parent)
+ def query(data)
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = @uri.scheme == 'https'
result = @with_refreshed_token.call(@token) do |token|
response = http.propfind(
- "#{@base_path}#{parent}",
+ "#{@base_path}#{data}",
requested_properties,
{
'Depth' => '1',
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
index 11d91662730..1f88a8b6fcf 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
@@ -37,6 +37,30 @@ module Storages::Peripherals::StorageInteraction
@oauth_client = oauth_client
end
+ def download_link_query
+ case @provider_type
+ when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
+ connection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client: @oauth_client)
+ connection_manager.get_access_token.match(
+ on_success: ->(token) do
+ ServiceResult.success(
+ result:
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::DownloadLinkQuery.new(
+ base_uri: @uri,
+ token:,
+ with_refreshed_token: connection_manager.method(:request_with_token_refresh).to_proc
+ )
+ )
+ end,
+ on_failure: ->(_) do
+ ServiceResult.failure(result: :not_authorized)
+ end
+ )
+ else
+ raise ArgumentError
+ end
+ end
+
def files_query
case @provider_type
when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
@@ -45,7 +69,7 @@ module Storages::Peripherals::StorageInteraction
on_success: ->(token) do
ServiceResult.success(
result:
- ::Storages::Peripherals::StorageInteraction::NextcloudStorageQuery.new(
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::FilesQuery.new(
base_uri: @uri,
token:,
with_refreshed_token: connection_manager.method(:request_with_token_refresh).to_proc
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_query.rb
new file mode 100644
index 00000000000..2ed688be0ee
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_query.rb
@@ -0,0 +1,35 @@
+#-- 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.
+#++
+
+module Storages::Peripherals::StorageInteraction
+ class StorageQuery
+ def query(data)
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
index 9484da745ed..6c87d14c4a4 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -33,30 +33,21 @@ module Storages::Peripherals
@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')
- body = { fileId: file_id }
- header = {
- 'Authorization' => "Bearer #{access_token}",
- 'OCS-APIRequest' => 'true',
- 'Accept' => 'application/json'
- }
-
- begin
- response = RestClient.post request_url, body, header
- rescue RestClient::Unauthorized
- return ServiceResult.failure(result: I18n.t('http.request.failed_authorization'))
- rescue StandardError => e
- return ServiceResult.failure(result: e.message)
- end
-
- ServiceResult.success(result: response)
- end
+ def download_link_query(user:)
+ storage_queries(user)
+ .download_link_query
+ .map { |query| query.method(:query).to_proc }
end
def files_query(user:)
+ storage_queries(user)
+ .files_query
+ .map { |query| query.method(:query).to_proc }
+ end
+
+ private
+
+ def storage_queries(user)
::Storages::Peripherals::StorageInteraction::StorageQueries
.new(
uri: URI(@storage.host),
@@ -64,8 +55,6 @@ module Storages::Peripherals
user:,
oauth_client: @oauth_client
)
- .files_query
- .map { |query| query.method(:files).to_proc }
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_url_helper.rb b/modules/storages/app/common/storages/peripherals/storage_url_helper.rb
index a92752851d7..6fdbf9a6015 100644
--- a/modules/storages/app/common/storages/peripherals/storage_url_helper.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_url_helper.rb
@@ -27,7 +27,7 @@
#++
module Storages::Peripherals
- # Helper for open and download links for a file link object.
+ # Helper for open links for a file link object.
module StorageUrlHelper
def storage_url_open_file(file_link, open_location: false)
location_flag = ActiveModel::Type::Boolean.new.cast(open_location) ? 0 : 1
@@ -38,78 +38,5 @@ module Storages::Peripherals
def storage_url_open(storage)
"#{storage.host}/index.php/apps/files"
end
-
- # rubocop:disable Metrics/AbcSize
- def make_download_url(file_link:, user:)
- storage = file_link.storage
- oauth_client = storage.oauth_client
-
- client_token = get_oauth_client_token(user:, oauth_client:)
- return client_token if client_token.failure?
-
- direct_download_response = make_direct_download storage:,
- access_token: client_token.result.access_token,
- file_id: file_link.origin_id
-
- return direct_download_response if direct_download_response.failure?
-
- download_token = direct_download_token(body: direct_download_response.result)
- return download_token if download_token.failure?
-
- url = "#{storage.host}/index.php/apps/integration_openproject/direct/#{download_token.result}/#{file_link.origin_name}"
- ServiceResult.success(result: url)
- end
-
- # rubocop:enable Metrics/AbcSize
-
- private
-
- def get_oauth_client_token(user:, oauth_client:)
- client_token = ::OAuthClients::ConnectionManager
- .new(user:, oauth_client:)
- .get_access_token
-
- client_token.success? ? client_token : ServiceResult.failure(result: I18n.t('http.request.missing_authorization'))
- end
-
- def make_direct_download(storage:, access_token:, file_id:)
- response = Storages::Peripherals::StorageRequests
- .new(storage:)
- .download_command
- .call(access_token:, file_id:)
-
- return response if response.failure?
-
- # The nextcloud API returns a successful response with empty body if the authorization is missing or expired
- return ServiceResult.failure(result: I18n.t('http.request.failed_authorization')) if response.result.body.blank?
-
- ServiceResult.success(result: response.result.body)
- end
-
- def direct_download_token(body:)
- token = parse_direct_download_token(body:)
- if token.blank?
- Rails.logger.error "Received unexpected json response: #{body}"
- return ServiceResult.failure(result: I18n.t('http.response.unexpected'))
- end
-
- ServiceResult.success(result: token)
- end
-
- def parse_direct_download_token(body:)
- begin
- json = JSON.parse body
- rescue JSON::ParserError
- return nil
- end
-
- direct_download_url = json.dig('ocs', 'data', 'url')
- return nil if direct_download_url.blank?
-
- path = URI.parse(direct_download_url).path
- return nil if path.blank?
-
- path.split('/').last
- end
end
end
diff --git a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb b/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
index 00eca9b4318..1ce89ea7e42 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
@@ -28,15 +28,41 @@
class API::V3::FileLinks::FileLinksDownloadAPI < ::API::OpenProjectAPI
helpers Storages::Peripherals::StorageUrlHelper
+ using Storages::Peripherals::ServiceResultRefinements
+
+ helpers do
+ def raise_error(error)
+ case error
+ when :not_found
+ raise API::Errors::NotFound.new
+ when :not_authorized
+ Rails.logger.error("An outbound request failed due to an authorization failure!")
+ raise API::Errors::InternalError.new
+ else
+ raise API::Errors::InternalError.new
+ end
+ end
+ end
resources :download do
get do
- download_url = make_download_url file_link: @file_link, user: User.current
-
- raise API::Errors::InternalError.new(download_url.result) if download_url.failure?
-
- redirect download_url.result, body: "The requested resource can be downloaded from #{download_url.result}"
- status 303
+ Storages::Peripherals::StorageRequests
+ .new(storage: @file_link.storage)
+ .download_link_query(user: User.current)
+ .match(
+ on_success: ->(download_link_query) {
+ download_link_query
+ .call(@file_link)
+ .match(
+ on_success: ->(url) do
+ redirect(url, body: "The requested resource can be downloaded from #{url}")
+ status(303)
+ end,
+ on_failure: ->(error) { raise_error(error) }
+ )
+ },
+ on_failure: ->(error) { raise_error(error) }
+ )
end
end
end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index 666f6ad654a..a346fd7a169 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -36,7 +36,8 @@ module API::V3::StorageFiles
when :not_found
raise API::Errors::NotFound.new
when :not_authorized
- raise API::Errors::Unauthorized.new
+ Rails.logger.error("An outbound request failed due to an authorization failure!")
+ raise API::Errors::InternalError.new
else
raise API::Errors::InternalError.new
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 0fda873f225..75998707ccf 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -41,7 +41,7 @@ module OpenProject::Storages
include OpenProject::Plugins::ActsAsOpEngine
initializer 'openproject_storages.feature_decisions' do
- OpenProject::FeatureDecisions.add :storage_file_linking
+ OpenProject::FeatureDecisions.add(:storage_file_linking)
end
# For documentation see the definition of register in "ActsAsOpEngine"
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index 9082448366c..69bc8133018 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -35,63 +35,6 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
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/Documents/</d:href>' \
- '<d:propstat>' \
- '<d:prop>' \
- '<d:getlastmodified>Tue, 06 Sep 2022 06:43:56 GMT</d:getlastmodified>' \
- '<oc:fileid>8</oc:fileid>' \
- '<oc:owner-display-name>admin</oc:owner-display-name>' \
- '<oc:size>399534</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:multistatus>'
- end
let(:storage) do
storage = instance_double(::Storages::Storage)
@@ -117,13 +60,138 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
subject { described_class.new(storage:) }
- 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: {})
+ describe '#download_link_query' do
+ let(:file_link) do
+ Struct.new(:file_link) do
+ def origin_id
+ 42
+ end
+
+ def origin_name
+ 'example.md'
+ end
+ end.new
+ end
+ let(:download_token) { "8dM3dC9iy1N74F5AJ0ClnjSF4dWTxfymVy1HTXBh8rbZVM81CpcBJaIYZvmR" }
+ let(:uri) do
+ URI::join(url, "/index.php/apps/integration_openproject/direct/#{download_token}/#{CGI.escape(file_link.origin_name)}")
+ end
+ let(:json) do
+ {
+ ocs: {
+ meta: {
+ status: 'ok',
+ statuscode: 200,
+ message: 'OK'
+ },
+ data: {
+ url: "https://example.com/remote.php/direct/#{download_token}"
+ }
+ }
+ }.to_json
+ end
+
+ before do
+ allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
+ stub_request(:post, "#{url}/ocs/v2.php/apps/dav/api/v1/direct")
+ .to_return(status: 200, body: json, headers: {})
+ end
+
+ describe 'with Nextcloud storage type selected' do
+ it 'must return a download link URL' do
+ subject
+ .download_link_query(user:)
+ .match(
+ on_success: ->(query) do
+ result = query.call(file_link)
+ expect(result).to be_success
+ expect(result.result).to be_eql(uri)
+ end,
+ on_failure: ->(error) do
+ raise "Files query could not be created: #{error}"
+ 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'.freeze)
+ end
+
+ it 'must raise ArgumentError' do
+ expect { subject.download_link_query(user:) }.to raise_error(ArgumentError)
+ end
+ end
+
+ describe 'with missing OAuth token' do
+ before do
+ allow(connection_manager).to receive(:get_access_token).and_return(ServiceResult.failure)
+ end
+
+ it 'must return ":not_authorized" ServiceResult' do
+ expect(subject.download_link_query(user:)).to be_failure
+ end
+ end
+
+ describe 'with outbound request returning 200 and an empty body' do
+ before do
+ stub_request(:post, "#{url}/ocs/v2.php/apps/dav/api/v1/direct").to_return(status: 200, body: '')
+ end
+
+ it 'must return :not_authorized ServiceResult' do
+ subject
+ .download_link_query(user:)
+ .match(
+ on_success: ->(query) do
+ result = query.call(file_link)
+ expect(result).to be_failure
+ expect(result.result).to be(:not_authorized)
+ end,
+ on_failure: ->(error) do
+ raise "Files query could not be created: #{error}"
+ 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(:post, "#{url}/ocs/v2.php/apps/dav/api/v1/direct").to_return(status: code)
+ end
+
+ it "must return :#{symbol} ServiceResult" do
+ subject
+ .download_link_query(user:)
+ .match(
+ on_success: ->(query) do
+ result = query.call(file_link)
+ expect(result).to be_failure
+ expect(result.result).to be(symbol)
+ end,
+ on_failure: ->(error) do
+ raise "Files query could not be created: #{error}"
+ end
+ )
+ end
+ end
+ end
+
+ it_behaves_like 'outbound is failing', 404, :not_found
+ it_behaves_like 'outbound is failing', 401, :not_authorized
+ it_behaves_like 'outbound is failing', 500, :error
end
describe '#files_query' do
+ let(:xml) { create(:webdav_data) }
+
+ 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 'with Nextcloud storage type selected' do
it 'must return a list of files when called' do
subject
@@ -132,7 +200,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
on_success: ->(query) do
result = query.call(nil)
expect(result).to be_success
- expect(result.result.size).to eq(2)
+ expect(result.result.size).to eq(3)
end,
on_failure: ->(error) do
raise "Files query could not be created: #{error}"
@@ -147,9 +215,9 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
on_success: ->(query) do
result = query.call(nil)
expect(result).to be_success
- expect(result.result[1].name).to eq('Documents')
- expect(result.result[1].mime_type).to eq('application/x-op-directory')
- expect(result.result[1].id).to eq('8')
+ expect(result.result[0].name).to eq('Folder1')
+ expect(result.result[0].mime_type).to eq('application/x-op-directory')
+ expect(result.result[0].id).to eq('11')
end,
on_failure: ->(error) do
raise "Files query could not be created: #{error}"
@@ -164,9 +232,9 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
on_success: ->(query) do
result = query.call(nil)
expect(result).to be_success
- expect(result.result[0].name).to eq('Nextcloud Manual.pdf')
- expect(result.result[0].mime_type).to eq('application/pdf')
- expect(result.result[0].id).to eq('7')
+ expect(result.result[1].name).to eq('README.md')
+ expect(result.result[1].mime_type).to eq('text/markdown')
+ expect(result.result[1].id).to eq('12')
end,
on_failure: ->(error) do
raise "Files query could not be created: #{error}"
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index 568c1931677..3b30e0ec35e 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -472,62 +472,96 @@ describe 'API v3 file links resource', type: :request do
end
describe 'GET /api/v3/file_links/:file_link_id/download' do
- let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }
- let(:request_factory) { instance_double(Storages::Peripherals::StorageRequests) }
- let(:access_token_result) { ServiceResult.success(result: create(:oauth_client_token)) }
let(:path) { api_v3_paths.file_link_download(file_link.id) }
- let(:download_response) { instance_double(RestClient::Response) }
- let(:download_command_result) { ServiceResult.success(result: download_response) }
- let(:response_body) { { ocs: { data: { url: 'https://starkiller.nextcloud.com/direct/xyz' } } }.to_json }
+ let(:url) { 'https://starkiller.nextcloud.com/direct/xyz' }
- before do
- allow(connection_manager).to receive(:get_access_token).and_return(access_token_result)
- allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
- allow(request_factory).to receive(:download_command).and_return(->(*) { download_command_result })
- allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(request_factory)
- allow(download_response).to receive(:body).and_return(response_body)
+ describe 'with successful response' do
+ before do
+ storage_requests = instance_double(Storages::Peripherals::StorageRequests)
+ download_link_query = Proc.new do
+ ServiceResult.success(result: url)
+ end
+ allow(storage_requests).to receive(:download_link_query).and_return(ServiceResult.success(result: download_link_query))
+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)
- get path
- end
+ get path
+ end
- it 'is successful' do
- expect(subject.status).to be 303
+ it 'responds successfully' do
+ expect(subject.status).to be(303)
+ expect(subject.location).to be(url)
+ end
end
- context 'if storage origin returns unexpected response body' do
- let(:response_body) { 'the force is not strong in this one ...' }
+ describe 'with download link query creation failed' do
+ let(:storage_requests) { instance_double(Storages::Peripherals::StorageRequests) }
- it_behaves_like 'error response',
- 500,
- 'InternalServerError',
- I18n.t('http.response.unexpected')
- end
+ before do
+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)
+ end
- context 'if storage origin returns empty response body' do
- let(:response_body) { '' }
+ describe 'due to authorization failure' do
+ before do
+ allow(storage_requests).to receive(:download_link_query).and_return(ServiceResult.failure(result: :not_authorized))
+ get path
+ end
- it_behaves_like 'error response',
- 500,
- 'InternalServerError',
- I18n.t('http.request.failed_authorization')
- end
+ it { expect(subject.status).to be(500) }
+ end
+
+ describe 'due to internal error' do
+ before do
+ allow(storage_requests).to receive(:download_link_query).and_return(ServiceResult.failure(result: :error))
+ get path
+ end
+
+ it { expect(subject.status).to be(500) }
+ end
- context 'if no access token is available' do
- let(:access_token_result) { ServiceResult.failure }
+ describe 'due to not found' do
+ before do
+ allow(storage_requests).to receive(:download_link_query).and_return(ServiceResult.failure(result: :not_found))
+ get path
+ end
- it_behaves_like 'error response',
- 500,
- 'InternalServerError',
- I18n.t('http.request.missing_authorization')
+ it { expect(subject.status).to be(404) }
+ end
end
- context 'if outbound request returns with failure (token expired or revoked)' do
- let(:download_command_result) { ServiceResult.failure(result: I18n.t('http.request.failed_authorization')) }
+ describe 'with query failed' do
+ let(:download_link_query) do
+ Struct.new('DownloadLinkQuery', :error) do
+ def query(_)
+ ServiceResult.failure(result: error)
+ end
+ end.new(error)
+ end
+
+ before do
+ storage_queries = instance_double(Storages::Peripherals::StorageInteraction::StorageQueries)
+ allow(storage_queries).to receive(:download_link_query).and_return(ServiceResult.success(result: download_link_query))
+ allow(Storages::Peripherals::StorageInteraction::StorageQueries).to receive(:new).and_return(storage_queries)
+
+ get path
+ end
+
+ describe 'due to authorization failure' do
+ let(:error) { :not_authorized }
+
+ it { expect(subject.status).to be(500) }
+ end
+
+ describe 'due to internal error' do
+ let(:error) { :error }
- it_behaves_like 'error response',
- 500,
- 'InternalServerError',
- I18n.t('http.request.failed_authorization')
+ it { expect(subject.status).to be(500) }
+ end
+
+ describe 'due to not found' do
+ let(:error) { :not_found }
+
+ it { expect(subject.status).to be(404) }
+ end
end
end
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
index 8821585e89a..7cbf7b5d71a 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -196,7 +196,7 @@ describe 'API v3 storages resource', type: :request, content_type: :json do
allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :not_authorized))
end
- it { expect(last_response.status).to be(403) }
+ it { expect(last_response.status).to be(500) }
end
describe 'due to internal error' do
@@ -219,7 +219,7 @@ describe 'API v3 storages resource', type: :request, content_type: :json do
describe 'with query failed' do
let(:files_query) do
Struct.new('FilesQuery', :error) do
- def files(_)
+ def query(_)
ServiceResult.failure(result: error)
end
end.new(error)
@@ -234,7 +234,7 @@ describe 'API v3 storages resource', type: :request, content_type: :json do
describe 'due to authorization failure' do
let(:error) { :not_authorized }
- it { expect(last_response.status).to be(403) }
+ it { expect(last_response.status).to be(500) }
end
describe 'due to internal error' do