Skip to content

Commit

Permalink
[#49124] added new unit tests and fixed changed ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharonus committed Sep 20, 2023
1 parent 90e3462 commit 0e375de
Show file tree
Hide file tree
Showing 14 changed files with 1,748 additions and 1,481 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ ij_json_keep_indents_on_empty_lines = false
ij_json_keep_line_breaks = true
ij_json_space_after_colon = true
ij_json_space_after_comma = true
ij_json_space_before_colon = true
ij_json_space_before_colon = false
ij_json_space_before_comma = false
ij_json_spaces_within_braces = false
ij_json_spaces_within_brackets = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module Peripherals
module StorageInteraction
module OneDrive
class OpenLinkQuery
using ::Storages::Peripherals::ServiceResultRefinements

def self.call(storage:, user:, file_id:, open_location: false)
new(storage).call(user:, file_id:, open_location:)
end
Expand All @@ -41,21 +43,41 @@ def initialize(storage)
@delegate = Internal::DriveItemQuery.new(storage)
end

# TODO: open in location
# rubocop:disable Lint/UnusedMethodArgument
def call(user:, file_id:, open_location: false)
@delegate.call(user:, drive_item_id: file_id, fields: %w[webUrl]).map(&web_url)
end
@user = user

# rubocop:enable Lint/UnusedMethodArgument
if open_location
request_parent_id.call(file_id) >> request_web_url
else
request_web_url.call(file_id)
end
end

private

def request_web_url
->(file_id) do
@delegate.call(user: @user, drive_item_id: file_id, fields: %w[webUrl]).map(&web_url)
end
end

def request_parent_id
->(file_id) do
@delegate.call(user: @user, drive_item_id: file_id, fields: %w[parentReference]).map(&parent_id)
end
end

def web_url
->(json) do
json[:webUrl]
end
end

def parent_id
->(json) do
json.dig(:parentReference, :id)
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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::OpenLinkQuery do
include JsonResponseHelper

let(:storage) { create(:nextcloud_storage, host: 'https://example.com/html') }
let(:user) { create(:user) }
let(:file_id) { '1337' }

it 'responds to .call' 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 file_id], %i[key open_location])
end

it 'returns the url for opening the file on storage' do
url = described_class.call(storage:, user:, file_id:).result
expect(url).to eq("#{storage.host}/index.php/f/#{file_id}?openfile=1")
end

it 'returns the url for opening the file\'s location on storage' do
url = described_class.call(storage:, user:, file_id:, open_location: true).result
expect(url).to eq("#{storage.host}/index.php/f/#{file_id}?openfile=0")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@
let(:not_found_json) { not_found_response }
let(:forbidden_json) { forbidden_response }

before do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_id}")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: read_json('folder_drive_item'), headers: {})
end

it 'responds to .call' do
expect(described_class).to respond_to(:call)

Expand All @@ -59,6 +53,10 @@
end

it 'returns a storage file info object' do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_id}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: read_json('folder_drive_item'), headers: {})

storage_file_info = described_class.call(storage:, user:, file_id:).result

# rubocop:disable Layout/LineLength
Expand All @@ -84,7 +82,7 @@

describe 'error handling' do
it 'returns a notfound error if the API call returns a 404' do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_id}")
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_id}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 404, body: not_found_json, headers: {})

Expand All @@ -96,7 +94,7 @@
end

it 'returns a forbidden error if the API call returns a 403' do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_id}")
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_id}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 403, body: forbidden_json, headers: {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
let(:file_ids) do
%w(
01BYE5RZ5MYLM2SMX75ZBIPQZIHT6OAYPB
01BYE5RZZ6FUE5272C5JCY3L7CLZ7XOUYM
01BYE5RZ47DTJGHO73WBH2ONNXQZVNNILJ
01BYE5RZ7T3DFLFS6TCRH2QAPWXL5APDLE
01BYE5RZ4VAJVBMWSWINA2QYFFNZ2GL3O5
not_existent
forbidden
)
Expand All @@ -54,19 +54,19 @@
let(:forbidden_json) { forbidden_response }

before do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[0]}")
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[0]}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: read_json('folder_drive_item'), headers: {})
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[1]}")
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[1]}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: read_json('file_drive_item_1'), headers: {})
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[2]}")
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[2]}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: read_json('file_drive_item_2'), headers: {})
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[3]}")
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[3]}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 404, body: not_found_json, headers: {})
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[4]}")
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/items/#{file_ids[4]}?$select=id,name,fileSystemInfo,file,size,createdBy,lastModifiedBy,parentReference")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 403, body: forbidden_json, headers: {})
end
Expand Down Expand Up @@ -111,25 +111,23 @@
it 'returns a file storage file info object' do
storage_file_info = described_class.call(storage:, user:, file_ids: file_ids.slice(1, 1)).result[0]

