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