gitlab-grack 2.0.0.pre → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +0,0 @@
1
- module Grack
2
- VERSION = "2.0.0.pre"
3
- end
@@ -1,239 +0,0 @@
1
- require 'rack'
2
- require 'rack/test'
3
- require 'test/unit'
4
- require 'mocha'
5
- require 'digest/sha1'
6
-
7
- require_relative '../lib/grack/server.rb'
8
- require 'pp'
9
-
10
- class GitHttpTest < Test::Unit::TestCase
11
- include Rack::Test::Methods
12
-
13
- def example
14
- File.expand_path(File.dirname(__FILE__))
15
- end
16
-
17
- def app
18
- config = {
19
- :project_root => example,
20
- :upload_pack => true,
21
- :receive_pack => true,
22
- }
23
- Grack::Server.new(config)
24
- end
25
-
26
- def test_upload_pack_advertisement
27
- get "/example/info/refs?service=git-upload-pack"
28
- assert_equal 200, r.status
29
- assert_equal "application/x-git-upload-pack-advertisement", r.headers["Content-Type"]
30
- assert_equal "001e# service=git-upload-pack", r.body.split("\n").first
31
- assert_match 'multi_ack_detailed', r.body
32
- end
33
-
34
- def test_no_access_wrong_content_type_up
35
- post "/example/git-upload-pack"
36
- assert_equal 403, r.status
37
- end
38
-
39
- def test_no_access_wrong_content_type_rp
40
- post "/example/git-receive-pack"
41
- assert_equal 403, r.status
42
- end
43
-
44
- def test_no_access_wrong_method_rcp
45
- get "/example/git-upload-pack"
46
- assert_equal 400, r.status
47
- end
48
-
49
- def test_no_access_wrong_command_rcp
50
- post "/example/git-upload-packfile"
51
- assert_equal 404, r.status
52
- end
53
-
54
- def test_no_access_wrong_path_rcp
55
- post "/example-wrong/git-upload-pack"
56
- assert_equal 404, r.status
57
- end
58
-
59
- def test_upload_pack_rpc
60
- IO.stubs(:popen).returns(MockProcess.new)
61
- post "/example/git-upload-pack", {}, {"CONTENT_TYPE" => "application/x-git-upload-pack-request"}
62
- assert_equal 200, r.status
63
- assert_equal "application/x-git-upload-pack-result", r.headers["Content-Type"]
64
- end
65
-
66
- def test_receive_pack_advertisement
67
- get "/example/info/refs?service=git-receive-pack"
68
- assert_equal 200, r.status
69
- assert_equal "application/x-git-receive-pack-advertisement", r.headers["Content-Type"]
70
- assert_equal "001f# service=git-receive-pack", r.body.split("\n").first
71
- assert_match 'report-status', r.body
72
- assert_match 'delete-refs', r.body
73
- assert_match 'ofs-delta', r.body
74
- end
75
-
76
- def test_recieve_pack_rpc
77
- IO.stubs(:popen).yields(MockProcess.new)
78
- post "/example/git-receive-pack", {}, {"CONTENT_TYPE" => "application/x-git-receive-pack-request"}
79
- assert_equal 200, r.status
80
- assert_equal "application/x-git-receive-pack-result", r.headers["Content-Type"]
81
- end
82
-
83
- def test_info_refs_dumb
84
- get "/example/.git/info/refs"
85
- assert_equal 200, r.status
86
- end
87
-
88
- def test_info_packs
89
- get "/example/.git/objects/info/packs"
90
- assert_equal 200, r.status
91
- assert_match /P pack-(.*?).pack/, r.body
92
- end
93
-
94
- def test_loose_objects
95
- path, content = write_test_objects
96
- get "/example/.git/objects/#{path}"
97
- assert_equal 200, r.status
98
- assert_equal content, r.body
99
- remove_test_objects
100
- end
101
-
102
- def test_pack_file
103
- path, content = write_test_objects
104
- get "/example/.git/objects/pack/pack-#{content}.pack"
105
- assert_equal 200, r.status
106
- assert_equal content, r.body
107
- remove_test_objects
108
- end
109
-
110
- def test_index_file
111
- path, content = write_test_objects
112
- get "/example/.git/objects/pack/pack-#{content}.idx"
113
- assert_equal 200, r.status
114
- assert_equal content, r.body
115
- remove_test_objects
116
- end
117
-
118
- def test_text_file
119
- get "/example/.git/HEAD"
120
- assert_equal 200, r.status
121
- assert_equal 41, r.body.size # submodules have detached head
122
- end
123
-
124
- def test_no_size_avail
125
- File.stubs('size?').returns(false)
126
- get "/example/.git/HEAD"
127
- assert_equal 200, r.status
128
- assert_equal 46, r.body.size # submodules have detached head
129
- end
130
-
131
- def test_config_upload_pack_off
132
- a1 = app
133
- a1.set_config_setting(:upload_pack, false)
134
- session = Rack::Test::Session.new(a1)
135
- session.get "/example/info/refs?service=git-upload-pack"
136
- assert_equal 404, session.last_response.status
137
- end
138
-
139
- def test_config_receive_pack_off
140
- a1 = app
141
- a1.set_config_setting(:receive_pack, false)
142
- session = Rack::Test::Session.new(a1)
143
- session.get "/example/info/refs?service=git-receive-pack"
144
- assert_equal 404, session.last_response.status
145
- end
146
-
147
- def test_config_bad_service
148
- get "/example/info/refs?service=git-receive-packfile"
149
- assert_equal 404, r.status
150
- end
151
-
152
- def test_git_config_receive_pack
153
- app1 = Grack::Server.new({:project_root => example})
154
- session = Rack::Test::Session.new(app1)
155
-
156
- app1.stubs(:get_git_config).with('http.receivepack').returns('')
157
- session.get "/example/info/refs?service=git-receive-pack"
158
- assert_equal 404, session.last_response.status
159
-
160
- app1.stubs(:get_git_config).with('http.receivepack').returns('true')
161
- session.get "/example/info/refs?service=git-receive-pack"
162
- assert_equal 200, session.last_response.status
163
-
164
- app1.stubs(:get_git_config).with('http.receivepack').returns('false')
165
- session.get "/example/info/refs?service=git-receive-pack"
166
- assert_equal 404, session.last_response.status
167
- end
168
-
169
- def test_git_config_upload_pack
170
- app1 = Grack::Server.new({:project_root => example})
171
- session = Rack::Test::Session.new(app1)
172
-
173
- app1.stubs(:get_git_config).with('http.uploadpack').returns('')
174
- session.get "/example/info/refs?service=git-upload-pack"
175
- assert_equal 200, session.last_response.status
176
-
177
- app1.stubs(:get_git_config).with('http.uploadpack').returns('true')
178
- session.get "/example/info/refs?service=git-upload-pack"
179
- assert_equal 200, session.last_response.status
180
-
181
- app1.stubs(:get_git_config).with('http.uploadpack').returns('false')
182
- session.get "/example/info/refs?service=git-upload-pack"
183
- assert_equal 404, session.last_response.status
184
- end
185
-
186
- private
187
-
188
- def r
189
- last_response
190
- end
191
-
192
- def write_test_objects
193
- content = Digest::SHA1.hexdigest('gitrocks')
194
- base = File.join(File.expand_path(File.dirname(__FILE__)), 'example', '.git', 'objects')
195
- obj = File.join(base, '20')
196
- Dir.mkdir(obj) rescue nil
197
- file = File.join(obj, content[0, 38])
198
- File.open(file, 'w') { |f| f.write(content) }
199
- pack = File.join(base, 'pack', "pack-#{content}.pack")
200
- File.open(pack, 'w') { |f| f.write(content) }
201
- idx = File.join(base, 'pack', "pack-#{content}.idx")
202
- File.open(idx, 'w') { |f| f.write(content) }
203
- ["20/#{content[0,38]}", content]
204
- end
205
-
206
- def remove_test_objects
207
- content = Digest::SHA1.hexdigest('gitrocks')
208
- base = File.join(File.expand_path(File.dirname(__FILE__)), 'example', '.git', 'objects')
209
- obj = File.join(base, '20')
210
- file = File.join(obj, content[0, 38])
211
- pack = File.join(base, 'pack', "pack-#{content}.pack")
212
- idx = File.join(base, 'pack', "pack-#{content}.idx")
213
- File.unlink(file)
214
- File.unlink(pack)
215
- File.unlink(idx)
216
- end
217
-
218
- end
219
-
220
- class MockProcess
221
- def initialize
222
- @counter = 0
223
- end
224
-
225
- def write(data)
226
- end
227
-
228
- def read(data)
229
- end
230
-
231
- def eof?
232
- @counter += 1
233
- @counter > 1 ? true : false
234
- end
235
-
236
- def close_write
237
- true
238
- end
239
- end