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

Implement the UploadLinkQuery for OneDrive

Marcello Rocha0cf1f791567d
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
new file mode 100644
index 00000000000..8903bd2c4c0
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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
+ module Peripherals
+ module StorageInteraction
+ module OneDrive
+ class UploadLinkQuery
+ def initialize(storage)
+ @storage = storage
+ @uri = storage.uri
+ end
+
+ def self.call(storage:, user:, data:)
+ new(storage).call(user:, data:)
+ end
+
+ def call(user:, data:)
+ folder, filename = data.slice(:parent, :fileName).values
+
+ Util.using_user_token(@storage, user) do |token|
+ response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ http.post(uri_path_for(folder, filename),
+ payload(filename),
+ { 'Authorization' => "Bearer #{token.access_token}", 'Content-Type' => 'application/json' })
+ end
+
+ handle_response(response)
+ end
+ end
+
+ private
+
+ def payload(filename)
+ { item: { "@microsoft.graph.conflictBehavior" => "rename", name: filename } }.to_json
+ end
+
+ def handle_response(response)
+ case response
+ when Net::HTTPSuccess
+ upload_url = MultiJson.load(response.body, symbolize_keys: true)[:uploadUrl]
+ ServiceResult.success(result: ::Storages::UploadLink.new(URI(upload_url)))
+ when Net::HTTPNotFound
+ ServiceResult.failure(result: :not_found, errors: ::Storages::StorageError.new(code: :not_found))
+ when Net::HTTPUnauthorized
+ ServiceResult.failure(result: :unauthorized, errors: ::Storages::StorageError.new(code: :unauthorized))
+ else
+ ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error))
+ end
+ end
+
+ def uri_path_for(folder, filename)
+ "/v1.0/drives/#{@storage.drive_id}/items/#{folder}:/#{filename}:/createUploadSession"
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/common/storages/peripherals/registry_spec.rb b/modules/storages/spec/common/storages/peripherals/registry_spec.rb
index 6144ec65261..335f38a733c 100644
--- a/modules/storages/spec/common/storages/peripherals/registry_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/registry_spec.rb
@@ -56,88 +56,6 @@ RSpec.describe Storages::Peripherals::Registry, :webmock do
end
end
- context 'when requests depend on OAuth token' do
- let(:token) do
- create(:oauth_client_token, origin_user_id:, access_token: 'xyz', oauth_client:, user:)
- end
- let(:oauth_client) { create(:oauth_client, integration: storage) }
-
- before { token }
-
- describe '#upload_link_query' do
- let(:query_payload) { Struct.new(:parent).new(42) }
- let(:upload_token) { 'valid-token' }
-
- before do
- stub_request(:post, "#{url}/index.php/apps/integration_openproject/direct-upload-token")
- .with(body: { folder_id: query_payload.parent })
- .to_return(
- status: 200,
- body: {
- token: upload_token,
- expires_on: 1673883865
- }.to_json
- )
- end
-
- describe 'with Nextcloud storage type selected' do
- it 'must return an upload link URL' do
- link = registry.resolve('queries.nextcloud.upload_link').call(storage:, user:, data: query_payload)
- .result
- expect(link.destination.path).to eql("/index.php/apps/integration_openproject/direct-upload/#{upload_token}")
- expect(link.destination.host).to eql(URI(url).host)
- expect(link.destination.scheme).to eql(URI(url).scheme)
- expect(link.destination.user).to be_nil
- expect(link.destination.password).to be_nil
- expect(link.method).to eq(:post)
- end
- end
-
- describe 'with not supported storage type selected' do
- before do
- allow(storage).to receive(:provider_type).and_return('not_supported_storage_type')
- end
-
- it 'must raise ArgumentError' do
- expect { registry.resolve('queries.nextcloud.upload_link').call(storage:, user:) }.to raise_error(ArgumentError)
- end
- end
-
- describe 'with missing OAuth token' do
- before do
- instance = instance_double(OAuthClients::ConnectionManager)
- allow(OAuthClients::ConnectionManager).to receive(:new).and_return(instance)
- allow(instance).to receive(:get_access_token).and_return(ServiceResult.failure)
- end
-
- it 'must return ":unauthorized" ServiceResult' do
- result = registry.resolve('queries.nextcloud.upload_link').call(storage:, user:, data: query_payload)
- expect(result).to be_failure
- expect(result.errors.code).to be(:unauthorized)
- end
- end
-
- shared_examples_for 'outbound is failing' do |code, symbol|
- describe "with outbound request returning #{code}" do
- before do
- stub_request(:post, "#{url}/index.php/apps/integration_openproject/direct-upload-token").to_return(status: code)
- end
-
- it "must return :#{symbol} ServiceResult" do
- result = registry.resolve('queries.nextcloud.upload_link').call(storage:, user:, data: query_payload)
- expect(result).to be_failure
- expect(result.errors.code).to be(symbol)
- end
- end
- end
-
- include_examples 'outbound is failing', 400, :error
- include_examples 'outbound is failing', 401, :unauthorized
- include_examples 'outbound is failing', 404, :not_found
- include_examples 'outbound is failing', 500, :error
- end
- end
-
describe '#group_users_query' do
let(:expected_response_body) do
<<~XML
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query_spec.rb
new file mode 100644
index 00000000000..e332c19c030
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query_spec.rb
@@ -0,0 +1,88 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 Storages::Peripherals::StorageInteraction::Nextcloud::UploadLinkQuery, :webmock do
+ let(:storage) { create(:nextcloud_storage, :with_oauth_client) }
+ let(:token) { create(:oauth_client_token, oauth_client: storage.oauth_client) }
+ let(:user) { token.user }
+ let(:url) { storage.uri.to_s }
+
+ let(:query_payload) { Struct.new(:parent).new(42) }
+ let(:upload_token) { 'valid-token' }
+
+ subject(:query) { described_class }
+
+ before do
+ allow(Storages::Peripherals::StorageInteraction::Nextcloud::Util).to receive(:token).and_yield(token)
+ stub_request(:post, "#{url}index.php/apps/integration_openproject/direct-upload-token")
+ .with(body: { folder_id: query_payload.parent })
+ .to_return(status: 200, body: { token: upload_token, expires_on: 1673883865 }.to_json)
+ end
+
+ it '.call requires 3 arguments: storage, user, and data' do
+ expect(described_class).to respond_to(:call)
+
+ method = described_class.method(:call)
+ expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq user], %i[keyreq data])
+ end
+
+ it 'must return an upload link URL' do
+ link = query.call(storage:, user:, data: query_payload).result
+ expect(link.destination.path).to eql("/index.php/apps/integration_openproject/direct-upload/#{upload_token}")
+ expect(link.destination.host).to eql(URI(url).host)
+ expect(link.destination.scheme).to eql(URI(url).scheme)
+ expect(link.destination.user).to be_nil
+ expect(link.destination.password).to be_nil
+ expect(link.method).to eq(:post)
+ end
+
+ shared_examples_for 'outbound is failing' do |code, symbol|
+ describe "with outbound request returning #{code}" do
+ before do
+ stub_request(:post, "#{url}index.php/apps/integration_openproject/direct-upload-token").and_return(status: code)
+ end
+
+ it "must return :#{symbol} ServiceResult" do
+ result = query.call(storage:, user:, data: query_payload)
+
+ expect(result).to be_failure
+ expect(result.errors.code).to be(symbol)
+ end
+ end
+ end
+
+ include_examples 'outbound is failing', 400, :error
+ include_examples 'outbound is failing', 401, :unauthorized
+ include_examples 'outbound is failing', 404, :not_found
+ include_examples 'outbound is failing', 500, :error
+end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/upload_link_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/upload_link_query_spec.rb
new file mode 100644
index 00000000000..b3b4868def7
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/upload_link_query_spec.rb
@@ -0,0 +1,93 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 Storages::Peripherals::StorageInteraction::OneDrive::UploadLinkQuery, :webmock do
+ let(:storage) { create(:one_drive_storage, :with_oauth_client, drive_id: 'b!~bunchOfLettersAndNumb3rs') }
+ let(:token) { create(:oauth_client_token, oauth_client: storage.oauth_client) }
+ let(:user) { token.user }
+
+ # Need to verify the actual object
+ let(:query_payload) { { parent: 'LFHLUDSILANC', fileName: 'it_is_a_trap.flac' } }
+
+ subject(:upload_query) { described_class.new(storage) }
+
+ before do
+ stub_request(
+ :post,
+ "https://graph.microsoft.com/v1.0/drives/b!~bunchOfLettersAndNumb3rs/items/LFHLUDSILANC:/it_is_a_trap.flac:/createUploadSession"
+ ).with(
+ headers: { 'Authorization' => "Bearer #{token.access_token}", 'Content-Type' => 'application/json' },
+ body: { item: { "@microsoft.graph.conflictBehavior" => "rename", name: query_payload[:fileName] } }
+ ).to_return(
+ status: 200,
+ body: { uploadUrl: "https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337",
+ expirationDateTime: "2015-01-29T09:21:55.523Z" }.to_json
+ )
+ end
+
+ it '.call requires 3 arguments: storage, user, and data' do
+ expect(described_class).to respond_to(:call)
+
+ method = described_class.method(:call)
+ expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq user], %i[keyreq data])
+ end
+
+ it 'must return an upload link URL' do
+ link = upload_query.call(user:, data: query_payload).result
+
+ expect(link.destination).not_to be_nil
+ expect(link.method).to eq(:post)
+ end
+
+ shared_examples_for 'outbound is failing' do |code, symbol|
+ describe "with outbound request returning #{code}" do
+ before do
+ stub_request(
+ :post,
+ "https://graph.microsoft.com/v1.0/drives/b!~bunchOfLettersAndNumb3rs/items/LFHLUDSILANC:/it_is_a_trap.flac:/createUploadSession"
+ ).to_return(status: code)
+ end
+
+ it "must return :#{symbol} ServiceResult" do
+ result = upload_query.call(user:, data: query_payload)
+ expect(result).to be_failure
+ expect(result.errors.code).to be(symbol)
+ end
+ end
+ end
+
+ include_examples 'outbound is failing', 400, :error
+ include_examples 'outbound is failing', 401, :unauthorized
+ include_examples 'outbound is failing', 404, :not_found
+ include_examples 'outbound is failing', 500, :error
+end