Refactored the PushEventProcessor to use the Grit classes
[gitorious:franklin-devs-phpbbhub.git] / test / unit / processors / push_event_processor_test.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18
19
20 require File.dirname(__FILE__) + '/../../test_helper'
21
22 class PushEventProcessorTest < ActiveSupport::TestCase
23
24   def setup
25     @processor = PushEventProcessor.new
26   end
27   
28   should "update the last_pushed_at attribute on initial push" do
29     repo = repositories(:johans)
30     repo.update_attribute(:last_pushed_at, nil)
31     @processor.expects(:log_events).returns(true)
32     json = {
33       :gitdir => repo.hashed_path,
34       :username => "johan",
35       :message => '0000000000000000000000000000000000000000 a9934c1d3a56edfa8f45e5f157869874c8dc2c34 refs/heads/master',
36     }.to_json
37     @processor.on_message(json)
38     assert_equal users(:johan), @processor.user
39     assert_equal repo, @processor.repository
40     assert_not_nil repo.reload.last_pushed_at
41     assert repo.last_pushed_at > 5.minutes.ago
42   end
43   
44   should "returns the correct type and identifier for a new tag" do
45     stub_git_show
46     @processor.commit_summary = "0000000000000000000000000000000000000000 a9934c1d3a56edfa8f45e5f157869874c8dc2c34 refs/tags/r1.1"
47     assert_equal :create, @processor.action
48     assert @processor.tag?
49     assert_equal 1, @processor.events.size
50     assert_equal Action::CREATE_TAG, @processor.events.first.event_type
51     assert_equal 'r1.1', @processor.events.first.identifier
52     @processor.expects(:log_event).once
53     @processor.log_events
54   end
55   
56   should "returns the correct type and identifier for a new branch" do
57     stub_git_log_and_user
58     @processor.commit_summary = '0000000000000000000000000000000000000000 a9934c1d3a56edfa8f45e5f157869874c8dc2c34 refs/heads/foo_branch'
59     @processor.repository = Repository.first
60     assert_equal :create, @processor.action
61     assert @processor.head?
62     assert_equal 1, @processor.events.size
63     assert_equal Action::CREATE_BRANCH, @processor.events.first.event_type
64     assert_equal 'foo_branch', @processor.events.first.identifier
65     assert_equal users(:johan), @processor.events.first.user
66     @processor.expects(:log_event).times(1)
67     @processor.log_events    
68   end
69   
70   should "return the correct namespaced identifier for a new branch" do
71      stub_git_log_and_user
72      @processor.commit_summary = '0000000000000000000000000000000000000000 a9934c1d3a56edfa8f45e5f157869874c8dc2c34 refs/heads/foo/bar_branch'
73      assert_equal 'foo/bar_branch', @processor.events.first.identifier
74    end
75   
76   should 'only fetch commits for new branches when the new branch is master' do
77     stub_git_log_and_user
78     @processor.commit_summary = '0000000000000000000000000000000000000000 a9934c1d3a56edfa8f45e5f157869874c8dc2c34 refs/heads/master'
79     @processor.repository = Repository.first
80     assert_equal :create, @processor.action
81     assert @processor.head?
82     assert_equal 4, @processor.events.size
83     assert_equal Action::CREATE_BRANCH, @processor.events.first.event_type
84     assert_equal 'master', @processor.events.first.identifier
85     assert_equal Action::COMMIT, @processor.events[1].event_type
86     @processor.expects(:log_event).times(4)
87     @processor.log_events
88   end
89   
90   should "returns the correct type and a set of events for a commit" do
91     stub_git_log_and_user
92     @processor.commit_summary = "a9934c1d3a56edfa8f45e5f157869874c8dc2c34 33f746e21ef5122511a5a69f381bfdf017f4d66c refs/heads/foo_branch"
93     @processor.repository = Repository.first
94     assert_equal :update, @processor.action
95     assert @processor.head?
96     assert_equal 1, @processor.events.size
97     first_event = @processor.events.first
98     assert_equal Action::PUSH, first_event.event_type
99     assert_equal users(:johan).email, first_event.email
100     assert_match(/foo_branch changed/, first_event.message)
101     assert_equal(3, first_event.commits.size)
102     
103     assert_incremented_by(Event, :count, 4) do
104       @processor.log_events
105     end
106   end
107   
108   should "set the correct user for the commit subevent, if a user exists with that email" do
109     emails(:johans1).update_attribute(:address, "john@nowhere.com")
110     stub_git_log_and_user
111     @processor.commit_summary = "a9934c1d3a56edfa8f45e5f157869874c8dc2c34 33f746e21ef5122511a5a69f381bfdf017f4d66c refs/heads/foo_branch"
112     @processor.repository = Repository.first
113     first_event = @processor.events.first
114     assert_equal users(:johan).email, first_event.email
115     assert_equal(3, first_event.commits.size)
116     assert_nil first_event.commits.first.email
117     assert_equal users(:johan), first_event.commits.first.user
118   end
119   
120   should "creates commit events even if the committer is unknown" do
121     stub_git_log_and_user
122     @processor.repository = Repository.first
123     @processor.commit_summary = '0000000000000000000000000000000000000000 a9934c1d3a56edfa8f45e5f157869874c8dc2c34 refs/heads/master'
124     assert_equal :create, @processor.action
125     assert_equal 4, @processor.events.size
126     assert_equal users(:johan), @processor.events.first.user
127     @processor.events[1..4].each do |e|
128       assert_equal 'john@nowhere.com', e.email
129     end
130   end
131   
132   should "returns the correct type and identifier for the deletion of a tag" do
133     stub_git_show
134     @processor.commit_summary = "a9934c1d3a56edfa8f45e5f157869874c8dc2c34 0000000000000000000000000000000000000000 refs/tags/r1.1"
135     assert_equal :delete, @processor.action
136     assert @processor.tag?
137     assert_equal 1, @processor.events.size
138     assert_equal Action::DELETE_TAG, @processor.events.first.event_type
139     assert_equal 'r1.1', @processor.events.first.identifier
140     assert_equal 'john@nowhere.com', @processor.events.first.email
141     assert_equal 'Deleted branch r1.1', @processor.events.first.message
142     @processor.expects(:log_event).once
143     @processor.log_events
144   end
145   
146   should "returns the correct type and identifier for the deletion of a branch" do
147     stub_git_show
148     frozen_now = Time.now
149     Time.expects(:now).returns(frozen_now)
150     @processor.commit_summary = 'a9934c1d3a56edfa8f45e5f157869874c8dc2c34 0000000000000000000000000000000000000000 refs/heads/foo_branch'
151     assert_equal :delete, @processor.action
152     assert @processor.head?
153     assert_equal 1, @processor.events.size
154     assert_equal Action::DELETE_BRANCH, @processor.events.first.event_type
155     assert_equal 'foo_branch', @processor.events.first.identifier
156     assert_equal frozen_now.utc, @processor.events.first.commit_time
157     @processor.expects(:log_event).once
158     @processor.log_events
159   end
160   
161   should "parse the git output correctly in the real world" do
162     grit = Grit::Repo.new(grit_test_repo("dot_git"), :is_bare => true)
163     @processor.stubs(:git_repo).returns(grit)
164     @processor.stubs(:user).returns(users(:johan))
165     
166     @processor.commit_summary = "2d3acf90f35989df8f262dc50beadc4ee3ae1560 ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a refs/heads/master"
167     @processor.repository = Repository.first
168     assert_equal :update, @processor.action
169     assert @processor.head?
170     assert_equal 1, @processor.events.size
171     first_event = @processor.events.first
172     assert_equal Action::PUSH, first_event.event_type
173     assert_equal users(:johan).email, first_event.email
174     assert_equal(2, first_event.commits.size)
175     commit_event = first_event.commits.first
176     assert_equal "ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a", commit_event.identifier
177     assert_equal "schacon@gmail.com", commit_event.email
178     assert_equal Time.at(1208561228), commit_event.commit_time
179     exp_msg = "added a pure-ruby git library and converted the cat_file commands to use it"
180     assert_equal exp_msg, commit_event.message
181   end
182   
183   def stub_git_log_and_user
184     git = mock
185     output = [Grit::Commit.create(git, {
186       :id => '33f746e21ef5122511a5a69f381bfdf017f4d66c',
187       :author => Grit::Actor.from_string("John McClane <john@nowhere.com>"),
188       :authored_date => Time.at(1233842115),
189       :message => "This is really nice",
190     })]
191     @processor.stubs(:commits_from_revspec).returns(output*3)
192     @processor.stubs(:git).returns(git)
193     @processor.stubs(:user).returns(users(:johan))
194   end
195   
196   def stub_git_show
197     git = mock
198     output = [Grit::Commit.create(git, {
199       :id => '33f746e21ef5122511a5a69f381bfdf017f4d66c',
200       :author => Grit::Actor.from_string("John McClane <john@nowhere.com>"),
201       :authored_date => Time.at(1233842115),
202       :message => "This is really nice",
203     })]
204     @processor.stubs(:commits_from_revspec).returns(output)
205     @processor.stubs(:git).returns(git)    
206   end
207   
208   # describe 'with stubbing towards a live repo' do
209   #   before(:each) do
210   #     @repo = Grit::Repo.new("/Users/marius/tmp/clone")
211   #     @processor.stubs(:git).returns(@repo.git)
212   #   end
213   #   
214   #   it 'should get decent output from git log' do
215   #     @processor.commit_summary = "b808ca5eb8ab40a9fdc3489f9f83f6cf6e726a61 abf17983b01f716f34ee10b3f74f14fe7f3bf4ed refs/heads/master"
216   #     assert_equal 1, @processor.events.size
217   #     assert_equal 'marius.mathiesen@gmail.com', @processor.events.first.email
218   #     assert_equal 'Adding some stuff', @processor.events.first.message
219   #   end
220   # end
221 end