;]
[askyou.git] / test / unit / repository_test.rb
blob6512c067aaf4d09d0057b09cfc9f144de52c6d9b
1 # redMine - project management software
2 # Copyright (C) 2006-2007  Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 require File.dirname(__FILE__) + '/../test_helper'
20 class RepositoryTest < ActiveSupport::TestCase
21   fixtures :projects,
22            :trackers,
23            :projects_trackers,
24            :repositories,
25            :issues,
26            :issue_statuses,
27            :changesets,
28            :changes,
29            :users,
30            :enumerations
31   
32   def setup
33     @repository = Project.find(1).repository
34   end
35   
36   def test_create
37     repository = Repository::Subversion.new(:project => Project.find(3))
38     assert !repository.save
39   
40     repository.url = "svn://localhost"
41     assert repository.save
42     repository.reload
43     
44     project = Project.find(3)
45     assert_equal repository, project.repository
46   end
47   
48   def test_destroy
49     changesets = Changeset.count(:all, :conditions => "repository_id = 10")
50     changes = Change.count(:all, :conditions => "repository_id = 10", :include => :changeset)
51     assert_difference 'Changeset.count', -changesets do
52       assert_difference 'Change.count', -changes do
53         Repository.find(10).destroy
54       end
55     end
56   end
57   
58   def test_should_not_create_with_disabled_scm
59     # disable Subversion
60     Setting.enabled_scm = ['Darcs', 'Git']
61     repository = Repository::Subversion.new(:project => Project.find(3), :url => "svn://localhost")
62     assert !repository.save
63     assert_equal I18n.translate('activerecord.errors.messages.invalid'), repository.errors.on(:type)
64     # re-enable Subversion for following tests
65     Setting.delete_all
66   end
67   
68   def test_scan_changesets_for_issue_ids
69     Setting.default_language = 'en'
70     
71     # choosing a status to apply to fix issues
72     Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
73     Setting.commit_fix_done_ratio = "90"
74     Setting.commit_ref_keywords = 'refs , references, IssueID'
75     Setting.commit_fix_keywords = 'fixes , closes'
76     Setting.default_language = 'en'
77     ActionMailer::Base.deliveries.clear
78     
79     # make sure issue 1 is not already closed
80     fixed_issue = Issue.find(1)
81     assert !fixed_issue.status.is_closed?
82     old_status = fixed_issue.status
83         
84     Repository.scan_changesets_for_issue_ids
85     assert_equal [101, 102], Issue.find(3).changeset_ids
86     
87     # fixed issues
88     fixed_issue.reload
89     assert fixed_issue.status.is_closed?
90     assert_equal 90, fixed_issue.done_ratio
91     assert_equal [101], fixed_issue.changeset_ids
92     
93     # issue change
94     journal = fixed_issue.journals.find(:first, :order => 'created_on desc')
95     assert_equal User.find_by_login('dlopper'), journal.user
96     assert_equal 'Applied in changeset r2.', journal.notes
97     
98     # 2 email notifications
99     assert_equal 2, ActionMailer::Base.deliveries.size
100     mail = ActionMailer::Base.deliveries.first
101     assert_kind_of TMail::Mail, mail
102     assert mail.subject.starts_with?("[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
103     assert mail.body.include?("Status changed from #{old_status} to #{fixed_issue.status}")
104     
105     # ignoring commits referencing an issue of another project
106     assert_equal [], Issue.find(4).changesets
107   end
108   
109   def test_for_changeset_comments_strip
110     repository = Repository::Mercurial.create( :project => Project.find( 4 ), :url => '/foo/bar/baz' )
111     comment = <<-COMMENT
112     This is a loooooooooooooooooooooooooooong comment                                                   
113                                                                                                        
114                                                                                             
115     COMMENT
116     changeset = Changeset.new(
117       :comments => comment, :commit_date => Time.now, :revision => 0, :scmid => 'f39b7922fb3c',
118       :committer => 'foo <foo@example.com>', :committed_on => Time.now, :repository => repository )
119     assert( changeset.save )
120     assert_not_equal( comment, changeset.comments )
121     assert_equal( 'This is a loooooooooooooooooooooooooooong comment', changeset.comments )
122   end
123   
124   def test_for_urls_strip
125     repository = Repository::Cvs.create(:project => Project.find(4), :url => ' :pserver:login:password@host:/path/to/the/repository',
126                                                                      :root_url => 'foo  ')
127     assert repository.save
128     repository.reload
129     assert_equal ':pserver:login:password@host:/path/to/the/repository', repository.url
130     assert_equal 'foo', repository.root_url
131   end
132   
133   def test_manual_user_mapping
134     assert_no_difference "Changeset.count(:conditions => 'user_id <> 2')" do
135       c = Changeset.create!(:repository => @repository, :committer => 'foo', :committed_on => Time.now, :revision => 100, :comments => 'Committed by foo.')
136       assert_nil c.user
137       @repository.committer_ids = {'foo' => '2'}
138       assert_equal User.find(2), c.reload.user
139       # committer is now mapped
140       c = Changeset.create!(:repository => @repository, :committer => 'foo', :committed_on => Time.now, :revision => 101, :comments => 'Another commit by foo.')
141       assert_equal User.find(2), c.user
142     end
143   end
144   
145   def test_auto_user_mapping_by_username
146     c = Changeset.create!(:repository => @repository, :committer => 'jsmith', :committed_on => Time.now, :revision => 100, :comments => 'Committed by john.')
147     assert_equal User.find(2), c.user
148   end
149   
150   def test_auto_user_mapping_by_email
151     c = Changeset.create!(:repository => @repository, :committer => 'john <jsmith@somenet.foo>', :committed_on => Time.now, :revision => 100, :comments => 'Committed by john.')
152     assert_equal User.find(2), c.user
153   end