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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#64193] add download query for share point
Eric Schubertf2b83ab3602c
diff --git a/modules/storages/app/common/storages/adapters/providers/share_point/queries/download_link_query.rb b/modules/storages/app/common/storages/adapters/providers/share_point/queries/download_link_query.rbnew file mode 100644index 00000000000..2fceddde675--- /dev/null+++ b/modules/storages/app/common/storages/adapters/providers/share_point/queries/download_link_query.rb@@ -0,0 +1,71 @@+# 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 Storages+ module Adapters+ module Providers+ module SharePoint+ module Queries+ class DownloadLinkQuery < Base+ def call(auth_strategy:, input_data:)+ Authentication[auth_strategy].call(storage: @storage) do |http|+ uri = request_uri(**split_identifier(input_data.file_link.origin_id))+ handle_errors http.get(uri)+ end+ end++ private++ def handle_errors(response)+ error = Results::Error.new(source: self.class, payload: response)++ case response+ in { status: 300..399 }+ Success(URI(response.headers["Location"]))+ in { status: 404 }+ Failure(error.with(code: :not_found))+ in { status: 403 }+ Failure(error.with(code: :forbidden))+ in { status: 401 }+ Failure(error.with(code: :unauthorized))+ else+ Failure(error.with(code: :error))+ end+ end++ def request_uri(drive_id:, location:)+ UrlBuilder.url(base_uri, "/v1.0/drives", drive_id, "items", location, "content")+ end+ end+ end+ end+ end+ end+enddiff --git a/modules/storages/app/common/storages/adapters/providers/share_point/queries/internal/children_query.rb b/modules/storages/app/common/storages/adapters/providers/share_point/queries/internal/children_query.rbindex 7a1037cd950..1f00ee69600 100644--- a/modules/storages/app/common/storages/adapters/providers/share_point/queries/internal/children_query.rb+++ b/modules/storages/app/common/storages/adapters/providers/share_point/queries/internal/children_query.rb@@ -59,14 +59,14 @@ module Storagesdef request_uri(drive_id, location)if location.root?- UrlBuilder.url(base_uri, "/v1.0/drives/#{drive_id}/root/children")+ UrlBuilder.url(base_uri, "/v1.0/drives", drive_id, "/root/children")else- UrlBuilder.url(base_uri, "/v1.0/drives/#{drive_id}/root:#{UrlBuilder.path(location.path)}:/children")+ UrlBuilder.url(base_uri, "/v1.0/drives", drive_id, "/root:#{UrlBuilder.path(location.path)}:/children")endenddef folder_uri(drive_id, folder)- base_url = UrlBuilder.url(base_uri, "/v1.0/drives/#{drive_id}/root")+ base_url = UrlBuilder.url(base_uri, "/v1.0/drives", drive_id, "/root")return base_url if folder.root?"#{base_url}:#{UrlBuilder.path(folder.path)}"diff --git a/modules/storages/spec/common/storages/adapters/providers/share_point/queries/download_link_query_spec.rb b/modules/storages/spec/common/storages/adapters/providers/share_point/queries/download_link_query_spec.rbnew file mode 100644index 00000000000..333140fd042--- /dev/null+++ b/modules/storages/spec/common/storages/adapters/providers/share_point/queries/download_link_query_spec.rb@@ -0,0 +1,101 @@+# 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++module Storages+ module Adapters+ module Providers+ module SharePoint+ module Queries+ RSpec.describe DownloadLinkQuery, :vcr, :webmock do+ let(:user) { create(:user) }+ let(:storage) { create(:share_point_dev_storage, oauth_client_token_user: user) }+ let(:auth_strategy) { Registry["one_drive.authentication.user_bound"].call(user, storage) }++ let(:drive_id) { "b!FeOZEMfQx0eGQKqVBLcP__BG8mq-4-9FuRqOyk3MXY87vnZ6fgfvQanZHX-XCAyw" }+ let(:file_link) { create(:file_link, origin_id: "#{drive_id}||01ANJ53W4ELLSQL3JZHNA2MHKKHKAUQWNS") }+ let(:not_existent_file_link) { create(:file_link, origin_id: "#{drive_id}||IHaveTheHighGround") }+ let(:folder_file_link) { create(:file_link, origin_id: "#{drive_id}||01ANJ53W4MWZNYS4FOTNDI3UVTLEXUZ5TQ") }++ let(:input_data) { Input::DownloadLink.build(file_link:).value! }++ subject { described_class.new(storage) }++ describe "#call" do+ it "responds with correct parameters" do+ expect(described_class).to respond_to(:call)++ method = described_class.method(:call)+ expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq auth_strategy], %i[keyreq input_data])+ end++ context "with outbound request successful" do+ it "returns a result with a download url", vcr: "share_point/download_link_query_success" do+ download_link = subject.call(auth_strategy:, input_data:)++ expect(download_link).to be_success++ uri = download_link.value!+ expect(uri.host).to eq("ymt6d.sharepoint.com")+ expect(uri.path).to eq("/sites/OPTest/_layouts/15/download.aspx")+ end++ it "returns an error on folders", vcr: "share_point/download_link_query_folder" do+ input_data = Input::DownloadLink.build(file_link: folder_file_link).value!++ download_link = subject.call(auth_strategy:, input_data:)++ expect(download_link).to be_failure++ error = download_link.failure+ expect(error.source).to eq(described_class)+ expect(error.code).to eq(:not_found)+ end++ it "returns an error if the file is not found", vcr: "share_point/download_link_query_not_found" do+ input_data = Input::DownloadLink.build(file_link: not_existent_file_link).value!++ download_link = subject.call(auth_strategy:, input_data:)+ expect(download_link).to be_failure++ error = download_link.failure+ expect(error.source).to eq(described_class)+ expect(error.code).to eq(:not_found)+ end+ end+ end+ end+ end+ end+ end+ end+enddiff --git a/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_folder.yml b/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_folder.ymlnew file mode 100644index 00000000000..c8dbc2c9918--- /dev/null+++ b/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_folder.yml@@ -0,0 +1,45 @@+---+http_interactions:+- request:+ method: get+ uri: https://graph.microsoft.com/v1.0/drives/b!FeOZEMfQx0eGQKqVBLcP__BG8mq-4-9FuRqOyk3MXY87vnZ6fgfvQanZHX-XCAyw/items/01ANJ53W4MWZNYS4FOTNDI3UVTLEXUZ5TQ/content+ body:+ encoding: US-ASCII+ string: ''+ headers:+ Authorization:+ - Bearer <BEARER TOKEN>+ User-Agent:+ - httpx.rb/1.3.4+ Accept:+ - "*/*"+ Accept-Encoding:+ - gzip, deflate+ response:+ status:+ code: 404+ message: Not Found+ headers:+ Cache-Control:+ - no-store, no-cache+ Content-Type:+ - application/octet-stream+ Strict-Transport-Security:+ - max-age=31536000+ Request-Id:+ - db3e2524-7065-473e-b60b-7ee87e84def9+ Client-Request-Id:+ - db3e2524-7065-473e-b60b-7ee87e84def9+ X-Ms-Ags-Diagnostic:+ - '{"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"4","ScaleUnit":"003","RoleInstance":"FR1PEPF00001378"}}'+ Splogid:+ - e4c1b9a1-e04f-0000-7534-c9510299716f+ Date:+ - Fri, 08 Aug 2025 13:49:17 GMT+ Content-Length:+ - '0'+ body:+ encoding: UTF-8+ string: ''+ recorded_at: Fri, 08 Aug 2025 13:49:17 GMT+recorded_with: VCR 6.3.1diff --git a/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_not_found.yml b/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_not_found.ymlnew file mode 100644index 00000000000..cfba31337ed--- /dev/null+++ b/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_not_found.yml@@ -0,0 +1,47 @@+---+http_interactions:+- request:+ method: get+ uri: https://graph.microsoft.com/v1.0/drives/b!FeOZEMfQx0eGQKqVBLcP__BG8mq-4-9FuRqOyk3MXY87vnZ6fgfvQanZHX-XCAyw/items/IHaveTheHighGround/content+ body:+ encoding: US-ASCII+ string: ''+ headers:+ Authorization:+ - Bearer <BEARER TOKEN>+ User-Agent:+ - httpx.rb/1.3.4+ Accept:+ - "*/*"+ Accept-Encoding:+ - gzip, deflate+ response:+ status:+ code: 404+ message: Not Found+ headers:+ Cache-Control:+ - no-store, no-cache+ Content-Type:+ - application/json; charset=utf-8+ Content-Encoding:+ - gzip+ Vary:+ - Accept-Encoding+ Strict-Transport-Security:+ - max-age=31536000+ Request-Id:+ - 133c0a57-0479-4e0b-9c4b-3a0370ff546c+ Client-Request-Id:+ - 133c0a57-0479-4e0b-9c4b-3a0370ff546c+ X-Ms-Ags-Diagnostic:+ - '{"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"4","ScaleUnit":"008","RoleInstance":"FR3PEPF00000CB3"}}'+ Splogid:+ - 8ac1b9a1-602b-0000-7534-c440de5133ee+ Date:+ - Fri, 08 Aug 2025 13:43:08 GMT+ body:+ encoding: UTF-8+ string: '{"error":{"code":"itemNotFound","message":"Item not found"}}'+ recorded_at: Fri, 08 Aug 2025 13:43:08 GMT+recorded_with: VCR 6.3.1diff --git a/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_success.yml b/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_success.ymlnew file mode 100644index 00000000000..c8950a0f3c3--- /dev/null+++ b/modules/storages/spec/support/fixtures/vcr_cassettes/share_point/download_link_query_success.yml@@ -0,0 +1,47 @@+---+http_interactions:+- request:+ method: get+ uri: https://graph.microsoft.com/v1.0/drives/b!FeOZEMfQx0eGQKqVBLcP__BG8mq-4-9FuRqOyk3MXY87vnZ6fgfvQanZHX-XCAyw/items/01ANJ53W4ELLSQL3JZHNA2MHKKHKAUQWNS/content+ body:+ encoding: US-ASCII+ string: ''+ headers:+ Authorization:+ - Bearer <BEARER TOKEN>+ User-Agent:+ - httpx.rb/1.3.4+ Accept:+ - "*/*"+ Accept-Encoding:+ - gzip, deflate+ response:+ status:+ code: 302+ message: Found+ headers:+ Cache-Control:+ - no-store, no-cache+ Content-Type:+ - application/octet-stream+ Location:+ - https://ymt6d.sharepoint.com/sites/OPTest/_layouts/15/download.aspx?UniqueId=05e55a84-39ed-413b-a61d-4a3a814859b2&Translate=false&tempauth=v1.eyJzaXRlaWQiOiIxMDk5ZTMxNS1kMGM3LTQ3YzctODY0MC1hYTk1MDRiNzBmZmYiLCJhcHBfZGlzcGxheW5hbWUiOiJPUCBTZWxlY3RlZCIsImFwcGlkIjoiNDA0ZTA1NjctMTlhMS00YTY0LWIyZDEtZDNiMGM5OWYyZWQ5IiwiYXVkIjoiMDAwMDAwMDMtMDAwMC0wZmYxLWNlMDAtMDAwMDAwMDAwMDAwL3ltdDZkLnNoYXJlcG9pbnQuY29tQGUzNmYxZGJjLWZkYWUtNDI3ZS1iNjFiLTBkOTZkZGZiODFhNCIsImV4cCI6IjE3NTQ2NjQwODcifQ.CkAKDGVudHJhX2NsYWltcxIwQ05YNTE4UUdFQUFhRm05RFVqZFhXV1IyV2xWeFZsRjRTVmh2UTFGTVFVRXFBQT09CjIKCmFjdG9yYXBwaWQSJDAwMDAwMDAzLTAwMDAtMDAwMC1jMDAwLTAwMDAwMDAwMDAwMAoKCgRzbmlkEgI2NBILCNC55OTpoqs-EAUaDTIwLjE5MC4xOTAuOTYqLFFVV1lMYUROSWRtTWNXemNIRW03YTJ5Q0NJcUhwWWVidXB4QUw4c2RVZnc9MIEBOAFCEKG5wXFqoAAAdTTPd_oT94ZKEGhhc2hlZHByb29mdG9rZW5SCFsia21zaSJdaiQ0MjNkNTYyMC1lNjlkLTRlMmYtYWM3ZS1jZTk2ODRmM2VhOTRyKTBoLmZ8bWVtYmVyc2hpcHwxMDAzMjAwMzIzZTFmNzg1QGxpdmUuY29tegEyggESCbwdb-Ou_X5CEbYbDZbd-4GkkgEERXJpY5oBCFNjaHViZXJ0ogEeb3AuYWRtaW5AeW10NmQub25taWNyb3NvZnQuY29tqgEQMTAwMzIwMDMyM0UxRjc4NbIBH2FsbGZpbGVzLndyaXRlIGFsbHByb2ZpbGVzLnJlYWTIAQE.dgiaNP9B8ptIC1y88oFyxmqW5sIfsnRsMXCjBpRmWrw&ApiVersion=2.0+ Strict-Transport-Security:+ - max-age=31536000+ Request-Id:+ - f25d6a8d-fb2f-45c2-9f54-7b476dfe1e60+ Client-Request-Id:+ - f25d6a8d-fb2f-45c2-9f54-7b476dfe1e60+ X-Ms-Ags-Diagnostic:+ - '{"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"4","ScaleUnit":"003","RoleInstance":"FR1PEPF00000AF4"}}'+ Splogid:+ - 71c1b9a1-a06a-0000-7534-cf77fa13f786+ Date:+ - Fri, 08 Aug 2025 13:41:26 GMT+ Content-Length:+ - '0'+ body:+ encoding: UTF-8+ string: ''+ recorded_at: Fri, 08 Aug 2025 13:41:27 GMT+recorded_with: VCR 6.3.1