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 new request spec for new storage files API endpoint

Andreas Pfohl5b4fc0a0455a
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 c8abc959084..d6019345b3f 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
@@ -31,8 +31,8 @@ module API::V3::StorageFiles
using Storages::Peripherals::ServiceResultRefinements
helpers do
- def handle_files_error(files)
- case files.result
+ def raise_error(error)
+ case error
when :not_found
raise API::Errors::NotFound.new
when :not_authorized
@@ -45,23 +45,26 @@ module API::V3::StorageFiles
resources :files do
get do
- ::Storages::Peripherals::StorageRequests
+ Storages::Peripherals::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:
- )
+ on_success: ->(files_query) {
+ files_query
+ .call
+ .map do |files|
+ API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
+ files,
+ self_link: api_v3_paths.storage_files(@storage.id),
+ current_user:
+ )
+ end
+ .match(
+ on_success: ->(representer) { representer },
+ on_failure: ->(error) { raise_error(error) }
+ )
},
- on_failure: ->(error) { handle_files_error(error) }
+ on_failure: ->(error) { raise_error(error) }
)
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 6ad62395ce1..acedfcd9526 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -152,4 +152,102 @@ describe 'API v3 storages resource', type: :request, content_type: :json do
end
end
end
+
+ describe 'GET /api/v3/storages/:storage_id/files' do
+ let(:path) { api_v3_paths.storage_files(storage.id) }
+
+ let(:files) do
+ [
+ Storages::StorageFile.new(1, 'new_younglings.md', 4096, 'plain/text', DateTime.now, DateTime.now,
+ 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/'),
+ Storages::StorageFile.new(2, 'holocron_inventory.md', 4096, 'plain/text', DateTime.now, DateTime.now,
+ 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/')
+ ]
+ end
+
+ describe 'with successful response' do
+ before do
+ storage_requests = instance_double(Storages::Peripherals::StorageRequests)
+ files_query = Proc.new do
+ ServiceResult.success(result: files)
+ end
+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.success(result: files_query))
+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)
+ end
+
+ subject { last_response.body }
+
+ it { is_expected.to be_json_eql(files.length.to_json).at_path('count') }
+ it { is_expected.to be_json_eql(files[0].id.to_json).at_path('_embedded/elements/0/id') }
+ it { is_expected.to be_json_eql(files[0].name.to_json).at_path('_embedded/elements/0/name') }
+ it { is_expected.to be_json_eql(files[1].id.to_json).at_path('_embedded/elements/1/id') }
+ it { is_expected.to be_json_eql(files[1].name.to_json).at_path('_embedded/elements/1/name') }
+ end
+
+ describe 'with files query creation failed' do
+ let(:storage_requests) { instance_double(Storages::Peripherals::StorageRequests) }
+
+ before do
+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)
+ end
+
+ describe 'due to authorization failure' do
+ before do
+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :not_authorized))
+ end
+
+ it { expect(last_response.status).to be(403) }
+ end
+
+ describe 'due to internal error' do
+ before do
+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :error))
+ end
+
+ it { expect(last_response.status).to be(500) }
+ end
+
+ describe 'due to not found' do
+ before do
+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :not_found))
+ end
+
+ it { expect(last_response.status).to be(404) }
+ end
+ end
+
+ describe 'with query failed' do
+ let(:files_query) do
+ Struct.new('FilesQuery', :error) do
+ def files
+ ServiceResult.failure(result: error)
+ end
+ end.new(error)
+ end
+
+ before do
+ storage_queries = instance_double(Storages::Peripherals::StorageInteraction::StorageQueries)
+ allow(storage_queries).to receive(:files_query).and_return(ServiceResult.success(result: files_query))
+ allow(Storages::Peripherals::StorageInteraction::StorageQueries).to receive(:new).and_return(storage_queries)
+ end
+
+ describe 'due to authorization failure' do
+ let(:error) { :not_authorized }
+
+ it { expect(last_response.status).to be(403) }
+ end
+
+ describe 'due to internal error' do
+ let(:error) { :error }
+
+ it { expect(last_response.status).to be(500) }
+ end
+
+ describe 'due to not found' do
+ let(:error) { :not_found }
+
+ it { expect(last_response.status).to be(404) }
+ end
+ end
+ end
end