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