Chris@119: # encoding: utf-8 Chris@119: # Chris@119: # Redmine - project management software Chris@119: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../test_helper', __FILE__) Chris@0: require 'attachments_controller' Chris@0: Chris@0: # Re-raise errors caught by the controller. Chris@0: class AttachmentsController; def rescue_action(e) raise e end; end Chris@0: Chris@0: class AttachmentsControllerTest < ActionController::TestCase Chris@909: fixtures :users, :projects, :roles, :members, :member_roles, Chris@909: :enabled_modules, :issues, :trackers, :attachments, Chris@0: :versions, :wiki_pages, :wikis, :documents Chris@441: Chris@0: def setup Chris@0: @controller = AttachmentsController.new Chris@0: @request = ActionController::TestRequest.new Chris@0: @response = ActionController::TestResponse.new Chris@909: Attachment.storage_path = "#{Rails.root}/test/fixtures/files" Chris@0: User.current = nil Chris@0: end Chris@441: Chris@0: def test_show_diff Chris@909: ['inline', 'sbs'].each do |dt| Chris@909: # 060719210727_changeset_utf8.diff Chris@909: get :show, :id => 14, :type => dt Chris@909: assert_response :success Chris@909: assert_template 'diff' Chris@909: assert_equal 'text/html', @response.content_type Chris@909: assert_tag 'th', Chris@909: :attributes => {:class => /filename/}, Chris@909: :content => /issues_controller.rb\t\(révision 1484\)/ Chris@909: assert_tag 'td', Chris@909: :attributes => {:class => /line-code/}, Chris@909: :content => /Demande créée avec succès/ Chris@909: end Chris@909: set_tmp_attachments_directory Chris@909: end Chris@909: Chris@909: def test_show_diff_replcace_cannot_convert_content Chris@909: with_settings :repositories_encodings => 'UTF-8' do Chris@909: ['inline', 'sbs'].each do |dt| Chris@909: # 060719210727_changeset_iso8859-1.diff Chris@909: get :show, :id => 5, :type => dt Chris@909: assert_response :success Chris@909: assert_template 'diff' Chris@909: assert_equal 'text/html', @response.content_type Chris@909: assert_tag 'th', Chris@909: :attributes => {:class => "filename"}, Chris@909: :content => /issues_controller.rb\t\(r\?vision 1484\)/ Chris@909: assert_tag 'td', Chris@909: :attributes => {:class => /line-code/}, Chris@909: :content => /Demande cr\?\?e avec succ\?s/ Chris@909: end Chris@909: end Chris@909: set_tmp_attachments_directory Chris@909: end Chris@909: Chris@909: def test_show_diff_latin_1 Chris@909: with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do Chris@909: ['inline', 'sbs'].each do |dt| Chris@909: # 060719210727_changeset_iso8859-1.diff Chris@909: get :show, :id => 5, :type => dt Chris@909: assert_response :success Chris@909: assert_template 'diff' Chris@909: assert_equal 'text/html', @response.content_type Chris@909: assert_tag 'th', Chris@909: :attributes => {:class => "filename"}, Chris@909: :content => /issues_controller.rb\t\(révision 1484\)/ Chris@909: assert_tag 'td', Chris@909: :attributes => {:class => /line-code/}, Chris@909: :content => /Demande créée avec succès/ Chris@909: end Chris@909: end Chris@909: set_tmp_attachments_directory Chris@909: end Chris@909: Chris@909: def test_save_diff_type Chris@909: @request.session[:user_id] = 1 # admin Chris@909: user = User.find(1) Chris@909: get :show, :id => 5 Chris@0: assert_response :success Chris@0: assert_template 'diff' Chris@909: user.reload Chris@909: assert_equal "inline", user.pref[:diff_type] Chris@909: get :show, :id => 5, :type => 'sbs' Chris@119: assert_response :success Chris@119: assert_template 'diff' Chris@909: user.reload Chris@909: assert_equal "sbs", user.pref[:diff_type] Chris@0: end Chris@441: Chris@0: def test_show_text_file Chris@0: get :show, :id => 4 Chris@0: assert_response :success Chris@0: assert_template 'file' Chris@0: assert_equal 'text/html', @response.content_type Chris@909: set_tmp_attachments_directory Chris@909: end Chris@909: Chris@909: def test_show_text_file_utf_8 Chris@909: set_tmp_attachments_directory Chris@909: a = Attachment.new(:container => Issue.find(1), Chris@909: :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"), Chris@909: :author => User.find(1)) Chris@909: assert a.save Chris@909: assert_equal 'japanese-utf-8.txt', a.filename Chris@909: Chris@909: str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" Chris@909: str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding) Chris@909: Chris@909: get :show, :id => a.id Chris@909: assert_response :success Chris@909: assert_template 'file' Chris@909: assert_equal 'text/html', @response.content_type Chris@909: assert_tag :tag => 'th', Chris@909: :content => '1', Chris@909: :attributes => { :class => 'line-num' }, Chris@909: :sibling => { :tag => 'td', :content => /#{str_japanese}/ } Chris@909: end Chris@909: Chris@909: def test_show_text_file_replcace_cannot_convert_content Chris@909: set_tmp_attachments_directory Chris@909: with_settings :repositories_encodings => 'UTF-8' do Chris@909: a = Attachment.new(:container => Issue.find(1), Chris@909: :file => uploaded_test_file("iso8859-1.txt", "text/plain"), Chris@909: :author => User.find(1)) Chris@909: assert a.save Chris@909: assert_equal 'iso8859-1.txt', a.filename Chris@909: Chris@909: get :show, :id => a.id Chris@909: assert_response :success Chris@909: assert_template 'file' Chris@909: assert_equal 'text/html', @response.content_type Chris@909: assert_tag :tag => 'th', Chris@909: :content => '7', Chris@909: :attributes => { :class => 'line-num' }, Chris@909: :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ } Chris@909: end Chris@909: end Chris@909: Chris@909: def test_show_text_file_latin_1 Chris@909: set_tmp_attachments_directory Chris@909: with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do Chris@909: a = Attachment.new(:container => Issue.find(1), Chris@909: :file => uploaded_test_file("iso8859-1.txt", "text/plain"), Chris@909: :author => User.find(1)) Chris@909: assert a.save Chris@909: assert_equal 'iso8859-1.txt', a.filename Chris@909: Chris@909: get :show, :id => a.id Chris@909: assert_response :success Chris@909: assert_template 'file' Chris@909: assert_equal 'text/html', @response.content_type Chris@909: assert_tag :tag => 'th', Chris@909: :content => '7', Chris@909: :attributes => { :class => 'line-num' }, Chris@909: :sibling => { :tag => 'td', :content => /Demande créée avec succès/ } Chris@909: end Chris@0: end Chris@441: Chris@0: def test_show_text_file_should_send_if_too_big Chris@0: Setting.file_max_size_displayed = 512 Chris@0: Attachment.find(4).update_attribute :filesize, 754.kilobyte Chris@441: Chris@0: get :show, :id => 4 Chris@0: assert_response :success Chris@0: assert_equal 'application/x-ruby', @response.content_type Chris@909: set_tmp_attachments_directory Chris@0: end Chris@441: Chris@0: def test_show_other Chris@0: get :show, :id => 6 Chris@0: assert_response :success Chris@0: assert_equal 'application/octet-stream', @response.content_type Chris@909: set_tmp_attachments_directory Chris@0: end Chris@441: Chris@441: def test_show_file_from_private_issue_without_permission Chris@441: get :show, :id => 15 Chris@441: assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15' Chris@909: set_tmp_attachments_directory Chris@441: end Chris@441: Chris@441: def test_show_file_from_private_issue_with_permission Chris@441: @request.session[:user_id] = 2 Chris@441: get :show, :id => 15 Chris@441: assert_response :success Chris@441: assert_tag 'h2', :content => /private.diff/ Chris@909: set_tmp_attachments_directory Chris@441: end Chris@441: Chris@0: def test_download_text_file Chris@0: get :download, :id => 4 Chris@0: assert_response :success Chris@0: assert_equal 'application/x-ruby', @response.content_type Chris@909: set_tmp_attachments_directory Chris@909: end Chris@909: Chris@909: def test_download_version_file_with_issue_tracking_disabled Chris@909: Project.find(1).disable_module! :issue_tracking Chris@909: get :download, :id => 9 Chris@909: assert_response :success Chris@0: end Chris@441: Chris@0: def test_download_should_assign_content_type_if_blank Chris@0: Attachment.find(4).update_attribute(:content_type, '') Chris@441: Chris@0: get :download, :id => 4 Chris@0: assert_response :success Chris@0: assert_equal 'text/x-ruby', @response.content_type Chris@909: set_tmp_attachments_directory Chris@0: end Chris@441: Chris@0: def test_download_missing_file Chris@0: get :download, :id => 2 Chris@0: assert_response 404 Chris@909: set_tmp_attachments_directory Chris@0: end Chris@441: Chris@0: def test_anonymous_on_private_private Chris@0: get :download, :id => 7 Chris@0: assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7' Chris@909: set_tmp_attachments_directory Chris@0: end Chris@441: Chris@0: def test_destroy_issue_attachment Chris@909: set_tmp_attachments_directory Chris@0: issue = Issue.find(3) Chris@0: @request.session[:user_id] = 2 Chris@441: Chris@0: assert_difference 'issue.attachments.count', -1 do Chris@909: delete :destroy, :id => 1 Chris@0: end Chris@0: # no referrer chris@37: assert_redirected_to '/projects/ecookbook' Chris@0: assert_nil Attachment.find_by_id(1) Chris@0: j = issue.journals.find(:first, :order => 'created_on DESC') Chris@0: assert_equal 'attachment', j.details.first.property Chris@0: assert_equal '1', j.details.first.prop_key Chris@0: assert_equal 'error281.txt', j.details.first.old_value Chris@0: end Chris@441: Chris@0: def test_destroy_wiki_page_attachment Chris@909: set_tmp_attachments_directory Chris@0: @request.session[:user_id] = 2 Chris@0: assert_difference 'Attachment.count', -1 do Chris@909: delete :destroy, :id => 3 Chris@0: assert_response 302 Chris@0: end Chris@0: end Chris@441: Chris@0: def test_destroy_project_attachment Chris@909: set_tmp_attachments_directory Chris@0: @request.session[:user_id] = 2 Chris@0: assert_difference 'Attachment.count', -1 do Chris@909: delete :destroy, :id => 8 Chris@0: assert_response 302 Chris@0: end Chris@0: end Chris@441: Chris@0: def test_destroy_version_attachment Chris@909: set_tmp_attachments_directory Chris@0: @request.session[:user_id] = 2 Chris@0: assert_difference 'Attachment.count', -1 do Chris@909: delete :destroy, :id => 9 Chris@0: assert_response 302 Chris@0: end Chris@0: end Chris@441: Chris@0: def test_destroy_without_permission Chris@909: set_tmp_attachments_directory Chris@909: assert_no_difference 'Attachment.count' do Chris@909: delete :destroy, :id => 3 Chris@909: end Chris@909: assert_response 302 Chris@0: assert Attachment.find_by_id(3) Chris@0: end Chris@0: end