2005-08-11 07:46:38 +02:00
|
|
|
#!/usr/bin/env ruby
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
|
|
|
require 'file_controller'
|
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
# Raise errors beyond the default web-based presentation
|
|
|
|
class FileController; def rescue_action(e) logger.error(e); raise e end; end
|
|
|
|
|
|
|
|
class FileControllerTest < Test::Unit::TestCase
|
2005-08-09 04:20:28 +02:00
|
|
|
fixtures :webs, :pages, :revisions, :system
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
Wiki.storage_path += "test/"
|
|
|
|
FILE_AREA = Wiki.storage_path + 'wiki1'
|
2005-01-24 19:52:04 +01:00
|
|
|
FileUtils.mkdir_p(FILE_AREA) unless File.directory?(FILE_AREA)
|
|
|
|
FileUtils.rm(Dir["#{FILE_AREA}/*"])
|
|
|
|
|
|
|
|
def setup
|
2005-08-09 04:20:28 +02:00
|
|
|
@controller = FileController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
@wiki = Wiki.new
|
|
|
|
@web = webs(:test_wiki)
|
|
|
|
@home = @page = pages(:home_page)
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_file
|
|
|
|
process 'file', 'web' => 'wiki1', 'id' => 'foo.tgz'
|
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_rendered_file 'file/file'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_download_text_file
|
|
|
|
File.open("#{FILE_AREA}/foo.txt", 'wb') { |f| f.write "aaa\nbbb\n" }
|
|
|
|
|
|
|
|
r = process 'file', 'web' => 'wiki1', 'id' => 'foo.txt'
|
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_equal "aaa\nbbb\n", r.binary_content
|
|
|
|
assert_equal 'text/plain', r.headers['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_download_pdf_file
|
|
|
|
File.open("#{FILE_AREA}/foo.pdf", 'wb') { |f| f.write "aaa\nbbb\n" }
|
|
|
|
|
|
|
|
r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf'
|
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_equal "aaa\nbbb\n", r.binary_content
|
|
|
|
assert_equal 'application/pdf', r.headers['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_download_gif
|
|
|
|
FileUtils.cp("#{RAILS_ROOT}/test/fixtures/rails.gif", FILE_AREA)
|
|
|
|
|
|
|
|
r = process 'pic', 'web' => 'wiki1', 'id' => 'rails.gif'
|
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_equal File.size("#{FILE_AREA}/rails.gif"), r.binary_content.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_unknown_pic
|
|
|
|
r = process 'pic', 'web' => 'wiki1', 'id' => 'non-existant.gif'
|
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_rendered_file 'file/file'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_upload_end_to_end
|
|
|
|
# edit and re-render home page so that it has an "unknown file" link to 'rails-e2e.gif'
|
|
|
|
@wiki.revise_page('wiki1', 'HomePage', '[[rails-e2e.gif:pic]]', Time.now, 'AnonymousBrave')
|
|
|
|
assert_equal "<p><span class=\"newWikiWord\">rails-e2e.gif<a href=\"../pic/rails-e2e.gif\">" +
|
|
|
|
"?</a></span></p>",
|
|
|
|
@home.display_content
|
|
|
|
|
|
|
|
# rails-e2e.gif is unknown to the system, so pic action goes to the file [upload] form
|
|
|
|
r = process 'pic', 'web' => 'wiki1', 'id' => 'rails-e2e.gif'
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_rendered_file 'file/file'
|
|
|
|
|
|
|
|
# User uploads the picture
|
|
|
|
picture = File.read("#{RAILS_ROOT}/test/fixtures/rails.gif")
|
|
|
|
r = process 'pic', 'web' => 'wiki1', 'id' => 'rails-e2e.gif', 'file' => StringIO.new(picture)
|
|
|
|
assert_redirect_url '/'
|
|
|
|
assert @wiki.file_yard(@web).has_file?('rails-e2e.gif')
|
|
|
|
assert_equal(picture, File.read("#{RAILS_ROOT}/storage/test/wiki1/rails-e2e.gif"))
|
|
|
|
|
|
|
|
# this should refresh the page display content (cached)
|
|
|
|
assert_equal "<p><img alt=\"rails-e2e.gif\" src=\"../pic/rails-e2e.gif\" /></p>",
|
|
|
|
@home.display_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_upload_end_to_end
|
|
|
|
# edit and re-render home page so that it has an "unknown file" link to 'rails-e2e.gif'
|
|
|
|
@wiki.revise_page('wiki1', 'HomePage', '[[instiki-e2e.txt:file]]', Time.now, 'AnonymousBrave')
|
|
|
|
assert_equal "<p><span class=\"newWikiWord\">instiki-e2e.txt" +
|
|
|
|
"<a href=\"../file/instiki-e2e.txt\">?</a></span></p>",
|
|
|
|
@home.display_content
|
|
|
|
|
|
|
|
# rails-e2e.gif is unknown to the system, so pic action goes to the file [upload] form
|
|
|
|
r = process 'file', 'web' => 'wiki1', 'id' => 'instiki-e2e.txt'
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_rendered_file 'file/file'
|
|
|
|
|
|
|
|
# User uploads the picture
|
|
|
|
file = "abcdefgh\n123"
|
|
|
|
r = process 'file', 'web' => 'wiki1', 'id' => 'instiki-e2e.txt', 'file' => StringIO.new(file)
|
2005-05-09 07:53:47 +02:00
|
|
|
assert_redirected_to :controller => 'wiki', :action => 'show', :web => 'wiki1', :id => 'HomePage'
|
2005-01-24 19:52:04 +01:00
|
|
|
assert @wiki.file_yard(@web).has_file?('instiki-e2e.txt')
|
|
|
|
assert_equal(file, File.read("#{RAILS_ROOT}/storage/test/wiki1/instiki-e2e.txt"))
|
|
|
|
|
|
|
|
# this should refresh the page display content (cached)
|
2005-08-09 04:20:28 +02:00
|
|
|
@home = Page.find(@home.id)
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_equal "<p><a class=\"existingWikiWord\" href=\"../file/instiki-e2e.txt\">" +
|
|
|
|
"instiki-e2e.txt</a></p>",
|
|
|
|
@home.display_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_uploads_blocking
|
2005-08-09 04:20:28 +02:00
|
|
|
set_web_property :allow_uploads, true
|
2005-01-24 19:52:04 +01:00
|
|
|
r = process 'file', 'web' => 'wiki1', 'id' => 'filename'
|
2005-08-09 04:20:28 +02:00
|
|
|
assert_response :success
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
set_web_property :allow_uploads, false
|
2005-01-24 19:52:04 +01:00
|
|
|
r = process 'file', 'web' => 'wiki1', 'id' => 'filename'
|
|
|
|
assert_equal '403 Forbidden', r.headers['Status']
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|