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

[#44874] Implement StorageQuery to create upload link

Andreas Pfohl12631259ca48
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 3b93462a005..4fe200e790c 100644
--- a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
+++ b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
@@ -33,7 +33,7 @@ module Storages::Peripherals
if success?
on_success.call(result)
else
- on_failure.call(result)
+ on_failure.call(errors)
end
end
@@ -43,5 +43,21 @@ module Storages::Peripherals
yield result
end
end
+
+ refine(ServiceResult.singleton_class) do
+ def chain(initial:, steps:) # rubocop:disable Metrics/AbcSize
+ unless initial.instance_of?(ServiceResult)
+ raise TypeError, "Expected a #{ServiceResult.name.split('::').last}, got #{initial.class.name}."
+ end
+
+ unless steps.instance_of?(Array) && steps.all? { |step| step.instance_of?(Method) }
+ raise TypeError, "Expected an Array of Method, got #{steps.class.name}."
+ end
+
+ steps.map(&:to_proc).reduce(initial) do |state, method|
+ state.bind { |value| method.call(value) }
+ end
+ 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
index 40b45e05d99..3b9795be54b 100644
--- 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
@@ -61,21 +61,35 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
}
)
)
- rescue RestClient::Unauthorized
- response = ServiceResult.failure(result: :not_authorized)
- rescue RestClient::NotFound
- response = ServiceResult.failure(result: :not_found)
+ rescue RestClient::Unauthorized => e
+ response = error(:not_authorized, 'Outbound request not authorized!', e.response)
+ rescue RestClient::NotFound => e
+ response = error(:not_found, 'Outbound request destination not found!', e.response)
+ rescue RestClient::ExceptionWithResponse => e
+ response = error(:error, 'Outbound request failed!', e.response)
rescue StandardError
- response = ServiceResult.failure(result: :error)
+ response = error(:error, 'Outbound request failed!')
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
+ response
+ .bind do |r|
+ # The nextcloud API returns a successful response with empty body if the authorization is missing or expired
+ if r.body.blank?
+ error(:not_authorized, 'Outbound request not authorized!')
+ else
+ ServiceResult.success(result: r)
+ end
+ end
end
end
+ def error(code, message = nil, data = nil)
+ ServiceResult.failure(
+ result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
+ errors: Storages::StorageError.new(code:, message:, data:)
+ )
+ end
+
def download_link(token, origin_name)
URI::join(@base_uri, "/index.php/apps/integration_openproject/direct/#{token}/#{CGI.escape(origin_name)}")
end
@@ -83,8 +97,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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)
+ return error(:error, "Received unexpected json response", body)
end
ServiceResult.success(result: token)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index a410b4c4147..d582da0bf27 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -84,14 +84,21 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def error(response)
case response
when Net::HTTPNotFound
- ServiceResult.failure(result: :not_found)
+ error_result(:not_found)
when Net::HTTPUnauthorized
- ServiceResult.failure(result: :not_authorized)
+ error_result(:not_authorized)
else
- ServiceResult.failure(result: :error)
+ error_result(:error)
end
end
+ def error_result(code, message = nil, data = nil)
+ ServiceResult.failure(
+ result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
+ errors: Storages::StorageError.new(code:, message:, data:)
+ )
+ end
+
def storage_files(response)
response.map do |xml|
Nokogiri::XML(xml)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
new file mode 100644
index 00000000000..1572cc3b173
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
@@ -0,0 +1,155 @@
+#-- 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 UploadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
+ using Storages::Peripherals::ServiceResultRefinements
+
+ URI_BASE_PATH = '/ocs/v2.php/apps/files_sharing/api/v1/shares'.freeze
+ UPLOAD_LINK_BASE = '/public.php/webdav'.freeze
+
+ def initialize(base_uri:, token:, with_refreshed_token:, finalize_url:)
+ super()
+
+ @base_uri = base_uri
+ @token = token
+ @with_refreshed_token = with_refreshed_token
+ @finalize_url = finalize_url
+ end
+
+ def query(data)
+ ServiceResult.chain(
+ initial: validated(data),
+ steps: [
+ method(:share),
+ method(:file_drop_share),
+ method(:upload_link)
+ ]
+ )
+ end
+
+ private
+
+ def validated(data)
+ if data.nil? || data['fileName'].nil? || data['parent'].nil?
+ error(:error, 'Data is not valid', data)
+ else
+ ServiceResult.success(
+ result: Struct.new(:file_name, :parent)
+ .new(data['fileName'], data['parent'])
+ )
+ end
+ end
+
+ def share(data)
+ password = SecureRandom.uuid
+
+ outbound_response(
+ method: :post,
+ relative_path: URI_BASE_PATH,
+ payload: {
+ shareType: 3,
+ password:,
+ path: data.parent,
+ expireDate: Date.tomorrow
+ }
+ ).map do |response|
+ Struct.new(:id, :token, :password, :file_name)
+ .new(response.ocs.data.id, response.ocs.data.token, password, data.file_name)
+ end
+ end
+
+ def file_drop_share(share)
+ outbound_response(
+ method: :put,
+ relative_path: "#{URI_BASE_PATH}/#{share.id}",
+ payload: {
+ permissions: 4
+ }
+ ).map { share }
+ end
+
+ def upload_link(share)
+ destination = @base_uri.merge("#{UPLOAD_LINK_BASE}/#{share.file_name}")
+ destination.user = share.token
+ destination.password = share.password
+
+ ServiceResult.success(result: Storages::UploadLink.new(destination))
+ end
+
+ def outbound_response(method:, relative_path:, payload:) # rubocop:disable Metrics/AbcSize
+ @with_refreshed_token.call(@token) do |token|
+ begin
+ response = ServiceResult.success(
+ result: RestClient::Request.execute(
+ method:,
+ url: @base_uri.merge(relative_path).to_s,
+ payload: payload.to_json,
+ headers: {
+ 'Authorization' => "Bearer #{token.access_token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json'
+ }
+ )
+ )
+ rescue RestClient::Unauthorized => e
+ response = error(:not_authorized, 'Outbound request not authorized!', e.response)
+ rescue RestClient::NotFound => e
+ response = error(:not_found, 'Outbound request destination not found!', e.response)
+ rescue RestClient::ExceptionWithResponse => e
+ response = error(:error, 'Outbound request failed!', e.response)
+ rescue StandardError
+ response = error(:error, 'Outbound request failed!')
+ end
+
+ # rubocop:disable Style/OpenStructUse
+ # rubocop:disable Style/MultilineBlockChain
+ response
+ .bind do |r|
+ # The nextcloud API returns a successful response with empty body if the authorization is missing or expired
+ if r.body.blank?
+ error(:not_authorized, 'Outbound request not authorized!')
+ else
+ ServiceResult.success(result: r)
+ end
+ end
+ .map { |r| JSON.parse(r.body, object_class: OpenStruct) }
+ # rubocop:enable Style/MultilineBlockChain
+ # rubocop:enable Style/OpenStructUse Style/MultilineBlockChain
+ end
+ end
+
+ def error(code, message = nil, data = nil)
+ ServiceResult.failure(
+ result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
+ errors: Storages::StorageError.new(code:, message:, data:)
+ )
+ end
+ end
+end
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 1f88a8b6fcf..31ebaf46371 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
@@ -40,22 +40,29 @@ module Storages::Peripherals::StorageInteraction
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
- )
+ with_connection_manager do |token, with_refreshed_token|
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::DownloadLinkQuery.new(
+ base_uri: @uri,
+ token:,
+ with_refreshed_token:
+ )
+ end
+ else
+ raise ArgumentError
+ end
+ end
+
+ def upload_link_query(finalize_url)
+ case @provider_type
+ when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
+ with_connection_manager do |token, with_refreshed_token|
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::UploadLinkQuery.new(
+ base_uri: @uri,
+ token:,
+ with_refreshed_token:,
+ finalize_url:
+ )
+ end
else
raise ArgumentError
end
@@ -64,25 +71,32 @@ module Storages::Peripherals::StorageInteraction
def files_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::FilesQuery.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
- )
+ with_connection_manager do |token, with_refreshed_token|
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::FilesQuery.new(
+ base_uri: @uri,
+ token:,
+ with_refreshed_token:
+ )
+ end
else
raise ArgumentError
end
end
+
+ private
+
+ def with_connection_manager
+ connection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client: @oauth_client)
+ connection_manager.get_access_token.match(
+ on_success: ->(token) do
+ ServiceResult.success(result: yield(token, connection_manager.method(:request_with_token_refresh).to_proc))
+ end,
+ on_failure: ->(_) { error(:not_authorized, 'Query could not be created! No access token found!') }
+ )
+ end
+
+ def error(code, message = nil, data = nil)
+ ServiceResult.failure(errors: Storages::StorageError.new(code:, message:, data:))
+ 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 bb5bd439438..6ac7e0173ca 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -45,30 +45,18 @@ module Storages::Peripherals
.map { |query| query.method(:query).to_proc }
end
- # ToDo: Remove if implemented
- # rubocop:disable Lint/UnusedMethodArgument
- # rubocop:disable Lint/UnusedBlockArgument
def upload_link_query(user:, finalize_url:)
- query_stub = ->(payload) do
- ServiceResult.success(
- result: Storages::UploadLink.new(
- "https://example.com/upload/xyz123",
- nil
- )
- )
- end
-
- ServiceResult.success(result: query_stub)
+ storage_queries(user)
+ .upload_link_query(finalize_url)
+ .map { |query| query.method(:query).to_proc }
end
- # rubocop:enable Lint/UnusedBlockArgument
- # rubocop:enable Lint/UnusedMethodArgument
private
def storage_queries(user)
::Storages::Peripherals::StorageInteraction::StorageQueries
.new(
- uri: URI(@storage.host),
+ uri: URI(@storage.host).normalize,
provider_type: @storage.provider_type,
user:,
oauth_client: @oauth_client
diff --git a/modules/storages/app/models/storages/storage_error.rb b/modules/storages/app/models/storages/storage_error.rb
new file mode 100644
index 00000000000..92d8ef70005
--- /dev/null
+++ b/modules/storages/app/models/storages/storage_error.rb
@@ -0,0 +1,44 @@
+#-- 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.
+#++
+
+class Storages::StorageError
+ attr_reader :code, :message, :data
+
+ def initialize(code:, message: nil, data: nil)
+ @code = code
+ @message = message
+ @data = data
+ end
+
+ def to_s
+ output = code.to_s
+ output << " | #{message}" unless message.nil?
+ output << " | #{data}" unless data.nil?
+ output
+ end
+end
diff --git a/modules/storages/app/models/storages/upload_finalize_link.rb b/modules/storages/app/models/storages/upload_finalize_link.rb
new file mode 100644
index 00000000000..c13f625ef3f
--- /dev/null
+++ b/modules/storages/app/models/storages/upload_finalize_link.rb
@@ -0,0 +1,36 @@
+#-- 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.
+#++
+
+class Storages::UploadFinalizeLink
+ attr_reader :destination, :payload
+
+ def initialize(destination = '', payload = nil)
+ @destination = destination
+ @payload = payload
+ 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 1ce89ea7e42..6b46b6dfcba 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
@@ -32,12 +32,11 @@ class API::V3::FileLinks::FileLinksDownloadAPI < ::API::OpenProjectAPI
helpers do
def raise_error(error)
- case error
+ Rails.logger.error(error)
+
+ case error.code
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
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 e715cf0c9c6..dc22fa3bd35 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
@@ -32,12 +32,11 @@ module API::V3::StorageFiles
helpers do
def raise_error(error)
- case error
+ Rails.logger.error(error)
+
+ case error.code
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
@@ -59,11 +58,10 @@ module API::V3::StorageFiles
self_link: api_v3_paths.storage_files(@storage.id),
current_user:
)
- end
- .match(
- on_success: ->(representer) { representer },
- on_failure: ->(error) { raise_error(error) }
- )
+ end.match(
+ on_success: ->(representer) { representer },
+ on_failure: ->(error) { raise_error(error) }
+ )
},
on_failure: ->(error) { raise_error(error) }
)
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index 69bc8133018..aa3faece490 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -146,7 +146,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
on_success: ->(query) do
result = query.call(file_link)
expect(result).to be_failure
- expect(result.result).to be(:not_authorized)
+ expect(result.errors.code).to be(:not_authorized)
end,
on_failure: ->(error) do
raise "Files query could not be created: #{error}"
@@ -168,7 +168,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
on_success: ->(query) do
result = query.call(file_link)
expect(result).to be_failure
- expect(result.result).to be(symbol)
+ expect(result.errors.code).to be(symbol)
end,
on_failure: ->(error) do
raise "Files query could not be created: #{error}"
@@ -296,7 +296,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
on_success: ->(query) do
result = query.call(nil)
expect(result).to be_failure
- expect(result.result).to be(symbol)
+ expect(result.errors.code).to be(symbol)
end,
on_failure: ->(error) do
raise "Files query could not be created: #{error}"
@@ -310,4 +310,174 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
it_behaves_like 'outbound is failing', 401, :not_authorized
it_behaves_like 'outbound is failing', 500, :error
end
+
+ describe '#upload_link_query' do
+ let(:query_payload) do
+ Struct.new(:fileName, :parent).new("ape.png", "/Pictures")
+ end
+
+ let(:uri) do
+ URI::join(url, "/public.php/webdav/#{query_payload[:fileName]}")
+ end
+
+ let(:share_id) { 37 }
+
+ before do
+ allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
+ stub_request(:post, "#{url}/ocs/v2.php/apps/files_sharing/api/v1/shares")
+ .with(
+ body: hash_including(
+ {
+ shareType: 3,
+ path: query_payload.parent,
+ expireDate: Date.tomorrow.iso8601
+ }
+ )
+ )
+ .to_return(
+ status: 200,
+ body: {
+ ocs: {
+ data: {
+ id: share_id,
+ token: 'jJ6t8yHe7CEX5Bp'
+ }
+ }
+ }.to_json
+ )
+ stub_request(:put, "#{url}/ocs/v2.php/apps/files_sharing/api/v1/shares/#{share_id}")
+ .with(body: { permissions: 4 })
+ .to_return(status: 200, body: {}.to_json)
+ end
+
+ describe 'with Nextcloud storage type selected' do
+ it 'must return an upload link URL' do
+ subject
+ .upload_link_query(user:, finalize_url: nil)
+ .match(
+ on_success: ->(query) do
+ query.call(query_payload).match(
+ on_success: ->(link) {
+ expect(link.destination.path).to be_eql("/public.php/webdav/#{query_payload.fileName}")
+ expect(link.destination.host).to be_eql(URI(url).host)
+ expect(link.destination.scheme).to be_eql(URI(url).scheme)
+ expect(link.destination.user).not_to be_nil
+ expect(link.destination.password).not_to be_nil
+ },
+ on_failure: ->(error) {
+ raise "Files query could not be executed: #{error}"
+ }
+ )
+ 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 first outbound request returning 200 and an empty body' do
+ before do
+ stub_request(:post, "#{url}/ocs/v2.php/apps/files_sharing/api/v1/shares")
+ .with(
+ body: hash_including(
+ {
+ shareType: 3,
+ path: query_payload.parent,
+ expireDate: Date.tomorrow.iso8601
+ }
+ )
+ )
+ .to_return(status: 200)
+ end
+
+ it 'must return :not_authorized ServiceResult' do
+ subject
+ .upload_link_query(user:, finalize_url: nil)
+ .match(
+ on_success: ->(query) do
+ result = query.call(query_payload)
+ expect(result).to be_failure
+ expect(result.errors.code).to be(:not_authorized)
+ end,
+ on_failure: ->(error) do
+ raise "Files query could not be created: #{error}"
+ end
+ )
+ end
+ end
+
+ describe 'with second outbound request returning 200 and an empty body' do
+ before do
+ stub_request(:put, "#{url}/ocs/v2.php/apps/files_sharing/api/v1/shares/#{share_id}")
+ .with(body: { permissions: 4 })
+ .to_return(status: 200)
+ end
+
+ it 'must return :not_authorized ServiceResult' do
+ subject
+ .upload_link_query(user:, finalize_url: nil)
+ .match(
+ on_success: ->(query) do
+ result = query.call(query_payload)
+ expect(result).to be_failure
+ expect(result.errors.code).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 |method = :get, path = '', code = 500, symbol = :error|
+ describe "with outbound request returning #{code}" do
+ before do
+ stub_request(method, "#{url}/ocs/v2.php/apps/files_sharing/api/v1/shares#{path}").to_return(status: code)
+ end
+
+ it "must return :#{symbol} ServiceResult" do
+ subject
+ .upload_link_query(user:, finalize_url: nil)
+ .match(
+ on_success: ->(query) do
+ result = query.call(query_payload)
+ expect(result).to be_failure
+ expect(result.errors.code).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', :post, '', 404, :not_found
+ it_behaves_like 'outbound is failing', :post, '', 401, :not_authorized
+ it_behaves_like 'outbound is failing', :post, '', 500, :error
+ it_behaves_like 'outbound is failing', :put, '/37', 404, :not_found
+ it_behaves_like 'outbound is failing', :put, '/37', 401, :not_authorized
+ it_behaves_like 'outbound is failing', :put, '/37', 500, :error
+ end
end
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 3b30e0ec35e..809407401b9 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
@@ -74,7 +74,7 @@ describe 'API v3 file links resource', type: :request do
.to receive(:new).and_return(connection_manager)
allow(connection_manager)
.to receive(:get_access_token)
- .and_return(ServiceResult.success(result: oauth_client_token))
+ .and_return(ServiceResult.success(result: oauth_client_token))
allow(connection_manager)
.to receive(:authorization_state).and_return(:connected)
allow(connection_manager)
@@ -502,7 +502,12 @@ describe 'API v3 file links resource', type: :request do
describe 'due to authorization failure' do
before do
- allow(storage_requests).to receive(:download_link_query).and_return(ServiceResult.failure(result: :not_authorized))
+ allow(storage_requests).to receive(:download_link_query).and_return(
+ ServiceResult.failure(
+ result: :not_authorized,
+ errors: Storages::StorageError.new(code: :not_authorized)
+ )
+ )
get path
end
@@ -511,7 +516,12 @@ describe 'API v3 file links resource', type: :request do
describe 'due to internal error' do
before do
- allow(storage_requests).to receive(:download_link_query).and_return(ServiceResult.failure(result: :error))
+ allow(storage_requests).to receive(:download_link_query).and_return(
+ ServiceResult.failure(
+ result: :error,
+ errors: Storages::StorageError.new(code: :error)
+ )
+ )
get path
end
@@ -520,7 +530,12 @@ describe 'API v3 file links resource', type: :request do
describe 'due to not found' do
before do
- allow(storage_requests).to receive(:download_link_query).and_return(ServiceResult.failure(result: :not_found))
+ allow(storage_requests).to receive(:download_link_query).and_return(
+ ServiceResult.failure(
+ result: :not_found,
+ errors: Storages::StorageError.new(code: :not_found)
+ )
+ )
get path
end
@@ -532,7 +547,7 @@ describe 'API v3 file links resource', type: :request do
let(:download_link_query) do
Struct.new('DownloadLinkQuery', :error) do
def query(_)
- ServiceResult.failure(result: error)
+ ServiceResult.failure(result: error, errors: Storages::StorageError.new(code: error))
end
end.new(error)
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 53a917e76e1..4db1adf84b2 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -221,7 +221,12 @@ describe 'API v3 storages resource', content_type: :json do
describe 'due to authorization failure' do
before do
- allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :not_authorized))
+ allow(storage_requests).to receive(:files_query).and_return(
+ ServiceResult.failure(
+ result: :not_authorized,
+ errors: Storages::StorageError.new(code: :not_authorized)
+ )
+ )
end
it { expect(last_response.status).to be(500) }
@@ -229,7 +234,12 @@ describe 'API v3 storages resource', content_type: :json do
describe 'due to internal error' do
before do
- allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :error))
+ allow(storage_requests).to receive(:files_query).and_return(
+ ServiceResult.failure(
+ result: :error,
+ errors: Storages::StorageError.new(code: :error)
+ )
+ )
end
it { expect(last_response.status).to be(500) }
@@ -237,7 +247,12 @@ describe 'API v3 storages resource', content_type: :json do
describe 'due to not found' do
before do
- allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :not_found))
+ allow(storage_requests).to receive(:files_query).and_return(
+ ServiceResult.failure(
+ result: :not_found,
+ errors: Storages::StorageError.new(code: :not_found)
+ )
+ )
end
it { expect(last_response.status).to be(404) }
@@ -248,7 +263,10 @@ describe 'API v3 storages resource', content_type: :json do
let(:files_query) do
Struct.new('FilesQuery', :error) do
def query(_)
- ServiceResult.failure(result: error)
+ ServiceResult.failure(
+ result: error,
+ errors: Storages::StorageError.new(code: error)
+ )
end
end.new(error)
end
@@ -303,7 +321,7 @@ describe 'API v3 storages resource', content_type: :json do
it do
expect(subject)
.to(be_json_eql("#{::API::V3::URN_PREFIX}storages:upload_link:no_link_provided".to_json)
- .at_path('_links/self/href'))
+ .at_path('_links/self/href'))
end
it { is_expected.to be_json_eql(upload_link.destination.to_json).at_path('_links/destination/href') }
@@ -320,7 +338,12 @@ describe 'API v3 storages resource', content_type: :json do
describe 'due to authorization failure' do
before do
- allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :not_authorized))
+ allow(storage_requests).to receive(:upload_link_query).and_return(
+ ServiceResult.failure(
+ result: :not_authorized,
+ errors: Storages::StorageError.new(code: :not_authorized)
+ )
+ )
end
it { expect(last_response.status).to be(500) }
@@ -328,7 +351,12 @@ describe 'API v3 storages resource', content_type: :json do
describe 'due to internal error' do
before do
- allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :error))
+ allow(storage_requests).to receive(:upload_link_query).and_return(
+ ServiceResult.failure(
+ result: :error,
+ errors: Storages::StorageError.new(code: :error)
+ )
+ )
end
it { expect(last_response.status).to be(500) }
@@ -336,7 +364,12 @@ describe 'API v3 storages resource', content_type: :json do
describe 'due to not found' do
before do
- allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :not_found))
+ allow(storage_requests).to receive(:upload_link_query).and_return(
+ ServiceResult.failure(
+ result: :not_found,
+ errors: Storages::StorageError.new(code: :not_found)
+ )
+ )
end
it { expect(last_response.status).to be(404) }