comparison test/functional/attachments_controller_test.rb @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents 753f1380d6bc
children 5e80956cc792
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
1 # redMine - project management software 1 # encoding: utf-8
2 # Copyright (C) 2006-2008 Jean-Philippe Lang 2 #
3 # Redmine - project management software
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 5 #
4 # This program is free software; you can redistribute it and/or 6 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 7 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 8 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 9 # of the License, or (at your option) any later version.
8 # 10 #
9 # This program is distributed in the hope that it will be useful, 11 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details. 14 # GNU General Public License for more details.
13 # 15 #
14 # You should have received a copy of the GNU General Public License 16 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software 17 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 19
18 require File.dirname(__FILE__) + '/../test_helper' 20 require File.expand_path('../../test_helper', __FILE__)
19 require 'attachments_controller' 21 require 'attachments_controller'
20 22
21 # Re-raise errors caught by the controller. 23 # Re-raise errors caught by the controller.
22 class AttachmentsController; def rescue_action(e) raise e end; end 24 class AttachmentsController; def rescue_action(e) raise e end; end
23 25
24 26
25 class AttachmentsControllerTest < ActionController::TestCase 27 class AttachmentsControllerTest < ActionController::TestCase
26 fixtures :users, :projects, :roles, :members, :member_roles, :enabled_modules, :issues, :trackers, :attachments, 28 fixtures :users, :projects, :roles, :members, :member_roles, :enabled_modules, :issues, :trackers, :attachments,
27 :versions, :wiki_pages, :wikis, :documents 29 :versions, :wiki_pages, :wikis, :documents
28 30
29 def setup 31 def setup
30 @controller = AttachmentsController.new 32 @controller = AttachmentsController.new
31 @request = ActionController::TestRequest.new 33 @request = ActionController::TestRequest.new
32 @response = ActionController::TestResponse.new 34 @response = ActionController::TestResponse.new
33 Attachment.storage_path = "#{RAILS_ROOT}/test/fixtures/files" 35 Attachment.storage_path = "#{RAILS_ROOT}/test/fixtures/files"
34 User.current = nil 36 User.current = nil
35 end 37 end
36 38
37 def test_show_diff 39 def test_show_diff
38 get :show, :id => 5 40 get :show, :id => 14 # 060719210727_changeset_utf8.diff
39 assert_response :success 41 assert_response :success
40 assert_template 'diff' 42 assert_template 'diff'
41 assert_equal 'text/html', @response.content_type 43 assert_equal 'text/html', @response.content_type
44
45 assert_tag 'th',
46 :attributes => {:class => /filename/},
47 :content => /issues_controller.rb\t\(révision 1484\)/
48 assert_tag 'td',
49 :attributes => {:class => /line-code/},
50 :content => /Demande créée avec succès/
42 end 51 end
43 52
53 def test_show_diff_should_strip_non_utf8_content
54 get :show, :id => 5 # 060719210727_changeset_iso8859-1.diff
55 assert_response :success
56 assert_template 'diff'
57 assert_equal 'text/html', @response.content_type
58
59 assert_tag 'th',
60 :attributes => {:class => /filename/},
61 :content => /issues_controller.rb\t\(rvision 1484\)/
62 assert_tag 'td',
63 :attributes => {:class => /line-code/},
64 :content => /Demande cre avec succs/
65 end
66
44 def test_show_text_file 67 def test_show_text_file
45 get :show, :id => 4 68 get :show, :id => 4
46 assert_response :success 69 assert_response :success
47 assert_template 'file' 70 assert_template 'file'
48 assert_equal 'text/html', @response.content_type 71 assert_equal 'text/html', @response.content_type
49 end 72 end
50 73
51 def test_show_text_file_should_send_if_too_big 74 def test_show_text_file_should_send_if_too_big
52 Setting.file_max_size_displayed = 512 75 Setting.file_max_size_displayed = 512
53 Attachment.find(4).update_attribute :filesize, 754.kilobyte 76 Attachment.find(4).update_attribute :filesize, 754.kilobyte
54 77
55 get :show, :id => 4 78 get :show, :id => 4
56 assert_response :success 79 assert_response :success
57 assert_equal 'application/x-ruby', @response.content_type 80 assert_equal 'application/x-ruby', @response.content_type
58 end 81 end
59 82
60 def test_show_other 83 def test_show_other
61 get :show, :id => 6 84 get :show, :id => 6
62 assert_response :success 85 assert_response :success
63 assert_equal 'application/octet-stream', @response.content_type 86 assert_equal 'application/octet-stream', @response.content_type
64 end 87 end
65 88
89 def test_show_file_from_private_issue_without_permission
90 get :show, :id => 15
91 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
92 end
93
94 def test_show_file_from_private_issue_with_permission
95 @request.session[:user_id] = 2
96 get :show, :id => 15
97 assert_response :success
98 assert_tag 'h2', :content => /private.diff/
99 end
100
66 def test_download_text_file 101 def test_download_text_file
67 get :download, :id => 4 102 get :download, :id => 4
68 assert_response :success 103 assert_response :success
69 assert_equal 'application/x-ruby', @response.content_type 104 assert_equal 'application/x-ruby', @response.content_type
70 end 105 end
71 106
72 def test_download_should_assign_content_type_if_blank 107 def test_download_should_assign_content_type_if_blank
73 Attachment.find(4).update_attribute(:content_type, '') 108 Attachment.find(4).update_attribute(:content_type, '')
74 109
75 get :download, :id => 4 110 get :download, :id => 4
76 assert_response :success 111 assert_response :success
77 assert_equal 'text/x-ruby', @response.content_type 112 assert_equal 'text/x-ruby', @response.content_type
78 end 113 end
79 114
80 def test_download_missing_file 115 def test_download_missing_file
81 get :download, :id => 2 116 get :download, :id => 2
82 assert_response 404 117 assert_response 404
83 end 118 end
84 119
85 def test_anonymous_on_private_private 120 def test_anonymous_on_private_private
86 get :download, :id => 7 121 get :download, :id => 7
87 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7' 122 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
88 end 123 end
89 124
90 def test_destroy_issue_attachment 125 def test_destroy_issue_attachment
91 issue = Issue.find(3) 126 issue = Issue.find(3)
92 @request.session[:user_id] = 2 127 @request.session[:user_id] = 2
93 128
94 assert_difference 'issue.attachments.count', -1 do 129 assert_difference 'issue.attachments.count', -1 do
95 post :destroy, :id => 1 130 post :destroy, :id => 1
96 end 131 end
97 # no referrer 132 # no referrer
98 assert_redirected_to '/projects/ecookbook' 133 assert_redirected_to '/projects/ecookbook'
100 j = issue.journals.find(:first, :order => 'created_on DESC') 135 j = issue.journals.find(:first, :order => 'created_on DESC')
101 assert_equal 'attachment', j.details.first.property 136 assert_equal 'attachment', j.details.first.property
102 assert_equal '1', j.details.first.prop_key 137 assert_equal '1', j.details.first.prop_key
103 assert_equal 'error281.txt', j.details.first.old_value 138 assert_equal 'error281.txt', j.details.first.old_value
104 end 139 end
105 140
106 def test_destroy_wiki_page_attachment 141 def test_destroy_wiki_page_attachment
107 @request.session[:user_id] = 2 142 @request.session[:user_id] = 2
108 assert_difference 'Attachment.count', -1 do 143 assert_difference 'Attachment.count', -1 do
109 post :destroy, :id => 3 144 post :destroy, :id => 3
110 assert_response 302 145 assert_response 302
111 end 146 end
112 end 147 end
113 148
114 def test_destroy_project_attachment 149 def test_destroy_project_attachment
115 @request.session[:user_id] = 2 150 @request.session[:user_id] = 2
116 assert_difference 'Attachment.count', -1 do 151 assert_difference 'Attachment.count', -1 do
117 post :destroy, :id => 8 152 post :destroy, :id => 8
118 assert_response 302 153 assert_response 302
119 end 154 end
120 end 155 end
121 156
122 def test_destroy_version_attachment 157 def test_destroy_version_attachment
123 @request.session[:user_id] = 2 158 @request.session[:user_id] = 2
124 assert_difference 'Attachment.count', -1 do 159 assert_difference 'Attachment.count', -1 do
125 post :destroy, :id => 9 160 post :destroy, :id => 9
126 assert_response 302 161 assert_response 302
127 end 162 end
128 end 163 end
129 164
130 def test_destroy_without_permission 165 def test_destroy_without_permission
131 post :destroy, :id => 3 166 post :destroy, :id => 3
132 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3' 167 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3'
133 assert Attachment.find_by_id(3) 168 assert Attachment.find_by_id(3)
134 end 169 end