##// END OF EJS Templates
Prevent fixture files from being deleted....
Jean-Philippe Lang -
r7863:9ed5d1dd027c
parent child
Show More
@@ -1,274 +1,274
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 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 ['inline', 'sbs'].each do |dt|
40 ['inline', 'sbs'].each do |dt|
41 # 060719210727_changeset_utf8.diff
41 # 060719210727_changeset_utf8.diff
42 get :show, :id => 14, :type => dt
42 get :show, :id => 14, :type => dt
43 assert_response :success
43 assert_response :success
44 assert_template 'diff'
44 assert_template 'diff'
45 assert_equal 'text/html', @response.content_type
45 assert_equal 'text/html', @response.content_type
46 assert_tag 'th',
46 assert_tag 'th',
47 :attributes => {:class => /filename/},
47 :attributes => {:class => /filename/},
48 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
48 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
49 assert_tag 'td',
49 assert_tag 'td',
50 :attributes => {:class => /line-code/},
50 :attributes => {:class => /line-code/},
51 :content => /Demande créée avec succès/
51 :content => /Demande créée avec succès/
52 end
52 end
53 set_tmp_attachments_directory
53 set_tmp_attachments_directory
54 end
54 end
55
55
56 def test_show_diff_replcace_cannot_convert_content
56 def test_show_diff_replcace_cannot_convert_content
57 with_settings :repositories_encodings => 'UTF-8' do
57 with_settings :repositories_encodings => 'UTF-8' do
58 ['inline', 'sbs'].each do |dt|
58 ['inline', 'sbs'].each do |dt|
59 # 060719210727_changeset_iso8859-1.diff
59 # 060719210727_changeset_iso8859-1.diff
60 get :show, :id => 5, :type => dt
60 get :show, :id => 5, :type => dt
61 assert_response :success
61 assert_response :success
62 assert_template 'diff'
62 assert_template 'diff'
63 assert_equal 'text/html', @response.content_type
63 assert_equal 'text/html', @response.content_type
64 assert_tag 'th',
64 assert_tag 'th',
65 :attributes => {:class => "filename"},
65 :attributes => {:class => "filename"},
66 :content => /issues_controller.rb\t\(r\?vision 1484\)/
66 :content => /issues_controller.rb\t\(r\?vision 1484\)/
67 assert_tag 'td',
67 assert_tag 'td',
68 :attributes => {:class => /line-code/},
68 :attributes => {:class => /line-code/},
69 :content => /Demande cr\?\?e avec succ\?s/
69 :content => /Demande cr\?\?e avec succ\?s/
70 end
70 end
71 end
71 end
72 set_tmp_attachments_directory
72 set_tmp_attachments_directory
73 end
73 end
74
74
75 def test_show_diff_latin_1
75 def test_show_diff_latin_1
76 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
76 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
77 ['inline', 'sbs'].each do |dt|
77 ['inline', 'sbs'].each do |dt|
78 # 060719210727_changeset_iso8859-1.diff
78 # 060719210727_changeset_iso8859-1.diff
79 get :show, :id => 5, :type => dt
79 get :show, :id => 5, :type => dt
80 assert_response :success
80 assert_response :success
81 assert_template 'diff'
81 assert_template 'diff'
82 assert_equal 'text/html', @response.content_type
82 assert_equal 'text/html', @response.content_type
83 assert_tag 'th',
83 assert_tag 'th',
84 :attributes => {:class => "filename"},
84 :attributes => {:class => "filename"},
85 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
85 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
86 assert_tag 'td',
86 assert_tag 'td',
87 :attributes => {:class => /line-code/},
87 :attributes => {:class => /line-code/},
88 :content => /Demande créée avec succès/
88 :content => /Demande créée avec succès/
89 end
89 end
90 end
90 end
91 set_tmp_attachments_directory
91 set_tmp_attachments_directory
92 end
92 end
93
93
94 def test_show_text_file
94 def test_show_text_file
95 get :show, :id => 4
95 get :show, :id => 4
96 assert_response :success
96 assert_response :success
97 assert_template 'file'
97 assert_template 'file'
98 assert_equal 'text/html', @response.content_type
98 assert_equal 'text/html', @response.content_type
99 set_tmp_attachments_directory
99 set_tmp_attachments_directory
100 end
100 end
101
101
102 def test_show_text_file_utf_8
102 def test_show_text_file_utf_8
103 set_tmp_attachments_directory
103 set_tmp_attachments_directory
104 a = Attachment.new(:container => Issue.find(1),
104 a = Attachment.new(:container => Issue.find(1),
105 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
105 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
106 :author => User.find(1))
106 :author => User.find(1))
107 assert a.save
107 assert a.save
108 assert_equal 'japanese-utf-8.txt', a.filename
108 assert_equal 'japanese-utf-8.txt', a.filename
109
109
110 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
110 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
111 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
111 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
112
112
113 get :show, :id => a.id
113 get :show, :id => a.id
114 assert_response :success
114 assert_response :success
115 assert_template 'file'
115 assert_template 'file'
116 assert_equal 'text/html', @response.content_type
116 assert_equal 'text/html', @response.content_type
117 assert_tag :tag => 'th',
117 assert_tag :tag => 'th',
118 :content => '1',
118 :content => '1',
119 :attributes => { :class => 'line-num' },
119 :attributes => { :class => 'line-num' },
120 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
120 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
121 end
121 end
122
122
123 def test_show_text_file_replcace_cannot_convert_content
123 def test_show_text_file_replcace_cannot_convert_content
124 set_tmp_attachments_directory
124 set_tmp_attachments_directory
125 with_settings :repositories_encodings => 'UTF-8' do
125 with_settings :repositories_encodings => 'UTF-8' do
126 a = Attachment.new(:container => Issue.find(1),
126 a = Attachment.new(:container => Issue.find(1),
127 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
127 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
128 :author => User.find(1))
128 :author => User.find(1))
129 assert a.save
129 assert a.save
130 assert_equal 'iso8859-1.txt', a.filename
130 assert_equal 'iso8859-1.txt', a.filename
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 => '7',
137 :content => '7',
138 :attributes => { :class => 'line-num' },
138 :attributes => { :class => 'line-num' },
139 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
139 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
140 end
140 end
141 end
141 end
142
142
143 def test_show_text_file_latin_1
143 def test_show_text_file_latin_1
144 set_tmp_attachments_directory
144 set_tmp_attachments_directory
145 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
145 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
146 a = Attachment.new(:container => Issue.find(1),
146 a = Attachment.new(:container => Issue.find(1),
147 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
147 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
148 :author => User.find(1))
148 :author => User.find(1))
149 assert a.save
149 assert a.save
150 assert_equal 'iso8859-1.txt', a.filename
150 assert_equal 'iso8859-1.txt', a.filename
151
151
152 get :show, :id => a.id
152 get :show, :id => a.id
153 assert_response :success
153 assert_response :success
154 assert_template 'file'
154 assert_template 'file'
155 assert_equal 'text/html', @response.content_type
155 assert_equal 'text/html', @response.content_type
156 assert_tag :tag => 'th',
156 assert_tag :tag => 'th',
157 :content => '7',
157 :content => '7',
158 :attributes => { :class => 'line-num' },
158 :attributes => { :class => 'line-num' },
159 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
159 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
160 end
160 end
161 end
161 end
162
162
163 def test_show_text_file_should_send_if_too_big
163 def test_show_text_file_should_send_if_too_big
164 Setting.file_max_size_displayed = 512
164 Setting.file_max_size_displayed = 512
165 Attachment.find(4).update_attribute :filesize, 754.kilobyte
165 Attachment.find(4).update_attribute :filesize, 754.kilobyte
166
166
167 get :show, :id => 4
167 get :show, :id => 4
168 assert_response :success
168 assert_response :success
169 assert_equal 'application/x-ruby', @response.content_type
169 assert_equal 'application/x-ruby', @response.content_type
170 set_tmp_attachments_directory
170 set_tmp_attachments_directory
171 end
171 end
172
172
173 def test_show_other
173 def test_show_other
174 get :show, :id => 6
174 get :show, :id => 6
175 assert_response :success
175 assert_response :success
176 assert_equal 'application/octet-stream', @response.content_type
176 assert_equal 'application/octet-stream', @response.content_type
177 set_tmp_attachments_directory
177 set_tmp_attachments_directory
178 end
178 end
179
179
180 def test_show_file_from_private_issue_without_permission
180 def test_show_file_from_private_issue_without_permission
181 get :show, :id => 15
181 get :show, :id => 15
182 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
182 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
183 set_tmp_attachments_directory
183 set_tmp_attachments_directory
184 end
184 end
185
185
186 def test_show_file_from_private_issue_with_permission
186 def test_show_file_from_private_issue_with_permission
187 @request.session[:user_id] = 2
187 @request.session[:user_id] = 2
188 get :show, :id => 15
188 get :show, :id => 15
189 assert_response :success
189 assert_response :success
190 assert_tag 'h2', :content => /private.diff/
190 assert_tag 'h2', :content => /private.diff/
191 set_tmp_attachments_directory
191 set_tmp_attachments_directory
192 end
192 end
193
193
194 def test_download_text_file
194 def test_download_text_file
195 get :download, :id => 4
195 get :download, :id => 4
196 assert_response :success
196 assert_response :success
197 assert_equal 'application/x-ruby', @response.content_type
197 assert_equal 'application/x-ruby', @response.content_type
198 set_tmp_attachments_directory
198 set_tmp_attachments_directory
199 end
199 end
200
200
201 def test_download_should_assign_content_type_if_blank
201 def test_download_should_assign_content_type_if_blank
202 Attachment.find(4).update_attribute(:content_type, '')
202 Attachment.find(4).update_attribute(:content_type, '')
203
203
204 get :download, :id => 4
204 get :download, :id => 4
205 assert_response :success
205 assert_response :success
206 assert_equal 'text/x-ruby', @response.content_type
206 assert_equal 'text/x-ruby', @response.content_type
207 set_tmp_attachments_directory
207 set_tmp_attachments_directory
208 end
208 end
209
209
210 def test_download_missing_file
210 def test_download_missing_file
211 get :download, :id => 2
211 get :download, :id => 2
212 assert_response 404
212 assert_response 404
213 set_tmp_attachments_directory
213 set_tmp_attachments_directory
214 end
214 end
215
215
216 def test_anonymous_on_private_private
216 def test_anonymous_on_private_private
217 get :download, :id => 7
217 get :download, :id => 7
218 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
218 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
219 set_tmp_attachments_directory
219 set_tmp_attachments_directory
220 end
220 end
221
221
222 def test_destroy_issue_attachment
222 def test_destroy_issue_attachment
223 set_tmp_attachments_directory
223 issue = Issue.find(3)
224 issue = Issue.find(3)
224 @request.session[:user_id] = 2
225 @request.session[:user_id] = 2
225
226
226 assert_difference 'issue.attachments.count', -1 do
227 assert_difference 'issue.attachments.count', -1 do
227 delete :destroy, :id => 1
228 delete :destroy, :id => 1
228 end
229 end
229 # no referrer
230 # no referrer
230 assert_redirected_to '/projects/ecookbook'
231 assert_redirected_to '/projects/ecookbook'
231 assert_nil Attachment.find_by_id(1)
232 assert_nil Attachment.find_by_id(1)
232 j = issue.journals.find(:first, :order => 'created_on DESC')
233 j = issue.journals.find(:first, :order => 'created_on DESC')
233 assert_equal 'attachment', j.details.first.property
234 assert_equal 'attachment', j.details.first.property
234 assert_equal '1', j.details.first.prop_key
235 assert_equal '1', j.details.first.prop_key
235 assert_equal 'error281.txt', j.details.first.old_value
236 assert_equal 'error281.txt', j.details.first.old_value
236 set_tmp_attachments_directory
237 end
237 end
238
238
239 def test_destroy_wiki_page_attachment
239 def test_destroy_wiki_page_attachment
240 set_tmp_attachments_directory
240 @request.session[:user_id] = 2
241 @request.session[:user_id] = 2
241 assert_difference 'Attachment.count', -1 do
242 assert_difference 'Attachment.count', -1 do
242 delete :destroy, :id => 3
243 delete :destroy, :id => 3
243 assert_response 302
244 assert_response 302
244 end
245 end
245 set_tmp_attachments_directory
246 end
246 end
247
247
248 def test_destroy_project_attachment
248 def test_destroy_project_attachment
249 set_tmp_attachments_directory
249 @request.session[:user_id] = 2
250 @request.session[:user_id] = 2
250 assert_difference 'Attachment.count', -1 do
251 assert_difference 'Attachment.count', -1 do
251 delete :destroy, :id => 8
252 delete :destroy, :id => 8
252 assert_response 302
253 assert_response 302
253 end
254 end
254 set_tmp_attachments_directory
255 end
255 end
256
256
257 def test_destroy_version_attachment
257 def test_destroy_version_attachment
258 set_tmp_attachments_directory
258 @request.session[:user_id] = 2
259 @request.session[:user_id] = 2
259 assert_difference 'Attachment.count', -1 do
260 assert_difference 'Attachment.count', -1 do
260 delete :destroy, :id => 9
261 delete :destroy, :id => 9
261 assert_response 302
262 assert_response 302
262 end
263 end
263 set_tmp_attachments_directory
264 end
264 end
265
265
266 def test_destroy_without_permission
266 def test_destroy_without_permission
267 set_tmp_attachments_directory
267 assert_no_difference 'Attachment.count' do
268 assert_no_difference 'Attachment.count' do
268 delete :destroy, :id => 3
269 delete :destroy, :id => 3
269 end
270 end
270 assert_response 302
271 assert_response 302
271 assert Attachment.find_by_id(3)
272 assert Attachment.find_by_id(3)
272 set_tmp_attachments_directory
273 end
273 end
274 end
274 end
General Comments 0
You need to be logged in to leave comments. Login now