##// END OF EJS Templates
Adds functional tests....
Jean-Philippe Lang -
r8828:c49ef8e543c4
parent child
Show More
@@ -1,308 +1,313
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 User.current = nil
35 User.current = nil
36 set_fixtures_attachments_directory
36 set_fixtures_attachments_directory
37 end
37 end
38
38
39 def teardown
39 def teardown
40 set_tmp_attachments_directory
40 set_tmp_attachments_directory
41 end
41 end
42
42
43 def test_show_diff
43 def test_show_diff
44 ['inline', 'sbs'].each do |dt|
44 ['inline', 'sbs'].each do |dt|
45 # 060719210727_changeset_utf8.diff
45 # 060719210727_changeset_utf8.diff
46 get :show, :id => 14, :type => dt
46 get :show, :id => 14, :type => dt
47 assert_response :success
47 assert_response :success
48 assert_template 'diff'
48 assert_template 'diff'
49 assert_equal 'text/html', @response.content_type
49 assert_equal 'text/html', @response.content_type
50 assert_tag 'th',
50 assert_tag 'th',
51 :attributes => {:class => /filename/},
51 :attributes => {:class => /filename/},
52 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
52 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
53 assert_tag 'td',
53 assert_tag 'td',
54 :attributes => {:class => /line-code/},
54 :attributes => {:class => /line-code/},
55 :content => /Demande créée avec succès/
55 :content => /Demande créée avec succès/
56 end
56 end
57 set_tmp_attachments_directory
57 set_tmp_attachments_directory
58 end
58 end
59
59
60 def test_show_diff_replcace_cannot_convert_content
60 def test_show_diff_replcace_cannot_convert_content
61 with_settings :repositories_encodings => 'UTF-8' do
61 with_settings :repositories_encodings => 'UTF-8' do
62 ['inline', 'sbs'].each do |dt|
62 ['inline', 'sbs'].each do |dt|
63 # 060719210727_changeset_iso8859-1.diff
63 # 060719210727_changeset_iso8859-1.diff
64 get :show, :id => 5, :type => dt
64 get :show, :id => 5, :type => dt
65 assert_response :success
65 assert_response :success
66 assert_template 'diff'
66 assert_template 'diff'
67 assert_equal 'text/html', @response.content_type
67 assert_equal 'text/html', @response.content_type
68 assert_tag 'th',
68 assert_tag 'th',
69 :attributes => {:class => "filename"},
69 :attributes => {:class => "filename"},
70 :content => /issues_controller.rb\t\(r\?vision 1484\)/
70 :content => /issues_controller.rb\t\(r\?vision 1484\)/
71 assert_tag 'td',
71 assert_tag 'td',
72 :attributes => {:class => /line-code/},
72 :attributes => {:class => /line-code/},
73 :content => /Demande cr\?\?e avec succ\?s/
73 :content => /Demande cr\?\?e avec succ\?s/
74 end
74 end
75 end
75 end
76 set_tmp_attachments_directory
76 set_tmp_attachments_directory
77 end
77 end
78
78
79 def test_show_diff_latin_1
79 def test_show_diff_latin_1
80 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
80 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
81 ['inline', 'sbs'].each do |dt|
81 ['inline', 'sbs'].each do |dt|
82 # 060719210727_changeset_iso8859-1.diff
82 # 060719210727_changeset_iso8859-1.diff
83 get :show, :id => 5, :type => dt
83 get :show, :id => 5, :type => dt
84 assert_response :success
84 assert_response :success
85 assert_template 'diff'
85 assert_template 'diff'
86 assert_equal 'text/html', @response.content_type
86 assert_equal 'text/html', @response.content_type
87 assert_tag 'th',
87 assert_tag 'th',
88 :attributes => {:class => "filename"},
88 :attributes => {:class => "filename"},
89 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
89 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
90 assert_tag 'td',
90 assert_tag 'td',
91 :attributes => {:class => /line-code/},
91 :attributes => {:class => /line-code/},
92 :content => /Demande créée avec succès/
92 :content => /Demande créée avec succès/
93 end
93 end
94 end
94 end
95 set_tmp_attachments_directory
95 set_tmp_attachments_directory
96 end
96 end
97
97
98 def test_save_diff_type
98 def test_save_diff_type
99 @request.session[:user_id] = 1 # admin
99 @request.session[:user_id] = 1 # admin
100 user = User.find(1)
100 user = User.find(1)
101 get :show, :id => 5
101 get :show, :id => 5
102 assert_response :success
102 assert_response :success
103 assert_template 'diff'
103 assert_template 'diff'
104 user.reload
104 user.reload
105 assert_equal "inline", user.pref[:diff_type]
105 assert_equal "inline", user.pref[:diff_type]
106 get :show, :id => 5, :type => 'sbs'
106 get :show, :id => 5, :type => 'sbs'
107 assert_response :success
107 assert_response :success
108 assert_template 'diff'
108 assert_template 'diff'
109 user.reload
109 user.reload
110 assert_equal "sbs", user.pref[:diff_type]
110 assert_equal "sbs", user.pref[:diff_type]
111 end
111 end
112
112
113 def test_show_text_file
113 def test_show_text_file
114 get :show, :id => 4
114 get :show, :id => 4
115 assert_response :success
115 assert_response :success
116 assert_template 'file'
116 assert_template 'file'
117 assert_equal 'text/html', @response.content_type
117 assert_equal 'text/html', @response.content_type
118 set_tmp_attachments_directory
118 set_tmp_attachments_directory
119 end
119 end
120
120
121 def test_show_text_file_utf_8
121 def test_show_text_file_utf_8
122 set_tmp_attachments_directory
122 set_tmp_attachments_directory
123 a = Attachment.new(:container => Issue.find(1),
123 a = Attachment.new(:container => Issue.find(1),
124 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
124 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
125 :author => User.find(1))
125 :author => User.find(1))
126 assert a.save
126 assert a.save
127 assert_equal 'japanese-utf-8.txt', a.filename
127 assert_equal 'japanese-utf-8.txt', a.filename
128
128
129 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
129 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
130 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
130 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
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 => '1',
137 :content => '1',
138 :attributes => { :class => 'line-num' },
138 :attributes => { :class => 'line-num' },
139 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
139 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
140 end
140 end
141
141
142 def test_show_text_file_replcace_cannot_convert_content
142 def test_show_text_file_replcace_cannot_convert_content
143 set_tmp_attachments_directory
143 set_tmp_attachments_directory
144 with_settings :repositories_encodings => 'UTF-8' do
144 with_settings :repositories_encodings => 'UTF-8' do
145 a = Attachment.new(:container => Issue.find(1),
145 a = Attachment.new(:container => Issue.find(1),
146 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
146 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
147 :author => User.find(1))
147 :author => User.find(1))
148 assert a.save
148 assert a.save
149 assert_equal 'iso8859-1.txt', a.filename
149 assert_equal 'iso8859-1.txt', a.filename
150
150
151 get :show, :id => a.id
151 get :show, :id => a.id
152 assert_response :success
152 assert_response :success
153 assert_template 'file'
153 assert_template 'file'
154 assert_equal 'text/html', @response.content_type
154 assert_equal 'text/html', @response.content_type
155 assert_tag :tag => 'th',
155 assert_tag :tag => 'th',
156 :content => '7',
156 :content => '7',
157 :attributes => { :class => 'line-num' },
157 :attributes => { :class => 'line-num' },
158 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
158 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
159 end
159 end
160 end
160 end
161
161
162 def test_show_text_file_latin_1
162 def test_show_text_file_latin_1
163 set_tmp_attachments_directory
163 set_tmp_attachments_directory
164 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
164 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
165 a = Attachment.new(:container => Issue.find(1),
165 a = Attachment.new(:container => Issue.find(1),
166 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
166 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
167 :author => User.find(1))
167 :author => User.find(1))
168 assert a.save
168 assert a.save
169 assert_equal 'iso8859-1.txt', a.filename
169 assert_equal 'iso8859-1.txt', a.filename
170
170
171 get :show, :id => a.id
171 get :show, :id => a.id
172 assert_response :success
172 assert_response :success
173 assert_template 'file'
173 assert_template 'file'
174 assert_equal 'text/html', @response.content_type
174 assert_equal 'text/html', @response.content_type
175 assert_tag :tag => 'th',
175 assert_tag :tag => 'th',
176 :content => '7',
176 :content => '7',
177 :attributes => { :class => 'line-num' },
177 :attributes => { :class => 'line-num' },
178 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
178 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
179 end
179 end
180 end
180 end
181
181
182 def test_show_text_file_should_send_if_too_big
182 def test_show_text_file_should_send_if_too_big
183 Setting.file_max_size_displayed = 512
183 Setting.file_max_size_displayed = 512
184 Attachment.find(4).update_attribute :filesize, 754.kilobyte
184 Attachment.find(4).update_attribute :filesize, 754.kilobyte
185
185
186 get :show, :id => 4
186 get :show, :id => 4
187 assert_response :success
187 assert_response :success
188 assert_equal 'application/x-ruby', @response.content_type
188 assert_equal 'application/x-ruby', @response.content_type
189 set_tmp_attachments_directory
189 set_tmp_attachments_directory
190 end
190 end
191
191
192 def test_show_other
192 def test_show_other
193 get :show, :id => 6
193 get :show, :id => 6
194 assert_response :success
194 assert_response :success
195 assert_equal 'application/octet-stream', @response.content_type
195 assert_equal 'application/octet-stream', @response.content_type
196 set_tmp_attachments_directory
196 set_tmp_attachments_directory
197 end
197 end
198
198
199 def test_show_file_from_private_issue_without_permission
199 def test_show_file_from_private_issue_without_permission
200 get :show, :id => 15
200 get :show, :id => 15
201 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
201 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
202 set_tmp_attachments_directory
202 set_tmp_attachments_directory
203 end
203 end
204
204
205 def test_show_file_from_private_issue_with_permission
205 def test_show_file_from_private_issue_with_permission
206 @request.session[:user_id] = 2
206 @request.session[:user_id] = 2
207 get :show, :id => 15
207 get :show, :id => 15
208 assert_response :success
208 assert_response :success
209 assert_tag 'h2', :content => /private.diff/
209 assert_tag 'h2', :content => /private.diff/
210 set_tmp_attachments_directory
210 set_tmp_attachments_directory
211 end
211 end
212
212
213 def test_show_file_without_container_should_be_denied
213 def test_show_file_without_container_should_be_denied
214 set_tmp_attachments_directory
214 set_tmp_attachments_directory
215 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
215 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
216
216
217 @request.session[:user_id] = 2
217 @request.session[:user_id] = 2
218 get :show, :id => attachment.id
218 get :show, :id => attachment.id
219 assert_response 403
219 assert_response 403
220 end
220 end
221
221
222 def test_show_invalid_should_respond_with_404
223 get :show, :id => 999
224 assert_response 404
225 end
226
222 def test_download_text_file
227 def test_download_text_file
223 get :download, :id => 4
228 get :download, :id => 4
224 assert_response :success
229 assert_response :success
225 assert_equal 'application/x-ruby', @response.content_type
230 assert_equal 'application/x-ruby', @response.content_type
226 set_tmp_attachments_directory
231 set_tmp_attachments_directory
227 end
232 end
228
233
229 def test_download_version_file_with_issue_tracking_disabled
234 def test_download_version_file_with_issue_tracking_disabled
230 Project.find(1).disable_module! :issue_tracking
235 Project.find(1).disable_module! :issue_tracking
231 get :download, :id => 9
236 get :download, :id => 9
232 assert_response :success
237 assert_response :success
233 end
238 end
234
239
235 def test_download_should_assign_content_type_if_blank
240 def test_download_should_assign_content_type_if_blank
236 Attachment.find(4).update_attribute(:content_type, '')
241 Attachment.find(4).update_attribute(:content_type, '')
237
242
238 get :download, :id => 4
243 get :download, :id => 4
239 assert_response :success
244 assert_response :success
240 assert_equal 'text/x-ruby', @response.content_type
245 assert_equal 'text/x-ruby', @response.content_type
241 set_tmp_attachments_directory
246 set_tmp_attachments_directory
242 end
247 end
243
248
244 def test_download_missing_file
249 def test_download_missing_file
245 get :download, :id => 2
250 get :download, :id => 2
246 assert_response 404
251 assert_response 404
247 set_tmp_attachments_directory
252 set_tmp_attachments_directory
248 end
253 end
249
254
250 def test_anonymous_on_private_private
255 def test_anonymous_on_private_private
251 get :download, :id => 7
256 get :download, :id => 7
252 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
257 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
253 set_tmp_attachments_directory
258 set_tmp_attachments_directory
254 end
259 end
255
260
256 def test_destroy_issue_attachment
261 def test_destroy_issue_attachment
257 set_tmp_attachments_directory
262 set_tmp_attachments_directory
258 issue = Issue.find(3)
263 issue = Issue.find(3)
259 @request.session[:user_id] = 2
264 @request.session[:user_id] = 2
260
265
261 assert_difference 'issue.attachments.count', -1 do
266 assert_difference 'issue.attachments.count', -1 do
262 delete :destroy, :id => 1
267 delete :destroy, :id => 1
263 end
268 end
264 # no referrer
269 # no referrer
265 assert_redirected_to '/projects/ecookbook'
270 assert_redirected_to '/projects/ecookbook'
266 assert_nil Attachment.find_by_id(1)
271 assert_nil Attachment.find_by_id(1)
267 j = issue.journals.find(:first, :order => 'created_on DESC')
272 j = issue.journals.find(:first, :order => 'created_on DESC')
268 assert_equal 'attachment', j.details.first.property
273 assert_equal 'attachment', j.details.first.property
269 assert_equal '1', j.details.first.prop_key
274 assert_equal '1', j.details.first.prop_key
270 assert_equal 'error281.txt', j.details.first.old_value
275 assert_equal 'error281.txt', j.details.first.old_value
271 end
276 end
272
277
273 def test_destroy_wiki_page_attachment
278 def test_destroy_wiki_page_attachment
274 set_tmp_attachments_directory
279 set_tmp_attachments_directory
275 @request.session[:user_id] = 2
280 @request.session[:user_id] = 2
276 assert_difference 'Attachment.count', -1 do
281 assert_difference 'Attachment.count', -1 do
277 delete :destroy, :id => 3
282 delete :destroy, :id => 3
278 assert_response 302
283 assert_response 302
279 end
284 end
280 end
285 end
281
286
282 def test_destroy_project_attachment
287 def test_destroy_project_attachment
283 set_tmp_attachments_directory
288 set_tmp_attachments_directory
284 @request.session[:user_id] = 2
289 @request.session[:user_id] = 2
285 assert_difference 'Attachment.count', -1 do
290 assert_difference 'Attachment.count', -1 do
286 delete :destroy, :id => 8
291 delete :destroy, :id => 8
287 assert_response 302
292 assert_response 302
288 end
293 end
289 end
294 end
290
295
291 def test_destroy_version_attachment
296 def test_destroy_version_attachment
292 set_tmp_attachments_directory
297 set_tmp_attachments_directory
293 @request.session[:user_id] = 2
298 @request.session[:user_id] = 2
294 assert_difference 'Attachment.count', -1 do
299 assert_difference 'Attachment.count', -1 do
295 delete :destroy, :id => 9
300 delete :destroy, :id => 9
296 assert_response 302
301 assert_response 302
297 end
302 end
298 end
303 end
299
304
300 def test_destroy_without_permission
305 def test_destroy_without_permission
301 set_tmp_attachments_directory
306 set_tmp_attachments_directory
302 assert_no_difference 'Attachment.count' do
307 assert_no_difference 'Attachment.count' do
303 delete :destroy, :id => 3
308 delete :destroy, :id => 3
304 end
309 end
305 assert_response 302
310 assert_response 302
306 assert Attachment.find_by_id(3)
311 assert Attachment.find_by_id(3)
307 end
312 end
308 end
313 end
@@ -1,116 +1,140
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'issue_categories_controller'
19 require 'issue_categories_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class IssueCategoriesController; def rescue_action(e) raise e end; end
22 class IssueCategoriesController; def rescue_action(e) raise e end; end
23
23
24 class IssueCategoriesControllerTest < ActionController::TestCase
24 class IssueCategoriesControllerTest < ActionController::TestCase
25 fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules, :issue_categories
25 fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules, :issue_categories
26
26
27 def setup
27 def setup
28 @controller = IssueCategoriesController.new
28 @controller = IssueCategoriesController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 @request.session[:user_id] = 2
32 @request.session[:user_id] = 2
33 end
33 end
34
34
35 def test_new
35 def test_new
36 @request.session[:user_id] = 2 # manager
36 @request.session[:user_id] = 2 # manager
37 get :new, :project_id => '1'
37 get :new, :project_id => '1'
38 assert_response :success
38 assert_response :success
39 assert_template 'new'
39 assert_template 'new'
40 end
40 end
41
41
42 def test_create
42 def test_create
43 @request.session[:user_id] = 2 # manager
43 @request.session[:user_id] = 2 # manager
44 assert_difference 'IssueCategory.count' do
44 assert_difference 'IssueCategory.count' do
45 post :create, :project_id => '1', :issue_category => {:name => 'New category'}
45 post :create, :project_id => '1', :issue_category => {:name => 'New category'}
46 end
46 end
47 assert_redirected_to '/projects/ecookbook/settings/categories'
47 assert_redirected_to '/projects/ecookbook/settings/categories'
48 category = IssueCategory.find_by_name('New category')
48 category = IssueCategory.find_by_name('New category')
49 assert_not_nil category
49 assert_not_nil category
50 assert_equal 1, category.project_id
50 assert_equal 1, category.project_id
51 end
51 end
52
52
53 def test_create_failure
53 def test_create_failure
54 @request.session[:user_id] = 2
54 @request.session[:user_id] = 2
55 post :create, :project_id => '1', :issue_category => {:name => ''}
55 post :create, :project_id => '1', :issue_category => {:name => ''}
56 assert_response :success
56 assert_response :success
57 assert_template 'new'
57 assert_template 'new'
58 end
58 end
59
59
60 def test_create_from_issue_form
61 @request.session[:user_id] = 2 # manager
62 assert_difference 'IssueCategory.count' do
63 xhr :post, :create, :project_id => '1', :issue_category => {:name => 'New category'}
64 end
65 category = IssueCategory.first(:order => 'id DESC')
66 assert_equal 'New category', category.name
67
68 assert_response :success
69 assert_select_rjs :replace, 'issue_category_id' do
70 assert_select "option[value=#{category.id}][selected=selected]"
71 end
72 end
73
74 def test_create_from_issue_form_with_failure
75 @request.session[:user_id] = 2 # manager
76 assert_no_difference 'IssueCategory.count' do
77 xhr :post, :create, :project_id => '1', :issue_category => {:name => ''}
78 end
79
80 assert_response :success
81 assert_match /alert/, @response.body
82 end
83
60 def test_edit
84 def test_edit
61 @request.session[:user_id] = 2
85 @request.session[:user_id] = 2
62 get :edit, :id => 2
86 get :edit, :id => 2
63 assert_response :success
87 assert_response :success
64 assert_template 'edit'
88 assert_template 'edit'
65 end
89 end
66
90
67 def test_update
91 def test_update
68 assert_no_difference 'IssueCategory.count' do
92 assert_no_difference 'IssueCategory.count' do
69 put :update, :id => 2, :issue_category => { :name => 'Testing' }
93 put :update, :id => 2, :issue_category => { :name => 'Testing' }
70 end
94 end
71 assert_redirected_to '/projects/ecookbook/settings/categories'
95 assert_redirected_to '/projects/ecookbook/settings/categories'
72 assert_equal 'Testing', IssueCategory.find(2).name
96 assert_equal 'Testing', IssueCategory.find(2).name
73 end
97 end
74
98
75 def test_update_failure
99 def test_update_failure
76 put :update, :id => 2, :issue_category => { :name => '' }
100 put :update, :id => 2, :issue_category => { :name => '' }
77 assert_response :success
101 assert_response :success
78 assert_template 'edit'
102 assert_template 'edit'
79 end
103 end
80
104
81 def test_update_not_found
105 def test_update_not_found
82 put :update, :id => 97, :issue_category => { :name => 'Testing' }
106 put :update, :id => 97, :issue_category => { :name => 'Testing' }
83 assert_response 404
107 assert_response 404
84 end
108 end
85
109
86 def test_destroy_category_not_in_use
110 def test_destroy_category_not_in_use
87 delete :destroy, :id => 2
111 delete :destroy, :id => 2
88 assert_redirected_to '/projects/ecookbook/settings/categories'
112 assert_redirected_to '/projects/ecookbook/settings/categories'
89 assert_nil IssueCategory.find_by_id(2)
113 assert_nil IssueCategory.find_by_id(2)
90 end
114 end
91
115
92 def test_destroy_category_in_use
116 def test_destroy_category_in_use
93 delete :destroy, :id => 1
117 delete :destroy, :id => 1
94 assert_response :success
118 assert_response :success
95 assert_template 'destroy'
119 assert_template 'destroy'
96 assert_not_nil IssueCategory.find_by_id(1)
120 assert_not_nil IssueCategory.find_by_id(1)
97 end
121 end
98
122
99 def test_destroy_category_in_use_with_reassignment
123 def test_destroy_category_in_use_with_reassignment
100 issue = Issue.find(:first, :conditions => {:category_id => 1})
124 issue = Issue.find(:first, :conditions => {:category_id => 1})
101 delete :destroy, :id => 1, :todo => 'reassign', :reassign_to_id => 2
125 delete :destroy, :id => 1, :todo => 'reassign', :reassign_to_id => 2
102 assert_redirected_to '/projects/ecookbook/settings/categories'
126 assert_redirected_to '/projects/ecookbook/settings/categories'
103 assert_nil IssueCategory.find_by_id(1)
127 assert_nil IssueCategory.find_by_id(1)
104 # check that the issue was reassign
128 # check that the issue was reassign
105 assert_equal 2, issue.reload.category_id
129 assert_equal 2, issue.reload.category_id
106 end
130 end
107
131
108 def test_destroy_category_in_use_without_reassignment
132 def test_destroy_category_in_use_without_reassignment
109 issue = Issue.find(:first, :conditions => {:category_id => 1})
133 issue = Issue.find(:first, :conditions => {:category_id => 1})
110 delete :destroy, :id => 1, :todo => 'nullify'
134 delete :destroy, :id => 1, :todo => 'nullify'
111 assert_redirected_to '/projects/ecookbook/settings/categories'
135 assert_redirected_to '/projects/ecookbook/settings/categories'
112 assert_nil IssueCategory.find_by_id(1)
136 assert_nil IssueCategory.find_by_id(1)
113 # check that the issue category was nullified
137 # check that the issue category was nullified
114 assert_nil issue.reload.category_id
138 assert_nil issue.reload.category_id
115 end
139 end
116 end
140 end
@@ -1,100 +1,114
1 require File.expand_path('../../test_helper', __FILE__)
1 require File.expand_path('../../test_helper', __FILE__)
2 require 'issue_statuses_controller'
2 require 'issue_statuses_controller'
3
3
4 # Re-raise errors caught by the controller.
4 # Re-raise errors caught by the controller.
5 class IssueStatusesController; def rescue_action(e) raise e end; end
5 class IssueStatusesController; def rescue_action(e) raise e end; end
6
6
7
7
8 class IssueStatusesControllerTest < ActionController::TestCase
8 class IssueStatusesControllerTest < ActionController::TestCase
9 fixtures :issue_statuses, :issues
9 fixtures :issue_statuses, :issues
10
10
11 def setup
11 def setup
12 @controller = IssueStatusesController.new
12 @controller = IssueStatusesController.new
13 @request = ActionController::TestRequest.new
13 @request = ActionController::TestRequest.new
14 @response = ActionController::TestResponse.new
14 @response = ActionController::TestResponse.new
15 User.current = nil
15 User.current = nil
16 @request.session[:user_id] = 1 # admin
16 @request.session[:user_id] = 1 # admin
17 end
17 end
18
18
19 def test_index
19 def test_index
20 get :index
20 get :index
21 assert_response :success
21 assert_response :success
22 assert_template 'index'
22 assert_template 'index'
23 end
23 end
24
24
25 def test_index_by_anonymous_should_redirect_to_login_form
25 def test_index_by_anonymous_should_redirect_to_login_form
26 @request.session[:user_id] = nil
26 @request.session[:user_id] = nil
27 get :index
27 get :index
28 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fissue_statuses'
28 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fissue_statuses'
29 end
29 end
30
30
31 def test_index_by_user_should_respond_with_406
31 def test_index_by_user_should_respond_with_406
32 @request.session[:user_id] = 2
32 @request.session[:user_id] = 2
33 get :index
33 get :index
34 assert_response 406
34 assert_response 406
35 end
35 end
36
36
37 def test_new
37 def test_new
38 get :new
38 get :new
39 assert_response :success
39 assert_response :success
40 assert_template 'new'
40 assert_template 'new'
41 end
41 end
42
42
43 def test_create
43 def test_create
44 assert_difference 'IssueStatus.count' do
44 assert_difference 'IssueStatus.count' do
45 post :create, :issue_status => {:name => 'New status'}
45 post :create, :issue_status => {:name => 'New status'}
46 end
46 end
47 assert_redirected_to :action => 'index'
47 assert_redirected_to :action => 'index'
48 status = IssueStatus.find(:first, :order => 'id DESC')
48 status = IssueStatus.find(:first, :order => 'id DESC')
49 assert_equal 'New status', status.name
49 assert_equal 'New status', status.name
50 end
50 end
51
51
52 def test_create_with_failure
53 post :create, :issue_status => {:name => ''}
54 assert_response :success
55 assert_template 'new'
56 assert_error_tag :content => /name can't be blank/i
57 end
58
52 def test_edit
59 def test_edit
53 get :edit, :id => '3'
60 get :edit, :id => '3'
54 assert_response :success
61 assert_response :success
55 assert_template 'edit'
62 assert_template 'edit'
56 end
63 end
57
64
58 def test_update
65 def test_update
59 put :update, :id => '3', :issue_status => {:name => 'Renamed status'}
66 put :update, :id => '3', :issue_status => {:name => 'Renamed status'}
60 assert_redirected_to :action => 'index'
67 assert_redirected_to :action => 'index'
61 status = IssueStatus.find(3)
68 status = IssueStatus.find(3)
62 assert_equal 'Renamed status', status.name
69 assert_equal 'Renamed status', status.name
63 end
70 end
64
71
72 def test_update_with_failure
73 put :update, :id => '3', :issue_status => {:name => ''}
74 assert_response :success
75 assert_template 'edit'
76 assert_error_tag :content => /name can't be blank/i
77 end
78
65 def test_destroy
79 def test_destroy
66 Issue.delete_all("status_id = 1")
80 Issue.delete_all("status_id = 1")
67
81
68 assert_difference 'IssueStatus.count', -1 do
82 assert_difference 'IssueStatus.count', -1 do
69 delete :destroy, :id => '1'
83 delete :destroy, :id => '1'
70 end
84 end
71 assert_redirected_to :action => 'index'
85 assert_redirected_to :action => 'index'
72 assert_nil IssueStatus.find_by_id(1)
86 assert_nil IssueStatus.find_by_id(1)
73 end
87 end
74
88
75 def test_destroy_should_block_if_status_in_use
89 def test_destroy_should_block_if_status_in_use
76 assert_not_nil Issue.find_by_status_id(1)
90 assert_not_nil Issue.find_by_status_id(1)
77
91
78 assert_no_difference 'IssueStatus.count' do
92 assert_no_difference 'IssueStatus.count' do
79 delete :destroy, :id => '1'
93 delete :destroy, :id => '1'
80 end
94 end
81 assert_redirected_to :action => 'index'
95 assert_redirected_to :action => 'index'
82 assert_not_nil IssueStatus.find_by_id(1)
96 assert_not_nil IssueStatus.find_by_id(1)
83 end
97 end
84
98
85 def test_update_issue_done_ratio_with_issue_done_ratio_set_to_issue_field
99 def test_update_issue_done_ratio_with_issue_done_ratio_set_to_issue_field
86 with_settings :issue_done_ratio => 'issue_field' do
100 with_settings :issue_done_ratio => 'issue_field' do
87 post :update_issue_done_ratio
101 post :update_issue_done_ratio
88 assert_match /not updated/, flash[:error].to_s
102 assert_match /not updated/, flash[:error].to_s
89 assert_redirected_to '/issue_statuses'
103 assert_redirected_to '/issue_statuses'
90 end
104 end
91 end
105 end
92
106
93 def test_update_issue_done_ratio_with_issue_done_ratio_set_to_issue_status
107 def test_update_issue_done_ratio_with_issue_done_ratio_set_to_issue_status
94 with_settings :issue_done_ratio => 'issue_status' do
108 with_settings :issue_done_ratio => 'issue_status' do
95 post :update_issue_done_ratio
109 post :update_issue_done_ratio
96 assert_match /Issue done ratios updated/, flash[:notice].to_s
110 assert_match /Issue done ratios updated/, flash[:notice].to_s
97 assert_redirected_to '/issue_statuses'
111 assert_redirected_to '/issue_statuses'
98 end
112 end
99 end
113 end
100 end
114 end
General Comments 0
You need to be logged in to leave comments. Login now