This commit is contained in:
karel.picman@lbcfree.net 2020-08-11 13:55:28 +02:00
parent ff786f9d7e
commit aafe725306
3 changed files with 114 additions and 112 deletions

View File

@ -34,7 +34,6 @@ module RedmineDmsf
def initialize(path, request, response, options)
raise NotFound if Setting.plugin_redmine_dmsf['dmsf_webdav'].blank?
@project = nil
@projectless_path = nil
@public_path = "#{options[:root_uri_path]}#{path}"
@children = nil
super path, request, response, options
@ -120,10 +119,8 @@ module RedmineDmsf
unless @project
i = 1
project_names = Setting.plugin_redmine_dmsf['dmsf_webdav_use_project_names']
puts ">>> path: #{@path} - #{project_names}"
while true
pinfo = @path.split('/').drop(i)
puts ">>> pinfo: #{pinfo} - #{i}"
if pinfo.length > 0
if project_names
if pinfo.first =~ / (\d+)$/
@ -134,22 +131,10 @@ module RedmineDmsf
end
end
else
begin
puts ">>> pinfo.first: #{pinfo.first}"
scope = Project.visible.where(identifier: pinfo.first)
scope = scope.where(parent_id: @project.id) if @project
prj = scope.first
puts ">>> prj: #{prj.identifier}" if prj
rescue => e
Rails.logger.error e.message
prj = Project.visible.find_by(identifier: pinfo.first)
end
end
end
unless prj
@projectless_path = '/' + @path.split('/').drop(i + 1).join('/')
puts ">>> less_path: #{@projectless_path} - #{i}"
break
end
break unless prj
i = i + 1
@project = prj
prj = nil
@ -160,9 +145,28 @@ module RedmineDmsf
# Make it easy to find the path without project in it.
def projectless_path
puts ">>> projectless_path ###"
self.project # Initialization
@projectless_path
i = 1
project_names = Setting.plugin_redmine_dmsf['dmsf_webdav_use_project_names']
while true
pinfo = @path.split('/').drop(i)
if pinfo.length > 0
if project_names
if pinfo.first =~ / (\d+)$/
prj = Project.visible.find_by(id: $1)
if prj
# Check again whether it's really the project and not a folder with a number as a suffix
prj = nil unless pinfo.first =~ /^#{prj.name}/
end
end
else
prj = Project.visible.find_by(identifier: pinfo.first)
end
end
return '/' + @path.split('/').drop(i).join('/') unless prj
i = i + 1
prj = nil
end
puts ">>> Error: #{@path}"
end
def path_prefix

View File

@ -471,7 +471,6 @@ module RedmineDmsf
end
begin
if entity.locked? && entity.locked_for_user?
puts ">>> lock failure"
raise DAV4Rack::LockFailure.new("Failed to lock: #{@path}")
else
# If scope and type are not defined, the only thing we can
@ -599,7 +598,6 @@ module RedmineDmsf
end
end
else
#raise BadRequest unless (parent.projectless_path == '/' || (parent.exist? && parent.folder))
f = DmsfFile.new
f.project_id = project.id
f.name = basename

View File

