@@ -1,95 +1,100 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class AttachmentsController < ApplicationController |
|
19 | 19 | before_filter :find_project |
|
20 | 20 | before_filter :file_readable, :read_authorize, :except => :destroy |
|
21 | 21 | before_filter :delete_authorize, :only => :destroy |
|
22 | 22 | |
|
23 | 23 | accept_api_auth :show, :download |
|
24 | 24 | |
|
25 | 25 | def show |
|
26 | 26 | respond_to do |format| |
|
27 | 27 | format.html { |
|
28 | 28 | if @attachment.is_diff? |
|
29 | 29 | @diff = File.new(@attachment.diskfile, "rb").read |
|
30 | 30 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' |
|
31 | 31 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) |
|
32 | # Save diff type as user preference | |
|
33 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] | |
|
34 | User.current.pref[:diff_type] = @diff_type | |
|
35 | User.current.preference.save | |
|
36 | end | |
|
32 | 37 | render :action => 'diff' |
|
33 | 38 | elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte |
|
34 | 39 | @content = File.new(@attachment.diskfile, "rb").read |
|
35 | 40 | render :action => 'file' |
|
36 | 41 | else |
|
37 | 42 | download |
|
38 | 43 | end |
|
39 | 44 | } |
|
40 | 45 | format.api |
|
41 | 46 | end |
|
42 | 47 | end |
|
43 | 48 | |
|
44 | 49 | def download |
|
45 | 50 | if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) |
|
46 | 51 | @attachment.increment_download |
|
47 | 52 | end |
|
48 | 53 | |
|
49 | 54 | # images are sent inline |
|
50 | 55 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
|
51 | 56 | :type => detect_content_type(@attachment), |
|
52 | 57 | :disposition => (@attachment.image? ? 'inline' : 'attachment') |
|
53 | 58 | |
|
54 | 59 | end |
|
55 | 60 | |
|
56 | 61 | verify :method => :delete, :only => :destroy |
|
57 | 62 | def destroy |
|
58 | 63 | # Make sure association callbacks are called |
|
59 | 64 | @attachment.container.attachments.delete(@attachment) |
|
60 | 65 | redirect_to :back |
|
61 | 66 | rescue ::ActionController::RedirectBackError |
|
62 | 67 | redirect_to :controller => 'projects', :action => 'show', :id => @project |
|
63 | 68 | end |
|
64 | 69 | |
|
65 | 70 | private |
|
66 | 71 | def find_project |
|
67 | 72 | @attachment = Attachment.find(params[:id]) |
|
68 | 73 | # Show 404 if the filename in the url is wrong |
|
69 | 74 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename |
|
70 | 75 | @project = @attachment.project |
|
71 | 76 | rescue ActiveRecord::RecordNotFound |
|
72 | 77 | render_404 |
|
73 | 78 | end |
|
74 | 79 | |
|
75 | 80 | # Checks that the file exists and is readable |
|
76 | 81 | def file_readable |
|
77 | 82 | @attachment.readable? ? true : render_404 |
|
78 | 83 | end |
|
79 | 84 | |
|
80 | 85 | def read_authorize |
|
81 | 86 | @attachment.visible? ? true : deny_access |
|
82 | 87 | end |
|
83 | 88 | |
|
84 | 89 | def delete_authorize |
|
85 | 90 | @attachment.deletable? ? true : deny_access |
|
86 | 91 | end |
|
87 | 92 | |
|
88 | 93 | def detect_content_type(attachment) |
|
89 | 94 | content_type = attachment.content_type |
|
90 | 95 | if content_type.blank? |
|
91 | 96 | content_type = Redmine::MimeType.of(attachment.filename) |
|
92 | 97 | end |
|
93 | 98 | content_type.to_s |
|
94 | 99 | end |
|
95 | 100 | end |
@@ -1,284 +1,299 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # Redmine - project management software |
|
4 | 4 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; either version 2 |
|
9 | 9 | # of the License, or (at your option) any later version. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 | 19 | |
|
20 | 20 | require File.expand_path('../../test_helper', __FILE__) |
|
21 | 21 | require 'attachments_controller' |
|
22 | 22 | |
|
23 | 23 | # Re-raise errors caught by the controller. |
|
24 | 24 | class AttachmentsController; def rescue_action(e) raise e end; end |
|
25 | 25 | |
|
26 | 26 | class AttachmentsControllerTest < ActionController::TestCase |
|
27 | 27 | fixtures :users, :projects, :roles, :members, :member_roles, |
|
28 | 28 | :enabled_modules, :issues, :trackers, :attachments, |
|
29 | 29 | :versions, :wiki_pages, :wikis, :documents |
|
30 | 30 | |
|
31 | 31 | def setup |
|
32 | 32 | @controller = AttachmentsController.new |
|
33 | 33 | @request = ActionController::TestRequest.new |
|
34 | 34 | @response = ActionController::TestResponse.new |
|
35 | 35 | User.current = nil |
|
36 | 36 | set_fixtures_attachments_directory |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def teardown |
|
40 | 40 | set_tmp_attachments_directory |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def test_show_diff |
|
44 | 44 | ['inline', 'sbs'].each do |dt| |
|
45 | 45 | # 060719210727_changeset_utf8.diff |
|
46 | 46 | get :show, :id => 14, :type => dt |
|
47 | 47 | assert_response :success |
|
48 | 48 | assert_template 'diff' |
|
49 | 49 | assert_equal 'text/html', @response.content_type |
|
50 | 50 | assert_tag 'th', |
|
51 | 51 | :attributes => {:class => /filename/}, |
|
52 | 52 | :content => /issues_controller.rb\t\(rΓ©vision 1484\)/ |
|
53 | 53 | assert_tag 'td', |
|
54 | 54 | :attributes => {:class => /line-code/}, |
|
55 | 55 | :content => /Demande créée avec succès/ |
|
56 | 56 | end |
|
57 | 57 | set_tmp_attachments_directory |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | def test_show_diff_replcace_cannot_convert_content |
|
61 | 61 | with_settings :repositories_encodings => 'UTF-8' do |
|
62 | 62 | ['inline', 'sbs'].each do |dt| |
|
63 | 63 | # 060719210727_changeset_iso8859-1.diff |
|
64 | 64 | get :show, :id => 5, :type => dt |
|
65 | 65 | assert_response :success |
|
66 | 66 | assert_template 'diff' |
|
67 | 67 | assert_equal 'text/html', @response.content_type |
|
68 | 68 | assert_tag 'th', |
|
69 | 69 | :attributes => {:class => "filename"}, |
|
70 | 70 | :content => /issues_controller.rb\t\(r\?vision 1484\)/ |
|
71 | 71 | assert_tag 'td', |
|
72 | 72 | :attributes => {:class => /line-code/}, |
|
73 | 73 | :content => /Demande cr\?\?e avec succ\?s/ |
|
74 | 74 | end |
|
75 | 75 | end |
|
76 | 76 | set_tmp_attachments_directory |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def test_show_diff_latin_1 |
|
80 | 80 | with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do |
|
81 | 81 | ['inline', 'sbs'].each do |dt| |
|
82 | 82 | # 060719210727_changeset_iso8859-1.diff |
|
83 | 83 | get :show, :id => 5, :type => dt |
|
84 | 84 | assert_response :success |
|
85 | 85 | assert_template 'diff' |
|
86 | 86 | assert_equal 'text/html', @response.content_type |
|
87 | 87 | assert_tag 'th', |
|
88 | 88 | :attributes => {:class => "filename"}, |
|
89 | 89 | :content => /issues_controller.rb\t\(rΓ©vision 1484\)/ |
|
90 | 90 | assert_tag 'td', |
|
91 | 91 | :attributes => {:class => /line-code/}, |
|
92 | 92 | :content => /Demande créée avec succès/ |
|
93 | 93 | end |
|
94 | 94 | end |
|
95 | 95 | set_tmp_attachments_directory |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | def test_save_diff_type | |
|
99 | @request.session[:user_id] = 1 # admin | |
|
100 | user = User.find(1) | |
|
101 | get :show, :id => 5 | |
|
102 | assert_response :success | |
|
103 | assert_template 'diff' | |
|
104 | user.reload | |
|
105 | assert_equal "inline", user.pref[:diff_type] | |
|
106 | get :show, :id => 5, :type => 'sbs' | |
|
107 | assert_response :success | |
|
108 | assert_template 'diff' | |
|
109 | user.reload | |
|
110 | assert_equal "sbs", user.pref[:diff_type] | |
|
111 | end | |
|
112 | ||
|
98 | 113 | def test_show_text_file |
|
99 | 114 | get :show, :id => 4 |
|
100 | 115 | assert_response :success |
|
101 | 116 | assert_template 'file' |
|
102 | 117 | assert_equal 'text/html', @response.content_type |
|
103 | 118 | set_tmp_attachments_directory |
|
104 | 119 | end |
|
105 | 120 | |
|
106 | 121 | def test_show_text_file_utf_8 |
|
107 | 122 | set_tmp_attachments_directory |
|
108 | 123 | a = Attachment.new(:container => Issue.find(1), |
|
109 | 124 | :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"), |
|
110 | 125 | :author => User.find(1)) |
|
111 | 126 | assert a.save |
|
112 | 127 | assert_equal 'japanese-utf-8.txt', a.filename |
|
113 | 128 | |
|
114 | 129 | str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" |
|
115 | 130 | str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding) |
|
116 | 131 | |
|
117 | 132 | get :show, :id => a.id |
|
118 | 133 | assert_response :success |
|
119 | 134 | assert_template 'file' |
|
120 | 135 | assert_equal 'text/html', @response.content_type |
|
121 | 136 | assert_tag :tag => 'th', |
|
122 | 137 | :content => '1', |
|
123 | 138 | :attributes => { :class => 'line-num' }, |
|
124 | 139 | :sibling => { :tag => 'td', :content => /#{str_japanese}/ } |
|
125 | 140 | end |
|
126 | 141 | |
|
127 | 142 | def test_show_text_file_replcace_cannot_convert_content |
|
128 | 143 | set_tmp_attachments_directory |
|
129 | 144 | with_settings :repositories_encodings => 'UTF-8' do |
|
130 | 145 | a = Attachment.new(:container => Issue.find(1), |
|
131 | 146 | :file => uploaded_test_file("iso8859-1.txt", "text/plain"), |
|
132 | 147 | :author => User.find(1)) |
|
133 | 148 | assert a.save |
|
134 | 149 | assert_equal 'iso8859-1.txt', a.filename |
|
135 | 150 | |
|
136 | 151 | get :show, :id => a.id |
|
137 | 152 | assert_response :success |
|
138 | 153 | assert_template 'file' |
|
139 | 154 | assert_equal 'text/html', @response.content_type |
|
140 | 155 | assert_tag :tag => 'th', |
|
141 | 156 | :content => '7', |
|
142 | 157 | :attributes => { :class => 'line-num' }, |
|
143 | 158 | :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ } |
|
144 | 159 | end |
|
145 | 160 | end |
|
146 | 161 | |
|
147 | 162 | def test_show_text_file_latin_1 |
|
148 | 163 | set_tmp_attachments_directory |
|
149 | 164 | with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do |
|
150 | 165 | a = Attachment.new(:container => Issue.find(1), |
|
151 | 166 | :file => uploaded_test_file("iso8859-1.txt", "text/plain"), |
|
152 | 167 | :author => User.find(1)) |
|
153 | 168 | assert a.save |
|
154 | 169 | assert_equal 'iso8859-1.txt', a.filename |
|
155 | 170 | |
|
156 | 171 | get :show, :id => a.id |
|
157 | 172 | assert_response :success |
|
158 | 173 | assert_template 'file' |
|
159 | 174 | assert_equal 'text/html', @response.content_type |
|
160 | 175 | assert_tag :tag => 'th', |
|
161 | 176 | :content => '7', |
|
162 | 177 | :attributes => { :class => 'line-num' }, |
|
163 | 178 | :sibling => { :tag => 'td', :content => /Demande créée avec succès/ } |
|
164 | 179 | end |
|
165 | 180 | end |
|
166 | 181 | |
|
167 | 182 | def test_show_text_file_should_send_if_too_big |
|
168 | 183 | Setting.file_max_size_displayed = 512 |
|
169 | 184 | Attachment.find(4).update_attribute :filesize, 754.kilobyte |
|
170 | 185 | |
|
171 | 186 | get :show, :id => 4 |
|
172 | 187 | assert_response :success |
|
173 | 188 | assert_equal 'application/x-ruby', @response.content_type |
|
174 | 189 | set_tmp_attachments_directory |
|
175 | 190 | end |
|
176 | 191 | |
|
177 | 192 | def test_show_other |
|
178 | 193 | get :show, :id => 6 |
|
179 | 194 | assert_response :success |
|
180 | 195 | assert_equal 'application/octet-stream', @response.content_type |
|
181 | 196 | set_tmp_attachments_directory |
|
182 | 197 | end |
|
183 | 198 | |
|
184 | 199 | def test_show_file_from_private_issue_without_permission |
|
185 | 200 | get :show, :id => 15 |
|
186 | 201 | assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15' |
|
187 | 202 | set_tmp_attachments_directory |
|
188 | 203 | end |
|
189 | 204 | |
|
190 | 205 | def test_show_file_from_private_issue_with_permission |
|
191 | 206 | @request.session[:user_id] = 2 |
|
192 | 207 | get :show, :id => 15 |
|
193 | 208 | assert_response :success |
|
194 | 209 | assert_tag 'h2', :content => /private.diff/ |
|
195 | 210 | set_tmp_attachments_directory |
|
196 | 211 | end |
|
197 | 212 | |
|
198 | 213 | def test_download_text_file |
|
199 | 214 | get :download, :id => 4 |
|
200 | 215 | assert_response :success |
|
201 | 216 | assert_equal 'application/x-ruby', @response.content_type |
|
202 | 217 | set_tmp_attachments_directory |
|
203 | 218 | end |
|
204 | 219 | |
|
205 | 220 | def test_download_version_file_with_issue_tracking_disabled |
|
206 | 221 | Project.find(1).disable_module! :issue_tracking |
|
207 | 222 | get :download, :id => 9 |
|
208 | 223 | assert_response :success |
|
209 | 224 | end |
|
210 | 225 | |
|
211 | 226 | def test_download_should_assign_content_type_if_blank |
|
212 | 227 | Attachment.find(4).update_attribute(:content_type, '') |
|
213 | 228 | |
|
214 | 229 | get :download, :id => 4 |
|
215 | 230 | assert_response :success |
|
216 | 231 | assert_equal 'text/x-ruby', @response.content_type |
|
217 | 232 | set_tmp_attachments_directory |
|
218 | 233 | end |
|
219 | 234 | |
|
220 | 235 | def test_download_missing_file |
|
221 | 236 | get :download, :id => 2 |
|
222 | 237 | assert_response 404 |
|
223 | 238 | set_tmp_attachments_directory |
|
224 | 239 | end |
|
225 | 240 | |
|
226 | 241 | def test_anonymous_on_private_private |
|
227 | 242 | get :download, :id => 7 |
|
228 | 243 | assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7' |
|
229 | 244 | set_tmp_attachments_directory |
|
230 | 245 | end |
|
231 | 246 | |
|
232 | 247 | def test_destroy_issue_attachment |
|
233 | 248 | set_tmp_attachments_directory |
|
234 | 249 | issue = Issue.find(3) |
|
235 | 250 | @request.session[:user_id] = 2 |
|
236 | 251 | |
|
237 | 252 | assert_difference 'issue.attachments.count', -1 do |
|
238 | 253 | delete :destroy, :id => 1 |
|
239 | 254 | end |
|
240 | 255 | # no referrer |
|
241 | 256 | assert_redirected_to '/projects/ecookbook' |
|
242 | 257 | assert_nil Attachment.find_by_id(1) |
|
243 | 258 | j = issue.journals.find(:first, :order => 'created_on DESC') |
|
244 | 259 | assert_equal 'attachment', j.details.first.property |
|
245 | 260 | assert_equal '1', j.details.first.prop_key |
|
246 | 261 | assert_equal 'error281.txt', j.details.first.old_value |
|
247 | 262 | end |
|
248 | 263 | |
|
249 | 264 | def test_destroy_wiki_page_attachment |
|
250 | 265 | set_tmp_attachments_directory |
|
251 | 266 | @request.session[:user_id] = 2 |
|
252 | 267 | assert_difference 'Attachment.count', -1 do |
|
253 | 268 | delete :destroy, :id => 3 |
|
254 | 269 | assert_response 302 |
|
255 | 270 | end |
|
256 | 271 | end |
|
257 | 272 | |
|
258 | 273 | def test_destroy_project_attachment |
|
259 | 274 | set_tmp_attachments_directory |
|
260 | 275 | @request.session[:user_id] = 2 |
|
261 | 276 | assert_difference 'Attachment.count', -1 do |
|
262 | 277 | delete :destroy, :id => 8 |
|
263 | 278 | assert_response 302 |
|
264 | 279 | end |
|
265 | 280 | end |
|
266 | 281 | |
|
267 | 282 | def test_destroy_version_attachment |
|
268 | 283 | set_tmp_attachments_directory |
|
269 | 284 | @request.session[:user_id] = 2 |
|
270 | 285 | assert_difference 'Attachment.count', -1 do |
|
271 | 286 | delete :destroy, :id => 9 |
|
272 | 287 | assert_response 302 |
|
273 | 288 | end |
|
274 | 289 | end |
|
275 | 290 | |
|
276 | 291 | def test_destroy_without_permission |
|
277 | 292 | set_tmp_attachments_directory |
|
278 | 293 | assert_no_difference 'Attachment.count' do |
|
279 | 294 | delete :destroy, :id => 3 |
|
280 | 295 | end |
|
281 | 296 | assert_response 302 |
|
282 | 297 | assert Attachment.find_by_id(3) |
|
283 | 298 | end |
|
284 | 299 | end |
General Comments 0
You need to be logged in to leave comments.
Login now