# encoding: utf-8 # # Redmine plugin for Document Management System "Features" # # Copyright (C) 2012 Daniel Munn # Copyright (C) 2011-17 Karel Picman # # 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. require File.expand_path('../../../test_helper', __FILE__) require 'fileutils' class DmsfWebdavMoveTest < RedmineDmsf::Test::IntegrationTest fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :enabled_modules, :dmsf_folders, :dmsf_files, :dmsf_file_revisions def setup DmsfLock.delete_all # Delete all locks that are in our test DB - probably not safe but ho hum timestamp = DateTime.now.strftime("%y%m%d%H%M") DmsfFile.storage_path = File.expand_path("./dmsf_test-#{timestamp}", DmsfHelper.temp_dir) Dir.mkdir(DmsfFile.storage_path) unless File.directory?(DmsfFile.storage_path) # Copy the physical files to the temporary storage FileUtils.cp_r File.dirname(__FILE__) + "/../../fixtures/files/.", DmsfFile.storage_path @admin = credentials 'admin' @jsmith = credentials 'jsmith' @project1 = Project.find_by_id 1 # Fix permissions for jsmith's role @role = Role.find 1 # @role.add_permission! :view_dmsf_folders @role.add_permission! :folder_manipulation Setting.plugin_redmine_dmsf['dmsf_webdav'] = '1' Setting.plugin_redmine_dmsf['dmsf_webdav_strategy'] = 'WEBDAV_READ_WRITE' super end def teardown # Delete our tmp folder begin FileUtils.rm_rf DmsfFile.storage_path rescue Exception => e error e.message end end def test_truth assert_kind_of Project, @project1 assert_kind_of Role, @role end def test_move_denied_for_anonymous file = DmsfFile.find_by_id 1 new_name = "#{file.name}.moved" assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, {:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"} assert_response 401 end end def test_move_to_new_filename_without_folder_manipulation_permission @role.remove_permission! :folder_manipulation file = DmsfFile.find_by_id 1 new_name = "#{file.name}.moved" assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 403 end end def test_move_to_new_filename_without_folder_manipulation_permission_as_admin @role.remove_permission! :folder_manipulation file = DmsfFile.find_by_id 1 new_name = "#{file.name}.moved" assert_difference 'file.dmsf_file_revisions.count', +1 do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @admin.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 201 # Created f = DmsfFile.find_file_by_name @project1, nil, "#{new_name}" assert f, "Moved file '#{new_name}' not found in project." end end def test_move_non_existent_file xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/not_a_file.txt", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/moved_file.txt"}) assert_response 404 # NotFound end def test_move_wrong_destination file = DmsfFile.find_by_id 1 new_name = "#{file.name}.moved" assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.wrong-url.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 502 # BadGateway end end def test_move_to_new_filename file = DmsfFile.find_by_id 1 new_name = "#{file.name}.moved" assert_difference 'file.dmsf_file_revisions.count', +1 do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 201 # Created f = DmsfFile.find_file_by_name @project1, nil, "#{new_name}" assert f, "Moved file '#{new_name}' not found in project." end end def test_move_zero_sized_to_new_filename file = DmsfFile.find_by_id 10 new_name = "#{file.name}.moved" assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 201 # Created f = DmsfFile.find_file_by_name @project1, nil, "#{new_name}" assert f, "Moved file '#{new_name}' not found in project." end end def test_move_to_new_folder file = DmsfFile.find_by_id 1 folder = DmsfFolder.find_by_id 1 assert_kind_of DmsfFile, file assert_kind_of DmsfFolder, folder assert_difference 'file.dmsf_file_revisions.count', +1 do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{folder.title}/#{file.name}"}) assert_response 201 # Created file2 = DmsfFile.find_by_id 1 assert_equal folder.id, file2.dmsf_folder_id end end def test_move_zero_sized_to_new_folder file = DmsfFile.find_by_id 10 folder = DmsfFolder.find_by_id 1 assert_kind_of DmsfFile, file assert_kind_of DmsfFolder, folder assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{folder.title}/#{file.name}"}) assert_response 201 # Created file2 = DmsfFile.find_by_id 10 assert_equal folder.id, file2.dmsf_folder_id end end def test_move_to_existing_filename file = DmsfFile.find_by_id 1 file2 = DmsfFile.find_by_id 9 new_name = "#{file2.name}" assert_no_difference 'file2.dmsf_file_revisions.count' do assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 405 # MethodNotAllowed end end end def test_move_when_file_is_locked_by_other file = DmsfFile.find_by_id 1 log_user 'admin', 'admin' # login as admin assert !User.current.anonymous?, 'Current user is anonymous' assert file.lock!, "File failed to be locked by #{User.current.name}" new_name = "#{file.name}.moved" assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 423 # Locked end end def test_move_when_file_is_locked_by_other_and_user_is_admin file = DmsfFile.find_by_id 1 log_user 'jsmith', 'jsmith' # login as jsmith assert !User.current.anonymous?, 'Current user is anonymous' assert file.lock!, "File failed to be locked by #{User.current.name}" new_name = "#{file.name}.moved" assert_no_difference 'file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @admin.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 423 # Locked end end def test_move_when_file_is_locked_by_user file = DmsfFile.find_by_id 1 log_user 'jsmith', 'jsmith' # login as jsmith assert !User.current.anonymous?, 'Current user is anonymous' assert file.lock!, "File failed to be locked by #{User.current.name}" # Move once new_name = "#{file.name}.m1" assert_difference 'file.dmsf_file_revisions.count', +1 do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name}"}) assert_response 201 # Created end # Move twice new_name2 = "#{new_name}.m2" assert_difference 'file.dmsf_file_revisions.count', +1 do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{new_name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{new_name2}"}) assert_response 201 # Created end end def test_move_msoffice_save_file # When some versions of MsOffice saves a file it use the following sequence: # 1. Save changes to a new temporary document, XXX.tmp # 2. Rename (MOVE) document to YYY.tmp. History is lost here if original document is moved. # 3. Rename (MOVE) XXX.tmp to document name. XXX.tmp must be merged to original document or else the history is lost. # 4. Delete YYY.tmp. # Verify that steps 2 and 3 works. original_file = DmsfFile.find_by_id 1 temp_file_name = "ABCDEF.tmp" # Make sure that the temp-file does not exist. temp_file = DmsfFile.find_file_by_name @project1, nil, "#{temp_file_name}" assert !temp_file, "File '#{temp_file_name}' should not exist yet." # Move the original file to ABCDEF.tmp. The original file should not change but a new file should be created. assert_no_difference 'original_file.dmsf_file_revisions.count' do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{original_file.name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{temp_file_name}"}) assert_response 201 # Created end # Verify that a new file acutally has been created temp_file = DmsfFile.find_file_by_name @project1, nil, "#{temp_file_name}" assert temp_file, "File '#{temp_file_name}' not found, move failed." assert_equal temp_file.dmsf_file_revisions.count,1 assert_not_equal temp_file.id, original_file.id # Move a temporary file (use ABCDEF.tmp) to the original file. assert_difference 'original_file.dmsf_file_revisions.count', +1 do xml_http_request :move, "/dmsf/webdav/#{@project1.identifier}/#{temp_file_name}", nil, @jsmith.merge!({:destination => "http://www.example.com/dmsf/webdav/#{@project1.identifier}/#{original_file.name}"}) assert_response 201 # Created end end end