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

[#53370] use webmock timeout in network error test

Eric Schubertb27f17e1bb42
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 c8d618489d8..d7a9eb0b0ea 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
@@ -41,13 +41,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def call(auth_strategy:, folder:)
if auth_strategy.user.nil?
error_data = Storages::StorageErrorData.new(source: self)
- return Util.error(:error, 'Cannot execute query without user context.', error_data)
+ return Util.error(:error, "Cannot execute query without user context.", error_data)
end
origin_user = origin_user_id(auth_strategy.user)
@location_prefix = Util.join_uri_path(@storage.uri.path,
"remote.php/dav/files",
- origin_user.gsub(' ', '%20'))
+ origin_user.gsub(" ", "%20"))
result = make_request(auth_strategy:, folder:, user: auth_strategy.user)
storage_files(result)
@@ -60,9 +60,9 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
Auth[auth_strategy].call(storage: @storage,
http_options: Util.webdav_request_with_depth(1)) do |http|
- response = http.request('PROPFIND',
+ response = http.request("PROPFIND",
Util.join_uri_path(@storage.uri,
- 'remote.php/dav/files',
+ "remote.php/dav/files",
CGI.escapeURIComponent(origin_user),
requested_folder(folder)),
xml: requested_properties)
@@ -71,26 +71,26 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def handle_response(response)
- error_data = Storages::StorageErrorData.new(source: self, payload: response)
+ error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
case response
in { status: 200..299 }
ServiceResult.success(result: response.body)
in { status: 404 }
- Util.error(:not_found, 'Outbound request destination not found', error_data)
+ Util.error(:not_found, "Outbound request destination not found", error_data)
in { status: 401 }
- Util.error(:unauthorized, 'Outbound request not authorized', error_data)
+ Util.error(:unauthorized, "Outbound request not authorized", error_data)
else
- Util.error(:error, 'Outbound request failed', error_data)
+ Util.error(:error, "Outbound request failed", error_data)
end
end
def origin_user_id(user)
- OAuthClientToken.find_by(user_id: user, oauth_client_id: @storage.oauth_client.id)&.origin_user_id || ''
+ OAuthClientToken.find_by(user_id: user, oauth_client_id: @storage.oauth_client.id)&.origin_user_id || ""
end
def requested_folder(folder)
- return '' if folder.root?
+ return "" if folder.root?
Util.escape_path(folder.path)
end
@@ -98,17 +98,17 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:disable Metrics/AbcSize
def requested_properties
Nokogiri::XML::Builder.new do |xml|
- xml['d'].propfind(
- 'xmlns:d' => 'DAV:',
- 'xmlns:oc' => 'http://owncloud.org/ns'
+ xml["d"].propfind(
+ "xmlns:d" => "DAV:",
+ "xmlns:oc" => "http://owncloud.org/ns"
) do
- xml['d'].prop do
- xml['oc'].fileid
- xml['oc'].size
- xml['d'].getcontenttype
- xml['d'].getlastmodified
- xml['oc'].permissions
- xml['oc'].send(:'owner-display-name')
+ xml["d"].prop do
+ xml["oc"].fileid
+ xml["oc"].size
+ xml["d"].getcontenttype
+ xml["d"].getlastmodified
+ xml["oc"].permissions
+ xml["oc"].send(:"owner-display-name")
end
end
end.to_xml
@@ -119,7 +119,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def storage_files(response)
response.map do |xml|
parent, *files = Nokogiri::XML(xml)
- .xpath('//d:response')
+ .xpath("//d:response")
.to_a
.map { |file_element| storage_file(file_element) }
@@ -128,12 +128,12 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def ancestors(parent_location)
- path = parent_location.split('/')
+ path = parent_location.split("/")
return [] if path.count == 0
path.take(path.count - 1).reduce([]) do |list, item|
last = list.last
- prefix = last.nil? || last.location[-1] != '/' ? '/' : ''
+ prefix = last.nil? || last.location[-1] != "/" ? "/" : ""
location = "#{last&.location}#{prefix}#{item}"
list.append(forge_ancestor(location))
end
@@ -146,7 +146,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def name(location)
- location == '/' ? 'Root' : CGI.unescape(location.split('/').last)
+ location == "/" ? "Root" : CGI.unescape(location.split("/").last)
end
def storage_file(file_element)
@@ -166,7 +166,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def id(element)
element
- .xpath('.//oc:fileid')
+ .xpath(".//oc:fileid")
.map(&:inner_text)
.reject(&:empty?)
.first
@@ -174,21 +174,21 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def location(element)
texts = element
- .xpath('d:href')
+ .xpath("d:href")
.map(&:inner_text)
return nil if texts.empty?
element_name = texts.first.delete_prefix(@location_prefix)
- return element_name if element_name == '/'
+ return element_name if element_name == "/"
- element_name.delete_suffix('/')
+ element_name.delete_suffix("/")
end
def size(element)
element
- .xpath('.//oc:size')
+ .xpath(".//oc:size")
.map(&:inner_text)
.map { |e| Integer(e) }
.first
@@ -196,22 +196,22 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def mime_type(element)
element
- .xpath('.//d:getcontenttype')
+ .xpath(".//d:getcontenttype")
.map(&:inner_text)
.reject(&:empty?)
- .first || 'application/x-op-directory'
+ .first || "application/x-op-directory"
end
def last_modified_at(element)
element
- .xpath('.//d:getlastmodified')
+ .xpath(".//d:getlastmodified")
.map { |e| DateTime.parse(e) }
.first
end
def created_by(element)
element
- .xpath('.//oc:owner-display-name')
+ .xpath(".//oc:owner-display-name")
.map(&:inner_text)
.reject(&:empty?)
.first
@@ -220,7 +220,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def permissions(element)
permissions_string =
element
- .xpath('.//oc:permissions')
+ .xpath(".//oc:permissions")
.map(&:inner_text)
.reject(&:empty?)
.first
@@ -228,7 +228,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# Nextcloud Dav permissions:
# https://github.com/nextcloud/server/blob/66648011c6bc278ace57230db44fd6d63d67b864/lib/public/Files/DavUtil.php
result = []
- result << :readable if permissions_string.include?('G')
+ result << :readable if permissions_string.include?("G")
result << :writeable if %w[CK W].reduce(false) { |s, v| s || permissions_string.include?(v) }
result
end
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 5c9b7d8207f..5aad9c64927 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
@@ -112,7 +112,7 @@ module Storages
[],
StorageFile.new(
id: parent_id,
- name: path.split('/').last,
+ name: path.split("/").last,
location: path,
permissions: %i[readable writeable]
),
@@ -121,7 +121,7 @@ module Storages
end
def parent(parent_reference)
- _, _, name = parent_reference[:path].gsub(/.*root:/, '').rpartition '/'
+ _, _, name = parent_reference[:path].gsub(/.*root:/, "").rpartition "/"
if name.empty?
root(parent_reference[:id])
@@ -136,10 +136,10 @@ module Storages
end
def forge_ancestors(parent_reference)
- path_elements = parent_reference[:path].gsub(/.+root:/, '').split('/')
+ path_elements = parent_reference[:path].gsub(/.+root:/, "").split("/")
path_elements[0..-2].map do |component|
- next root(Digest::SHA256.hexdigest('i_am_root')) if component.blank?
+ next root(Digest::SHA256.hexdigest("i_am_root")) if component.blank?
StorageFile.new(
id: Digest::SHA256.hexdigest(component),
@@ -169,7 +169,7 @@ module Storages
end
def encode_path(path)
- path.split('/').map { |fragment| URI.encode_uri_component(fragment) }.join('/')
+ path.split("/").map { |fragment| URI.encode_uri_component(fragment) }.join("/")
end
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/network_error_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/network_error_spec.rb
index 28f171acfc5..446e8588987 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/network_error_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/network_error_spec.rb
@@ -32,59 +32,30 @@ require "spec_helper"
require_module_spec_helper
# rubocop:disable RSpec/DescribeClass
-RSpec.describe "network errors for storage interaction" do
+RSpec.describe "network errors for storage interaction", :webmock do
using Storages::Peripherals::ServiceResultRefinements
let(:user) { create(:user) }
let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }
- let(:request_url) { "https://my.timeout.org/" }
+ let(:fields) { Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery::FIELDS }
+ let(:request_url) { "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root/children#{fields}" }
+ let(:folder) { Storages::Peripherals::ParentFolder.new("/") }
let(:auth_strategy) do
Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)
end
context "if a timeout happens" do
- before do
- request = HTTPX::Request.new(:get, request_url)
- httpx_double = class_double(HTTPX, get: HTTPX::ErrorResponse.new(request, "Timeout happens", {}))
- allow(httpx_double).to receive(:with).and_return(httpx_double)
- allow(OpenProject).to receive(:httpx).and_return(httpx_double)
- end
-
it "must return an error with wrapped network error response" do
- result = Storages::Peripherals::StorageInteraction::Authentication[auth_strategy].call(storage:) do |http|
- make_request(http)
- end
+ # Test network error handling specifically with the files query.
+ # Other queries and commands should implement the network error handling in the same way.
+ stub_request(:get, request_url).to_timeout
+ result = Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery.call(storage:, auth_strategy:, folder:)
expect(result).to be_failure
expect(result.result).to eq(:error)
+ expect(result.error_source).to be(Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery)
expect(result.error_payload).to be_a(HTTPX::ErrorResponse)
end
end
-
- private
-
- def make_request(http)
- handle_response http.get(request_url)
- end
-
- def handle_response(response)
- case response
- in { status: 200..299 }
- ServiceResult.success(result: "EXPECTED_RESULT")
- in { status: 401 }
- error(:unauthorized)
- in { status: 403 }
- error(:forbidden)
- in { status: 404 }
- error(:not_found)
- else
- error(:error, response)
- end
- end
-
- def error(code, payload = nil)
- data = Storages::StorageErrorData.new(source: "EXECUTING_QUERY", payload:)
- ServiceResult.failure(result: code, errors: Storages::StorageError.new(code:, data:))
- end
end
# rubocop:enable RSpec/DescribeClass