Mercurial > hg > soundsoftware-site
comparison test/functional/attachments_controller_test.rb @ 909:cbb26bc654de redmine-1.3
Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author | Chris Cannam |
---|---|
date | Fri, 24 Feb 2012 19:09:32 +0000 |
parents | cbce1fd3b1b7 |
children | 5e80956cc792 433d4f72a19b |
comparison
equal
deleted
inserted
replaced
908:c6c2cbd0afee | 909:cbb26bc654de |
---|---|
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 | |
27 class AttachmentsControllerTest < ActionController::TestCase | 26 class AttachmentsControllerTest < ActionController::TestCase |
28 fixtures :users, :projects, :roles, :members, :member_roles, :enabled_modules, :issues, :trackers, :attachments, | 27 fixtures :users, :projects, :roles, :members, :member_roles, |
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 Attachment.storage_path = "#{RAILS_ROOT}/test/fixtures/files" | 35 Attachment.storage_path = "#{Rails.root}/test/fixtures/files" |
36 User.current = nil | 36 User.current = nil |
37 end | 37 end |
38 | 38 |
39 def test_show_diff | 39 def test_show_diff |
40 get :show, :id => 14 # 060719210727_changeset_utf8.diff | 40 ['inline', 'sbs'].each do |dt| |
41 # 060719210727_changeset_utf8.diff | |
42 get :show, :id => 14, :type => dt | |
43 assert_response :success | |
44 assert_template 'diff' | |
45 assert_equal 'text/html', @response.content_type | |
46 assert_tag 'th', | |
47 :attributes => {:class => /filename/}, | |
48 :content => /issues_controller.rb\t\(révision 1484\)/ | |
49 assert_tag 'td', | |
50 :attributes => {:class => /line-code/}, | |
51 :content => /Demande créée avec succès/ | |
52 end | |
53 set_tmp_attachments_directory | |
54 end | |
55 | |
56 def test_show_diff_replcace_cannot_convert_content | |
57 with_settings :repositories_encodings => 'UTF-8' do | |
58 ['inline', 'sbs'].each do |dt| | |
59 # 060719210727_changeset_iso8859-1.diff | |
60 get :show, :id => 5, :type => dt | |
61 assert_response :success | |
62 assert_template 'diff' | |
63 assert_equal 'text/html', @response.content_type | |
64 assert_tag 'th', | |
65 :attributes => {:class => "filename"}, | |
66 :content => /issues_controller.rb\t\(r\?vision 1484\)/ | |
67 assert_tag 'td', | |
68 :attributes => {:class => /line-code/}, | |
69 :content => /Demande cr\?\?e avec succ\?s/ | |
70 end | |
71 end | |
72 set_tmp_attachments_directory | |
73 end | |
74 | |
75 def test_show_diff_latin_1 | |
76 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do | |
77 ['inline', 'sbs'].each do |dt| | |
78 # 060719210727_changeset_iso8859-1.diff | |
79 get :show, :id => 5, :type => dt | |
80 assert_response :success | |
81 assert_template 'diff' | |
82 assert_equal 'text/html', @response.content_type | |
83 assert_tag 'th', | |
84 :attributes => {:class => "filename"}, | |
85 :content => /issues_controller.rb\t\(révision 1484\)/ | |
86 assert_tag 'td', | |
87 :attributes => {:class => /line-code/}, | |
88 :content => /Demande créée avec succès/ | |
89 end | |
90 end | |
91 set_tmp_attachments_directory | |
92 end | |
93 | |
94 def test_save_diff_type | |
95 @request.session[:user_id] = 1 # admin | |
96 user = User.find(1) | |
97 get :show, :id => 5 | |
41 assert_response :success | 98 assert_response :success |
42 assert_template 'diff' | 99 assert_template 'diff' |
43 assert_equal 'text/html', @response.content_type | 100 user.reload |
44 | 101 assert_equal "inline", user.pref[:diff_type] |
45 assert_tag 'th', | 102 get :show, :id => 5, :type => 'sbs' |
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/ | |
51 end | |
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 | 103 assert_response :success |
56 assert_template 'diff' | 104 assert_template 'diff' |
57 assert_equal 'text/html', @response.content_type | 105 user.reload |
58 | 106 assert_equal "sbs", user.pref[:diff_type] |
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 | 107 end |
66 | 108 |
67 def test_show_text_file | 109 def test_show_text_file |
68 get :show, :id => 4 | 110 get :show, :id => 4 |
69 assert_response :success | 111 assert_response :success |
70 assert_template 'file' | 112 assert_template 'file' |
71 assert_equal 'text/html', @response.content_type | 113 assert_equal 'text/html', @response.content_type |
114 set_tmp_attachments_directory | |
115 end | |
116 | |
117 def test_show_text_file_utf_8 | |
118 set_tmp_attachments_directory | |
119 a = Attachment.new(:container => Issue.find(1), | |
120 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"), | |
121 :author => User.find(1)) | |
122 assert a.save | |
123 assert_equal 'japanese-utf-8.txt', a.filename | |
124 | |
125 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" | |
126 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding) | |
127 | |
128 get :show, :id => a.id | |
129 assert_response :success | |
130 assert_template 'file' | |
131 assert_equal 'text/html', @response.content_type | |
132 assert_tag :tag => 'th', | |
133 :content => '1', | |
134 :attributes => { :class => 'line-num' }, | |
135 :sibling => { :tag => 'td', :content => /#{str_japanese}/ } | |
136 end | |
137 | |
138 def test_show_text_file_replcace_cannot_convert_content | |
139 set_tmp_attachments_directory | |
140 with_settings :repositories_encodings => 'UTF-8' do | |
141 a = Attachment.new(:container => Issue.find(1), | |
142 :file => uploaded_test_file("iso8859-1.txt", "text/plain"), | |
143 :author => User.find(1)) | |
144 assert a.save | |
145 assert_equal 'iso8859-1.txt', a.filename | |
146 | |
147 get :show, :id => a.id | |
148 assert_response :success | |
149 assert_template 'file' | |
150 assert_equal 'text/html', @response.content_type | |
151 assert_tag :tag => 'th', | |
152 :content => '7', | |
153 :attributes => { :class => 'line-num' }, | |
154 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ } | |
155 end | |
156 end | |
157 | |
158 def test_show_text_file_latin_1 | |
159 set_tmp_attachments_directory | |
160 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do | |
161 a = Attachment.new(:container => Issue.find(1), | |
162 :file => uploaded_test_file("iso8859-1.txt", "text/plain"), | |
163 :author => User.find(1)) | |
164 assert a.save | |
165 assert_equal 'iso8859-1.txt', a.filename | |
166 | |
167 get :show, :id => a.id | |
168 assert_response :success | |
169 assert_template 'file' | |
170 assert_equal 'text/html', @response.content_type | |
171 assert_tag :tag => 'th', | |
172 :content => '7', | |
173 :attributes => { :class => 'line-num' }, | |
174 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ } | |
175 end | |
72 end | 176 end |
73 | 177 |
74 def test_show_text_file_should_send_if_too_big | 178 def test_show_text_file_should_send_if_too_big |
75 Setting.file_max_size_displayed = 512 | 179 Setting.file_max_size_displayed = 512 |
76 Attachment.find(4).update_attribute :filesize, 754.kilobyte | 180 Attachment.find(4).update_attribute :filesize, 754.kilobyte |
77 | 181 |
78 get :show, :id => 4 | 182 get :show, :id => 4 |
79 assert_response :success | 183 assert_response :success |
80 assert_equal 'application/x-ruby', @response.content_type | 184 assert_equal 'application/x-ruby', @response.content_type |
185 set_tmp_attachments_directory | |
81 end | 186 end |
82 | 187 |
83 def test_show_other | 188 def test_show_other |
84 get :show, :id => 6 | 189 get :show, :id => 6 |
85 assert_response :success | 190 assert_response :success |
86 assert_equal 'application/octet-stream', @response.content_type | 191 assert_equal 'application/octet-stream', @response.content_type |
192 set_tmp_attachments_directory | |
87 end | 193 end |
88 | 194 |
89 def test_show_file_from_private_issue_without_permission | 195 def test_show_file_from_private_issue_without_permission |
90 get :show, :id => 15 | 196 get :show, :id => 15 |
91 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15' | 197 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15' |
198 set_tmp_attachments_directory | |
92 end | 199 end |
93 | 200 |
94 def test_show_file_from_private_issue_with_permission | 201 def test_show_file_from_private_issue_with_permission |
95 @request.session[:user_id] = 2 | 202 @request.session[:user_id] = 2 |
96 get :show, :id => 15 | 203 get :show, :id => 15 |
97 assert_response :success | 204 assert_response :success |
98 assert_tag 'h2', :content => /private.diff/ | 205 assert_tag 'h2', :content => /private.diff/ |
206 set_tmp_attachments_directory | |
99 end | 207 end |
100 | 208 |
101 def test_download_text_file | 209 def test_download_text_file |
102 get :download, :id => 4 | 210 get :download, :id => 4 |
103 assert_response :success | 211 assert_response :success |
104 assert_equal 'application/x-ruby', @response.content_type | 212 assert_equal 'application/x-ruby', @response.content_type |
213 set_tmp_attachments_directory | |
214 end | |
215 | |
216 def test_download_version_file_with_issue_tracking_disabled | |
217 Project.find(1).disable_module! :issue_tracking | |
218 get :download, :id => 9 | |
219 assert_response :success | |
105 end | 220 end |
106 | 221 |
107 def test_download_should_assign_content_type_if_blank | 222 def test_download_should_assign_content_type_if_blank |
108 Attachment.find(4).update_attribute(:content_type, '') | 223 Attachment.find(4).update_attribute(:content_type, '') |
109 | 224 |
110 get :download, :id => 4 | 225 get :download, :id => 4 |
111 assert_response :success | 226 assert_response :success |
112 assert_equal 'text/x-ruby', @response.content_type | 227 assert_equal 'text/x-ruby', @response.content_type |
228 set_tmp_attachments_directory | |
113 end | 229 end |
114 | 230 |
115 def test_download_missing_file | 231 def test_download_missing_file |
116 get :download, :id => 2 | 232 get :download, :id => 2 |
117 assert_response 404 | 233 assert_response 404 |
234 set_tmp_attachments_directory | |
118 end | 235 end |
119 | 236 |
120 def test_anonymous_on_private_private | 237 def test_anonymous_on_private_private |
121 get :download, :id => 7 | 238 get :download, :id => 7 |
122 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7' | 239 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7' |
240 set_tmp_attachments_directory | |
123 end | 241 end |
124 | 242 |
125 def test_destroy_issue_attachment | 243 def test_destroy_issue_attachment |
244 set_tmp_attachments_directory | |
126 issue = Issue.find(3) | 245 issue = Issue.find(3) |
127 @request.session[:user_id] = 2 | 246 @request.session[:user_id] = 2 |
128 | 247 |
129 assert_difference 'issue.attachments.count', -1 do | 248 assert_difference 'issue.attachments.count', -1 do |
130 post :destroy, :id => 1 | 249 delete :destroy, :id => 1 |
131 end | 250 end |
132 # no referrer | 251 # no referrer |
133 assert_redirected_to '/projects/ecookbook' | 252 assert_redirected_to '/projects/ecookbook' |
134 assert_nil Attachment.find_by_id(1) | 253 assert_nil Attachment.find_by_id(1) |
135 j = issue.journals.find(:first, :order => 'created_on DESC') | 254 j = issue.journals.find(:first, :order => 'created_on DESC') |
137 assert_equal '1', j.details.first.prop_key | 256 assert_equal '1', j.details.first.prop_key |
138 assert_equal 'error281.txt', j.details.first.old_value | 257 assert_equal 'error281.txt', j.details.first.old_value |
139 end | 258 end |
140 | 259 |
141 def test_destroy_wiki_page_attachment | 260 def test_destroy_wiki_page_attachment |
261 set_tmp_attachments_directory | |
142 @request.session[:user_id] = 2 | 262 @request.session[:user_id] = 2 |
143 assert_difference 'Attachment.count', -1 do | 263 assert_difference 'Attachment.count', -1 do |
144 post :destroy, :id => 3 | 264 delete :destroy, :id => 3 |
145 assert_response 302 | 265 assert_response 302 |
146 end | 266 end |
147 end | 267 end |
148 | 268 |
149 def test_destroy_project_attachment | 269 def test_destroy_project_attachment |
270 set_tmp_attachments_directory | |
150 @request.session[:user_id] = 2 | 271 @request.session[:user_id] = 2 |
151 assert_difference 'Attachment.count', -1 do | 272 assert_difference 'Attachment.count', -1 do |
152 post :destroy, :id => 8 | 273 delete :destroy, :id => 8 |
153 assert_response 302 | 274 assert_response 302 |
154 end | 275 end |
155 end | 276 end |
156 | 277 |
157 def test_destroy_version_attachment | 278 def test_destroy_version_attachment |
279 set_tmp_attachments_directory | |
158 @request.session[:user_id] = 2 | 280 @request.session[:user_id] = 2 |
159 assert_difference 'Attachment.count', -1 do | 281 assert_difference 'Attachment.count', -1 do |
160 post :destroy, :id => 9 | 282 delete :destroy, :id => 9 |
161 assert_response 302 | 283 assert_response 302 |
162 end | 284 end |
163 end | 285 end |
164 | 286 |
165 def test_destroy_without_permission | 287 def test_destroy_without_permission |
166 post :destroy, :id => 3 | 288 set_tmp_attachments_directory |
167 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3' | 289 assert_no_difference 'Attachment.count' do |
290 delete :destroy, :id => 3 | |
291 end | |
292 assert_response 302 | |
168 assert Attachment.find_by_id(3) | 293 assert Attachment.find_by_id(3) |
169 end | 294 end |
170 end | 295 end |