From 64313ca20837c062872c5266f29b40a280290c2d Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Thu, 23 Mar 2006 07:14:51 +0000 Subject: [PATCH] Fixing FileController#import; sort of works, but fails on some interesting tests --- app/controllers/application.rb | 12 ++++++------ app/controllers/file_controller.rb | 14 +++++++------- app/models/wiki.rb | 2 +- test/fixtures/exported_markup.zip | Bin 0 -> 283 bytes test/functional/file_controller_test.rb | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 test/fixtures/exported_markup.zip diff --git a/app/controllers/application.rb b/app/controllers/application.rb index ea405188..852b47d5 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -2,7 +2,7 @@ # Likewise will all the methods added be available for all controllers. class ApplicationController < ActionController::Base - before_filter :connect_to_model, :check_authorization, :setup_url_generator, :set_content_type_header, :set_robots_metatag + before_filter :fetch_model_data, :check_authorization, :setup_url_generator, :set_content_type_header, :set_robots_metatag after_filter :remember_location, :teardown_url_generator # For injecting a different wiki model implementation. Intended for use in tests @@ -19,7 +19,7 @@ class ApplicationController < ActionController::Base end protected - + def check_authorization if in_a_web? and authorization_needed? and not authorized? redirect_to :controller => 'wiki', :action => 'login', :web => @web_name @@ -27,18 +27,18 @@ class ApplicationController < ActionController::Base end end - def connect_to_model + def fetch_model_data @action_name = @params['action'] || 'index' @web_name = @params['web'] @wiki = wiki + @author = cookies['author'] || 'AnonymousCoward' if @web_name @web = @wiki.webs[@web_name] if @web.nil? - render :status => 404, :text => "Unknown web '#{@web_name}'" - return false + render(:status => 404, :text => "Unknown web '#{@web_name}'") + return false end end - @author = cookies['author'] || 'AnonymousCoward' end FILE_TYPES = { diff --git a/app/controllers/file_controller.rb b/app/controllers/file_controller.rb index 57e8783a..0dc87f90 100644 --- a/app/controllers/file_controller.rb +++ b/app/controllers/file_controller.rb @@ -1,5 +1,7 @@ # Controller responsible for serving files and pictures. +require 'zip/zip' + class FileController < ApplicationController layout 'default' @@ -19,7 +21,6 @@ class FileController < ApplicationController render end else - render(:status => 404, :text => 'Unspecified file name') and return unless @file_name # no form supplied, this is a request to download the file file = WikiFile.find_by_file_name(@file_name) if file @@ -39,8 +40,7 @@ class FileController < ApplicationController if @params['file'] @problems = [] import_file_name = "#{@web.address}-import-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.zip" - file_yard.upload_file(import_file_name, @params['file']) - import_from_archive(file_yard.file_path(import_file_name)) + import_from_archive(@params['file'].path) if @problems.empty? flash[:info] = 'Import successfully finished' else @@ -56,6 +56,7 @@ class FileController < ApplicationController protected def check_allow_uploads + render(:status => 404, :text => "Web #{@params['web'].inspect} not found") and return false unless @web if @web.allow_uploads? return true else @@ -65,8 +66,7 @@ class FileController < ApplicationController end def connect_to_model - super - @file_name = @params['id'] + super and @file_name = @params['id'] end private @@ -87,10 +87,10 @@ class FileController < ApplicationController next else logger.info "Page '#{page_name}' already exists. Adding a new revision to it." - wiki.revise_page(@web.address, page_name, page_content, Time.now, @author) + wiki.revise_page(@web.address, page_name, page_content, Time.now, @author, PageRenderer.new) end else - wiki.write_page(@web.address, page_name, page_content, Time.now, @author) + wiki.write_page(@web.address, page_name, page_content, Time.now, @author, PageRenderer.new) end rescue => e logger.error(e) diff --git a/app/models/wiki.rb b/app/models/wiki.rb index 1e7f8d17..46073065 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -28,7 +28,7 @@ class Wiki if not (web = Web.find_by_address(old_address)) raise Instiki::ValidationError.new("Web with address '#{old_address}' does not exist") end - + web.update_attributes(:address => new_address, :name => name, :markup => markup, :color => color, :additional_style => additional_style, :safe_mode => safe_mode, :password => password, :published => published, :brackets_only => brackets_only, :count_pages => count_pages, :allow_uploads => allow_uploads, :max_upload_size => max_upload_size) diff --git a/test/fixtures/exported_markup.zip b/test/fixtures/exported_markup.zip new file mode 100644 index 0000000000000000000000000000000000000000..565834b05c3195cf64b6d222d9a221ace664552f GIT binary patch literal 283 zcmWIWW@h1H009YxauYUt(H&wyHV6vsE_VdlWZ6krB0GKnzYb_UQC21W)25CsJ2 mjtcNb=*H#%kRFI`7a$8}HL|w?yjj^ms+fSV0Z6xkI1B*8r#CYI literal 0 HcmV?d00001 diff --git a/test/functional/file_controller_test.rb b/test/functional/file_controller_test.rb index 5009dfca..98288864 100755 --- a/test/functional/file_controller_test.rb +++ b/test/functional/file_controller_test.rb @@ -93,4 +93,22 @@ class FileControllerTest < Test::Unit::TestCase assert_equal(picture, WikiFile.find_by_file_name('rails-e2e.gif').content) end + 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 + end