@ -59,96 +59,96 @@ class DmsfWebdavLockTest < RedmineDmsf::Test::IntegrationTest
Setting.plugin_redmine_dmsf['dmsf_webdav'] = @dmsf_webdav
Setting.plugin_redmine_dmsf['dmsf_webdav_strategy'] = @dmsf_webdav_strategy
end
#
# def test_truth
# assert_kind_of Project, @project1
# assert_kind_of Project, @project3
# assert_kind_of DmsfFile, @file1
# assert_kind_of DmsfFile, @file12
# assert_kind_of DmsfFolder, @folder10
# assert_kind_of Role, @role
# assert_kind_of User, @admin_user
# end
#
# def test_lock_file_already_locked_by_other
# log_user 'admin', 'admin' # login as admin
# User.current = @admin_user
# assert @file1.lock!, "File failed to be locked by #{User.current}"
# process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@file1.name}", params: @xml,
# headers: @jsmith.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
# assert_response :locked
# end
#
# def test_lock_file
# create_time = Time.utc(2000, 1, 2, 3, 4, 5)
# refresh_time = Time.utc(2000, 1, 2, 6, 7, 8)
# locktoken = nil
#
# # Time travel, will make the usec part of the time 0
# travel_to create_time do
# # Lock file
# process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@file1.name}", params: @xml,
# headers: @jsmith.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
# assert_response :success
# # Verify the response
# # <?xml version=\"1.0\"?>
# # <d:prop xmlns:d=\"DAV:\">
# # <d:lockdiscovery>
# # <d:activelock>
# # <d:lockscope>exclusive</d:lockscope>
# # <d:locktype>write</d:locktype>
# # <d:depth>infinity</d:depth>
# # <d:timeout>Second-604800</d:timeout>
# # <d:locktoken>
# # <d:href>f5762389-6b49-4482-9a4b-ff1c8f975765</d:href>
# # </d:locktoken>
# # </d:activelock>
# # </d:lockdiscovery>
# # </d:prop>
# assert_match '<d:lockscope>exclusive</d:lockscope>', response.body
# assert_match '<d:locktype>write</d:locktype>', response.body
# assert_match '<d:depth>infinity</d:depth>', response.body
# # 1.week = 7*24*3600=604800 seconds
# assert_match '<d:timeout>Second-604800</d:timeout>', response.body
# assert_match(/<d:locktoken><d:href>([a-z0-9\-]+)<\/d:href><\/d:locktoken>/, response.body)
# # Extract the locktoken, needed when refreshing the lock
# response.body.match(/<d:locktoken><d:href>([a-z0-9\-]+)<\/d:href><\/d:locktoken>/)
# locktoken = $1
# # Verify the lock in the db
# l = @file1.lock.first
# assert_equal create_time, l.created_at
# assert_equal create_time, l.updated_at
# assert_equal (create_time + 1.week), l.expires_at
# end
#
# travel_to refresh_time do
# # Refresh lock
# process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@file1.name}",
# params: nil,
# headers: @jsmith.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite', HTTP_IF: locktoken })
# assert_response :success
# # 1.week = 7*24*3600=604800 seconds
# assert_match '<d:timeout>Second-604800</d:timeout>', response.body
# # Verify the lock in the db
# @file1.reload
# l = @file1.lock.first
# assert_equal create_time, l.created_at
# assert_equal refresh_time, l.updated_at
# assert_equal (refresh_time + 1.week), l.expires_at
# end
# end
#
# def test_lock_file_in_subproject
# process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@project3.identifier}/#{@file12.name}", params: @xml,
# headers: @admin.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
# assert_response :success
# end
#
# def test_lock_folder_in_subproject
# process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@project3.identifier}/#{@folder10.title}", params: @xml,
# headers: @admin.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
# assert_response :success
# end
def test_truth
assert_kind_of Project, @project1
assert_kind_of Project, @project3
assert_kind_of DmsfFile, @file1
assert_kind_of DmsfFile, @file12
assert_kind_of DmsfFolder, @folder10
assert_kind_of Role, @role
assert_kind_of User, @admin_user
end
def test_lock_file_already_locked_by_other
log_user 'admin', 'admin' # login as admin
User.current = @admin_user
assert @file1.lock!, "File failed to be locked by #{User.current}"
process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@file1.name}", params: @xml,
headers: @jsmith.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
assert_response :locked
end
def test_lock_file
create_time = Time.utc(2000, 1, 2, 3, 4, 5)
refresh_time = Time.utc(2000, 1, 2, 6, 7, 8)
locktoken = nil
# Time travel, will make the usec part of the time 0
travel_to create_time do
# Lock file
process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@file1.name}", params: @xml,
headers: @jsmith.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
assert_response :success
# Verify the response
# <?xml version=\"1.0\"?>
# <d:prop xmlns:d=\"DAV:\">
# <d:lockdiscovery>
# <d:activelock>
# <d:lockscope>exclusive</d:lockscope>
# <d:locktype>write</d:locktype>
# <d:depth>infinity</d:depth>
# <d:timeout>Second-604800</d:timeout>
# <d:locktoken>
# <d:href>f5762389-6b49-4482-9a4b-ff1c8f975765</d:href>
# </d:locktoken>
# </d:activelock>
# </d:lockdiscovery>
# </d:prop>
assert_match '<d:lockscope>exclusive</d:lockscope>', response.body
assert_match '<d:locktype>write</d:locktype>', response.body
assert_match '<d:depth>infinity</d:depth>', response.body
# 1.week = 7*24*3600=604800 seconds
assert_match '<d:timeout>Second-604800</d:timeout>', response.body
assert_match(/<d:locktoken><d:href>([a-z0-9\-]+)<\/d:href><\/d:locktoken>/, response.body)
# Extract the locktoken, needed when refreshing the lock
response.body.match(/<d:locktoken><d:href>([a-z0-9\-]+)<\/d:href><\/d:locktoken>/)
locktoken = $1
# Verify the lock in the db
l = @file1.lock.first
assert_equal create_time, l.created_at
assert_equal create_time, l.updated_at
assert_equal (create_time + 1.week), l.expires_at
end
travel_to refresh_time do
# Refresh lock
process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@file1.name}",
params: nil,
headers: @jsmith.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite', HTTP_IF: locktoken })
assert_response :success
# 1.week = 7*24*3600=604800 seconds
assert_match '<d:timeout>Second-604800</d:timeout>', response.body
# Verify the lock in the db
@file1.reload
l = @file1.lock.first
assert_equal create_time, l.created_at
assert_equal refresh_time, l.updated_at
assert_equal (refresh_time + 1.week), l.expires_at
end
end
def test_lock_file_in_subproject
process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@project3.identifier}/#{@file12.name}", params: @xml,
headers: @admin.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
assert_response :success
end
def test_lock_folder_in_subproject
process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@project3.identifier}/#{@folder10.title}", params: @xml,
headers: @admin.merge!({ HTTP_DEPTH: 'infinity', HTTP_TIMEOUT: 'Infinite' })
assert_response :success
end
def test_lock_subproject
process :lock, "/dmsf/webdav/#{@project1.identifier}/#{@project3.identifier}", params: @xml,