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

Initial mess

Marcello Rochac0d1aa7f1264
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
new file mode 100644
index 00000000000..919e8295a5a
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
@@ -0,0 +1,68 @@
+# 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 DownloadLinkQuery
+ def initialize(storage)
+ @storage = storage
+ @uri = storage.uri
+ end
+
+ def self.call(storage:, user:, file_link:)
+ new(storage).call(user:, file_link:)
+ end
+
+ def call(user:, file_link:)
+ Util.using_user_token(@storage, user) do |token|
+ response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ http.get(uri_path_for(file_link.origin_id), { 'Authorization' => "Bearer #{token.access_token}" })
+ end
+
+ if Net::HTTPRedirection === response
+ ServiceResult.success(result: response.header['Location'])
+ else
+ ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data: response.body))
+ end
+ end
+ end
+
+ private
+
+ def uri_path_for(file_id)
+ "/v1.0/drives/#{@storage.drive_id}/items/#{file_id}"
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/download_link_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/download_link_query_spec.rb
new file mode 100644
index 00000000000..f8725b45be9
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/download_link_query_spec.rb
@@ -0,0 +1,66 @@
+# 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::DownloadLinkQuery, :webmock do
+ let(:storage) { create(:one_drive_storage, :with_oauth_client, drive_id: 'JUMBLEOFLETTERSANDNUMB3R5') }
+ let(:user) { create(:user) }
+ let(:file_link) { create(:file_link) }
+ let(:token) { create(:oauth_client_token, oauth_client: storage.oauth_client, user:) }
+
+ subject(:download_link_query) { described_class.new(storage) }
+
+ before do
+ allow(Storages::Peripherals::StorageInteraction::OneDrive).to receive(:token).and_yield(token)
+ stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_link.origin_id}")
+ .with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
+ .and_return(status: 302, body: nil, headers: { 'Location' => 'https://somecool.link/from/microsoft' })
+ end
+
+ it 'returns a result with a download url' do
+ download_link = download_link_query.call(user:, file_link:)
+
+ expect(download_link).to be_success
+ expect(download_link.result).to eq('https://somecool.link/from/microsoft')
+ end
+
+ it 'return an error if any other response is received' do
+ stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_link.origin_id}")
+ .with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
+ .and_return(status: 200, body: "")
+
+ download_link = download_link_query.call(user:, file_link:)
+
+ expect(download_link).to be_failure
+ expect(download_link.result).to eq :error
+ end
+end