Skip to content

Commit

Permalink
Fix broken tests all around
Browse files Browse the repository at this point in the history
  • Loading branch information
mereghost committed Mar 6, 2024
1 parent 55870c2 commit bcf95e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
expect(command_result).to be_success
expect(command_result.result[:url]).to match %r</drives/#{storage.drive_id}/items/.+\?.+$>
ensure
delete_copied_folder(command_result.result[:id])
delete_copied_folder(command_result.result[:url])
end

describe 'error handling' do
Expand Down Expand Up @@ -190,7 +190,11 @@ def existing_folder_tuples
end
end

def delete_copied_folder(location)
def delete_copied_folder(url)
extractor_regex = /.+\/items\/(?<item_id>\w+)\?/
match_data = extractor_regex.match(url)
location = match_data[:item_id]

Storages::Peripherals::Registry
.resolve('one_drive.commands.delete_folder')
.call(storage:, location:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,72 +41,51 @@
context "with automatically managed project folders" do
let(:source) { create(:project_storage, :as_automatically_managed, storage:) }

it 'updates the target project storage to point to newly copied remote folder' do
it 'if polling is required, returns a nil id and an url' do
Storages::Peripherals::Registry
.stub("#{source.storage.short_provider_type}.commands.copy_template_folder",
->(args) do
# Validating the arguments ensure that the call is correctly made
expect(args[:storage]).to eq(source.storage)
expect(args[:source_path]).to eq(source.project_folder_location)
expect(args[:destination_path]).to eq(target.managed_project_folder_path)

# Return a success for the provider copy with no polling required
ServiceResult.success(result: { id: 'newly_created_remote_folder', url: 'https://resource.url' })
end)

expect(service.call(source_id: source.id, target_id: target.id)).to be_success

target.reload
expect(target.project_folder_mode).to eq(source.project_folder_mode)
expect(target.project_folder_id).to eq('newly_created_remote_folder')
end

it 'if polling is required, returns an error with the polling url' do
Storages::Peripherals::Registry
.stub("#{source.storage.short_provider_type}.commands.copy_template_folder",
->(args) do
# Validating the arguments ensure that the call is correctly made
expect(args[:storage]).to eq(source.storage)
expect(args[:source_path]).to eq(source.project_folder_location)
expect(args[:destination_path]).to eq(target.managed_project_folder_path)
->(storage:, source_path:, destination_path:) do
expect(storage).to eq(source.storage)
expect(source_path).to eq(source.project_folder_location)
expect(destination_path).to eq(target.managed_project_folder_path)

# Return a success for the provider copy with no polling required
ServiceResult.success(result: { id: nil, url: 'https://polling.url.de/cool/subresources' })
end)

result = service.call(source_id: source.id, target_id: target.id)
result = service.call(source:, target:)

expect(result).to be_failure
expect(result.result).to eq('https://polling.url.de/cool/subresources')
expect(result.errors).to eq(:polling_required)
expect(result).to be_success
expect(result.result).to eq({ id: nil, url: 'https://polling.url.de/cool/subresources' })
end
end

context "with manually managed project folders" do
let(:source) { create(:project_storage, project_folder_id: 'this_is_a_unique_id', project_folder_mode: 'manual') }

it "succeeds" do
expect(service.call(source_id: source.id, target_id: target.id)).to be_success
result = service.call(source:, target:)
expect(result).to be_success
end

it "updates to the target project storage to point to the same project_folder_id than the source" do
expect { service.call(source_id: source.id, target_id: target.id) }
.to change { target.reload.project_folder_id }
.to(source.project_folder_id)
.and(change { target.reload.project_folder_mode }
.to(source.project_folder_mode))
it 'returns the source folder id' do
result = service.call(source:, target:)

expect(result.result[:id]).to eq(source.project_folder_id)
end
end

context "with non-managed project folders" do
let(:source) { create(:project_storage, project_folder_id: 'this_is_a_unique_id') }
let(:source) { create(:project_storage, project_folder_id: nil, project_folder_mode: 'inactive') }

it "succeeds" do
expect(service.call(source_id: source.id, target_id: target.id)).to be_success
expect(service.call(source:, target:)).to be_success
end

it "doesn't require any updates to the target project storage" do
expect { service.call(source_id: source.id, target_id: target.id) }.not_to change(target, :project_folder_id)
it 'returns the origin folder id (nil)' do
result = service.call(source:, target:)

expect(result.result[:id]).to eq(source.project_folder_id)
end
end
end
227 changes: 6 additions & 221 deletions spec/services/projects/copy_service_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@
boards
overview
storages
storage_project_folders
file_links
)
)
end
Expand Down Expand Up @@ -219,45 +217,6 @@
let(:storage1) { source_automatic_project_storage.storage }
let(:storage2) { source_manual_project_storage.storage }
# rubocop:enable RSpec/IndexedLet
let(:host) { storage1.host }
let!(:file_outside_project_folder_link) do
create(:file_link,
origin_id: "100",
origin_name: "file_name1.txt",
container: source_wp,
storage: storage1)
end
let!(:file_inside_automatic_project_folder_link) do
create(:file_link,
origin_id: "101",
origin_name: "file_name2.txt",
container: source_wp,
storage: storage1)
end
let!(:folder_inside_automatic_project_folder_link) do
create(:file_link,
origin_id: "103",
origin_name: "This is a folder",
container: source_wp,
storage: storage1)
end
let!(:file_inside_manual_project_folder_link) do
create(:file_link,
origin_id: "102",
origin_name: "file_name3.txt",
container: source_wp,
storage: storage2)
end
let!(:oauth_client) do
create(:oauth_client,
client_id: "nwz34rWsolvJvchfQ1bVHXfMb1ETK89lCBgzrLhWx3ACW5nKfmdcyf5ftlCyKGbk",
client_secret: "A08n6CRBOOr41iqkWRynnP6BbmEnau7LeP9t9xrIbiYX46iXgmIZgqhJoDFjUMEq",
integration: storage1)
end
let!(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user: current_user) }
let(:destination_url) { %r{#{host}/remote.php/dav/files/OpenProject/OpenProject/Target%20Project%20Name%20} }
let(:source_url) { %r{#{host}/remote.php/dav/files/OpenProject/OpenProject/Source%20Project%20Name%20} }
let(:new_project_folder_id) { "819" }

shared_let(:source_automatic_project_storage) do
storage = create(:nextcloud_storage)
Expand All @@ -269,161 +228,6 @@
create(:project_storage, storage:, project: source, project_folder_id: '345', project_folder_mode: 'manual')
end

before do
stub_request(:head, destination_url).to_return(status: 404)
stub_request(:copy, source_url).to_return(status: 201)
stub_request(:propfind, destination_url).with(headers: { 'Depth' => '1' }).to_return do |_request|
project_id = Project.where(name: "Target Project Name").pick(:id)
body = <<~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/Target%20Project%20Name%20(#{project_id})/</d:href>
<d:propstat>
<d:prop>
<oc:fileid>#{new_project_folder_id}</oc:fileid>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
</d:multistatus>
XML
{ status: 200, body:, headers: {} }
end
stub_request(:propfind, destination_url).with(headers: { 'Depth' => 'infinity' }).to_return do |_request|
project_id = Project.where(name: "Target Project Name").pick(:id)
body = <<~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/Target%20Project%20Name%20(#{project_id})/</d:href>
<d:propstat>
<d:prop>
<oc:fileid>#{new_project_folder_id}</oc:fileid>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
<d:propstat>
<d:prop>
<nc:acl-list/>
</d:prop>
<d:status>HTTP/1.1 404 Not Found</d:status>
</d:propstat>
</d:response>
<d:response>
<d:href>/remote.php/dav/files/OpenProject/OpenProject/Target%20Project%20Name%20(#{project_id})/#{file_inside_automatic_project_folder_link.origin_name}</d:href>
<d:propstat>
<d:prop>
<oc:fileid>430</oc:fileid>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
<d:propstat>
<d:prop>
<nc:acl-list/>
</d:prop>
<d:status>HTTP/1.1 404 Not Found</d:status>
</d:propstat>
</d:response>
<d:response>
<d:href>/remote.php/dav/files/OpenProject/OpenProject/Target%20Project%20Name%20(#{project_id})/#{folder_inside_automatic_project_folder_link.origin_name}/</d:href>
<d:propstat>
<d:prop>
<oc:fileid>431</oc:fileid>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
<d:propstat>
<d:prop>
<nc:acl-list/>
</d:prop>
<d:status>HTTP/1.1 404 Not Found</d:status>
</d:propstat>
</d:response>
</d:multistatus>
XML
{ status: 200, body:, headers: {} }
end

filesinfo_response_body = <<~JSON
{
"ocs": {
"meta": {
"status": "ok",
"statuscode": 100,
"message": "OK",
"totalitems": "",
"itemsperpage": ""
},
"data": {
"#{file_outside_project_folder_link.origin_id}": {
"status": "OK",
"statuscode": 200,
"id": #{file_outside_project_folder_link.origin_id},
"name": "#{file_outside_project_folder_link.origin_name}",
"mtime": 1688632254,
"ctime": 0,
"mimetype": "application\\/pdf",
"size": 15181180,
"owner_name": "admin",
"owner_id": "admin",
"trashed": false,
"modifier_name": "admin",
"modifier_id": "admin",
"dav_permissions": "RGDNVW",
"path": "files\\/#{file_outside_project_folder_link.origin_name}"
},
"#{file_inside_automatic_project_folder_link.origin_id}": {
"status": "OK",
"statuscode": 200,
"id": #{file_inside_automatic_project_folder_link.origin_id},
"name": "#{file_inside_automatic_project_folder_link.origin_name}",
"mtime": 1689687843,
"ctime": 0,
"mimetype": "image\\/jpeg",
"size": 94064,
"owner_name": "admin",
"owner_id": "admin",
"trashed": false,
"modifier_name": null,
"modifier_id": null,
"dav_permissions": "RMGDNVW",
"path": "files\\/OpenProject\\/Source Project Name (#{source.id})\\/#{file_inside_automatic_project_folder_link.origin_name}"
},
"#{folder_inside_automatic_project_folder_link.origin_id}": {
"status": "OK",
"statuscode": 200,
"id": #{folder_inside_automatic_project_folder_link.origin_id},
"name": "#{folder_inside_automatic_project_folder_link.origin_name}",
"mtime": 1689687111,
"ctime": 0,
"mimetype": "application\\/x-op-directory",
"size": 0,
"owner_name": "admin",
"owner_id": "admin",
"trashed": false,
"modifier_name": null,
"modifier_id": null,
"dav_permissions": "RMGDNVCK",
"path": "files\\/OpenProject\\/Source Project Name (#{source.id})\\/#{folder_inside_automatic_project_folder_link.origin_name}"
}
}
}
}
JSON
stub_request(:post, "#{host}/ocs/v1.php/apps/integration_openproject/filesinfo")
.with(body: '{"fileIds":["100","101","103"]}')
.to_return(status: 200, body: filesinfo_response_body, headers: { 'Content-Type' => 'application/json' })
end

# rubocop:disable RSpec/ExampleLength
# rubocop:disable RSpec/MultipleExpectations
it 'copies all dependencies and set attributes' do
Expand Down Expand Up @@ -468,36 +272,17 @@
expect(automatic_project_storage_copy.id).not_to eq(source_automatic_project_storage.id)
expect(automatic_project_storage_copy.project_id).to eq(project_copy.id)
expect(automatic_project_storage_copy.creator_id).to eq(current_user.id)
expect(automatic_project_storage_copy.project_folder_id).to eq("819")
expect(automatic_project_storage_copy.project_folder_mode).to eq('automatic')
expect(automatic_project_storage_copy.project_folder_id).to be_nil
expect(automatic_project_storage_copy.project_folder_mode).to eq('inactive')

manual_project_storage_copy = project_copy.project_storages.find_by(storage: storage2)
expect(manual_project_storage_copy.id).not_to eq(source_manual_project_storage.id)
expect(manual_project_storage_copy.project_id).to eq(project_copy.id)
expect(manual_project_storage_copy.creator_id).to eq(current_user.id)
expect(manual_project_storage_copy.project_folder_id).to eq("345")
expect(manual_project_storage_copy.project_folder_mode).to eq('manual')

wp_copy = project_copy.work_packages.where(subject: "source wp").first
expect(wp_copy.file_links.count).to eq(4)
file_outside_project_folder_link_copy = wp_copy.file_links.find_by(origin_name: "file_name1.txt")
file_inside_automatic_project_folder_link_copy = wp_copy.file_links.find_by(origin_name: "file_name2.txt")
file_inside_manual_project_folder_link_copy = wp_copy.file_links.find_by(origin_name: "file_name3.txt")
folder_inside_automatic_project_folder_link_copy = wp_copy.file_links.find_by(origin_name: "This is a folder")
expect(file_outside_project_folder_link_copy.id).not_to eq(file_outside_project_folder_link.id)
expect(file_outside_project_folder_link_copy.origin_id).to eq(file_outside_project_folder_link.origin_id)
expect(file_outside_project_folder_link_copy.storage_id).to eq(file_outside_project_folder_link.storage_id)
expect(file_inside_automatic_project_folder_link_copy.id).not_to eq(file_inside_automatic_project_folder_link.id)
expect(file_inside_automatic_project_folder_link_copy.origin_id).to eq("430")
expect(file_inside_automatic_project_folder_link_copy.storage_id)
.to eq(file_inside_automatic_project_folder_link.storage_id)
expect(folder_inside_automatic_project_folder_link_copy.id).not_to eq(folder_inside_automatic_project_folder_link.id)
expect(folder_inside_automatic_project_folder_link_copy.origin_id).to eq("431")
expect(folder_inside_automatic_project_folder_link_copy.storage_id)
.to eq(folder_inside_automatic_project_folder_link.storage_id)
expect(file_inside_manual_project_folder_link_copy.id).not_to eq(file_inside_manual_project_folder_link.id)
expect(file_inside_manual_project_folder_link_copy.origin_id).to eq("102")
expect(file_inside_manual_project_folder_link_copy.storage_id).to eq(file_inside_manual_project_folder_link.storage_id)
expect(manual_project_storage_copy.project_folder_id).to be_nil
expect(manual_project_storage_copy.project_folder_mode).to eq('inactive')

expect(Storages::CopyProjectFoldersJob).to have_been_enqueued.exactly(2).times
end
# rubocop:enable RSpec/ExampleLength
# rubocop:enable RSpec/MultipleExpectations
Expand Down

0 comments on commit bcf95e7

Please sign in to comment.