Skip to content

Commit ef97f05

Browse files
authored
[N/A] Add expand support to SharePoint DriveItemQuery (#21705)
Add `expand` support to SharePoint `DriveItemQuery` Enhanced the DriveItemQuery to support the $expand query parameter in addition to $select. Added corresponding RSpec tests and VCR cassettes to verify both select and expand functionality.
1 parent d3904d7 commit ef97f05

File tree

4 files changed

+319
-8
lines changed

4 files changed

+319
-8
lines changed

modules/storages/app/common/storages/adapters/providers/sharepoint/queries/internal/drive_item_query.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,22 @@ module Sharepoint
3535
module Queries
3636
module Internal
3737
class DriveItemQuery < Base
38-
def call(http:, drive_id:, item_id:, fields: [])
39-
select_url_query = if fields.empty?
40-
""
41-
else
42-
"?$select=#{fields.join(',')}"
43-
end
44-
45-
handle_response http.get("#{request_uri(drive_id:, item_id:)}#{select_url_query}")
38+
def call(http:, drive_id:, item_id:, fields: [], expand: [])
39+
handle_response http.get("#{request_uri(drive_id:, item_id:)}#{query_string(fields:, expand:)}")
4640
end
4741

4842
private
4943

44+
def query_string(fields:, expand:)
45+
params = []
46+
params << "$select=#{fields.join(',')}" if fields.any?
47+
params << "$expand=#{expand.join(',')}" if expand.any?
48+
49+
return "" if params.empty?
50+
51+
"?#{params.join('&')}"
52+
end
53+
5054
def handle_response(response)
5155
error = Results::Error.new(payload: response, source: self.class)
5256

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
require "spec_helper"
32+
require_module_spec_helper
33+
34+
module Storages
35+
module Adapters
36+
module Providers
37+
module Sharepoint
38+
module Queries
39+
RSpec.describe Internal::DriveItemQuery, :vcr, :webmock do
40+
let(:storage) { create(:sharepoint_storage, :sandbox) }
41+
let(:auth_strategy) { Adapters::Registry["sharepoint.authentication.userless"].call }
42+
let(:drive_id) { "b!FeOZEMfQx0eGQKqVBLcP__BG8mq-4-9FuRqOyk3MXY8Qconfm2i6SKEoCmuGYqQK" }
43+
let(:item_id) { Peripherals::ParentFolder.new("01ANJ53W6FB7TEDYKAZVCKYK5WKIX66ZTF") }
44+
45+
subject(:query) { described_class.new(storage) }
46+
47+
context "with selected fields", vcr: "sharepoint/drive_item_query_select" do
48+
it "returns the drive item payload" do
49+
result = Authentication[auth_strategy].call(storage:) do |http|
50+
query.call(http:, drive_id:, item_id:, fields: %w[id name])
51+
end
52+
53+
expect(result).to be_success
54+
expect(result.value!).to include(
55+
id: "01ANJ53W6FB7TEDYKAZVCKYK5WKIX66ZTF",
56+
name: "Berlin's office Christmas"
57+
)
58+
end
59+
end
60+
61+
context "with expand requested", vcr: "sharepoint/drive_item_query_expand" do
62+
it "returns the expanded list item payload" do
63+
result = Authentication[auth_strategy].call(storage:) do |http|
64+
query.call(http:, drive_id:, item_id:, fields: %w[id name], expand: %w[listItem])
65+
end
66+
67+
expect(result).to be_success
68+
expect(result.value!).to include(:listItem)
69+
expect(result.value!.dig(:listItem, :parentReference, :id))
70+
.to eq("0c9632a6-7219-4422-b66f-324a6f61eecd")
71+
end
72+
end
73+
end
74+
end
75+
end
76+
end
77+
end
78+
end

modules/storages/spec/support/fixtures/vcr_cassettes/sharepoint/drive_item_query_expand.yml

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/storages/spec/support/fixtures/vcr_cassettes/sharepoint/drive_item_query_select.yml

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)