2007-01-22 14:43:50 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2009-11-30 23:28:18 +01:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2007-01-22 14:43:50 +01:00
|
|
|
require 'file_controller'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
# Raise errors beyond the default web-based presentation
|
|
|
|
class FileController; def rescue_action(e) logger.error(e); raise e end; end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class FileControllerTest < ActionController::TestCase
|
2007-01-22 14:43:50 +01:00
|
|
|
fixtures :webs, :pages, :revisions, :system
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@controller = FileController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2008-01-21 18:59:55 +01:00
|
|
|
class << @request.session
|
|
|
|
attr_accessor :dbman
|
|
|
|
end
|
|
|
|
# simulate a cookie session store
|
|
|
|
@request.session.dbman = FakeSessionDbMan
|
2007-01-22 14:43:50 +01:00
|
|
|
@web = webs(:test_wiki)
|
|
|
|
@wiki = Wiki.new
|
|
|
|
WikiFile.delete_all
|
|
|
|
require 'fileutils'
|
2009-12-27 06:43:18 +01:00
|
|
|
FileUtils.rm_rf("#{RAILS_ROOT}/webs/wiki1")
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_upload_form
|
|
|
|
get :file, :web => 'wiki1', :id => 'new_file.txt'
|
2007-05-11 18:47:38 +02:00
|
|
|
assert_response(:success)
|
|
|
|
assert_template 'file/file'
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_download_text_file
|
|
|
|
@web.wiki_files.create(:file_name => 'foo.txt', :description => 'Text file',
|
|
|
|
:content => "Contents of the file")
|
|
|
|
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'foo.txt'
|
|
|
|
|
2007-05-11 18:47:38 +02:00
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal "Contents of the file", r.body
|
2009-02-04 21:26:08 +01:00
|
|
|
assert_equal 'text/plain', r.headers['Content-Type']
|
2009-01-26 15:57:13 +01:00
|
|
|
assert_equal 'inline; filename="foo.txt"', r.headers['Content-Disposition']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_download_html_file
|
|
|
|
@web.wiki_files.create(:file_name => 'foo.html', :description => 'Text file',
|
|
|
|
:content => "Contents of the file")
|
|
|
|
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'foo.html'
|
|
|
|
|
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
|
|
|
assert_equal "Contents of the file", r.body
|
2009-02-04 21:26:08 +01:00
|
|
|
assert_equal 'application/octet-stream', r.headers['Content-Type']
|
2009-01-26 15:57:13 +01:00
|
|
|
assert_equal 'attachment; filename="foo.html"', r.headers['Content-Disposition']
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_download_pdf_file
|
|
|
|
@web.wiki_files.create(:file_name => 'foo.pdf', :description => 'PDF file',
|
|
|
|
:content => "aaa\nbbb\n")
|
|
|
|
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'foo.pdf'
|
|
|
|
|
2007-05-11 18:47:38 +02:00
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal "aaa\nbbb\n", r.body
|
2009-02-04 21:26:08 +01:00
|
|
|
assert_equal 'application/pdf', r.headers['Content-Type']
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_download_gif
|
|
|
|
pic = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
|
|
|
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'An image', :content => pic)
|
|
|
|
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
|
|
|
|
2007-05-11 18:47:38 +02:00
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
2009-02-04 21:26:08 +01:00
|
|
|
assert_equal 'image/gif', r.headers['Content-Type']
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal pic.size, r.body.size
|
|
|
|
assert_equal pic, r.body
|
2009-01-26 15:57:13 +01:00
|
|
|
assert_equal 'inline; filename="rails.gif"', r.headers['Content-Disposition']
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-06-17 18:17:25 +02:00
|
|
|
|
|
|
|
def test_pic_download_gif_published_web
|
|
|
|
@web.update_attribute(:published, true)
|
|
|
|
@web.update_attribute(:password, 'pswd')
|
|
|
|
pic = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
|
|
|
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'An image', :content => pic)
|
|
|
|
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
|
|
|
|
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
|
|
|
assert_equal 'image/gif', r.headers['Content-Type']
|
|
|
|
assert_equal pic.size, r.body.size
|
|
|
|
assert_equal pic, r.body
|
|
|
|
assert_equal 'inline; filename="rails.gif"', r.headers['Content-Disposition']
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-06-17 18:17:25 +02:00
|
|
|
def test_pic_download_gif_unpublished_web
|
|
|
|
@web.update_attribute(:published, false)
|
|
|
|
@web.update_attribute(:password, 'pswd')
|
|
|
|
pic = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
|
|
|
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'An image', :content => pic)
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
|
|
|
|
|
|
|
assert_response(:forbidden)
|
|
|
|
end
|
|
|
|
|
2009-03-04 05:27:26 +01:00
|
|
|
def test_pic_x_sendfile
|
|
|
|
pic = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
|
|
|
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'An image', :content => pic)
|
|
|
|
@request.env.update({ 'HTTP_X_SENDFILE_TYPE' => 'foo' })
|
2009-03-05 19:14:03 +01:00
|
|
|
@request.remote_addr = '127.0.0.1'
|
2009-03-04 05:27:26 +01:00
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
|
|
|
|
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
|
|
|
assert_match '/rails.gif', r.headers['X-Sendfile']
|
|
|
|
assert_equal 'image/gif', r.headers['Content-Type']
|
|
|
|
assert_equal 'inline; filename="rails.gif"', r.headers['Content-Disposition']
|
|
|
|
end
|
|
|
|
|
2009-06-17 18:17:25 +02:00
|
|
|
def test_pic_x_sendfile_published_web
|
|
|
|
@web.update_attribute(:published, true)
|
|
|
|
@web.update_attribute(:password, 'pswd')
|
|
|
|
pic = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
|
|
|
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'An image', :content => pic)
|
|
|
|
@request.env.update({ 'HTTP_X_SENDFILE_TYPE' => 'foo' })
|
|
|
|
@request.remote_addr = '127.0.0.1'
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
|
|
|
|
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
|
|
|
assert_match '/rails.gif', r.headers['X-Sendfile']
|
|
|
|
assert_equal 'image/gif', r.headers['Content-Type']
|
|
|
|
assert_equal 'inline; filename="rails.gif"', r.headers['Content-Disposition']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_x_sendfile_unpublished_web
|
|
|
|
@web.update_attribute(:published, false)
|
|
|
|
@web.update_attribute(:password, 'pswd')
|
|
|
|
pic = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
|
|
|
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'An image', :content => pic)
|
|
|
|
@request.env.update({ 'HTTP_X_SENDFILE_TYPE' => 'foo' })
|
|
|
|
@request.remote_addr = '127.0.0.1'
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
|
|
|
|
|
|
|
assert_response(:forbidden)
|
|
|
|
end
|
|
|
|
|
2009-03-05 19:14:03 +01:00
|
|
|
def test_pic_x_sendfile_type_nonlocal
|
|
|
|
pic = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
|
|
|
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'An image', :content => pic)
|
|
|
|
@request.env.update({ 'HTTP_X_SENDFILE_TYPE' => 'foo' })
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
|
|
|
|
|
|
|
assert_response(:success, bypass_body_parsing = true)
|
|
|
|
assert_equal 'image/gif', r.headers['Content-Type']
|
|
|
|
assert_equal pic.size, r.body.size
|
|
|
|
assert_equal pic, r.body
|
|
|
|
assert_equal 'inline; filename="rails.gif"', r.headers['Content-Disposition']
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_pic_unknown_pic
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'non-existant.gif'
|
|
|
|
|
2007-05-11 18:47:38 +02:00
|
|
|
assert_response(:success)
|
|
|
|
assert_template 'file/file'
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-06-17 18:17:25 +02:00
|
|
|
def test_pic_upload_published_web
|
|
|
|
@web.update_attribute(:published, true)
|
|
|
|
@web.update_attribute(:password, 'pswd')
|
|
|
|
@web.update_attribute(:allow_uploads, true)
|
|
|
|
# edit and re-render a page so that it has an "unknown file" link to 'rails-e2e.gif'
|
|
|
|
PageRenderer.setup_url_generator(StubUrlGenerator.new)
|
|
|
|
renderer = PageRenderer.new
|
|
|
|
@wiki.revise_page('wiki1', 'Oak', 'Oak', '[[rails-e2e.gif:pic]]',
|
|
|
|
Time.now, 'AnonymousBrave', renderer)
|
|
|
|
assert_equal "<p><span class='newWikiWord'>rails-e2e.gif</span></p>",
|
|
|
|
renderer.display_published
|
|
|
|
|
|
|
|
# rails-e2e.gif is unknown to the system, so pic action goes to the file [upload] form
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails-e2e.gif'
|
|
|
|
assert_response(:forbidden)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_upload_unpublished_web
|
|
|
|
@web.update_attribute(:published, false)
|
|
|
|
@web.update_attribute(:password, 'pswd')
|
|
|
|
@web.update_attribute(:allow_uploads, true)
|
|
|
|
# edit and re-render a page so that it has an "unknown file" link to 'rails-e2e.gif'
|
|
|
|
PageRenderer.setup_url_generator(StubUrlGenerator.new)
|
|
|
|
renderer = PageRenderer.new
|
|
|
|
@wiki.revise_page('wiki1', 'Oak', 'Oak', '[[rails-e2e.gif:pic]]',
|
|
|
|
Time.now, 'AnonymousBrave', renderer)
|
|
|
|
assert_equal "<p><span class='newWikiWord'>rails-e2e.gif</span></p>",
|
|
|
|
renderer.display_published
|
|
|
|
|
|
|
|
# rails-e2e.gif is unknown to the system, so pic action goes to the file [upload] form
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails-e2e.gif'
|
|
|
|
assert_response(:forbidden)
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_pic_upload_end_to_end
|
2009-02-18 08:40:11 +01:00
|
|
|
# edit and re-render a page so that it has an "unknown file" link to 'rails-e2e.gif'
|
2007-01-22 14:43:50 +01:00
|
|
|
PageRenderer.setup_url_generator(StubUrlGenerator.new)
|
|
|
|
renderer = PageRenderer.new
|
2009-06-03 05:17:15 +02:00
|
|
|
@wiki.revise_page('wiki1', 'Oak', 'Oak', '[[rails-e2e.gif:pic]]',
|
2007-01-22 14:43:50 +01:00
|
|
|
Time.now, 'AnonymousBrave', renderer)
|
2008-05-15 08:22:13 +02:00
|
|
|
assert_equal "<p><span class='newWikiWord'>rails-e2e.gif<a href='../file/rails-e2e.gif'>" +
|
2007-01-22 14:43:50 +01:00
|
|
|
"?</a></span></p>",
|
|
|
|
renderer.display_content
|
|
|
|
|
|
|
|
# rails-e2e.gif is unknown to the system, so pic action goes to the file [upload] form
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails-e2e.gif'
|
2007-05-11 18:47:38 +02:00
|
|
|
assert_response(:success)
|
|
|
|
assert_template 'file/file'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# User uploads the picture
|
2008-11-12 16:47:24 +01:00
|
|
|
begin # Ruby 1.9
|
|
|
|
picture = File.read("#{RAILS_ROOT}/test/fixtures/rails.gif", :encoding => 'ascii-8bit')
|
|
|
|
rescue #Ruby 1.8
|
|
|
|
picture = File.read("#{RAILS_ROOT}/test/fixtures/rails.gif")
|
|
|
|
end
|
2007-05-11 18:47:38 +02:00
|
|
|
# updated from post to get - post fails the spam protection (no javascript)
|
2007-10-07 08:59:50 +02:00
|
|
|
# Moron! If substituting GET for POST actually works, you
|
|
|
|
# have much, much bigger problems.
|
2009-02-18 08:40:11 +01:00
|
|
|
r = get :file, :web => 'wiki1', :referring_page => '/wiki1/show/Oak',
|
2007-10-07 08:59:50 +02:00
|
|
|
:file => {:file_name => 'rails-e2e.gif',
|
|
|
|
:content => StringIO.new(picture),
|
|
|
|
:description => 'Rails, end-to-end'}
|
2009-02-18 08:40:11 +01:00
|
|
|
assert_redirected_to '/wiki1/show/Oak'
|
2007-01-22 14:43:50 +01:00
|
|
|
assert @web.has_file?('rails-e2e.gif')
|
|
|
|
assert_equal(picture, WikiFile.find_by_file_name('rails-e2e.gif').content)
|
2008-12-20 20:56:50 +01:00
|
|
|
PageRenderer.setup_url_generator(StubUrlGenerator.new)
|
2009-06-03 05:17:15 +02:00
|
|
|
@wiki.revise_page('wiki1', 'Oak', 'Oak', 'Try [[rails-e2e.gif:pic]] again.',
|
2008-12-20 20:56:50 +01:00
|
|
|
Time.now, 'AnonymousBrave', renderer)
|
|
|
|
assert_equal "<p>Try <img alt='Rails, end-to-end' src='../file/rails-e2e.gif'/> again.</p>",
|
|
|
|
renderer.display_content
|
|
|
|
assert_equal "<p>Try <img alt='Rails, end-to-end' src='../file/rails-e2e.gif'/> again.</p>",
|
|
|
|
renderer.display_published
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_import
|
2007-05-11 18:47:38 +02:00
|
|
|
# updated from post to get - post fails the spam protection (no javascript)
|
|
|
|
r = get :import, :web => 'wiki1', :file => uploaded_file("#{RAILS_ROOT}/test/fixtures/exported_markup.zip")
|
|
|
|
assert_response(:redirect)
|
2007-01-22 14:43:50 +01:00
|
|
|
assert @web.has_page?('ImportedPage')
|
|
|
|
end
|
|
|
|
|
|
|
|
def uploaded_file(path, content_type="application/octet-stream", filename=nil)
|
|
|
|
filename ||= File.basename(path)
|
|
|
|
t = Tempfile.new(filename)
|
|
|
|
FileUtils.copy_file(path, t.path)
|
|
|
|
(class << t; self; end;).class_eval do
|
|
|
|
alias local_path path
|
|
|
|
define_method(:original_filename) { filename }
|
|
|
|
define_method(:content_type) { content_type }
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|