##// END OF EJS Templates
attachment: add a functional test to show invalid UTF-8 text file (#2371)...
Toshi MARUYAMA -
r7704:fdf6e8547b6b
parent child
Show More
@@ -0,0 +1,13
1 Index: trunk/app/controllers/issues_controller.rb
2 ===================================================================
3 --- trunk/app/controllers/issues_controller.rb (r�vision 1483)
4 +++ trunk/app/controllers/issues_controller.rb (r�vision 1484)
5 @@ -149,7 +149,7 @@
6 attach_files(@issue, params[:attachments])
7 flash[:notice] = 'Demande cr��e avec succ�s'
8 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
9 - redirect_to :controller => 'issues', :action => 'show', :id => @issue, :project_id => @project
10 + redirect_to :controller => 'issues', :action => 'show', :id => @issue
11 return
12 end
13 end
@@ -1,190 +1,207
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 get :show, :id => 14 # 060719210727_changeset_utf8.diff
40 get :show, :id => 14 # 060719210727_changeset_utf8.diff
41 assert_response :success
41 assert_response :success
42 assert_template 'diff'
42 assert_template 'diff'
43 assert_equal 'text/html', @response.content_type
43 assert_equal 'text/html', @response.content_type
44
44
45 assert_tag 'th',
45 assert_tag 'th',
46 :attributes => {:class => /filename/},
46 :attributes => {:class => /filename/},
47 :content => /issues_controller.rb\t\(révision 1484\)/
47 :content => /issues_controller.rb\t\(révision 1484\)/
48 assert_tag 'td',
48 assert_tag 'td',
49 :attributes => {:class => /line-code/},
49 :attributes => {:class => /line-code/},
50 :content => /Demande créée avec succès/
50 :content => /Demande créée avec succès/
51 end
51 end
52
52
53 def test_show_diff_should_strip_non_utf8_content
53 def test_show_diff_should_strip_non_utf8_content
54 get :show, :id => 5 # 060719210727_changeset_iso8859-1.diff
54 get :show, :id => 5 # 060719210727_changeset_iso8859-1.diff
55 assert_response :success
55 assert_response :success
56 assert_template 'diff'
56 assert_template 'diff'
57 assert_equal 'text/html', @response.content_type
57 assert_equal 'text/html', @response.content_type
58
58
59 assert_tag 'th',
59 assert_tag 'th',
60 :attributes => {:class => /filename/},
60 :attributes => {:class => /filename/},
61 :content => /issues_controller.rb\t\(rvision 1484\)/
61 :content => /issues_controller.rb\t\(rvision 1484\)/
62 assert_tag 'td',
62 assert_tag 'td',
63 :attributes => {:class => /line-code/},
63 :attributes => {:class => /line-code/},
64 :content => /Demande cre avec succs/
64 :content => /Demande cre avec succs/
65 end
65 end
66
66
67 def test_show_text_file
67 def test_show_text_file
68 get :show, :id => 4
68 get :show, :id => 4
69 assert_response :success
69 assert_response :success
70 assert_template 'file'
70 assert_template 'file'
71 assert_equal 'text/html', @response.content_type
71 assert_equal 'text/html', @response.content_type
72 end
72 end
73
73
74 def test_show_text_file_utf_8
74 def test_show_text_file_utf_8
75 a = Attachment.new(:container => Issue.find(1),
75 a = Attachment.new(:container => Issue.find(1),
76 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
76 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
77 :author => User.find(1))
77 :author => User.find(1))
78 assert a.save
78 assert a.save
79 assert_equal 'japanese-utf-8.txt', a.filename
79 assert_equal 'japanese-utf-8.txt', a.filename
80
80
81 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
81 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
82 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
82 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
83
83
84 get :show, :id => a.id
84 get :show, :id => a.id
85 assert_response :success
85 assert_response :success
86 assert_template 'file'
86 assert_template 'file'
87 assert_equal 'text/html', @response.content_type
87 assert_equal 'text/html', @response.content_type
88 assert_tag :tag => 'th',
88 assert_tag :tag => 'th',
89 :content => '1',
89 :content => '1',
90 :attributes => { :class => 'line-num' },
90 :attributes => { :class => 'line-num' },
91 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
91 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
92 end
92 end
93
93
94 def test_show_text_file_should_strip_non_utf8_content
95 a = Attachment.new(:container => Issue.find(1),
96 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
97 :author => User.find(1))
98 assert a.save
99 assert_equal 'iso8859-1.txt', a.filename
100
101 get :show, :id => a.id
102 assert_response :success
103 assert_template 'file'
104 assert_equal 'text/html', @response.content_type
105 assert_tag :tag => 'th',
106 :content => '7',
107 :attributes => { :class => 'line-num' },
108 :sibling => { :tag => 'td', :content => /Demande cre avec succs/ }
109 end
110
94 def test_show_text_file_should_send_if_too_big
111 def test_show_text_file_should_send_if_too_big
95 Setting.file_max_size_displayed = 512
112 Setting.file_max_size_displayed = 512
96 Attachment.find(4).update_attribute :filesize, 754.kilobyte
113 Attachment.find(4).update_attribute :filesize, 754.kilobyte
97
114
98 get :show, :id => 4
115 get :show, :id => 4
99 assert_response :success
116 assert_response :success
100 assert_equal 'application/x-ruby', @response.content_type
117 assert_equal 'application/x-ruby', @response.content_type
101 end
118 end
102
119
103 def test_show_other
120 def test_show_other
104 get :show, :id => 6
121 get :show, :id => 6
105 assert_response :success
122 assert_response :success
106 assert_equal 'application/octet-stream', @response.content_type
123 assert_equal 'application/octet-stream', @response.content_type
107 end
124 end
108
125
109 def test_show_file_from_private_issue_without_permission
126 def test_show_file_from_private_issue_without_permission
110 get :show, :id => 15
127 get :show, :id => 15
111 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
128 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
112 end
129 end
113
130
114 def test_show_file_from_private_issue_with_permission
131 def test_show_file_from_private_issue_with_permission
115 @request.session[:user_id] = 2
132 @request.session[:user_id] = 2
116 get :show, :id => 15
133 get :show, :id => 15
117 assert_response :success
134 assert_response :success
118 assert_tag 'h2', :content => /private.diff/
135 assert_tag 'h2', :content => /private.diff/
119 end
136 end
120
137
121 def test_download_text_file
138 def test_download_text_file
122 get :download, :id => 4
139 get :download, :id => 4
123 assert_response :success
140 assert_response :success
124 assert_equal 'application/x-ruby', @response.content_type
141 assert_equal 'application/x-ruby', @response.content_type
125 end
142 end
126
143
127 def test_download_should_assign_content_type_if_blank
144 def test_download_should_assign_content_type_if_blank
128 Attachment.find(4).update_attribute(:content_type, '')
145 Attachment.find(4).update_attribute(:content_type, '')
129
146
130 get :download, :id => 4
147 get :download, :id => 4
131 assert_response :success
148 assert_response :success
132 assert_equal 'text/x-ruby', @response.content_type
149 assert_equal 'text/x-ruby', @response.content_type
133 end
150 end
134
151
135 def test_download_missing_file
152 def test_download_missing_file
136 get :download, :id => 2
153 get :download, :id => 2
137 assert_response 404
154 assert_response 404
138 end
155 end
139
156
140 def test_anonymous_on_private_private
157 def test_anonymous_on_private_private
141 get :download, :id => 7
158 get :download, :id => 7
142 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
159 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
143 end
160 end
144
161
145 def test_destroy_issue_attachment
162 def test_destroy_issue_attachment
146 issue = Issue.find(3)
163 issue = Issue.find(3)
147 @request.session[:user_id] = 2
164 @request.session[:user_id] = 2
148
165
149 assert_difference 'issue.attachments.count', -1 do
166 assert_difference 'issue.attachments.count', -1 do
150 post :destroy, :id => 1
167 post :destroy, :id => 1
151 end
168 end
152 # no referrer
169 # no referrer
153 assert_redirected_to '/projects/ecookbook'
170 assert_redirected_to '/projects/ecookbook'
154 assert_nil Attachment.find_by_id(1)
171 assert_nil Attachment.find_by_id(1)
155 j = issue.journals.find(:first, :order => 'created_on DESC')
172 j = issue.journals.find(:first, :order => 'created_on DESC')
156 assert_equal 'attachment', j.details.first.property
173 assert_equal 'attachment', j.details.first.property
157 assert_equal '1', j.details.first.prop_key
174 assert_equal '1', j.details.first.prop_key
158 assert_equal 'error281.txt', j.details.first.old_value
175 assert_equal 'error281.txt', j.details.first.old_value
159 end
176 end
160
177
161 def test_destroy_wiki_page_attachment
178 def test_destroy_wiki_page_attachment
162 @request.session[:user_id] = 2
179 @request.session[:user_id] = 2
163 assert_difference 'Attachment.count', -1 do
180 assert_difference 'Attachment.count', -1 do
164 post :destroy, :id => 3
181 post :destroy, :id => 3
165 assert_response 302
182 assert_response 302
166 end
183 end
167 end
184 end
168
185
169 def test_destroy_project_attachment
186 def test_destroy_project_attachment
170 @request.session[:user_id] = 2
187 @request.session[:user_id] = 2
171 assert_difference 'Attachment.count', -1 do
188 assert_difference 'Attachment.count', -1 do
172 post :destroy, :id => 8
189 post :destroy, :id => 8
173 assert_response 302
190 assert_response 302
174 end
191 end
175 end
192 end
176
193
177 def test_destroy_version_attachment
194 def test_destroy_version_attachment
178 @request.session[:user_id] = 2
195 @request.session[:user_id] = 2
179 assert_difference 'Attachment.count', -1 do
196 assert_difference 'Attachment.count', -1 do
180 post :destroy, :id => 9
197 post :destroy, :id => 9
181 assert_response 302
198 assert_response 302
182 end
199 end
183 end
200 end
184
201
185 def test_destroy_without_permission
202 def test_destroy_without_permission
186 post :destroy, :id => 3
203 post :destroy, :id => 3
187 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3'
204 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3'
188 assert Attachment.find_by_id(3)
205 assert Attachment.find_by_id(3)
189 end
206 end
190 end
207 end
General Comments 0
You need to be logged in to leave comments. Login now