annotate test/unit/repository_test.rb @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents dffacf8a6908
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@441 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@441 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class RepositoryTest < ActiveSupport::TestCase
Chris@0 21 fixtures :projects,
Chris@0 22 :trackers,
Chris@0 23 :projects_trackers,
Chris@119 24 :enabled_modules,
Chris@0 25 :repositories,
Chris@0 26 :issues,
Chris@0 27 :issue_statuses,
Chris@119 28 :issue_categories,
Chris@0 29 :changesets,
Chris@0 30 :changes,
Chris@0 31 :users,
Chris@119 32 :members,
Chris@119 33 :member_roles,
Chris@119 34 :roles,
Chris@0 35 :enumerations
Chris@441 36
Chris@1115 37 include Redmine::I18n
Chris@1115 38
Chris@0 39 def setup
Chris@0 40 @repository = Project.find(1).repository
Chris@0 41 end
Chris@441 42
Chris@1115 43 def test_blank_log_encoding_error_message
Chris@1115 44 set_language_if_valid 'en'
Chris@1115 45 repo = Repository::Bazaar.new(
Chris@1115 46 :project => Project.find(3),
Chris@1115 47 :url => "/test",
Chris@1115 48 :log_encoding => ''
Chris@1115 49 )
Chris@1115 50 assert !repo.save
Chris@1115 51 assert_include "Commit messages encoding can't be blank",
Chris@1115 52 repo.errors.full_messages
Chris@1115 53 end
Chris@1115 54
Chris@1115 55 def test_blank_log_encoding_error_message_fr
Chris@1115 56 set_language_if_valid 'fr'
Chris@1115 57 str = "Encodage des messages de commit doit \xc3\xaatre renseign\xc3\xa9(e)"
Chris@1115 58 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
Chris@1115 59 repo = Repository::Bazaar.new(
Chris@1115 60 :project => Project.find(3),
Chris@1115 61 :url => "/test"
Chris@1115 62 )
Chris@1115 63 assert !repo.save
Chris@1115 64 assert_include str, repo.errors.full_messages
Chris@1115 65 end
Chris@1115 66
Chris@0 67 def test_create
Chris@0 68 repository = Repository::Subversion.new(:project => Project.find(3))
Chris@0 69 assert !repository.save
Chris@441 70
Chris@0 71 repository.url = "svn://localhost"
Chris@0 72 assert repository.save
Chris@0 73 repository.reload
Chris@441 74
Chris@0 75 project = Project.find(3)
Chris@0 76 assert_equal repository, project.repository
Chris@0 77 end
Chris@441 78
Chris@1115 79 def test_first_repository_should_be_set_as_default
Chris@1115 80 repository1 = Repository::Subversion.new(
Chris@1115 81 :project => Project.find(3),
Chris@1115 82 :identifier => 'svn1',
Chris@1115 83 :url => 'file:///svn1'
Chris@1115 84 )
Chris@1115 85 assert repository1.save
Chris@1115 86 assert repository1.is_default?
Chris@1115 87
Chris@1115 88 repository2 = Repository::Subversion.new(
Chris@1115 89 :project => Project.find(3),
Chris@1115 90 :identifier => 'svn2',
Chris@1115 91 :url => 'file:///svn2'
Chris@1115 92 )
Chris@1115 93 assert repository2.save
Chris@1115 94 assert !repository2.is_default?
Chris@1115 95
Chris@1115 96 assert_equal repository1, Project.find(3).repository
Chris@1115 97 assert_equal [repository1, repository2], Project.find(3).repositories.sort
Chris@1115 98 end
Chris@1115 99
Chris@1517 100 def test_default_repository_should_be_one
Chris@1517 101 assert_equal 0, Project.find(3).repositories.count
Chris@1517 102 repository1 = Repository::Subversion.new(
Chris@1517 103 :project => Project.find(3),
Chris@1517 104 :identifier => 'svn1',
Chris@1517 105 :url => 'file:///svn1'
Chris@1517 106 )
Chris@1517 107 assert repository1.save
Chris@1517 108 assert repository1.is_default?
Chris@1517 109
Chris@1517 110 repository2 = Repository::Subversion.new(
Chris@1517 111 :project => Project.find(3),
Chris@1517 112 :identifier => 'svn2',
Chris@1517 113 :url => 'file:///svn2',
Chris@1517 114 :is_default => true
Chris@1517 115 )
Chris@1517 116 assert repository2.save
Chris@1517 117 assert repository2.is_default?
Chris@1517 118 repository1.reload
Chris@1517 119 assert !repository1.is_default?
Chris@1517 120
Chris@1517 121 assert_equal repository2, Project.find(3).repository
Chris@1517 122 assert_equal [repository2, repository1], Project.find(3).repositories.sort
Chris@1517 123 end
Chris@1517 124
Chris@1115 125 def test_identifier_should_accept_letters_digits_dashes_and_underscores
Chris@1115 126 r = Repository::Subversion.new(
Chris@1115 127 :project_id => 3,
Chris@1115 128 :identifier => 'svn-123_45',
Chris@1115 129 :url => 'file:///svn'
Chris@1115 130 )
Chris@1115 131 assert r.save
Chris@1115 132 end
Chris@1517 133
Chris@1115 134 def test_identifier_should_not_be_frozen_for_a_new_repository
Chris@1115 135 assert_equal false, Repository.new.identifier_frozen?
Chris@1115 136 end
Chris@1115 137
Chris@1115 138 def test_identifier_should_not_be_frozen_for_a_saved_repository_with_blank_identifier
Chris@1517 139 Repository.where(:id => 10).update_all(["identifier = ''"])
Chris@1115 140 assert_equal false, Repository.find(10).identifier_frozen?
Chris@1115 141 end
Chris@1115 142
Chris@1115 143 def test_identifier_should_be_frozen_for_a_saved_repository_with_valid_identifier
Chris@1517 144 Repository.where(:id => 10).update_all(["identifier = 'abc123'"])
Chris@1115 145 assert_equal true, Repository.find(10).identifier_frozen?
Chris@1115 146 end
Chris@1115 147
Chris@1115 148 def test_identifier_should_not_accept_change_if_frozen
Chris@1115 149 r = Repository.new(:identifier => 'foo')
Chris@1115 150 r.stubs(:identifier_frozen?).returns(true)
Chris@1115 151
Chris@1115 152 r.identifier = 'bar'
Chris@1115 153 assert_equal 'foo', r.identifier
Chris@1115 154 end
Chris@1115 155
Chris@1115 156 def test_identifier_should_accept_change_if_not_frozen
Chris@1115 157 r = Repository.new(:identifier => 'foo')
Chris@1115 158 r.stubs(:identifier_frozen?).returns(false)
Chris@1115 159
Chris@1115 160 r.identifier = 'bar'
Chris@1115 161 assert_equal 'bar', r.identifier
Chris@1115 162 end
Chris@1115 163
Chris@0 164 def test_destroy
Chris@1115 165 repository = Repository.find(10)
Chris@1115 166 changesets = repository.changesets.count
Chris@1115 167 changes = repository.filechanges.count
Chris@1115 168
Chris@0 169 assert_difference 'Changeset.count', -changesets do
Chris@0 170 assert_difference 'Change.count', -changes do
Chris@0 171 Repository.find(10).destroy
Chris@0 172 end
Chris@0 173 end
Chris@0 174 end
Chris@441 175
Chris@1115 176 def test_destroy_should_delete_parents_associations
Chris@1115 177 changeset = Changeset.find(102)
Chris@1517 178 changeset.parents = Changeset.where(:id => [100, 101]).all
Chris@1517 179 assert_difference 'Changeset.connection.select_all("select * from changeset_parents").count', -2 do
Chris@1115 180 Repository.find(10).destroy
Chris@1115 181 end
Chris@1115 182 end
Chris@1115 183
Chris@1115 184 def test_destroy_should_delete_issues_associations
Chris@1115 185 changeset = Changeset.find(102)
Chris@1517 186 changeset.issues = Issue.where(:id => [1, 2]).all
Chris@1517 187 assert_difference 'Changeset.connection.select_all("select * from changesets_issues").count', -2 do
Chris@1115 188 Repository.find(10).destroy
Chris@1115 189 end
Chris@1115 190 end
Chris@1115 191
Chris@0 192 def test_should_not_create_with_disabled_scm
Chris@0 193 # disable Subversion
Chris@128 194 with_settings :enabled_scm => ['Darcs', 'Git'] do
Chris@441 195 repository = Repository::Subversion.new(
Chris@441 196 :project => Project.find(3), :url => "svn://localhost")
Chris@128 197 assert !repository.save
Chris@1115 198 assert_include I18n.translate('activerecord.errors.messages.invalid'),
Chris@1115 199 repository.errors[:type]
Chris@128 200 end
Chris@0 201 end
Chris@441 202
Chris@0 203 def test_scan_changesets_for_issue_ids
Chris@0 204 Setting.default_language = 'en'
Chris@0 205 Setting.commit_ref_keywords = 'refs , references, IssueID'
Chris@1464 206 Setting.commit_update_keywords = [
Chris@1517 207 {'keywords' => 'fixes , closes',
Chris@1517 208 'status_id' => IssueStatus.where(:is_closed => true).first.id,
Chris@1517 209 'done_ratio' => '90'}
Chris@1464 210 ]
Chris@0 211 Setting.default_language = 'en'
Chris@0 212 ActionMailer::Base.deliveries.clear
Chris@441 213
Chris@0 214 # make sure issue 1 is not already closed
Chris@0 215 fixed_issue = Issue.find(1)
Chris@0 216 assert !fixed_issue.status.is_closed?
Chris@0 217 old_status = fixed_issue.status
Chris@441 218
Chris@1115 219 with_settings :notified_events => %w(issue_added issue_updated) do
Chris@1115 220 Repository.scan_changesets_for_issue_ids
Chris@1115 221 end
Chris@0 222 assert_equal [101, 102], Issue.find(3).changeset_ids
Chris@441 223
Chris@0 224 # fixed issues
Chris@0 225 fixed_issue.reload
Chris@0 226 assert fixed_issue.status.is_closed?
Chris@0 227 assert_equal 90, fixed_issue.done_ratio
Chris@0 228 assert_equal [101], fixed_issue.changeset_ids
Chris@441 229
Chris@0 230 # issue change
Chris@1464 231 journal = fixed_issue.journals.reorder('created_on desc').first
Chris@0 232 assert_equal User.find_by_login('dlopper'), journal.user
Chris@0 233 assert_equal 'Applied in changeset r2.', journal.notes
Chris@441 234
Chris@0 235 # 2 email notifications
Chris@0 236 assert_equal 2, ActionMailer::Base.deliveries.size
Chris@0 237 mail = ActionMailer::Base.deliveries.first
Chris@1115 238 assert_not_nil mail
Chris@441 239 assert mail.subject.starts_with?(
Chris@441 240 "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
Chris@1115 241 assert_mail_body_match(
Chris@1115 242 "Status changed from #{old_status} to #{fixed_issue.status}", mail)
Chris@441 243
Chris@0 244 # ignoring commits referencing an issue of another project
Chris@0 245 assert_equal [], Issue.find(4).changesets
Chris@0 246 end
Chris@441 247
Chris@0 248 def test_for_changeset_comments_strip
Chris@441 249 repository = Repository::Mercurial.create(
Chris@441 250 :project => Project.find( 4 ),
Chris@441 251 :url => '/foo/bar/baz' )
Chris@0 252 comment = <<-COMMENT
Chris@0 253 This is a loooooooooooooooooooooooooooong comment
Chris@0 254
Chris@0 255
Chris@0 256 COMMENT
Chris@0 257 changeset = Changeset.new(
Chris@441 258 :comments => comment, :commit_date => Time.now,
Chris@441 259 :revision => 0, :scmid => 'f39b7922fb3c',
Chris@441 260 :committer => 'foo <foo@example.com>',
Chris@441 261 :committed_on => Time.now, :repository => repository )
Chris@0 262 assert( changeset.save )
Chris@0 263 assert_not_equal( comment, changeset.comments )
Chris@441 264 assert_equal( 'This is a loooooooooooooooooooooooooooong comment',
Chris@441 265 changeset.comments )
Chris@0 266 end
Chris@245 267
Chris@909 268 def test_for_urls_strip_cvs
Chris@245 269 repository = Repository::Cvs.create(
Chris@245 270 :project => Project.find(4),
Chris@245 271 :url => ' :pserver:login:password@host:/path/to/the/repository',
Chris@245 272 :root_url => 'foo ',
Chris@245 273 :log_encoding => 'UTF-8')
Chris@0 274 assert repository.save
Chris@0 275 repository.reload
Chris@441 276 assert_equal ':pserver:login:password@host:/path/to/the/repository',
Chris@441 277 repository.url
Chris@0 278 assert_equal 'foo', repository.root_url
Chris@0 279 end
Chris@245 280
Chris@909 281 def test_for_urls_strip_subversion
Chris@909 282 repository = Repository::Subversion.create(
Chris@909 283 :project => Project.find(4),
Chris@909 284 :url => ' file:///dummy ')
Chris@909 285 assert repository.save
Chris@909 286 repository.reload
Chris@909 287 assert_equal 'file:///dummy', repository.url
Chris@909 288 end
Chris@909 289
Chris@909 290 def test_for_urls_strip_git
Chris@909 291 repository = Repository::Git.create(
Chris@909 292 :project => Project.find(4),
Chris@909 293 :url => ' c:\dummy ')
Chris@909 294 assert repository.save
Chris@909 295 repository.reload
Chris@909 296 assert_equal 'c:\dummy', repository.url
Chris@909 297 end
Chris@909 298
Chris@0 299 def test_manual_user_mapping
Chris@1464 300 assert_no_difference "Changeset.where('user_id <> 2').count" do
Chris@441 301 c = Changeset.create!(
Chris@441 302 :repository => @repository,
Chris@441 303 :committer => 'foo',
Chris@441 304 :committed_on => Time.now,
Chris@441 305 :revision => 100,
Chris@441 306 :comments => 'Committed by foo.'
Chris@441 307 )
Chris@0 308 assert_nil c.user
Chris@0 309 @repository.committer_ids = {'foo' => '2'}
Chris@0 310 assert_equal User.find(2), c.reload.user
Chris@0 311 # committer is now mapped
Chris@441 312 c = Changeset.create!(
Chris@441 313 :repository => @repository,
Chris@441 314 :committer => 'foo',
Chris@441 315 :committed_on => Time.now,
Chris@441 316 :revision => 101,
Chris@441 317 :comments => 'Another commit by foo.'
Chris@441 318 )
Chris@0 319 assert_equal User.find(2), c.user
Chris@0 320 end
Chris@0 321 end
Chris@441 322
Chris@0 323 def test_auto_user_mapping_by_username
Chris@441 324 c = Changeset.create!(
Chris@441 325 :repository => @repository,
Chris@441 326 :committer => 'jsmith',
Chris@441 327 :committed_on => Time.now,
Chris@441 328 :revision => 100,
Chris@441 329 :comments => 'Committed by john.'
Chris@441 330 )
Chris@0 331 assert_equal User.find(2), c.user
Chris@0 332 end
Chris@441 333
Chris@0 334 def test_auto_user_mapping_by_email
Chris@441 335 c = Changeset.create!(
Chris@441 336 :repository => @repository,
Chris@441 337 :committer => 'john <jsmith@somenet.foo>',
Chris@441 338 :committed_on => Time.now,
Chris@441 339 :revision => 100,
Chris@441 340 :comments => 'Committed by john.'
Chris@441 341 )
Chris@0 342 assert_equal User.find(2), c.user
Chris@0 343 end
Chris@441 344
Chris@441 345 def test_filesystem_avaialbe
Chris@441 346 klass = Repository::Filesystem
Chris@441 347 assert klass.scm_adapter_class
Chris@441 348 assert_equal true, klass.scm_available
Chris@441 349 end
Chris@441 350
Chris@1494 351 def test_extra_info_should_not_return_non_hash_value
Chris@1494 352 repo = Repository.new
Chris@1494 353 repo.extra_info = "foo"
Chris@1494 354 assert_nil repo.extra_info
Chris@1494 355 end
Chris@1494 356
Chris@441 357 def test_merge_extra_info
Chris@441 358 repo = Repository::Subversion.new(:project => Project.find(3))
Chris@441 359 assert !repo.save
Chris@441 360 repo.url = "svn://localhost"
Chris@441 361 assert repo.save
Chris@441 362 repo.reload
Chris@441 363 project = Project.find(3)
Chris@441 364 assert_equal repo, project.repository
Chris@441 365 assert_nil repo.extra_info
Chris@441 366 h1 = {"test_1" => {"test_11" => "test_value_11"}}
Chris@441 367 repo.merge_extra_info(h1)
Chris@441 368 assert_equal h1, repo.extra_info
Chris@441 369 h2 = {"test_2" => {
Chris@441 370 "test_21" => "test_value_21",
Chris@441 371 "test_22" => "test_value_22",
Chris@441 372 }}
Chris@441 373 repo.merge_extra_info(h2)
Chris@441 374 assert_equal (h = {"test_11" => "test_value_11"}),
Chris@441 375 repo.extra_info["test_1"]
Chris@441 376 assert_equal "test_value_21",
Chris@441 377 repo.extra_info["test_2"]["test_21"]
Chris@441 378 h3 = {"test_2" => {
Chris@441 379 "test_23" => "test_value_23",
Chris@441 380 "test_24" => "test_value_24",
Chris@441 381 }}
Chris@441 382 repo.merge_extra_info(h3)
Chris@441 383 assert_equal (h = {"test_11" => "test_value_11"}),
Chris@441 384 repo.extra_info["test_1"]
Chris@441 385 assert_nil repo.extra_info["test_2"]["test_21"]
Chris@441 386 assert_equal "test_value_23",
Chris@441 387 repo.extra_info["test_2"]["test_23"]
Chris@441 388 end
Chris@1115 389
Chris@1115 390 def test_sort_should_not_raise_an_error_with_nil_identifiers
Chris@1115 391 r1 = Repository.new
Chris@1115 392 r2 = Repository.new
Chris@1115 393
Chris@1115 394 assert_nothing_raised do
Chris@1115 395 [r1, r2].sort
Chris@1115 396 end
Chris@1115 397 end
Chris@0 398 end