# rubocop:disable Layout/LineLength
expect(storage_file_info.to_h).to eq({
status: 'ok',
status_code: 200,
id: '01BYE5RZZ6FUE5272C5JCY3L7CLZ7XOUYM',
name: 'All Japan Revenues By City.xlsx',
size: 20051,
id: '01BYE5RZ7T3DFLFS6TCRH2QAPWXL5APDLE',
name: 'Popular Mixed Drinks.xlsx',
size: 7064929,
mime_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
created_at: Time.parse('2017-08-07T16:07:10Z'),
last_modified_at: Time.parse('2017-08-07T16:07:10Z'),
created_at: Time.parse('2017-08-07T16:16:53Z'),
last_modified_at: Time.parse('2017-08-07T16:16:53Z'),
owner_name: 'Megan Bowen',
owner_id: '48d31887-5fad-4d73-a9f5-3c356e68a038',
last_modified_by_id: '48d31887-5fad-4d73-a9f5-3c356e68a038',
last_modified_by_name: 'Megan Bowen',
permissions: nil,
trashed: false,
location: '/drives/b!-RIj2DuyvEyV1T4NlOaMHk8XkS_I8MdFlUCq1BlcjgmhRfAj3-Z8RY2VpuvV_tpd/root:'
location: '/drive/root:/Business Data'
})
# rubocop:enable Layout/LineLength
end

it 'returns an error storage file info object for not found' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
drive_id: 'b!-RIj2DuyvEyV1T4NlOaMHk8XkS_I8MdFlUCq1BlcjgmhRfAj3-Z8RY2VpuvV_tpd')
end
let(:user) { create(:user) }
let(:json) { read_json('root_drive') }
let(:json) { read_json('root_drive_children') }
let(:token) { create(:oauth_client_token, user:, oauth_client: storage.oauth_client) }

it 'responds to .call' do
Expand Down
Original file line number Diff line number Diff line change
@@ -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::OneDrive::OpenDriveLinkQuery, :webmock do
include JsonResponseHelper

let(:storage) do
create(:one_drive_storage,
:with_oauth_client,
drive_id: 'b!-RIj2DuyvEyV1T4NlOaMHk8XkS_I8MdFlUCq1BlcjgmhRfAj3-Z8RY2VpuvV_tpd')
end
let(:user) { create(:user) }
let(:token) { create(:oauth_client_token, user:, oauth_client: storage.oauth_client) }
let(:not_found_json) { not_found_response }
let(:forbidden_json) { forbidden_response }

it 'responds to .call' 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])
end

it 'returns the url for opening the drive root on storage' do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}?$select=webUrl")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: read_json('root_drive'), headers: {})

url = described_class.call(storage:, user:).result
expect(url).to eq('https://m365x214355-my.sharepoint.com/personal/meganb_m365x214355_onmicrosoft_com/Documents')
end

describe 'error handling' do
it 'returns a notfound error if the API call returns a 404' do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}?$select=webUrl")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 404, body: not_found_json, headers: {})

open_drive_link_result = described_class.call(storage:, user:)

expect(open_drive_link_result).to be_failure
expect(open_drive_link_result.result).to eq(:not_found)
expect(open_drive_link_result.errors.data.to_json).to eq(not_found_json)
end

it 'returns a forbidden error if the API call returns a 403' do
stub_request(:get, "https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}?$select=webUrl")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 403, body: forbidden_json, headers: {})

open_drive_link_result = described_class.call(storage:, user:)

expect(open_drive_link_result).to be_failure
expect(open_drive_link_result.result).to eq(:forbidden)
expect(open_drive_link_result.errors.data.to_json).to eq(forbidden_json)
end
end
end
Loading

0 comments on commit 0e375de

Please sign in to comment.