Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2008 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@0: # 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@0: # 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@0: require File.dirname(__FILE__) + '/../test_helper' 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: Chris@0: class AttachmentsControllerTest < ActionController::TestCase Chris@0: fixtures :users, :projects, :roles, :members, :member_roles, :enabled_modules, :issues, :trackers, :attachments, Chris@0: :versions, :wiki_pages, :wikis, :documents Chris@0: Chris@0: def setup Chris@0: @controller = AttachmentsController.new Chris@0: @request = ActionController::TestRequest.new Chris@0: @response = ActionController::TestResponse.new Chris@0: Attachment.storage_path = "#{RAILS_ROOT}/test/fixtures/files" Chris@0: User.current = nil Chris@0: end Chris@0: Chris@0: def test_show_diff Chris@0: get :show, :id => 5 Chris@0: assert_response :success Chris@0: assert_template 'diff' Chris@0: assert_equal 'text/html', @response.content_type Chris@0: end Chris@0: 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@0: end Chris@0: 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@0: Chris@0: get :show, :id => 4 Chris@0: assert_response :success Chris@0: assert_equal 'application/x-ruby', @response.content_type Chris@0: end Chris@0: 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@0: end Chris@0: 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@0: end Chris@0: Chris@0: def test_download_should_assign_content_type_if_blank Chris@0: Attachment.find(4).update_attribute(:content_type, '') Chris@0: Chris@0: get :download, :id => 4 Chris@0: assert_response :success Chris@0: assert_equal 'text/x-ruby', @response.content_type Chris@0: end Chris@0: Chris@0: def test_download_missing_file Chris@0: get :download, :id => 2 Chris@0: assert_response 404 Chris@0: end Chris@0: 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@0: end Chris@0: Chris@0: def test_destroy_issue_attachment Chris@0: issue = Issue.find(3) Chris@0: @request.session[:user_id] = 2 Chris@0: Chris@0: assert_difference 'issue.attachments.count', -1 do Chris@0: post :destroy, :id => 1 Chris@0: end Chris@0: # no referrer Chris@0: 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@0: Chris@0: def test_destroy_wiki_page_attachment Chris@0: @request.session[:user_id] = 2 Chris@0: assert_difference 'Attachment.count', -1 do Chris@0: post :destroy, :id => 3 Chris@0: assert_response 302 Chris@0: end Chris@0: end Chris@0: Chris@0: def test_destroy_project_attachment Chris@0: @request.session[:user_id] = 2 Chris@0: assert_difference 'Attachment.count', -1 do Chris@0: post :destroy, :id => 8 Chris@0: assert_response 302 Chris@0: end Chris@0: end Chris@0: Chris@0: def test_destroy_version_attachment Chris@0: @request.session[:user_id] = 2 Chris@0: assert_difference 'Attachment.count', -1 do Chris@0: post :destroy, :id => 9 Chris@0: assert_response 302 Chris@0: end Chris@0: end Chris@0: Chris@0: def test_destroy_without_permission Chris@0: post :destroy, :id => 3 Chris@0: assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3' Chris@0: assert Attachment.find_by_id(3) Chris@0: end Chris@0: end