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