Handle wiki updates and web hooks from the PushProcessor.
[gitorious:mainline.git] / test / unit / processors / push_processor_test.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2011 Gitorious AS
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 PushProcessorTest < ActiveSupport::TestCase
23   NULL_SHA = "0" * 32
24   SHA = "a" * 32
25   OTHER_SHA = "f" * 32
26
27   def setup
28     @processor = PushProcessor.new
29   end
30
31   should_subscribe_to :push
32
33   context "Parsing" do
34     setup do
35       @repository = repositories(:johans)
36       @user = @repository.user
37       json = {
38         :gitdir => @repository.hashed_path,
39         :username => @user.login,
40         :message => "#{NULL_SHA} #{SHA} refs/heads/master"
41       }.to_json
42       @processor.on_message(json)
43     end
44
45     should "recognize the user who pushed" do
46       assert_equal @user, @processor.user
47     end
48
49     should "recognize the repository pushed to" do
50       assert_equal @repository, @processor.repository
51     end
52
53     should "recognize the push spec" do
54       assert_equal NULL_SHA, @processor.spec.from_sha.sha
55       assert_equal SHA, @processor.spec.to_sha.sha
56       assert_equal "master", @processor.spec.ref_name
57     end
58   end
59
60   context "Merge request update" do
61     setup do
62       @repository = repositories(:johans)
63       @user = @repository.user
64       @merge_request = merge_requests(:moes_to_johans)
65       @json = {
66         :gitdir => @repository.hashed_path,
67         :username => @user.login,
68         :message => "#{SHA} #{OTHER_SHA} refs/merge-requests/#{@merge_request.sequence_number}"
69       }.to_json
70     end
71
72     should "be processed as such" do
73       @processor.expects(:process_merge_request)
74       @processor.expects(:process_push).never
75       @processor.expects(:process_wiki_update).never
76       @processor.on_message(@json)
77     end
78
79     should "update merge request" do
80       @processor.stubs(:merge_request).returns(@merge_request)
81       @merge_request.expects(:update_from_push!)
82       @processor.parse_message(@json)
83       @processor.process_merge_request
84     end
85
86     should_eventually "locate the correct merge request" do
87       # @repository.merge_requests.expect(:find_by_sequence_number!).with(@merge_request.sequence_number)
88     end
89   end
90
91   context "Regular push" do
92     setup do
93       @repository = repositories(:johans)
94       @user = @repository.user
95       @json = {
96         :gitdir => @repository.hashed_path,
97         :username => @user.login,
98         :message => "#{SHA} #{OTHER_SHA} refs/heads/master"
99       }.to_json
100       PushEventLogger.any_instance.stubs(:calculate_commit_count).returns(2)
101     end
102
103     should "be processed as such" do
104       @processor.expects(:process_push)
105       @processor.expects(:process_merge_request).never
106       @processor.expects(:process_wiki_update).never
107       @processor.on_message(@json)
108     end
109
110     should "log push event" do
111       PushEventLogger.any_instance.stubs(:create_push_event?).returns(true)
112       PushEventLogger.any_instance.expects(:create_push_event)
113       @processor.parse_message(@json)
114       @processor.process_push
115     end
116
117     should "log meta event" do
118       PushEventLogger.any_instance.stubs(:create_meta_event?).returns(true)
119       PushEventLogger.any_instance.expects(:create_meta_event)
120       @processor.parse_message(@json)
121       @processor.process_push
122     end
123
124     should "register push on repository" do
125       @processor.stubs(:repository).returns(@repository)
126       @repository.expects(:register_push)
127       @processor.parse_message(@json)
128       @processor.process_push
129     end
130   end
131
132   context "Wiki update" do
133     setup do
134       @repository = repositories(:johans_wiki)
135       @user = @repository.user
136       @json = {
137         :gitdir => @repository.hashed_path,
138         :username => @user.login,
139         :message => "#{SHA} #{OTHER_SHA} refs/heads/master"
140       }.to_json
141     end
142
143     should "be processed as such" do
144       @processor.expects(:process_wiki_update)
145       @processor.expects(:process_push).never
146       @processor.expects(:process_merge_request).never
147       @processor.on_message(@json)
148     end
149
150     should "log wiki events" do
151       Gitorious::Wiki::UpdateEventLogger.any_instance.expects(:create_wiki_events).returns(true)
152       @processor.parse_message(@json)
153       @processor.process_wiki_update
154     end
155   end
156
157   context "Triggering the web hooks" do
158     setup do
159       @repository = repositories(:johans)
160       @user = @repository.user
161       @json = {
162         :gitdir => @repository.hashed_path,
163         :username => @user.login,
164         :message => "#{SHA} #{OTHER_SHA} refs/heads/master"
165       }.to_json
166       @processor.parse_message(@json)
167       PushEventLogger.any_instance.stubs(:calculate_commit_count).returns(2)
168     end
169
170     should "not trigger web hooks unless repository has some" do
171       @processor.expects(:trigger_hooks).never
172       @processor.process_push
173     end
174
175     should "trigger web hooks if repository has hooks" do
176       @repository.hooks.create!(:user => users(:moe), :url => "http://g.org/hooks")
177       @processor.expects(:trigger_hooks)
178       @processor.process_push
179     end
180
181     should "create a generator and generate for repos with hooks" do
182       @repository.hooks.create!(:user => users(:moe), :url => "http://g.org/hooks")
183       Gitorious::WebHookGenerator.any_instance.expects(:generate!).once
184       @processor.trigger_hooks
185     end
186   end
187 end