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'
|
2005-11-02 06:59:49 +01:00
|
|
|
require 'stringio'
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
def setup
|
2005-08-09 04:20:28 +02:00
|
|
|
@controller = FileController.new
|
2005-11-13 14:37:47 +01:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2005-08-09 04:20:28 +02:00
|
|
|
@web = webs(:test_wiki)
|
2005-11-14 09:38:37 +01:00
|
|
|
@wiki = Wiki.new
|
2005-11-13 14:37:47 +01:00
|
|
|
WikiFile.delete_all
|
|
|
|
require 'fileutils'
|
|
|
|
FileUtils.rm_rf("#{RAILS_ROOT}/public/wiki1/files/*")
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2005-11-13 14:37:47 +01:00
|
|
|
def test_file_upload_form
|
|
|
|
get :file, :web => 'wiki1', :id => 'new_file.txt'
|
2005-09-09 05:23:33 +02:00
|
|
|
assert_success
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_rendered_file 'file/file'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_download_text_file
|
2005-11-13 14:37:47 +01:00
|
|
|
@web.wiki_files.create(:file_name => 'foo.txt', :description => 'Text file',
|
|
|
|
:content => "Contents of the file")
|
|
|
|
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'foo.txt'
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2005-09-09 05:23:33 +02:00
|
|
|
assert_success(bypass_body_parsing = true)
|
2005-11-13 14:37:47 +01:00
|
|
|
assert_equal "Contents of the file", r.body
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_equal 'text/plain', r.headers['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_download_pdf_file
|
2005-11-13 14:37:47 +01:00
|
|
|
@web.wiki_files.create(:file_name => 'foo.pdf', :description => 'PDF file',
|
|
|
|
:content => "aaa\nbbb\n")
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2005-11-13 14:37:47 +01:00
|
|
|
r = get :file, :web => 'wiki1', :id => 'foo.pdf'
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2005-09-09 05:23:33 +02:00
|
|
|
assert_success(bypass_body_parsing = true)
|
|
|
|
assert_equal "aaa\nbbb\n", r.body
|
2005-01-24 19:52:04 +01:00
|
|
|
assert_equal 'application/pdf', r.headers['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pic_download_gif
|
2005-11-13 14:37:47 +01:00
|
|
|
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)
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2005-11-13 14:37:47 +01:00
|
|
|
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2005-09-09 05:23:33 +02:00
|
|
|
assert_success(bypass_body_parsing = true)
|
2005-11-13 14:37:47 +01:00
|
|
|
assert_equal 'image/gif', r.headers['Content-Type']
|
|
|
|
assert_equal pic.size, r.body.size
|
|
|
|
assert_equal pic, r.body
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2005-11-14 09:38:37 +01:00
|
|
|
def test_pic_unknown_pic
|
|
|
|
r = get :file, :web => 'wiki1', :id => 'non-existant.gif'
|
|
|
|
|
|
|
|
assert_success
|
|
|
|
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'
|
|
|
|
PageRenderer.setup_url_generator(StubUrlGenerator.new)
|
|
|
|
renderer = PageRenderer.new
|
|
|
|
@wiki.revise_page('wiki1', 'HomePage', '[[rails-e2e.gif:pic]]',
|
|
|
|
Time.now, 'AnonymousBrave', renderer)
|
|
|
|
assert_equal "<p><span class=\"newWikiWord\">rails-e2e.gif<a href=\"../file/rails-e2e.gif\">" +
|
|
|
|
"?</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'
|
|
|
|
assert_success
|
|
|
|
assert_rendered_file 'file/file'
|
|
|
|
|
|
|
|
# User uploads the picture
|
|
|
|
picture = File.read("#{RAILS_ROOT}/test/fixtures/rails.gif")
|
2005-12-30 09:42:06 +01:00
|
|
|
r = post :file, :web => 'wiki1',
|
|
|
|
:file => {:file_name => 'rails-e2e.gif', :content => StringIO.new(picture)}
|
|
|
|
assert_redirected_to({})
|
2005-11-14 09:38:37 +01:00
|
|
|
assert @web.has_file?('rails-e2e.gif')
|
2005-12-30 09:42:06 +01:00
|
|
|
assert_equal(picture, WikiFile.find_by_file_name('rails-e2e.gif').content)
|
2005-11-14 09:38:37 +01:00
|
|
|
end
|
|
|
|
|
2006-03-23 08:14:51 +01:00
|
|
|
def test_import
|
|
|
|
r = post :import, :web => 'wiki1', :file => uploaded_file("#{RAILS_ROOT}/test/fixtures/exported_markup.zip")
|
|
|
|
assert_redirect
|
|
|
|
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
|
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|