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
Merge remote-tracking branch 'origin/dev' into feature/46331-project-members-experience-a-fully-automatically-managed-project-folder
Pavel Balashov163ab48cb25a
diff --cc modules/storages/spec/common/peripherals/storage_requests_spec.rbindex cbda8f7af91,78b6fbe298e..fafcc7f541e--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb@@@ -28,586 -28,43 +28,588 @@@require 'spec_helper'- describe Storages::Peripherals::StorageRequests, webmock: true do+ RSpec.describe Storages::Peripherals::StorageRequests, webmock: true do+ using Storages::Peripherals::ServiceResultRefinements+- let(:user) { build_stubbed(:user) }-+ let(:user) { create(:user) }let(:url) { 'https://example.com' }let(:origin_user_id) { 'admin' }+ let(:storage) { build(:storage, host: url, password: 'OpenProjectSecurePassword') }++ subject { described_class.new(storage:) }++ 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 '#download_link_query' do+ let(:file_link) do+ Struct.new(:file_link) do+ def origin_id+ 42+ end++ def origin_name+ 'example.md'+ end+ end.new+ end+ let(:download_token) { "8dM3dC9iy1N74F5AJ0ClnjSF4dWTxfymVy1HTXBh8rbZVM81CpcBJaIYZvmR" }+ let(:uri) do+ "#{url}/index.php/apps/integration_openproject/direct/#{download_token}/#{CGI.escape(file_link.origin_name)}"+ end+ let(:json) do+ {+ ocs: {+ meta: {+ status: 'ok',+ statuscode: 200,+ message: 'OK'+ },+ data: {+ url: "https://example.com/remote.php/direct/#{download_token}"+ }+ }+ }.to_json+ end++ before do+ stub_request(:post, "#{url}/ocs/v2.php/apps/dav/api/v1/direct")+ .to_return(status: 200, body: json, headers: {})+ end++ describe 'with Nextcloud storage type selected' do+ it 'must return a download link URL' do+ result = subject+ .download_link_query+ .call(user:, file_link:)+ expect(result).to be_success+ expect(result.result).to be_eql(uri)+ end++ context 'if Nextcloud is running on a sub path' do+ let(:url) { 'https://example.com/html' }- let(:storage) do- build(:storage, host: url, password: 'OpenProjectSecurePassword')+ it 'must return a download link URL' do+ result = subject+ .download_link_query+ .call(user:, file_link:)+ expect(result).to be_success+ expect(result.result).to be_eql(uri)+ end+ end+ end++ describe 'with not supported storage type selected' do+ before do+ allow(storage).to receive(:provider_type).and_return('not_supported_storage_type'.freeze)+ end++ it 'must raise ArgumentError' do+ expect { subject.download_link_query }.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 ":not_authorized" ServiceResult' do+ result = subject+ .download_link_query+ .call(user:, file_link:)+ expect(result).to be_failure+ expect(result.errors.code).to be(:not_authorized)+ end+ end++ describe 'with outbound request returning 200 and an empty body' do+ before do+ stub_request(:post, "#{url}/ocs/v2.php/apps/dav/api/v1/direct").to_return(status: 200, body: '')+ end++ it 'must return :not_authorized ServiceResult' do+ result = subject+ .download_link_query+ .call(user:, file_link:)+ expect(result).to be_failure+ expect(result.errors.code).to be(:not_authorized)+ end+ end++ shared_examples_for 'outbound is failing' do |code = 500, symbol = :error|+ describe "with outbound request returning #{code}" do+ before do+ stub_request(:post, "#{url}/ocs/v2.php/apps/dav/api/v1/direct").to_return(status: code)+ end++ it "must return :#{symbol} ServiceResult" do+ result = subject+ .download_link_query+ .call(user:, file_link:)+ expect(result).to be_failure+ expect(result.errors.code).to be(symbol)+ end+ end+ end++ include_examples 'outbound is failing', 404, :not_found+ include_examples 'outbound is failing', 401, :not_authorized+ include_examples 'outbound is failing', 500, :error+ end++ describe '#files_query' do+ let(:parent) { '' }+ let(:root_path) { '' }+ let(:xml) { create(:webdav_data, parent_path: parent, root_path:) }+ let(:url) { "https://example.com#{root_path}" }+ let(:request_url) do+ Storages::Peripherals::StorageInteraction::Nextcloud::Util.join_uri_path(+ url,+ "/remote.php/dav/files/",+ CGI.escapeURIComponent(origin_user_id),+ parent+ )+ end++ context 'when outbound is success' do+ before do+ stub_request(:propfind, request_url).to_return(status: 207, body: xml, headers: {})+ end++ describe 'with Nextcloud storage type selected' do+ it 'returns a list files directories with names and permissions' do+ result = subject.files_query.call(folder: nil, user:)+ expect(result).to be_success+ expect(result.result.files.size).to eq(4)+ expect(result.result.ancestors.size).to eq(0)+ expect(result.result.parent).not_to be_nil+ expect(result.result.files[0]).to have_attributes(+ id: '11',+ name: 'Folder1',+ mime_type: 'application/x-op-directory',+ permissions: include(:readable, :writeable)+ )+ expect(result.result.files[1]).to have_attributes(+ mime_type: 'application/x-op-directory',+ permissions: %i[readable]+ )+ expect(result.result.files[2]).to have_attributes(+ id: '12',+ name: 'README.md',+ mime_type: 'text/markdown',+ permissions: include(:readable, :writeable)+ )+ expect(result.result.files[3]).to have_attributes(+ mime_type: 'application/pdf',+ permissions: %i[readable]+ )+ end++ describe 'with origin user id containing whitespaces' do+ let(:origin_user_id) { 'my user' }+ let(:xml) { create(:webdav_data, origin_user_id:) }++ it do+ result = subject+ .files_query+ .call(folder: parent, user:)+ expect(result.result.files[0].location).to eq('/Folder1')++ assert_requested(:propfind, request_url)+ end+ end++ describe 'with parent query parameter' do+ let(:parent) { '/Photos/Birds' }++ it do+ result = subject+ .files_query+ .call(folder: parent, user:)+ expect(result.result.files[2].location).to eq('/Photos/Birds/README.md')+ expect(result.result.ancestors[0].location).to eq('/')+ expect(result.result.ancestors[1].location).to eq('/Photos')++ assert_requested(:propfind, request_url)+ end+ end++ describe 'with storage running on a sub path' do+ let(:root_path) { '/storage' }++ it do+ result = subject+ .files_query+ .call(folder: nil, user:)+ expect(result.result.files[2].location).to eq('/README.md')+ assert_requested(:propfind, request_url)+ end+ end++ describe 'with storage running on a sub path and with parent parameter' do+ let(:root_path) { '/storage' }+ let(:parent) { '/Photos/Birds' }++ it do+ result = subject+ .files_query+ .call(folder: parent, user:)++ expect(result.result.files[2].location).to eq('/Photos/Birds/README.md')+ assert_requested(:propfind, request_url)+ end+ end+ end++ describe 'with not supported storage type selected' do+ before do+ allow(storage).to receive(:provider_type).and_return('not_supported_storage_type'.freeze)+ end++ it 'must raise ArgumentError' do+ expect { subject.files_query }.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 ":not_authorized" ServiceResult' do+ result = subject+ .files_query+ .call(folder: parent, user:)+ expect(result).to be_failure+ expect(result.errors.code).to be(:not_authorized)+ end+ end+ end++ shared_examples_for 'outbound is failing' do |code = 500, symbol = :error|+ describe "with outbound request returning #{code}" do+ before do+ stub_request(:propfind, request_url).to_return(status: code)+ end++ it "must return :#{symbol} ServiceResult" do+ result = subject+ .files_query+ .call(folder: parent, user:)+ expect(result).to be_failure+ expect(result.errors.code).to be(symbol)+ end+ end+ end++ include_examples 'outbound is failing', 404, :not_found+ include_examples 'outbound is failing', 401, :not_authorized+ include_examples 'outbound is failing', 500, :error+ end++ describe '#file_query' do+ let(:file_id) { '819' }+ let(:expected_response_body) do+ <<~JSON+ {+ "ocs": {+ "meta": {+ "status": "ok",+ "statuscode": 100,+ "message": "OK",+ "totalitems": "",+ "itemsperpage": ""+ },+ "data": {+ "status": "OK",+ "statuscode": 200,+ "id": 819,+ "name": "[Sample] Project Name | Ehuuu(10)",+ "mtime": 1684491252,+ "ctime": 0,+ "mimetype": "application\\/x-op-directory",+ "size": 0,+ "owner_name": "OpenProject",+ "owner_id": "OpenProject",+ "trashed": false,+ "modifier_name": null,+ "modifier_id": null,+ "dav_permissions": "RMGDNVCK",+ "path": "files\\/OpenProject\\/[Sample] Project Name | Ehuuu(10)"+ }+ }+ }+ JSON+ end++ before do+ stub_request(:get, "https://example.com/ocs/v1.php/apps/integration_openproject/fileinfo/#{file_id}")+ .with(headers: {+ 'Accept' => 'application/json',+ 'Authorization' => 'Bearer xyz',+ 'Content-Type' => 'application/json'+ })+ .to_return(status: 200, body: expected_response_body, headers: {})+ end++ context 'with Nextcloud storage type selected' do+ it 'must return a list of files when called' do+ result = subject+ .file_query+ .call(user:, file_id:)+ expect(result).to be_success+ storage_file = result.result+ expect(storage_file.id).to eq(819)+ expect(storage_file.location).to eq("/OpenProject/[Sample] Project Name | Ehuuu(10)")+ expect(storage_file.mime_type).to eq("application/x-op-directory")+ expect(storage_file.name).to eq("[Sample] Project Name | Ehuuu(10)")+ expect(storage_file.permissions).to eq("RMGDNVCK")+ expect(storage_file.size).to eq(0)+ end+ end+ end++ 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 = subject+ .upload_link_query+ .call(user:, data: query_payload)+ .result+ expect(link.destination.path).to be_eql("/index.php/apps/integration_openproject/direct-upload/#{upload_token}")+ expect(link.destination.host).to be_eql(URI(url).host)+ expect(link.destination.scheme).to be_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'.freeze)+ end++ it 'must raise ArgumentError' do+ expect { subject.upload_link_query }.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 ":not_authorized" ServiceResult' do+ result = subject+ .upload_link_query+ .call(user:, data: query_payload)+ expect(result).to be_failure+ expect(result.errors.code).to be(:not_authorized)+ 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 = subject+ .upload_link_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, :not_authorized+ include_examples 'outbound is failing', 404, :not_found+ include_examples 'outbound is failing', 500, :error+ endend- let(:token) do- token = instance_double(OAuthClientToken)- allow(token).to receive(:origin_user_id).and_return(origin_user_id)- allow(token).to receive(:access_token).and_return('xyz')- token+ describe '#group_users_query' do+ let(:expected_response_body) do+ <<~XML+ <?xml version="1.0"?>+ <ocs>+ <meta>+ <status>ok</status>+ <statuscode>100</statuscode>+ <message>OK</message>+ <totalitems></totalitems>+ <itemsperpage></itemsperpage>+ </meta>+ <data>+ <users>+ <element>admin</element>+ <element>OpenProject</element>+ <element>reader</element>+ <element>TestUser</element>+ <element>TestUser34</element>+ </users>+ </data>+ </ocs>+ XML+ end+ let(:expected_response) do+ {+ status: 200,+ body: expected_response_body,+ headers: {}+ }+ end++ before do+ stub_request(:get, "https://example.com/ocs/v1.php/cloud/groups/#{storage.group}")+ .with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6T3BlblByb2plY3RTZWN1cmVQYXNzd29yZA==',+ 'OCS-APIRequest' => 'true'+ }+ )+ .to_return(expected_response)+ end++ it 'responds with a strings array with group users' do+ result = subject+ .group_users_query+ .call+ expect(result).to be_success+ expect(result.result).to eq(["admin", "OpenProject", "reader", "TestUser", "TestUser34"])+ endend- let(:connection_manager) do- connection_manager = instance_double(OAuthClients::ConnectionManager)- allow(connection_manager).to receive(:get_access_token).and_return(ServiceResult.success(result: token))- allow(connection_manager).to receive(:request_with_token_refresh).and_yield(token)- connection_manager+ describe '#add_user_to_group_command' do+ let(:expected_response) do+ {+ status: 200,+ body: expected_response_body,+ headers: {}+ }+ end+ let(:expected_response_body) do+ <<~XML+ <?xml version="1.0"?>+ <ocs>+ <meta>+ <status>ok</status>+ <statuscode>100</statuscode>+ <message>OK</message>+ <totalitems></totalitems>+ <itemsperpage></itemsperpage>+ </meta>+ <data/>+ </ocs>+ XML+ end++ before do+ stub_request(:post, "https://example.com/ocs/v1.php/cloud/users/#{origin_user_id}/groups")+ .with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6T3BlblByb2plY3RTZWN1cmVQYXNzd29yZA==',+ 'OCS-APIRequest' => 'true'+ }+ )+ .to_return(expected_response)+ end++ it 'adds user to the group' do+ result = subject+ .add_user_to_group_command+ .call(user: origin_user_id)+ expect(result).to be_success+ expect(result.message).to eq("User has been added successfully")+ endend- subject { described_class.new(storage:) }+ describe '#remove_user_from_group' do+ let(:expected_response) do+ {+ status: 200,+ body: expected_response_body,+ headers: {}+ }+ end+ let(:expected_response_body) do+ <<~XML+ <?xml version="1.0"?>+ <ocs>+ <meta>+ <status>ok</status>+ <statuscode>100</statuscode>+ <message>OK</message>+ <totalitems></totalitems>+ <itemsperpage></itemsperpage>+ </meta>+ <data/>+ </ocs>+ XML+ end++ before do+ stub_request(:delete, "https://example.com/ocs/v1.php/cloud/users/#{origin_user_id}/groups?groupid=#{storage.group}")+ .with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6T3BlblByb2plY3RTZWN1cmVQYXNzd29yZA==',+ 'OCS-APIRequest' => 'true'+ }+ )+ .to_return(expected_response)+ end++ it 'removes user from the group' do+ result = subject+ .remove_user_from_group_command+ .call(user: origin_user_id)+ expect(result).to be_success+ expect(result.message).to eq("User has been removed from group")+ end+ enddescribe '#create_folder_command' do+ let(:folder_path) { 'OpenProject/JediProject' }+before dostub_request(:mkcol, "https://example.com/remote.php/dav/files/OpenProject/OpenProject/JediProject")- .with(- headers: {- 'Authorization' => 'Basic T3BlblByb2plY3Q6T3BlblByb2plY3RTZWN1cmVQYXNzd29yZA=='- }- )- .to_return(expected_response)+ .with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6T3BlblByb2plY3RTZWN1cmVQYXNzd29yZA=='+ }+ )+ .to_return(expected_response)endcontext 'when folder does not exist yet' dodiff --cc modules/storages/spec/workers/manage_nextcloud_integration_job_spec.rbindex 0a16fd9b029,00000000000..d0dc348a286mode 100644,000000..100644--- a/modules/storages/spec/workers/manage_nextcloud_integration_job_spec.rb+++ b/modules/storages/spec/workers/manage_nextcloud_integration_job_spec.rb@@@ -1,449 -1,0 +1,449 @@@+#-- 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'+- describe ManageNextcloudIntegrationJob, type: :job, webmock: true do++RSpec.describe ManageNextcloudIntegrationJob, type: :job, webmock: true do+ # rubocop:disable RSpec/IndexedLet+ let(:group_users_response_body) do+ <<~XML+ <?xml version="1.0"?>+ <ocs>+ <meta>+ <status>ok</status>+ <statuscode>100</statuscode>+ <message>OK</message>+ <totalitems></totalitems>+ <itemsperpage></itemsperpage>+ </meta>+ <data>+ <users>+ <element>Darth Maul</element>+ </users>+ </data>+ </ocs>+ XML+ end+ let(:set_permissions_response_body1) do+ <<~XML+ <?xml version="1.0"?>+ <d:multistatus+ xmlns:d="DAV:"+ xmlns:s="http://sabredav.org/ns"+ xmlns:oc="http://owncloud.org/ns"+ xmlns:nc="http://nextcloud.org/ns">+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject</d:href>+ <d:propstat>+ <d:prop>+ <nc:acl-list/>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ </d:multistatus>+ XML+ end+ let(:set_permissions_request_body1) do+ <<~XML+ <?xml version="1.0"?>+ <d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">+ <d:set>+ <d:prop>+ <nc:acl-list>+ <nc:acl>+ <nc:acl-mapping-type>group</nc:acl-mapping-type>+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>+ <nc:acl-mask>31</nc:acl-mask>+ <nc:acl-permissions>1</nc:acl-permissions>+ </nc:acl>+ <nc:acl>+ <nc:acl-mapping-type>user</nc:acl-mapping-type>+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>+ <nc:acl-mask>31</nc:acl-mask>+ <nc:acl-permissions>31</nc:acl-permissions>+ </nc:acl>+ </nc:acl-list>+ </d:prop>+ </d:set>+ </d:propertyupdate>+ XML+ end+ let(:propfind_request_body) do+ <<~XML+ <?xml version="1.0"?>+ <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">+ <d:prop>+ <oc:fileid/>+ </d:prop>+ </d:propfind>+ XML+ end+ let(:propfind_response_body1) do+ <<~XML+ <?xml version="1.0"?>+ <d:multistatus+ xmlns:d="DAV:"+ xmlns:s="http://sabredav.org/ns"+ xmlns:oc="http://owncloud.org/ns"+ xmlns:nc="http://nextcloud.org/ns">+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/</d:href>+ <d:propstat>+ <d:prop>+ <oc:fileid>349</oc:fileid>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Lost%20Jedi%20Project%20Folder%20%232/</d:href>+ <d:propstat>+ <d:prop>+ <oc:fileid>783</oc:fileid>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Lost%20Jedi%20Project%20Folder%20%233/</d:href>+ <d:propstat>+ <d:prop>+ <oc:fileid>123</oc:fileid>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ </d:multistatus>+ XML+ end+ let(:propfind_response_body2) do+ <<~XML+ <?xml version="1.0"?>+ <d:multistatus+ xmlns:d="DAV:"+ xmlns:s="http://sabredav.org/ns"+ xmlns:oc="http://owncloud.org/ns"+ xmlns:nc="http://nextcloud.org/ns">+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/%5bSample%5d%20Project%20Name%20%7c%20Ehuu(#{project1.id})/</d:href>+ <d:propstat>+ <d:prop>+ <oc:fileid>819</oc:fileid>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ </d:multistatus>+ XML+ end+ let(:add_user_to_group_response_body) do+ <<~XML+ <?xml version="1.0"?>+ <ocs>+ <meta>+ <status>ok</status>+ <statuscode>100</statuscode>+ <message>OK</message>+ <totalitems></totalitems>+ <itemsperpage></itemsperpage>+ </meta>+ <data/>+ </ocs>+ XML+ end+ let(:set_permissions_request_body2) do+ <<~XML+ <?xml version="1.0"?>+ <d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">+ <d:set>+ <d:prop>+ <nc:acl-list>+ <nc:acl>+ <nc:acl-mapping-type>group</nc:acl-mapping-type>+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>+ <nc:acl-mask>31</nc:acl-mask>+ <nc:acl-permissions>0</nc:acl-permissions>+ </nc:acl>+ <nc:acl>+ <nc:acl-mapping-type>user</nc:acl-mapping-type>+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>+ <nc:acl-mask>31</nc:acl-mask>+ <nc:acl-permissions>31</nc:acl-permissions>+ </nc:acl>+ <nc:acl>+ <nc:acl-mapping-type>user</nc:acl-mapping-type>+ <nc:acl-mapping-id>Obi-Wan</nc:acl-mapping-id>+ <nc:acl-mask>31</nc:acl-mask>+ <nc:acl-permissions>3</nc:acl-permissions>+ </nc:acl>+ </nc:acl-list>+ </d:prop>+ </d:set>+ </d:propertyupdate>+ XML+ end+ let(:set_permissions_response_body2) do+ <<~XML+ <?xml version="1.0"?>+ <d:multistatus+ xmlns:d="DAV:"+ xmlns:s="http://sabredav.org/ns"+ xmlns:oc="http://owncloud.org/ns"+ xmlns:nc="http://nextcloud.org/ns">+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/%5bSample%5d%20Project%20Name%20%7c%20Ehuu(#{project1.id})</d:href>+ <d:propstat>+ <d:prop>+ <nc:acl-list/>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ </d:multistatus>+ XML+ end+ let(:remove_user_from_group_response) do+ <<~XML+ <?xml version="1.0"?>+ <ocs>+ <meta>+ <status>ok</status>+ <statuscode>100</statuscode>+ <message>OK</message>+ <totalitems></totalitems>+ <itemsperpage></itemsperpage>+ </meta>+ <data/>+ </ocs>+ XML+ end+ let(:set_permissions_request_body3) do+ <<~XML+ <?xml version="1.0"?>+ <d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">+ <d:set>+ <d:prop>+ <nc:acl-list>+ <nc:acl>+ <nc:acl-mapping-type>group</nc:acl-mapping-type>+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>+ <nc:acl-mask>31</nc:acl-mask>+ <nc:acl-permissions>0</nc:acl-permissions>+ </nc:acl>+ <nc:acl>+ <nc:acl-mapping-type>user</nc:acl-mapping-type>+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>+ <nc:acl-mask>31</nc:acl-mask>+ <nc:acl-permissions>31</nc:acl-permissions>+ </nc:acl>+ </nc:acl-list>+ </d:prop>+ </d:set>+ </d:propertyupdate>+ XML+ end+ let(:set_permissions_response_body3) do+ <<~XML+ <?xml version="1.0"?>+ <d:multistatus+ xmlns:d="DAV:"+ xmlns:s="http://sabredav.org/ns"+ xmlns:oc="http://owncloud.org/ns"+ xmlns:nc="http://nextcloud.org/ns">+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Lost%20Jedi%20Project%20Folder%20%232</d:href>+ <d:propstat>+ <d:prop>+ <nc:acl-list/>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ </d:multistatus>+ XML+ end+ let(:set_permissions_response_body4) do+ <<~XML+ <?xml version="1.0"?>+ <d:multistatus+ xmlns:d="DAV:"+ xmlns:s="http://sabredav.org/ns"+ xmlns:oc="http://owncloud.org/ns"+ xmlns:nc="http://nextcloud.org/ns">+ <d:response>+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Jedi%20Project%20Folder%20%7C%7C%7C%28#{project2.id}%29</d:href>+ <d:propstat>+ <d:prop>+ <nc:acl-list/>+ </d:prop>+ <d:status>HTTP/1.1 200 OK</d:status>+ </d:propstat>+ </d:response>+ </d:multistatus>+ XML+ end++ let(:project1) { create(:project, name: '[Sample] Project Name / Ehuu', members: { user => role }) }+ let(:project2) { create(:project, name: 'Jedi Project Folder ///', members: { user => role }) }+ let(:user) { create(:user) }+ let(:role) { create(:role, permissions: %w[read_files write_files]) }+ # rubocop:enable RSpec/IndexedLet++ it 'has a schedule set' do+ expect(described_class.cron_expression).to eq('*/5 * * * *')+ end++ # rubocop:disable RSpec/ExampleLength+ it 'does the job' do+ storage = create(:storage,+ host: 'https://example.com',+ has_managed_project_folders: true,+ password: '12345678')+ projects_storage1 = create(:project_storage,+ project_folder_mode: 'automatic',+ project: project1,+ storage:)++ projects_storage2 = create(:project_storage,+ project_folder_mode: 'automatic',+ project: project2,+ storage:,+ project_folder_id: '123')++ oauth_client = create(:oauth_client, integration: storage)+ create(:oauth_client_token,+ origin_user_id: 'Obi-Wan',+ user:,+ oauth_client:)++ request_stubs = []+ request_stubs << stub_request(:get, "https://example.com/ocs/v1.php/cloud/groups/#{storage.group}")+ .with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg=',+ 'OCS-APIRequest' => 'true'+ }+ ).to_return(status: 200, body: group_users_response_body, headers: {})+ request_stubs << stub_request(:proppatch, "https://example.com/remote.php/dav/files/OpenProject/OpenProject")+ .with(+ body: set_permissions_request_body1,+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg='+ }+ )+ .to_return(status: 207, body: set_permissions_response_body1, headers: {})+ request_stubs << stub_request(:propfind, "https://example.com/remote.php/dav/files/OpenProject/OpenProject")+ .with(+ body: propfind_request_body,+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg=',+ 'Depth' => '1'+ }+ )+ .to_return(status: 207, body: propfind_response_body1, headers: {})+ request_stubs << stub_request(+ :mkcol,+ "https://example.com/remote.php/dav/files/OpenProject/OpenProject/%5BSample%5D%20Project%20Name%20%7C%20Ehuu(#{project1.id})"+ ).with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg='+ }+ ).to_return(status: 201, body: "", headers: {})+ request_stubs << stub_request(:propfind, "https://example.com/remote.php/dav/files/OpenProject/OpenProject/%5BSample%5D%20Project%20Name%20%7C%20Ehuu(#{project1.id})")+ .with(+ body: propfind_request_body,+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg=',+ 'Depth' => '0'+ }+ )+ .to_return(status: 207, body: propfind_response_body2, headers: {})+ request_stubs << stub_request(:post, "https://example.com/ocs/v1.php/cloud/users/Obi-Wan/groups")+ .with(+ body: "groupid=OpenProject",+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg=',+ 'Ocs-Apirequest' => 'true'+ }+ ).to_return(status: 200, body: add_user_to_group_response_body, headers: {})+ request_stubs << stub_request(:proppatch, "https://example.com/remote.php/dav/files/OpenProject/OpenProject/%5BSample%5D%20Project%20Name%20%7C%20Ehuu(#{project1.id})")+ .with(+ body: set_permissions_request_body2,+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg='+ }+ )+ .to_return(status: 207, body: set_permissions_response_body2, headers: {})+ request_stubs << stub_request(:move, "https://example.com/remote.php/dav/files/OpenProject/OpenProject/Lost%20Jedi%20Project%20Folder%20%233")+ .with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg=',+ 'Destination' => "/remote.php/dav/files/OpenProject/OpenProject/Jedi%20Project%20Folder%20%7C%7C%7C%28#{project2.id}%29"+ }+ )+ .to_return(status: 201, body: "", headers: {})+ request_stubs << stub_request(:proppatch, "https://example.com/remote.php/dav/files/OpenProject/OpenProject/Jedi%20Project%20Folder%20%7C%7C%7C%28#{project2.id}%29")+ .with(+ body: set_permissions_request_body2,+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg='+ }+ )+ .to_return(status: 207, body: set_permissions_response_body4, headers: {})+ request_stubs << stub_request(:delete, "https://example.com/ocs/v1.php/cloud/users/Darth%20Maul/groups?groupid=OpenProject")+ .with(+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg=',+ 'Ocs-Apirequest' => 'true'+ }+ )+ .to_return(status: 200, body: remove_user_from_group_response, headers: {})+ request_stubs << stub_request(:proppatch, "https://example.com/remote.php/dav/files/OpenProject/OpenProject/Lost%20Jedi%20Project%20Folder%20%232")+ .with(+ body: set_permissions_request_body3,+ headers: {+ 'Authorization' => 'Basic T3BlblByb2plY3Q6MTIzNDU2Nzg='+ }+ )+ .to_return(status: 207, body: set_permissions_response_body3, headers: {})++ expect(projects_storage1.project_folder_id).to be_nil+ expect(projects_storage2.project_folder_id).to eq('123')++ described_class.new.perform++ projects_storage1.reload+ projects_storage2.reload+ expect(projects_storage1.project_folder_id).to eq('819')+ expect(projects_storage2.project_folder_id).to eq('123')++ expect(request_stubs).to all have_been_requested+ end+ # rubocop:enable RSpec/ExampleLength+end