-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api_mock.rb
More file actions
285 lines (239 loc) · 7.08 KB
/
github_api_mock.rb
File metadata and controls
285 lines (239 loc) · 7.08 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
require 'active_support/all'
require 'addressable/uri'
require 'fileutils'
require 'haml'
require 'json'
require 'pry'
require 'rest-client'
require 'sinatra'
require 'yaml'
CLIENT_ID = 'the GitHub OAuth client_id'.freeze
CLIENT_SECRET = 'the GitHub OAuth client_secrete'.freeze
USERS = {
adam: {
login: 'adam',
id: 1,
email: 'adam.admin@bogus.com',
access_token: 'the-access-token-for-adam',
name: 'Adam Admin',
emails: [
{ email: 'adam.admin@example.com',
verified: true }
]
},
normin: {
login: 'normin',
id: 2,
email: 'normin.normalo@bogus.com',
name: 'Normin Normalo',
access_token: 'the-access-token-for-normin',
emails: [
]
},
silvan: {
login: 'silvan',
id: 3,
email: 'silvan.stranger@bogus.com',
name: 'Silvan Strange',
access_token: 'the-access-token-for-silvan',
emails: [
]
},
tessa: {
login: 'tessa',
email: 'tessa.team@bogus.com',
name: 'Tessa Team',
access_token: 'the-access-token-for-tessa',
emails: [
]
} }.with_indifferent_access
ORGS = {
"TestOrg": [:normin]
}.freeze
USER = USERS[(ENV['GITHUB_MOCK_USER'].presence || 'adam')]
CALLBACK_URL = 'http://localhost:' \
<< (ENV['REVERSE_PROXY_HTTP_PORT'].presence || '8888') \
<< '/cider-ci/ui/public/auth_provider/github/sign_in'
def find_user_by_access_token(access_token)
USERS.find { |k, v| v['access_token'] == access_token }.try(:second)
end
### intialize #################################################################
def initialize
super()
@hooks = []
end
### Meta ######################################################################
get '/status' do
'OK'
end
### Repositories ##############################################################
post '/repos/:owner/:repo/statuses/:sha' do
sleep 1
auth_token = env['HTTP_AUTHORIZATION'].match(/Bearer\s+(.*)/)[1] rescue nil
if auth_token != 'test-token'
status 403
else
FileUtils.mkdir_p 'tmp'
data = params.merge('auth_token' => auth_token,
'body' => JSON.parse(request.body.read))
IO.write 'tmp/last-status-post.yml', data.to_yaml
if File.exist? 'tmp/last-status-post.yml'
logger.info "written tmp/last-status-post.yml with content: #{data.to_json}"
else
logger.warn "NOT WRITTEN tmp/last-status-post.yml with content: #{data.to_json}"
end
content_type 'application/json'
status 201
'{}'
end
end
### Oauth #####################################################################
get '/login/oauth/authorize' do
halt(422, 'No such client') unless params[:client_id] == CLIENT_ID
html = USERS.map do |k, v|
Haml::Engine.new(
<<-HAML.strip_heredoc
%form{method: 'POST'}
%input{type: 'hidden', name: 'login', value: '#{v[:login]}'}
%button{type: 'submit'}
Sign in as #{v[:login]}
%hr
HAML
).render
end.join("\n")
html
end
post '/login/oauth/authorize' do
uri = Addressable::URI.parse(CALLBACK_URL)
uri.query_values = { state: params[:state], code: params[:login] }
redirect(uri.to_s, 303)
end
post '/login/oauth/access_token' do
halt(403, 'CODE missmatch') unless USERS[params[:code]]
halt(403, 'CLIENT_ID missmatch') unless params[:client_id] == CLIENT_ID
halt(403, 'CLIENT_SECRET missmatch') unless params[:client_secret] == CLIENT_SECRET
content_type 'application/json'
{ access_token: USERS[params[:code]]['access_token'] }.to_json
end
# User ######################################################################
get '/user' do
unless user = find_user_by_access_token(params[:access_token])
halt(404, '')
else
content_type 'application/json'
user.slice(:login, :id, :email).to_json
end
end
get '/user/emails' do
unless user = find_user_by_access_token(params[:access_token])
halt(404, '')
else
content_type 'application/json'
user[:emails].to_json
end
end
get '/users/:user' do
unless user = USERS[params[:user]]
halt(404, 'User not found')
unless user['access_token'] == params[:access_token]
halt(403, 'Wrong Access Token')
else
content_type 'application/json'
user.slice(:login, :id, :email).to_json
end
end
end
###############################################################################
### Org ######################################################################
###############################################################################
get '/orgs/TestOrg/members/normin' do
halt(204, '')
end
### push notifications / aka webhooks #########################################
get '/repos/:owner/:repo/hooks' do
content_type 'application/json'
@hooks.to_json
end
post '/repos/:owner/:repo/hooks' do
auth_token = env['HTTP_AUTHORIZATION'].match(/Bearer\s+(.*)/)[1] rescue nil
if auth_token != 'test-token'
status 403
else
id = rand(10000)
hooks_url = "http://localhost:#{ENV['GITHUB_API_MOCK_PORT']}/repos/#{params[:owner]}/#{params[:repo]}/hooks/#{id}"
defaults = {
id: id,
url: hooks_url,
test_url: "#{hooks_url}/test",
ping_url: "#{hooks_url}/pings",
events: ["push"],
created_at: Time.now,
updated_at: Time.now,
}.with_indifferent_access
body = JSON.parse(request.body.read).with_indifferent_access
hook = defaults.deep_merge(body)
@hooks << hook
content_type 'application/json'
status 201
hook.to_json
end
end
post '/repos/:owner/:repo/hooks/:id/test' do
if repo = @hooks.select{|r| r[:id].to_s == params[:id]}.first
url = repo[:config][:url]
RestClient.post url, {}.to_json, {content_type: :json, accept: :json}
status 204
else
status 404
end
end
### Team ######################################################################
get '/orgs/:org/teams' do
content_type 'application/json'
if params[:org] == 'TestOrg'
[{ id: 'admin_team_id',
name: 'Admins' }].to_json
else
[].to_json
end
end
get '/teams/:id/memberships/:username' do
content_type 'application/json'
if params[:id] == 'admin_team_id'
if params[:username] == 'tessa'
{ role: 'member',
state: 'active'
}.to_json
else
halt(404, '{}')
end
else
halt(404, '{}')
end
end
### demo project git ##########################################################
Dir.chdir("../demo-project-bash") do
system("git repack -a") || raise("`git repack -a ` failed")
system("git update-server-info") || raise("`git update-server-info` failed")
puts "GIT-SERVER: repacked and server-info updated"
end
get '/git/demo-project-bash/*' do
auth = Rack::Auth::Basic::Request.new(request.env)
path = "../demo-project-bash/.git/#{params[:splat].join("/")}"
unless auth.provided?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
else
unless auth.credentials.first == "the-github-token"
logger.warn "GIT-SERVER: auth token is missing or wrong #{auth.credentials}"
halt 404, "Not found\n"
else
if File.exists? path
send_file path
else
logger.info "GIT-SERVER: file really not found"
halt 404, "Not found\n"
end
end
end
end