-
Notifications
You must be signed in to change notification settings - Fork 6
/
git_shell_executor_test.rb
158 lines (140 loc) · 5.78 KB
/
git_shell_executor_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'test_helper'
class GitShellExecutorTest < ActiveSupport::TestCase
# Raised by the mock error method.
class ShellExitError < RuntimeError; end
class GitExitError < RuntimeError; end
class AccessExitError < RuntimeError; end
class BackendExitError < RuntimeError; end
def setup
@executor = GitShellExecutor.new
# Mock the error method.
class <<@executor
def error(message)
case message
when /git\+ssh/
raise ShellExitError, message
when /Git/
raise GitExitError, message
when /Access/
raise AccessExitError, message
when /Backend/
raise BackendExitError, message
end
end
end
@key_id = ssh_keys(:rsa).to_param
@server = 'http://test:1234/'
@repo = repositories(:dexter_ghost)
@repo_path = @repo.profile.name + '/' + @repo.name + '.git'
@repo_dir = 'repos/' + @repo_path
end
test 'invalid command' do
assert_raise ShellExitError do
@executor.run [@key_id, @server, 'ls', '-l']
end
end
test 'garbage parameters' do
assert_raise ShellExitError do
@executor.run [@key_id, @server, 'git-upload-pack', @repo_path, '--bsbs']
end
end
test 'access denied pull' do
app_req = "check_access.json?repo_path=#{@repo_path}&" +
"ssh_key_id=#{@key_id}&commit=false"
flexmock(@executor).should_receive(:app_request).
with(nil, @server, app_req).
and_return('{"access": false, "message": "no such repository"}').once
assert_raise AccessExitError do
@executor.run [@key_id, @server, 'git-upload-pack', @repo_path]
end
end
test 'access denied push' do
app_req = "check_access.json?repo_path=#{@repo_path}&" +
"ssh_key_id=#{@key_id}&commit=true"
flexmock(@executor).should_receive(:app_request).
with(nil, @server, app_req).
and_return('{"access": false, "message": "no such repository"}').once
assert_raise AccessExitError do
@executor.run [@key_id, @server, 'git-receive-pack', @repo_path]
end
end
test 'successful pull' do
app_req = "check_access.json?repo_path=#{@repo_path}&" +
"ssh_key_id=#{@key_id}&commit=false"
flexmock(@executor).should_receive(:app_request).
with(nil, @server, app_req).
and_return('{"access": true}').once
flexmock(@executor).should_receive(:exec_git).
with('git-upload-archive', @repo_dir).and_return(true).
once
@executor.run [@key_id, @server, 'git-upload-archive', @repo_path]
end
test 'failed pull' do
app_req = "check_access.json?repo_path=#{@repo_path}&" +
"ssh_key_id=#{@key_id}&commit=false"
flexmock(@executor).should_receive(:app_request).
with(nil, @server, app_req).
and_return('{"access": true}').once
flexmock(@executor).should_receive(:exec_git).once.
with('git-upload-archive', @repo_dir).and_return(false)
assert_raise GitExitError do
@executor.run [@key_id, @server, 'git-upload-archive', @repo_path]
end
end
test 'successful push' do
app_req = "check_access.json?repo_path=#{@repo_path}&" +
"ssh_key_id=#{@key_id}&commit=true"
flexmock(@executor).should_receive(:app_request).
with(nil, @server, app_req).
and_return('{"access": true}').once
flexmock(@executor).should_receive(:exec_git).once.
with('git-receive-pack', @repo_dir).and_return(true)
app_req = "change_notice.json"
flexmock(@executor).should_receive(:app_request).
with({'repo_path' => @repo_path}, @server, app_req).
and_return('{"success": true}').once
@executor.run [@key_id, @server, 'git-receive-pack', @repo_path]
end
test 'un-acknowledged push' do
app_req = "check_access.json?repo_path=#{@repo_path}&" +
"ssh_key_id=#{@key_id}&commit=true"
flexmock(@executor).should_receive(:app_request).
with(nil, @server, app_req).
and_return('{"access": true}').once
flexmock(@executor).should_receive(:exec_git).once.
with('git-receive-pack', @repo_dir).and_return(true)
app_req = "change_notice.json"
flexmock(@executor).should_receive(:app_request).
with({'repo_path' => @repo_path}, @server, app_req).
and_return('{"success": false}').times(3)
assert_raise BackendExitError do
@executor.run [@key_id, @server, 'git-receive-pack', @repo_path]
end
end
test 'app request get' do
@server = 'http://test:1234'
flexmock(Net::HTTP).should_receive(:get).once.
with(URI.parse("http://test:1234/get.json?p=true")).
and_return('HTTP response')
assert_equal 'HTTP response',
@executor.app_request(false, @server, 'get.json?p=true')
end
test 'app request broken get' do
@server = 'http://test:1234'
flexmock(Net::HTTP).should_receive(:get).once.
with(URI.parse("http://test:1234/get.json?p=true")).
and_raise(RuntimeError)
assert_raise BackendExitError do
@executor.app_request(nil, @server, 'get.json?p=true')
end
end
test 'app request post' do
response = Object.new
flexmock(response).should_receive(:body).and_return('HTTP response').once
flexmock(Net::HTTP).should_receive(:post_form).once.
with(URI.parse("http://test:1234/check_access.json"), {}).
and_return(response)
assert_equal 'HTTP response',
@executor.app_request({}, @server, 'check_access.json')
end
end