[BREAKS BUILD] Some work on File uploads, half-done, committing as a backup
This commit is contained in:
parent
a61c11854d
commit
8bdee631f6
13 changed files with 308 additions and 277 deletions
|
@ -11,41 +11,38 @@ class FileController; def rescue_action(e) logger.error(e); raise e end; end
|
|||
class FileControllerTest < Test::Unit::TestCase
|
||||
fixtures :webs, :pages, :revisions, :system
|
||||
|
||||
Wiki.storage_path += "test/"
|
||||
FILE_AREA = Wiki.storage_path + 'wiki1'
|
||||
FileUtils.mkdir_p(FILE_AREA) unless File.directory?(FILE_AREA)
|
||||
FileUtils.rm(Dir["#{FILE_AREA}/*"])
|
||||
|
||||
def setup
|
||||
@controller = FileController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@wiki = Wiki.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@web = webs(:test_wiki)
|
||||
@home = @page = pages(:home_page)
|
||||
WikiFile.delete_all
|
||||
require 'fileutils'
|
||||
FileUtils.rm_rf("#{RAILS_ROOT}/public/wiki1/files/*")
|
||||
end
|
||||
|
||||
def test_file
|
||||
process 'file', 'web' => 'wiki1', 'id' => 'foo.tgz'
|
||||
|
||||
def test_file_upload_form
|
||||
get :file, :web => 'wiki1', :id => 'new_file.txt'
|
||||
assert_success
|
||||
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'
|
||||
@web.wiki_files.create(:file_name => 'foo.txt', :description => 'Text file',
|
||||
:content => "Contents of the file")
|
||||
|
||||
r = get :file, :web => 'wiki1', :id => 'foo.txt'
|
||||
|
||||
assert_success(bypass_body_parsing = true)
|
||||
assert_equal "aaa\nbbb\n", r.body
|
||||
assert_equal "Contents of the file", r.body
|
||||
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" }
|
||||
@web.wiki_files.create(:file_name => 'foo.pdf', :description => 'PDF file',
|
||||
:content => "aaa\nbbb\n")
|
||||
|
||||
r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf'
|
||||
r = get :file, :web => 'wiki1', :id => 'foo.pdf'
|
||||
|
||||
assert_success(bypass_body_parsing = true)
|
||||
assert_equal "aaa\nbbb\n", r.body
|
||||
|
@ -53,80 +50,83 @@ class FileControllerTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_pic_download_gif
|
||||
FileUtils.cp("#{RAILS_ROOT}/test/fixtures/rails.gif", FILE_AREA)
|
||||
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 = process 'pic', 'web' => 'wiki1', 'id' => 'rails.gif'
|
||||
r = get :file, :web => 'wiki1', :id => 'rails.gif'
|
||||
|
||||
assert_success(bypass_body_parsing = true)
|
||||
assert_equal File.size("#{FILE_AREA}/rails.gif"), r.body.size
|
||||
assert_equal 'image/gif', r.headers['Content-Type']
|
||||
assert_equal pic.size, r.body.size
|
||||
assert_equal pic, r.body
|
||||
end
|
||||
|
||||
def test_pic_unknown_pic
|
||||
r = process 'pic', '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'
|
||||
@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'
|
||||
assert_success
|
||||
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',
|
||||
test_renderer)
|
||||
assert_equal "<p><span class=\"newWikiWord\">instiki-e2e.txt" +
|
||||
"<a href=\"../file/instiki-e2e.txt\">?</a></span></p>",
|
||||
test_renderer(@home.revisions.last).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'
|
||||
assert_success
|
||||
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)
|
||||
assert_redirected_to :controller => 'wiki', :action => 'show', :web => 'wiki1', :id => 'HomePage'
|
||||
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)
|
||||
@home = Page.find(@home.id)
|
||||
assert_equal "<p><a class=\"existingWikiWord\" href=\"../file/instiki-e2e.txt\">" +
|
||||
"instiki-e2e.txt</a></p>",
|
||||
test_renderer(@home.revisions.last).display_content
|
||||
end
|
||||
|
||||
def test_uploads_blocking
|
||||
set_web_property :allow_uploads, true
|
||||
process 'file', 'web' => 'wiki1', 'id' => 'filename'
|
||||
assert_success
|
||||
|
||||
set_web_property :allow_uploads, false
|
||||
process 'file', 'web' => 'wiki1', 'id' => 'filename'
|
||||
assert_response 403
|
||||
end
|
||||
# def test_pic_unknown_pic
|
||||
# r = process 'pic', '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'
|
||||
# @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'
|
||||
# assert_success
|
||||
# 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',
|
||||
# test_renderer)
|
||||
# assert_equal "<p><span class=\"newWikiWord\">instiki-e2e.txt" +
|
||||
# "<a href=\"../file/instiki-e2e.txt\">?</a></span></p>",
|
||||
# test_renderer(@home.revisions.last).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'
|
||||
# assert_success
|
||||
# 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)
|
||||
# assert_redirected_to :controller => 'wiki', :action => 'show', :web => 'wiki1', :id => 'HomePage'
|
||||
# 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)
|
||||
# @home = Page.find(@home.id)
|
||||
# assert_equal "<p><a class=\"existingWikiWord\" href=\"../file/instiki-e2e.txt\">" +
|
||||
# "instiki-e2e.txt</a></p>",
|
||||
# test_renderer(@home.revisions.last).display_content
|
||||
# end
|
||||
#
|
||||
# def test_uploads_blocking
|
||||
# set_web_property :allow_uploads, true
|
||||
# process 'file', 'web' => 'wiki1', 'id' => 'filename'
|
||||
# assert_success
|
||||
#
|
||||
# set_web_property :allow_uploads, false
|
||||
# process 'file', 'web' => 'wiki1', 'id' => 'filename'
|
||||
# assert_response 403
|
||||
# end
|
||||
|
||||
end
|
||||
|
|
|
@ -33,10 +33,8 @@ class RoutesTest < Test::Unit::TestCase
|
|||
:controller => 'wiki', :web => 'web', :action => 'show',
|
||||
:id => 'HomePage?arg1=value1&arg2=value2')
|
||||
|
||||
assert_routing('web/file/abc.zip',
|
||||
assert_routing('web/files/abc.zip',
|
||||
:web => 'web', :controller => 'file', :action => 'file', :id => 'abc.zip')
|
||||
assert_routing('web/pic/abc.jpg',
|
||||
:web => 'web', :controller => 'file', :action => 'pic', :id => 'abc.jpg')
|
||||
assert_routing('web/import', :web => 'web', :controller => 'file', :action => 'import')
|
||||
# default option is wiki
|
||||
assert_recognizes({:controller => 'wiki', :web => 'unknown_path', :action => 'index', },
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
require 'fileutils'
|
||||
require 'file_yard'
|
||||
require 'stringio'
|
||||
|
||||
class FileYardTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
FileUtils.mkdir_p(file_path)
|
||||
FileUtils.rm(Dir["#{file_path}/*"])
|
||||
@yard = FileYard.new(file_path, 100)
|
||||
end
|
||||
|
||||
def test_check_upload_size
|
||||
assert_nothing_raised { @yard.check_upload_size(100.kilobytes) }
|
||||
assert_raises(Instiki::ValidationError) { @yard.check_upload_size(100.kilobytes + 1) }
|
||||
end
|
||||
|
||||
def test_files
|
||||
assert_equal [], @yard.files
|
||||
|
||||
# FileYard gets the list of files from directory in the constructor
|
||||
@yard.upload_file('aaa', StringIO.new('file contents'))
|
||||
assert_equal ["#{file_path}/aaa"], Dir["#{file_path}/*"]
|
||||
assert_equal ['aaa'], @yard.files
|
||||
assert @yard.has_file?('aaa')
|
||||
assert_equal 'file contents', File.read(@yard.file_path('aaa'))
|
||||
end
|
||||
|
||||
def test_file_path
|
||||
assert_equal "#{file_path}/abcd", @yard.file_path('abcd')
|
||||
end
|
||||
|
||||
def test_size_limit
|
||||
@yard = FileYard.new(file_path, 1)
|
||||
one_kilobyte_string = "a" * 1.kilobyte
|
||||
|
||||
# as StringIO
|
||||
assert_nothing_raised {
|
||||
@yard.upload_file('acceptable_file', StringIO.new(one_kilobyte_string))
|
||||
}
|
||||
assert_raises(Instiki::ValidationError) {
|
||||
@yard.upload_file('one_byte_too_long', StringIO.new(one_kilobyte_string + 'a'))
|
||||
}
|
||||
|
||||
# as Tempfile
|
||||
require 'tempfile'
|
||||
|
||||
Tempfile.open('acceptable_file') do |f|
|
||||
f.write(one_kilobyte_string)
|
||||
assert_nothing_raised {
|
||||
@yard.upload_file('acceptable_file', f)
|
||||
}
|
||||
end
|
||||
|
||||
Tempfile.open('one_byte_too_long') do |f|
|
||||
f.write(one_kilobyte_string + 'a')
|
||||
assert_nothing_raised {
|
||||
@yard.upload_file('one_byte_too_long_2', f)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def file_path
|
||||
"#{RAILS_ROOT}/storage/test/instiki"
|
||||
end
|
||||
|
||||
end
|
|
@ -277,9 +277,10 @@ class PageRendererTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_link_to_pic
|
||||
FileUtils.mkdir_p "#{RAILS_ROOT}/storage/test/wiki1"
|
||||
FileUtils.rm(Dir["#{RAILS_ROOT}/storage/test/wiki1/*"])
|
||||
@wiki.file_yard(@web).upload_file('square.jpg', StringIO.new(''))
|
||||
WikiFile.delete_all
|
||||
require 'fileutils'
|
||||
FileUtils.rm_rf("#{RAILS_ROOT}/public/wiki1/files/*")
|
||||
@web.wiki_files.create(:file_name => 'square.jpg', :description => 'Square', :content => 'never mind')
|
||||
assert_markup_parsed_as(
|
||||
'<p><img alt="Square" src="../pic/square.jpg" /></p>',
|
||||
'[[square.jpg|Square:pic]]')
|
||||
|
@ -305,8 +306,6 @@ class PageRendererTest < Test::Unit::TestCase
|
|||
'[[With:Colon]]')
|
||||
end
|
||||
|
||||
# TODO Remove the leading underscores from this test when upgrading to RedCloth 3.0.1;
|
||||
# also add a test for the "Unhappy Face" problem (another interesting RedCloth bug)
|
||||
def test_list_with_tildas
|
||||
list_with_tildas = <<-EOL
|
||||
* "a":~b
|
||||
|
|
84
test/unit/wiki_file_test.rb
Normal file
84
test/unit/wiki_file_test.rb
Normal file
|
@ -0,0 +1,84 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
require 'fileutils'
|
||||
|
||||
class WikiFileTest < Test::Unit::TestCase
|
||||
include FileUtils
|
||||
fixtures :webs, :pages, :revisions, :system, :wiki_references
|
||||
|
||||
def setup
|
||||
@web = webs(:test_wiki)
|
||||
mkdir_p("#{RAILS_ROOT}/public/wiki1/files/")
|
||||
rm_rf("#{RAILS_ROOT}/public/wiki1/files/*")
|
||||
WikiFile.delete_all
|
||||
end
|
||||
|
||||
def test_basic_store_and_retrieve_ascii_file
|
||||
@web.wiki_files.create(:file_name => 'binary_file', :description => 'Binary file', :content => "\001\002\003")
|
||||
binary = WikiFile.find_by_file_name('binary_file')
|
||||
assert_equal "\001\002\003", binary.content
|
||||
end
|
||||
|
||||
def test_basic_store_and_retrieve_binary_file
|
||||
@web.wiki_files.create(:file_name => 'text_file', :description => 'Text file', :content => "abc")
|
||||
text = WikiFile.find_by_file_name('text_file')
|
||||
assert_equal "abc", text.content
|
||||
end
|
||||
|
||||
def test_storing_an_image
|
||||
rails_gif = File.open("#{RAILS_ROOT}/test/fixtures/rails.gif", 'rb') { |f| f.read }
|
||||
assert_equal rails_gif.size, File.size("#{RAILS_ROOT}/test/fixtures/rails.gif")
|
||||
|
||||
@web.wiki_files.create(:file_name => 'rails.gif', :description => 'Rails logo', :content => rails_gif)
|
||||
|
||||
rails_gif_from_db = WikiFile.find_by_file_name('rails.gif')
|
||||
assert_equal rails_gif.size, rails_gif_from_db.content.size
|
||||
assert_equal rails_gif, rails_gif_from_db.content
|
||||
end
|
||||
|
||||
def test_mandatory_fields_validations
|
||||
assert_validation(:file_name, '', :fail)
|
||||
assert_validation(:file_name, nil, :fail)
|
||||
assert_validation(:content, '', :fail)
|
||||
assert_validation(:content, nil, :fail)
|
||||
end
|
||||
|
||||
def test_upload_size_validation
|
||||
assert_validation(:content, 'a' * 100.kilobytes, :pass)
|
||||
assert_validation(:content, 'a' * (100.kilobytes + 1), :fail)
|
||||
end
|
||||
|
||||
def test_file_name_size_validation
|
||||
assert_validation(:file_name, '', :fail)
|
||||
assert_validation(:file_name, 'a', :pass)
|
||||
assert_validation(:file_name, 'a' * 50, :pass)
|
||||
assert_validation(:file_name, 'a' * 51, :fail)
|
||||
end
|
||||
|
||||
def test_file_name_pattern_validation
|
||||
assert_validation(:file_name, ".. Accep-table File.name", :pass)
|
||||
assert_validation(:file_name, "/bad", :fail)
|
||||
assert_validation(:file_name, "~bad", :fail)
|
||||
assert_validation(:file_name, "..\bad", :fail)
|
||||
assert_validation(:file_name, "\001bad", :fail)
|
||||
assert_validation(:file_name, ".", :fail)
|
||||
assert_validation(:file_name, "..", :fail)
|
||||
end
|
||||
|
||||
def test_find_by_file_name
|
||||
assert_equal @file1, WikiFile.find_by_file_name('file1.txt')
|
||||
assert_nil WikiFile.find_by_file_name('unknown_file')
|
||||
end
|
||||
|
||||
def assert_validation(field, value, expected_result)
|
||||
values = {:file_name => '0', :description => '0', :content => '0'}
|
||||
raise "WikiFile has no attribute named #{field.inspect}" unless values.has_key?(field)
|
||||
values[field] = value
|
||||
|
||||
new_object = @web.wiki_files.create(values)
|
||||
if expected_result == :pass then assert(new_object.valid?, new_object.errors.inspect)
|
||||
elsif expected_result == :fail then assert(!new_object.valid?)
|
||||
else raise "Unknown value of expected_result: #{expected_result.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue