Fix test setup
[gitorious:bclow-gitorious.git] / test / micro / pre_receive_guard_test.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2012 Gitorious AS
4 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
5 #
6 #   This program is free software: you can redistribute it and/or modify
7 #   it under the terms of the GNU Affero General Public License as published by
8 #   the Free Software Foundation, either version 3 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU Affero General Public License for more details.
15 #
16 #   You should have received a copy of the GNU Affero General Public License
17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #++
19 require "fast_test_helper"
20 require "gitorious/ssh/pre_receive_guard"
21
22 class PreReceiveGuardTest < MiniTest::Shoulda
23   context "In general" do
24     setup do
25       @env = {
26           "GITORIOUS_WRITABLE_BY_URL" => "http://gitorious.example/repositories/writable_by?user=john",
27           "SSH_ORIGINAL_COMMAND"  => "git-receive-pack foo.git"}
28       @guard = Gitorious::SSH::PreReceiveGuard.new(@env, "#{'0'*10} #{'fca'*10} refs/merge-requests/123")
29     end
30
31     should "know if the hook is called via SSH and allow pushes if local" do
32       assert !@guard.local_connection?
33       @env.delete("SSH_ORIGINAL_COMMAND")
34       @guard = Gitorious::SSH::PreReceiveGuard.new(@env, "#{'0'*10} #{'fca'*10} refs/merge-requests/123")
35       assert @guard.local_connection?
36       assert @guard.allow_push?
37     end
38
39     should "never deny force-pushes for merge-requests" do
40       guard = Gitorious::SSH::PreReceiveGuard.new(@env,
41         "#{'aaa'*10} #{'bbb'*10} refs/merge-requests/123\n")
42       assert !guard.deny_force_pushes?
43
44       @env["GITORIOUS_DENY_FORCE_PUSHES"] = "true"
45       guard = Gitorious::SSH::PreReceiveGuard.new(@env,
46         "#{'aaa'*10} #{'bbb'*10} refs/merge-requests/123\n")
47       assert !guard.deny_force_pushes?
48     end
49
50     should "deny fast forwards if the correct env var is set" do
51       @env["GITORIOUS_DENY_FORCE_PUSHES"] = "true"
52       guard = Gitorious::SSH::PreReceiveGuard.new(@env,
53         "#{'0'*10} #{'fca'*10} refs/heads/master")
54       assert guard.deny_force_pushes?
55     end
56
57     should "build the correct authentication URL" do
58       assert_equal "http://gitorious.example/repositories/writable_by?user=john&git_path=#{CGI.escape('refs/merge-requests/123')}", @guard.authentication_url
59     end
60
61     should "extract the Git target correctly" do
62       assert_equal "refs/merge-requests/123", @guard.git_target
63     end
64
65     should "chomp newlines from the git_target" do
66       guard = Gitorious::SSH::PreReceiveGuard.new(@env,
67         "#{'0'*10} #{'fca'*10} refs/heads/master\n")
68       assert_equal "refs/heads/master", guard.git_target
69     end
70
71     should "not allow push when Gitorious says no" do
72       @guard.stubs(:get_via_http).returns("false")
73       assert !@guard.allow_push?
74     end
75
76     should "allow push when Gitorious says it is ok" do
77       @guard.stubs(:get_via_http).returns("true")
78       assert !@guard.deny_force_pushes?
79       assert @guard.allow_push?
80     end
81
82     should "know if something is a null sha1" do
83       assert !@guard.null_sha?("abcd"*10)
84       assert @guard.null_sha?("0000000000000000000000000000000000000000")
85     end
86
87     should "allow deletion of merge requests on local connections" do
88       @env.delete("SSH_ORIGINAL_COMMAND")
89       guard = Gitorious::SSH::PreReceiveGuard.new(@env,
90         "#{'0'*10} #{'fca'*10} refs/merge-requests/123\n")
91       assert !guard.deny_merge_request_update_with_sha?("0"*40)
92     end
93
94     should "deny deletion of merge requests on remote connections" do
95       guard = Gitorious::SSH::PreReceiveGuard.new(@env,
96         "#{'0'*10} #{'fca'*10} refs/merge-requests/123\n")
97       assert guard.deny_merge_request_update_with_sha?("0"*40)
98     end
99   end
100 end