##// 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 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 98 def test_save_diff_type
99 99 @request.session[:user_id] = 1 # admin
100 100 user = User.find(1)
101 101 get :show, :id => 5
102 102 assert_response :success
103 103 assert_template 'diff'
104 104 user.reload
105 105 assert_equal "inline", user.pref[:diff_type]
106 106 get :show, :id => 5, :type => 'sbs'
107 107 assert_response :success
108 108 assert_template 'diff'
109 109 user.reload
110 110 assert_equal "sbs", user.pref[:diff_type]
111 111 end
112 112
113 113 def test_show_text_file
114 114 get :show, :id => 4
115 115 assert_response :success
116 116 assert_template 'file'
117 117 assert_equal 'text/html', @response.content_type
118 118 set_tmp_attachments_directory
119 119 end
120 120
121 121 def test_show_text_file_utf_8
122 122 set_tmp_attachments_directory
123 123 a = Attachment.new(:container => Issue.find(1),
124 124 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
125 125 :author => User.find(1))
126 126 assert a.save
127 127 assert_equal 'japanese-utf-8.txt', a.filename
128 128
129 129 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
130 130 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
131 131
132 132 get :show, :id => a.id
133 133 assert_response :success
134 134 assert_template 'file'
135 135 assert_equal 'text/html', @response.content_type
136 136 assert_tag :tag => 'th',
137 137 :content => '1',
138 138 :attributes => { :class => 'line-num' },
139 139 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
140 140 end
141 141
142 142 def test_show_text_file_replcace_cannot_convert_content
143 143 set_tmp_attachments_directory
144 144 with_settings :repositories_encodings => 'UTF-8' do
145 145 a = Attachment.new(:container => Issue.find(1),
146 146 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
147 147 :author => User.find(1))
148 148 assert a.save
149 149 assert_equal 'iso8859-1.txt', a.filename
150 150
151 151 get :show, :id => a.id
152 152 assert_response :success
153 153 assert_template 'file'
154 154 assert_equal 'text/html', @response.content_type
155 155 assert_tag :tag => 'th',
156 156 :content => '7',
157 157 :attributes => { :class => 'line-num' },
158 158 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
159 159 end
160 160 end
161 161
162 162 def test_show_text_file_latin_1
163 163 set_tmp_attachments_directory
164 164 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
165 165 a = Attachment.new(:container => Issue.find(1),
166 166 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
167 167 :author => User.find(1))
168 168 assert a.save
169 169 assert_equal 'iso8859-1.txt', a.filename
170 170
171 171 get :show, :id => a.id
172 172 assert_response :success
173 173 assert_template 'file'
174 174 assert_equal 'text/html', @response.content_type
175 175 assert_tag :tag => 'th',
176 176 :content => '7',
177 177 :attributes => { :class => 'line-num' },
178 178 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
179 179 end
180 180 end
181 181
182 182 def test_show_text_file_should_send_if_too_big
183 183 Setting.file_max_size_displayed = 512
184 184 Attachment.find(4).update_attribute :filesize, 754.kilobyte
185 185
186 186 get :show, :id => 4
187 187 assert_response :success
188 188 assert_equal 'application/x-ruby', @response.content_type
189 189 set_tmp_attachments_directory
190 190 end
191 191
192 192 def test_show_other
193 193 get :show, :id => 6
194 194 assert_response :success
195 195 assert_equal 'application/octet-stream', @response.content_type
196 196 set_tmp_attachments_directory
197 197 end
198 198
199 199 def test_show_file_from_private_issue_without_permission
200 200 get :show, :id => 15
201 201 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
202 202 set_tmp_attachments_directory
203 203 end
204 204
205 205 def test_show_file_from_private_issue_with_permission
206 206 @request.session[:user_id] = 2
207 207 get :show, :id => 15
208 208 assert_response :success
209 209 assert_tag 'h2', :content => /private.diff/
210 210 set_tmp_attachments_directory
211 211 end
212 212
213 213 def test_show_file_without_container_should_be_denied
214 set_tmp_attachments_directory
214 215 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
215 216
216 217 @request.session[:user_id] = 2
217 218 get :show, :id => attachment.id
218 219 assert_response 403
219 220 end
220 221
221 222 def test_download_text_file
222 223 get :download, :id => 4
223 224 assert_response :success
224 225 assert_equal 'application/x-ruby', @response.content_type
225 226 set_tmp_attachments_directory
226 227 end
227 228
228 229 def test_download_version_file_with_issue_tracking_disabled
229 230 Project.find(1).disable_module! :issue_tracking
230 231 get :download, :id => 9
231 232 assert_response :success
232 233 end
233 234
234 235 def test_download_should_assign_content_type_if_blank
235 236 Attachment.find(4).update_attribute(:content_type, '')
236 237
237 238 get :download, :id => 4
238 239 assert_response :success
239 240 assert_equal 'text/x-ruby', @response.content_type
240 241 set_tmp_attachments_directory
241 242 end
242 243
243 244 def test_download_missing_file
244 245 get :download, :id => 2
245 246 assert_response 404
246 247 set_tmp_attachments_directory
247 248 end
248 249
249 250 def test_anonymous_on_private_private
250 251 get :download, :id => 7
251 252 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
252 253 set_tmp_attachments_directory
253 254 end
254 255
255 256 def test_destroy_issue_attachment
256 257 set_tmp_attachments_directory
257 258 issue = Issue.find(3)
258 259 @request.session[:user_id] = 2
259 260
260 261 assert_difference 'issue.attachments.count', -1 do
261 262 delete :destroy, :id => 1
262 263 end
263 264 # no referrer
264 265 assert_redirected_to '/projects/ecookbook'
265 266 assert_nil Attachment.find_by_id(1)
266 267 j = issue.journals.find(:first, :order => 'created_on DESC')
267 268 assert_equal 'attachment', j.details.first.property
268 269 assert_equal '1', j.details.first.prop_key
269 270 assert_equal 'error281.txt', j.details.first.old_value
270 271 end
271 272
272 273 def test_destroy_wiki_page_attachment
273 274 set_tmp_attachments_directory
274 275 @request.session[:user_id] = 2
275 276 assert_difference 'Attachment.count', -1 do
276 277 delete :destroy, :id => 3
277 278 assert_response 302
278 279 end
279 280 end
280 281
281 282 def test_destroy_project_attachment
282 283 set_tmp_attachments_directory
283 284 @request.session[:user_id] = 2
284 285 assert_difference 'Attachment.count', -1 do
285 286 delete :destroy, :id => 8
286 287 assert_response 302
287 288 end
288 289 end
289 290
290 291 def test_destroy_version_attachment
291 292 set_tmp_attachments_directory
292 293 @request.session[:user_id] = 2
293 294 assert_difference 'Attachment.count', -1 do
294 295 delete :destroy, :id => 9
295 296 assert_response 302
296 297 end
297 298 end
298 299
299 300 def test_destroy_without_permission
300 301 set_tmp_attachments_directory
301 302 assert_no_difference 'Attachment.count' do
302 303 delete :destroy, :id => 3
303 304 end
304 305 assert_response 302
305 306 assert Attachment.find_by_id(3)
306 307 end
307 308 end
General Comments 0
You need to be logged in to leave comments. Login now