-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19c8369
commit 22ea280
Showing
5 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
modules/storages/app/services/storages/create_folder_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 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 | ||
class CreateFolderService < BaseService | ||
def self.call(storage:, user:, name:, parent_location:) | ||
new.call(storage:, user:, name:, parent_location:) | ||
end | ||
|
||
def call(storage:, user:, name:, parent_location:) | ||
auth_strategy = Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user: user) | ||
|
||
::Storages::Peripherals::Registry | ||
.resolve("#{storage}.commands.create_folder") | ||
.call(storage:, auth_strategy:, folder_name: name, parent_location:) | ||
end | ||
end | ||
end |
80 changes: 80 additions & 0 deletions
80
modules/storages/lib/api/v3/storage_files/storage_folders_api.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 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 API | ||
module V3 | ||
module StorageFiles | ||
class StorageFoldersAPI < ::API::OpenProjectAPI | ||
using ::Storages::Peripherals::ServiceResultRefinements | ||
|
||
helpers ::Storages::Peripherals::StorageFileInfoConverter, | ||
::Storages::Peripherals::StorageErrorHelper | ||
|
||
helpers do | ||
def parent_path_from_parent_id(parent_id) | ||
::Storages::StorageFileService | ||
.call(storage: @storage, user: current_user, file_id: parent_id) | ||
.match( | ||
on_success: lambda { |folder_info| | ||
path = URI.decode_uri_component(folder_info.location) | ||
::Storages::Peripherals::ParentFolder.new(path) | ||
}, | ||
on_failure: ->(error) { raise_error(error) } | ||
) | ||
end | ||
end | ||
|
||
resources :folders do | ||
params do | ||
requires :name, type: String, desc: "Folder name" | ||
end | ||
|
||
post do | ||
::Storages::CreateFolderService.call( | ||
storage: @storage, | ||
user: current_user, | ||
name: params["name"], | ||
parent_location: parent_path_from_parent_id(params["parent_id"]) | ||
).match( | ||
on_success: lambda { |storage_folder| | ||
API::V3::StorageFiles::StorageFileRepresenter.new( | ||
storage_folder, | ||
@storage, | ||
current_user: | ||
) | ||
}, | ||
on_failure: ->(error) { raise_error(error) } | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
modules/storages/spec/requests/api/v3/storages/storage_folders_api_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 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. | ||
#++ | ||
|
||
require "spec_helper" | ||
require_module_spec_helper | ||
|
||
RSpec.describe "API v3 storage folders", :webmock, content_type: :json do | ||
include API::V3::Utilities::PathHelper | ||
include StorageServerHelpers | ||
|
||
let(:permissions) { %i(view_work_packages view_file_links manage_file_links) } | ||
let(:project) { create(:project) } | ||
|
||
let(:current_user) do | ||
create(:user, member_with_permissions: { project => permissions }) | ||
end | ||
|
||
let(:oauth_application) { create(:oauth_application) } | ||
let(:storage) { create(:nextcloud_storage_configured, creator: current_user, oauth_application:) } | ||
let(:oauth_token) { create(:oauth_client_token, user: current_user, oauth_client: storage.oauth_client) } | ||
let(:project_storage) { create(:project_storage, project:, storage:) } | ||
|
||
subject(:last_response) { post(path, body) } | ||
|
||
before do | ||
oauth_application | ||
project_storage | ||
login_as current_user | ||
end | ||
|
||
describe "POST /api/v3/storages/:storage_id/folders" do | ||
let(:path) { api_v3_paths.storage_folders(storage.id) } | ||
let(:body) { { parent_id: "1234", name: folder_name }.to_json } | ||
let(:folder_name) { "TestFolder" } | ||
|
||
let(:response) do | ||
Storages::StorageFile.new( | ||
id: 1, | ||
name: folder_name, | ||
size: 128, | ||
mime_type: "application/x-op-directory", | ||
created_at: DateTime.now, | ||
last_modified_at: DateTime.now, | ||
created_by_name: "Obi-Wan Kenobi", | ||
last_modified_by_name: "Obi-Wan Kenobi", | ||
location: "/", | ||
permissions: %i[readable] | ||
) | ||
end | ||
|
||
let(:file_info) do | ||
Storages::StorageFileInfo.new( | ||
status: "OK", | ||
status_code: 200, | ||
id: SecureRandom.hex, | ||
name: "/", | ||
location: "/" | ||
) | ||
end | ||
|
||
before do | ||
Storages::Peripherals::Registry.stub( | ||
"nextcloud.queries.file_info", | ||
->(_) { ServiceResult.success(result: file_info) } | ||
) | ||
end | ||
|
||
context "with successful response" do | ||
subject { last_response.body } | ||
|
||
before do | ||
Storages::Peripherals::Registry.stub( | ||
"nextcloud.commands.create_folder", | ||
->(_) { ServiceResult.success(result: response) } | ||
) | ||
end | ||
|
||
it "responds with appropriate JSON" do | ||
expect(subject).to be_json_eql(response.id.to_json).at_path("id") | ||
expect(subject).to be_json_eql(response.name.to_json).at_path("name") | ||
expect(subject).to be_json_eql(response.permissions.to_json).at_path("permissions") | ||
end | ||
end | ||
|
||
context "with query failed" do | ||
before do | ||
Storages::Peripherals::Registry.stub( | ||
"nextcloud.commands.create_folder", | ||
->(_) { ServiceResult.failure(result: error, errors: Storages::StorageError.new(code: error)) } | ||
) | ||
end | ||
|
||
context "with authorization failure" do | ||
let(:error) { :unauthorized } | ||
|
||
it { expect(last_response).to have_http_status(:internal_server_error) } | ||
end | ||
|
||
context "with internal error" do | ||
let(:error) { :error } | ||
|
||
it { expect(last_response).to have_http_status(:internal_server_error) } | ||
end | ||
end | ||
end | ||
end |