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

Added additional tests for storage files query

Andreas Pfohl96c5f2cd0fd5
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index bf69ff54a9b..902ee59387a 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -173,7 +173,7 @@ class ServiceResult
if success?
on_success.call(result)
else
- on_failure.call
+ on_failure.call(result)
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 c7a09b209d4..006ef200647 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
@@ -43,18 +43,24 @@ module API::V3::StorageFiles
resources :files do
get do
- files = ::API::V3::Utilities::StorageRequests
- .new(storage: @storage)
- .files_query(user: current_user)
- .call
- .on_failure { |r| handle_files_error(r) }
- .result
+ ::API::V3::Utilities::StorageRequests
+ .new(storage: @storage)
+ .files_query(user: current_user)
+ .match(
+ on_success: ->(query) {
+ files = query
+ .call
+ .on_failure { |r| handle_files_error(r) }
+ .result
- ::API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
- files,
- self_link: api_v3_paths.storage_files(@storage.id),
- current_user:
- )
+ ::API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
+ files,
+ self_link: api_v3_paths.storage_files(@storage.id),
+ current_user:
+ )
+ },
+ on_failure: ->(error) { handle_files_error(error) }
+ )
end
end
end
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.rb
index d72f693124f..17a68aabb02 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
@@ -13,14 +13,17 @@ module API::V3::Utilities::StorageInteraction
connection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client: @oauth_client)
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
+ ServiceResult.success(
+ result:
+ ::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
+ on_failure: ->(_) do
ServiceResult.failure(result: :not_authorized)
end
)
diff --git a/modules/storages/lib/api/v3/utilities/storage_requests.rb b/modules/storages/lib/api/v3/utilities/storage_requests.rb
index 36b25213c64..d2889a92f8f 100644
--- a/modules/storages/lib/api/v3/utilities/storage_requests.rb
+++ b/modules/storages/lib/api/v3/utilities/storage_requests.rb
@@ -64,7 +64,6 @@ class API::V3::Utilities::StorageRequests
oauth_client: @oauth_client
)
.files_query
- .method(:files)
- .to_proc
+ .map { |query| query.method(:files).to_proc }
end
end
diff --git a/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb b/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb
index 3badd03cda6..42b4b967a05 100644
--- a/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb
+++ b/modules/storages/spec/lib/api/v3/utilities/storage_requests_spec.rb
@@ -89,8 +89,8 @@ describe API::V3::Utilities::StorageRequests, webmock: true do
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)
+ allow(storage).to receive(:host).and_return(url)
storage
end
@@ -108,7 +108,7 @@ describe API::V3::Utilities::StorageRequests, webmock: true do
token
end
- subject { described_class.new(storage:).files_query(user:) }
+ subject { described_class.new(storage:) }
before do
allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
@@ -117,10 +117,56 @@ describe API::V3::Utilities::StorageRequests, webmock: true do
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)
+ describe 'with Nextcloud storage type selected' do
+ it 'must return a list of files when called' do
+ subject
+ .files_query(user:)
+ .match(
+ on_success: ->(q) do
+ result = q.call
+ expect(result.success).to be_truthy
+ expect(result.result.size).to eq(2)
+ end,
+ on_failure: ->(_) do
+ raise "Files query could not be created."
+ end
+ )
+ end
+
+ # test if results are correct
+ 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.files_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 if OAuth token is missing' do
+ expect(subject.files_query(user:)).to be_failure
+ end
+ end
+
+ #
+ # it 'must return ":not_authorized" ServiceResult if outbound request returns "401 Not Authorized"' do
+ #
+ # end
+ #
+ # it 'must return ":not_found" ServiceResult if outbound request cannot find ressource' do
+ #
+ # end
+ #
+ # it 'must return ":error" ServiceResult if outbound request is not successful' do
+ #
+ # end
end
end