Fix the issue with duplicate merge request versions
[gitorious:sirfabios-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   def setup
24     @processor = PushProcessor.new
25   end
26
27   #should_subscribe_to :push
28   should_consume "/queue/GitoriousPush"
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.consume(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 "ActiveRecord connections" do
58     setup do
59       @repository = repositories(:johans)
60       @user = @repository.user
61       @processor.stubs(:process_push)
62       @json = {
63         :gitdir => @repository.hashed_path,
64         :username => @user.login,
65         :message => "#{NULL_SHA} #{SHA} refs/heads/master"
66       }.to_json
67     end
68
69     should "be re-established" do
70       @processor.expects(:verify_connections!)
71       @processor.consume(@json)
72     end
73   end
74
75   context "Merge request update" do
76     setup do
77       @repository = repositories(:johans)
78       @user = @repository.user
79       @merge_request = merge_requests(:moes_to_johans)
80       @payload = {
81         "gitdir" => @repository.hashed_path,
82         "username" => @user.login,
83         "message" => "#{SHA} #{OTHER_SHA} refs/merge-requests/#{@merge_request.sequence_number}"
84       }
85     end
86
87     should "be processed as such" do
88       @processor.expects(:process_merge_request)
89       @processor.expects(:process_push).never
90       @processor.expects(:process_wiki_update).never
91       @processor.consume(@payload.to_json)
92     end
93
94     should "update merge request" do
95       @processor.stubs(:merge_request).returns(@merge_request)
96       @merge_request.expects(:update_from_push!)
97       @processor.load_message(@payload)
98       @processor.process_merge_request
99     end
100
101     should "not fail if username is nil" do
102       @payload[:username] = nil
103       @processor.stubs(:merge_request).returns(@merge_request)
104       @merge_request.expects(:update_from_push!)
105       @processor.consume(@payload.to_json)
106     end
107
108     should "not process if action is delete" do
109       @payload["message"] = "#{SHA} #{NULL_SHA} refs/merge-requests/19"
110       @processor.stubs(:merge_request).returns(@merge_request)
111
112       assert_nothing_raised do
113         @processor.consume(@payload.to_json)
114       end
115     end
116
117     should "not process if action is create" do
118       @payload["message"] = "#{NULL_SHA} #{SHA} refs/merge-requests/19"
119       @processor.stubs(:merge_request).returns(@merge_request)
120       @merge_request.expects(:update_from_push!).never
121
122       @processor.consume(@payload.to_json)
123     end
124
125     should_eventually "locate the correct merge request" do
126       # @repository.merge_requests.expect(:find_by_sequence_number!).with(@merge_request.sequence_number)
127     end
128   end
129
130   context "Regular push" do
131     setup do
132       @repository = repositories(:johans)
133       @user = @repository.user
134       @payload = {
135         "gitdir" => @repository.hashed_path,
136         "username" => @user.login,
137         "message" => "#{SHA} #{OTHER_SHA} refs/heads/master"
138       }
139       PushEventLogger.any_instance.stubs(:calculate_commit_count).returns(2)
140     end
141
142     should "be processed as such" do
143       @processor.expects(:process_push)
144       @processor.expects(:process_merge_request).never
145       @processor.expects(:process_wiki_update).never
146       @processor.consume(@payload.to_json)
147     end
148
149     should "log push event" do
150       PushEventLogger.any_instance.stubs(:create_push_event?).returns(true)
151       PushEventLogger.any_instance.expects(:create_push_event)
152       @processor.load_message(@payload)
153       @processor.process_push
154     end
155
156     should "log meta event" do
157       PushEventLogger.any_instance.stubs(:create_meta_event?).returns(true)
158       PushEventLogger.any_instance.expects(:create_meta_event)
159       @processor.load_message(@payload)
160       @processor.process_push
161     end
162
163     should "register push on repository" do
164       @processor.stubs(:repository).returns(@repository)
165       @repository.expects(:register_push)
166       @processor.load_message(@payload)
167       @processor.process_push
168     end
169
170     should "fail if username is nil" do
171       @payload["username"] = nil
172       @processor.stubs(:repository).returns(@repository)
173       @repository.expects(:register_push).never
174       @processor.load_message(@payload)
175
176       assert_raise RuntimeError do
177         @processor.process_push
178       end
179     end
180   end
181
182   context "Wiki update" do
183     setup do
184       @repository = repositories(:johans_wiki)
185       @user = @repository.user
186       @payload = {
187         "gitdir" => @repository.hashed_path,
188         "username" => @user.login,
189         "message" => "#{SHA} #{OTHER_SHA} refs/heads/master"
190       }
191     end
192
193     should "be processed as such" do
194       @processor.expects(:process_wiki_update)
195       @processor.expects(:process_push).never
196       @processor.expects(:process_merge_request).never
197       @processor.consume(@payload.to_json)
198     end
199
200     should "log wiki events" do
201       Gitorious::Wiki::UpdateEventLogger.any_instance.expects(:create_wiki_events).returns(true)
202       @processor.load_message(@payload)
203       @processor.process_wiki_update
204     end
205
206     should "fail if username is nil" do
207       @payload["username"] = nil
208       Gitorious::Wiki::UpdateEventLogger.any_instance.expects(:create_wiki_events).returns(true).never
209       @processor.load_message(@payload)
210
211       assert_raise RuntimeError do
212         @processor.process_wiki_update
213       end
214     end
215   end
216
217   context "Triggering the web hooks" do
218     setup do
219       @repository = repositories(:johans)
220       @user = @repository.user
221
222       message = {
223         "gitdir" => @repository.hashed_path,
224         "username" => @user.login,
225         "message" => "#{SHA} #{OTHER_SHA} refs/heads/master"
226       }
227
228       @processor.load_message(message)
229       PushEventLogger.any_instance.stubs(:calculate_commit_count).returns(2)
230     end
231
232     should "not trigger web hooks unless repository has some" do
233       @processor.expects(:trigger_hooks).never
234       @processor.process_push
235     end
236
237     should "trigger web hooks if repository has hooks" do
238       @repository.hooks.create!(:user => users(:moe), :url => "http://g.org/hooks")
239       @processor.expects(:trigger_hooks)
240       @processor.process_push
241     end
242
243     should "create a generator and generate for repos with hooks" do
244       @repository.hooks.create!(:user => users(:moe), :url => "http://g.org/hooks")
245       Gitorious::WebHookGenerator.any_instance.expects(:generate!).once
246       @processor.trigger_hooks
247     end
248   end
249 end