diff --git a/app/site_files.rb b/app/site_files.rb index fa429c50..9cf8bd17 100644 --- a/app/site_files.rb +++ b/app/site_files.rb @@ -180,12 +180,19 @@ post '/site_files/rename' do new_path = HTMLEntities.new.decode params[:new_path] site_file = current_site.site_files.select {|s| s.path == path}.first - res = site_file.rename new_path + escaped_path = Rack::Utils.escape_html path + escaped_new_path = Rack::Utils.escape_html new_path - if res.first == true - flash[:success] = "Renamed #{Rack::Utils.escape_html path} to #{Rack::Utils.escape_html new_path}" + if site_file.nil? + flash[:error] = "File #{escaped_path} does not exist." else - flash[:error] = "Failed to rename #{Rack::Utils.escape_html path} to #{Rack::Utils.escape_html new_path}: #{Rack::Utils.escape_html res.last}" + res = site_file.rename new_path + + if res.first == true + flash[:success] = "Renamed #{escaped_path} to #{escaped_new_path}" + else + flash[:error] = "Failed to rename #{escaped_path} to #{escaped_new_path}: #{Rack::Utils.escape_html res.last}" + end end dirname = Pathname(path).dirname diff --git a/tests/site_file_tests.rb b/tests/site_file_tests.rb index 24643d17..8a531481 100644 --- a/tests/site_file_tests.rb +++ b/tests/site_file_tests.rb @@ -41,6 +41,16 @@ describe 'site_files' do _(File.exist?(@site.files_path('derp.jpg'))).must_equal true end + it 'fails when file does not exist' do + #uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg') + #upload 'files[]' => uploaded_file + + post '/site_files/rename', {path: 'derp.jpg', new_path: 'derp2.jpg', csrf_token: 'abcd'}, {'rack.session' => { 'id' => @site.id, '_csrf_token' => 'abcd' }} + _(last_response.headers['Location']).must_match /dashboard/ + get '/dashboard', {}, {'rack.session' => { 'id' => @site.id, '_csrf_token' => 'abcd' }} + _(last_response.body).must_match /file derp.jpg does not exist/i + end + it 'fails for bad extension change' do uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg') upload 'files[]' => uploaded_file