comparison test/functional/repositories_mercurial_controller_test.rb @ 524:1248a47e81b3 feature_36

Merge from branch "luisf"
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 25 Jul 2011 14:39:38 +0100
parents 851510f1b535
children 5e80956cc792
comparison
equal deleted inserted replaced
519:3be6bc3c2a17 524:1248a47e81b3
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2008 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.dirname(__FILE__) + '/../test_helper' 18 require File.expand_path('../../test_helper', __FILE__)
19 require 'repositories_controller' 19 require 'repositories_controller'
20 20
21 # Re-raise errors caught by the controller. 21 # Re-raise errors caught by the controller.
22 class RepositoriesController; def rescue_action(e) raise e end; end 22 class RepositoriesController; def rescue_action(e) raise e end; end
23 23
24 class RepositoriesMercurialControllerTest < ActionController::TestCase 24 class RepositoriesMercurialControllerTest < ActionController::TestCase
25 fixtures :projects, :users, :roles, :members, :member_roles, :repositories, :enabled_modules 25 fixtures :projects, :users, :roles, :members, :member_roles, :repositories, :enabled_modules
26 26
27 # No '..' in the repository path 27 # No '..' in the repository path
28 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository' 28 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') +
29 '/tmp/test/mercurial_repository'
30 CHAR_1_HEX = "\xc3\x9c"
31 PRJ_ID = 3
32
33 ruby19_non_utf8_pass =
34 (RUBY_VERSION >= '1.9' && Encoding.default_external.to_s != 'UTF-8')
29 35
30 def setup 36 def setup
31 @controller = RepositoriesController.new 37 @controller = RepositoriesController.new
32 @request = ActionController::TestRequest.new 38 @request = ActionController::TestRequest.new
33 @response = ActionController::TestResponse.new 39 @response = ActionController::TestResponse.new
34 User.current = nil 40 User.current = nil
35 Repository::Mercurial.create(:project => Project.find(3), :url => REPOSITORY_PATH) 41 @project = Project.find(PRJ_ID)
42 @repository = Repository::Mercurial.create(
43 :project => @project,
44 :url => REPOSITORY_PATH,
45 :path_encoding => 'ISO-8859-1'
46 )
47 assert @repository
48 @diff_c_support = true
49 @char_1 = CHAR_1_HEX.dup
50 @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
51 @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
52 @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
53 if @char_1.respond_to?(:force_encoding)
54 @char_1.force_encoding('UTF-8')
55 @tag_char_1.force_encoding('UTF-8')
56 @branch_char_0.force_encoding('UTF-8')
57 @branch_char_1.force_encoding('UTF-8')
58 end
36 end 59 end
37 60
38 if File.directory?(REPOSITORY_PATH) 61 if ruby19_non_utf8_pass
39 def test_show 62 puts "TODO: Mercurial functional test fails in Ruby 1.9 " +
40 get :show, :id => 3 63 "and Encoding.default_external is not UTF-8. " +
64 "Current value is '#{Encoding.default_external.to_s}'"
65 def test_fake; assert true end
66 elsif File.directory?(REPOSITORY_PATH)
67 def test_show_root
68 @repository.fetch_changesets
69 @repository.reload
70 get :show, :id => PRJ_ID
41 assert_response :success 71 assert_response :success
42 assert_template 'show' 72 assert_template 'show'
43 assert_not_nil assigns(:entries) 73 assert_not_nil assigns(:entries)
74 assert_equal 4, assigns(:entries).size
75 assert assigns(:entries).detect {|e| e.name == 'images' && e.kind == 'dir'}
76 assert assigns(:entries).detect {|e| e.name == 'sources' && e.kind == 'dir'}
77 assert assigns(:entries).detect {|e| e.name == 'README' && e.kind == 'file'}
44 assert_not_nil assigns(:changesets) 78 assert_not_nil assigns(:changesets)
45 end 79 assigns(:changesets).size > 0
46 80 end
47 def test_show_root 81
48 get :show, :id => 3
49 assert_response :success
50 assert_template 'show'
51 assert_not_nil assigns(:entries)
52 assert_equal 3, assigns(:entries).size
53 assert assigns(:entries).detect {|e| e.name == 'images' && e.kind == 'dir'}
54 assert assigns(:entries).detect {|e| e.name == 'sources' && e.kind == 'dir'}
55 assert assigns(:entries).detect {|e| e.name == 'README' && e.kind == 'file'}
56 end
57
58 def test_show_directory 82 def test_show_directory
59 get :show, :id => 3, :path => ['images'] 83 @repository.fetch_changesets
84 @repository.reload
85 get :show, :id => PRJ_ID, :path => ['images']
60 assert_response :success 86 assert_response :success
61 assert_template 'show' 87 assert_template 'show'
62 assert_not_nil assigns(:entries) 88 assert_not_nil assigns(:entries)
63 assert_equal ['delete.png', 'edit.png'], assigns(:entries).collect(&:name) 89 assert_equal ['delete.png', 'edit.png'], assigns(:entries).collect(&:name)
64 entry = assigns(:entries).detect {|e| e.name == 'edit.png'} 90 entry = assigns(:entries).detect {|e| e.name == 'edit.png'}
65 assert_not_nil entry 91 assert_not_nil entry
66 assert_equal 'file', entry.kind 92 assert_equal 'file', entry.kind
67 assert_equal 'images/edit.png', entry.path 93 assert_equal 'images/edit.png', entry.path
68 end 94 assert_not_nil assigns(:changesets)
69 95 assigns(:changesets).size > 0
96 end
97
70 def test_show_at_given_revision 98 def test_show_at_given_revision
71 get :show, :id => 3, :path => ['images'], :rev => 0 99 @repository.fetch_changesets
72 assert_response :success 100 @repository.reload
73 assert_template 'show' 101 [0, '0', '0885933ad4f6'].each do |r1|
74 assert_not_nil assigns(:entries) 102 get :show, :id => PRJ_ID, :path => ['images'], :rev => r1
75 assert_equal ['delete.png'], assigns(:entries).collect(&:name) 103 assert_response :success
76 end 104 assert_template 'show'
77 105 assert_not_nil assigns(:entries)
106 assert_equal ['delete.png'], assigns(:entries).collect(&:name)
107 assert_not_nil assigns(:changesets)
108 assigns(:changesets).size > 0
109 end
110 end
111
112 def test_show_directory_sql_escape_percent
113 @repository.fetch_changesets
114 @repository.reload
115 [13, '13', '3a330eb32958'].each do |r1|
116 get :show, :id => PRJ_ID, :path => ['sql_escape', 'percent%dir'],
117 :rev => r1
118 assert_response :success
119 assert_template 'show'
120
121 assert_not_nil assigns(:entries)
122 assert_equal ['percent%file1.txt', 'percentfile1.txt'],
123 assigns(:entries).collect(&:name)
124 changesets = assigns(:changesets)
125 assert_not_nil changesets
126 assigns(:changesets).size > 0
127 assert_equal %w(13 11 10 9), changesets.collect(&:revision)
128 end
129 end
130
131 def test_show_directory_latin_1_path
132 @repository.fetch_changesets
133 @repository.reload
134 [21, '21', 'adf805632193'].each do |r1|
135 get :show, :id => PRJ_ID, :path => ['latin-1-dir'], :rev => r1
136 assert_response :success
137 assert_template 'show'
138
139 assert_not_nil assigns(:entries)
140 assert_equal ["make-latin-1-file.rb",
141 "test-#{@char_1}-1.txt",
142 "test-#{@char_1}-2.txt",
143 "test-#{@char_1}.txt"], assigns(:entries).collect(&:name)
144 changesets = assigns(:changesets)
145 assert_not_nil changesets
146 assert_equal %w(21 20 19 18 17), changesets.collect(&:revision)
147 end
148 end
149
150 def test_show_branch
151 @repository.fetch_changesets
152 @repository.reload
153 [
154 'default',
155 @branch_char_1,
156 'branch (1)[2]&,%.-3_4',
157 @branch_char_0,
158 'test_branch.latin-1',
159 'test-branch-00',
160 ].each do |bra|
161 get :show, :id => PRJ_ID, :rev => bra
162 assert_response :success
163 assert_template 'show'
164 assert_not_nil assigns(:entries)
165 assert assigns(:entries).size > 0
166 assert_not_nil assigns(:changesets)
167 assigns(:changesets).size > 0
168 end
169 end
170
171 def test_show_tag
172 @repository.fetch_changesets
173 @repository.reload
174 [
175 @tag_char_1,
176 'tag_test.00',
177 'tag-init-revision'
178 ].each do |tag|
179 get :show, :id => PRJ_ID, :rev => tag
180 assert_response :success
181 assert_template 'show'
182 assert_not_nil assigns(:entries)
183 assert assigns(:entries).size > 0
184 assert_not_nil assigns(:changesets)
185 assigns(:changesets).size > 0
186 end
187 end
188
78 def test_changes 189 def test_changes
79 get :changes, :id => 3, :path => ['images', 'edit.png'] 190 get :changes, :id => PRJ_ID, :path => ['images', 'edit.png']
80 assert_response :success 191 assert_response :success
81 assert_template 'changes' 192 assert_template 'changes'
82 assert_tag :tag => 'h2', :content => 'edit.png' 193 assert_tag :tag => 'h2', :content => 'edit.png'
83 end 194 end
84 195
85 def test_entry_show 196 def test_entry_show
86 get :entry, :id => 3, :path => ['sources', 'watchers_controller.rb'] 197 get :entry, :id => PRJ_ID, :path => ['sources', 'watchers_controller.rb']
87 assert_response :success 198 assert_response :success
88 assert_template 'entry' 199 assert_template 'entry'
89 # Line 19 200 # Line 10
90 assert_tag :tag => 'th', 201 assert_tag :tag => 'th',
91 :content => /10/, 202 :content => '10',
92 :attributes => { :class => /line-num/ }, 203 :attributes => { :class => 'line-num' },
93 :sibling => { :tag => 'td', :content => /WITHOUT ANY WARRANTY/ } 204 :sibling => { :tag => 'td', :content => /WITHOUT ANY WARRANTY/ }
94 end 205 end
95 206
207 def test_entry_show_latin_1_path
208 [21, '21', 'adf805632193'].each do |r1|
209 get :entry, :id => PRJ_ID,
210 :path => ['latin-1-dir', "test-#{@char_1}-2.txt"], :rev => r1
211 assert_response :success
212 assert_template 'entry'
213 assert_tag :tag => 'th',
214 :content => '1',
215 :attributes => { :class => 'line-num' },
216 :sibling => { :tag => 'td',
217 :content => /Mercurial is a distributed version control system/ }
218 end
219 end
220
221 def test_entry_show_latin_1_contents
222 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
223 [27, '27', '7bbf4c738e71'].each do |r1|
224 get :entry, :id => PRJ_ID,
225 :path => ['latin-1-dir', "test-#{@char_1}.txt"], :rev => r1
226 assert_response :success
227 assert_template 'entry'
228 assert_tag :tag => 'th',
229 :content => '1',
230 :attributes => { :class => 'line-num' },
231 :sibling => { :tag => 'td',
232 :content => /test-#{@char_1}.txt/ }
233 end
234 end
235 end
236
96 def test_entry_download 237 def test_entry_download
97 get :entry, :id => 3, :path => ['sources', 'watchers_controller.rb'], :format => 'raw' 238 get :entry, :id => PRJ_ID,
239 :path => ['sources', 'watchers_controller.rb'], :format => 'raw'
98 assert_response :success 240 assert_response :success
99 # File content 241 # File content
100 assert @response.body.include?('WITHOUT ANY WARRANTY') 242 assert @response.body.include?('WITHOUT ANY WARRANTY')
101 end 243 end
102 244
245 def test_entry_binary_force_download
246 get :entry, :id => PRJ_ID, :rev => 1, :path => ['images', 'edit.png']
247 assert_response :success
248 assert_equal 'image/png', @response.content_type
249 end
250
103 def test_directory_entry 251 def test_directory_entry
104 get :entry, :id => 3, :path => ['sources'] 252 get :entry, :id => PRJ_ID, :path => ['sources']
105 assert_response :success 253 assert_response :success
106 assert_template 'show' 254 assert_template 'show'
107 assert_not_nil assigns(:entry) 255 assert_not_nil assigns(:entry)
108 assert_equal 'sources', assigns(:entry).name 256 assert_equal 'sources', assigns(:entry).name
109 end 257 end
110 258
111 def test_diff 259 def test_diff
112 # Full diff of changeset 4 260 @repository.fetch_changesets
113 get :diff, :id => 3, :rev => 4 261 @repository.reload
114 assert_response :success 262 [4, '4', 'def6d2f1254a'].each do |r1|
115 assert_template 'diff' 263 # Full diff of changeset 4
116 # Line 22 removed 264 get :diff, :id => PRJ_ID, :rev => r1
265 assert_response :success
266 assert_template 'diff'
267 if @diff_c_support
268 # Line 22 removed
269 assert_tag :tag => 'th',
270 :content => '22',
271 :sibling => { :tag => 'td',
272 :attributes => { :class => /diff_out/ },
273 :content => /def remove/ }
274 assert_tag :tag => 'h2', :content => /4:def6d2f1254a/
275 end
276 end
277 end
278
279 def test_diff_two_revs
280 @repository.fetch_changesets
281 @repository.reload
282 [2, '400bb8672109', '400', 400].each do |r1|
283 [4, 'def6d2f1254a'].each do |r2|
284 get :diff, :id => PRJ_ID, :rev => r1,
285 :rev_to => r2
286 assert_response :success
287 assert_template 'diff'
288
289 diff = assigns(:diff)
290 assert_not_nil diff
291 assert_tag :tag => 'h2', :content => /4:def6d2f1254a 2:400bb8672109/
292 end
293 end
294 end
295
296 def test_diff_latin_1_path
297 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
298 [21, 'adf805632193'].each do |r1|
299 get :diff, :id => PRJ_ID, :rev => r1
300 assert_response :success
301 assert_template 'diff'
302 assert_tag :tag => 'thead',
303 :descendant => {
304 :tag => 'th',
305 :attributes => { :class => 'filename' } ,
306 :content => /latin-1-dir\/test-#{@char_1}-2.txt/ ,
307 },
308 :sibling => {
309 :tag => 'tbody',
310 :descendant => {
311 :tag => 'td',
312 :attributes => { :class => /diff_in/ },
313 :content => /It is written in Python/
314 }
315 }
316 end
317 end
318 end
319
320 def test_annotate
321 get :annotate, :id => PRJ_ID, :path => ['sources', 'watchers_controller.rb']
322 assert_response :success
323 assert_template 'annotate'
324 # Line 23, revision 4:def6d2f1254a
117 assert_tag :tag => 'th', 325 assert_tag :tag => 'th',
118 :content => /22/, 326 :content => '23',
119 :sibling => { :tag => 'td', 327 :attributes => { :class => 'line-num' },
120 :attributes => { :class => /diff_out/ }, 328 :sibling =>
121 :content => /def remove/ } 329 {
122 end 330 :tag => 'td',
123 331 :attributes => { :class => 'revision' },
124 def test_annotate 332 :child => { :tag => 'a', :content => '4:def6d2f1254a' }
125 get :annotate, :id => 3, :path => ['sources', 'watchers_controller.rb'] 333 }
126 assert_response :success 334 assert_tag :tag => 'th',
127 assert_template 'annotate' 335 :content => '23',
128 # Line 23, revision 4 336 :attributes => { :class => 'line-num' },
129 assert_tag :tag => 'th', :content => /23/, 337 :sibling =>
130 :sibling => { :tag => 'td', :child => { :tag => 'a', :content => /4/ } }, 338 {
131 :sibling => { :tag => 'td', :content => /jsmith/ }, 339 :tag => 'td' ,
340 :content => 'jsmith' ,
341 :attributes => { :class => 'author' },
342 }
343 assert_tag :tag => 'th',
344 :content => '23',
345 :attributes => { :class => 'line-num' },
132 :sibling => { :tag => 'td', :content => /watcher =/ } 346 :sibling => { :tag => 'td', :content => /watcher =/ }
347 end
348
349 def test_annotate_not_in_tip
350 @repository.fetch_changesets
351 @repository.reload
352 assert @repository.changesets.size > 0
353
354 get :annotate, :id => PRJ_ID,
355 :path => ['sources', 'welcome_controller.rb']
356 assert_response 404
357 assert_error_tag :content => /was not found/
358 end
359
360 def test_annotate_at_given_revision
361 @repository.fetch_changesets
362 @repository.reload
363 [2, '400bb8672109', '400', 400].each do |r1|
364 get :annotate, :id => PRJ_ID, :rev => r1,
365 :path => ['sources', 'watchers_controller.rb']
366 assert_response :success
367 assert_template 'annotate'
368 assert_tag :tag => 'h2', :content => /@ 2:400bb8672109/
369 end
370 end
371
372 def test_annotate_latin_1_path
373 [21, '21', 'adf805632193'].each do |r1|
374 get :annotate, :id => PRJ_ID,
375 :path => ['latin-1-dir', "test-#{@char_1}-2.txt"], :rev => r1
376 assert_response :success
377 assert_template 'annotate'
378 assert_tag :tag => 'th',
379 :content => '1',
380 :attributes => { :class => 'line-num' },
381 :sibling =>
382 {
383 :tag => 'td',
384 :attributes => { :class => 'revision' },
385 :child => { :tag => 'a', :content => '20:709858aafd1b' }
386 }
387 assert_tag :tag => 'th',
388 :content => '1',
389 :attributes => { :class => 'line-num' },
390 :sibling =>
391 {
392 :tag => 'td' ,
393 :content => 'jsmith' ,
394 :attributes => { :class => 'author' },
395 }
396 assert_tag :tag => 'th',
397 :content => '1',
398 :attributes => { :class => 'line-num' },
399 :sibling => { :tag => 'td',
400 :content => /Mercurial is a distributed version control system/ }
401
402 end
403 end
404
405 def test_annotate_latin_1_contents
406 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
407 [27, '7bbf4c738e71'].each do |r1|
408 get :annotate, :id => PRJ_ID,
409 :path => ['latin-1-dir', "test-#{@char_1}.txt"], :rev => r1
410 assert_tag :tag => 'th',
411 :content => '1',
412 :attributes => { :class => 'line-num' },
413 :sibling => { :tag => 'td',
414 :content => /test-#{@char_1}.txt/ }
415 end
416 end
417 end
418
419 def test_empty_revision
420 @repository.fetch_changesets
421 @repository.reload
422 ['', ' ', nil].each do |r|
423 get :revision, :id => PRJ_ID, :rev => r
424 assert_response 404
425 assert_error_tag :content => /was not found/
426 end
427 end
428
429 def test_destroy_valid_repository
430 @request.session[:user_id] = 1 # admin
431 @repository.fetch_changesets
432 @repository.reload
433 assert @repository.changesets.count > 0
434
435 get :destroy, :id => PRJ_ID
436 assert_response 302
437 @project.reload
438 assert_nil @project.repository
439 end
440
441 def test_destroy_invalid_repository
442 @request.session[:user_id] = 1 # admin
443 @repository.fetch_changesets
444 @repository.reload
445 assert @repository.changesets.count > 0
446
447 get :destroy, :id => PRJ_ID
448 assert_response 302
449 @project.reload
450 assert_nil @project.repository
451
452 @repository = Repository::Mercurial.create(
453 :project => Project.find(PRJ_ID),
454 :url => "/invalid",
455 :path_encoding => 'ISO-8859-1'
456 )
457 assert @repository
458 @repository.fetch_changesets
459 @repository.reload
460 assert_equal 0, @repository.changesets.count
461
462 get :destroy, :id => PRJ_ID
463 assert_response 302
464 @project.reload
465 assert_nil @project.repository
133 end 466 end
134 else 467 else
135 puts "Mercurial test repository NOT FOUND. Skipping functional tests !!!" 468 puts "Mercurial test repository NOT FOUND. Skipping functional tests !!!"
136 def test_fake; assert true end 469 def test_fake; assert true end
137 end 470 end