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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
# frozen_string_literal: true
require 'json'
require 'octokit'
require 'rspec'
require 'webmock/rspec'
require 'base64'
require 'jwt'
# latest version of pry-byebug is not compatible with Ruby 3.2.0
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2.0')
require 'pry-byebug'
end
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
require 'faraday/multipart'
end
WebMock.disable_net_connect!
RSpec.configure do |config|
config.warnings = true
config.raise_errors_for_deprecations!
config.before(:all) do
@test_repo = "#{test_github_login}/#{test_github_repository}"
@test_repo_id = test_github_repository_id
@test_org_repo = "#{test_github_org}/#{test_github_repository}"
end
end
require 'vcr'
VCR.configure do |c| # rubocop:disable Metrics/BlockLength
c.configure_rspec_metadata!
c.filter_sensitive_data('<GITHUB_LOGIN>') do
test_github_login
end
c.filter_sensitive_data('<GITHUB_COLLABORATOR_LOGIN>') do
test_github_collaborator_login
end
c.filter_sensitive_data('<GITHUB_TEAM_SLUG>') do
test_github_team_slug
end
c.filter_sensitive_data('<GITHUB_TEAM_ID>') do
test_github_team_id
end
c.filter_sensitive_data('<GITHUB_PASSWORD>') do
test_github_password
end
c.filter_sensitive_data('<<ACCESS_TOKEN>>') do
test_github_token
end
c.filter_sensitive_data('<<ACCESS_TOKEN>>') do
test_github_token_two
end
c.filter_sensitive_data('<GITHUB_COLLABORATOR_TOKEN>') do
test_github_collaborator_token
end
c.filter_sensitive_data('<GITHUB_CLIENT_ID>') do
test_github_client_id
end
c.filter_sensitive_data('<GITHUB_CLIENT_SECRET>') do
test_github_client_secret
end
c.filter_sensitive_data('<<ENTERPRISE_GITHUB_LOGIN>>') do
test_github_enterprise_login
end
c.filter_sensitive_data('<<ENTERPRISE_ACCESS_TOKEN>>') do
test_github_enterprise_token
end
c.filter_sensitive_data('<<ENTERPRISE_MANAGEMENT_CONSOLE_PASSWORD>>') do
test_github_enterprise_management_console_password
end
c.filter_sensitive_data('<<ENTERPRISE_MANAGEMENT_CONSOLE_ENDPOINT>>') do
test_github_enterprise_management_console_endpoint
end
c.filter_sensitive_data('<<MANAGE_GHES_ENDPOINT>>') do
test_github_manage_ghes_endpoint
end
c.filter_sensitive_data('<<MANAGE_GHES_PASSWORD>>') do
test_github_manage_ghes_password
end
c.filter_sensitive_data('<<ENTERPRISE_HOSTNAME>>') do
test_github_enterprise_endpoint
end
c.define_cassette_placeholder('<GITHUB_TEST_REPOSITORY>') do
test_github_repository
end
c.define_cassette_placeholder('<GITHUB_TEST_REPOSITORY_ID>') do
test_github_repository_id
end
c.define_cassette_placeholder('<GITHUB_TEST_ORGANIZATION>') do
test_github_org
end
c.define_cassette_placeholder('<GITHUB_TEST_ORG_TEAM_ID>') do
'10050505050000'
end
c.define_cassette_placeholder('<GITHUB_TEST_INTEGRATION>') do
test_github_integration
end
c.define_cassette_placeholder('<GITHUB_TEST_INTEGRATION_INSTALLATION>') do
test_github_integration_installation
end
# This MUST belong to the app used for test_github_client_id and
# test_github_client_secret
c.define_cassette_placeholder('<GITHUB_TEST_OAUTH_TOKEN>') do
test_github_oauth_token
end
c.before_http_request(:real?) do |request|
next if request.headers['X-Vcr-Test-Repo-Setup']
next unless request.uri.include? test_github_repository
options = {
headers: { 'X-Vcr-Test-Repo-Setup' => 'true' },
auto_init: true
}
test_repo = "#{test_github_login}/#{test_github_repository}"
unless oauth_client.repository?(test_repo, options)
Octokit.octokit_warn "NOTICE: Creating #{test_repo} test repository."
oauth_client.create_repository(test_github_repository, options)
end
test_org_repo = "#{test_github_org}/#{test_github_repository}"
unless oauth_client.repository?(test_org_repo, options)
Octokit.octokit_warn "NOTICE: Creating #{test_org_repo} test repository."
options[:organization] = test_github_org
oauth_client.create_repository(test_github_repository, options)
end
end
c.ignore_request do |request|
!!request.headers['X-Vcr-Test-Repo-Setup']
end
record_mode =
if ENV['GITHUB_CI']
:none
elsif ENV['OCTOKIT_TEST_VCR_RECORD']
:all
else
:once
end
c.default_cassette_options = {
serialize_with: :json,
# TODO: Track down UTF-8 issue and remove
preserve_exact_body_bytes: true,
decode_compressed_response: true,
record: record_mode
}
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :webmock
end
def delete_test_repo
oauth_client.delete_repository @test_repo
rescue Octokit::NotFound
end
def test_github_login
ENV.fetch 'OCTOKIT_TEST_GITHUB_LOGIN', 'api-padawan'
end
def test_github_collaborator_login
ENV.fetch 'OCTOKIT_TEST_GITHUB_COLLABORATOR_LOGIN', 'hubot'
end
def test_github_team_slug
ENV.fetch 'OCTOKIT_TEST_GITHUB_TEAM_SLUG', 'the-a-team'
end
def test_github_team_id
ENV.fetch 'OCTOKIT_TEST_GITHUB_TEAM_ID', 123_456
end
def test_github_password
ENV.fetch 'OCTOKIT_TEST_GITHUB_PASSWORD', 'wow_such_password'
end
def test_github_token
ENV.fetch 'OCTOKIT_TEST_GITHUB_TOKEN', 'x' * 40
end
def test_github_token_two
ENV.fetch 'OCTOKIT_TEST_GITHUB_TOKEN_TWO', 'y' * 40
end
def test_github_collaborator_token
ENV.fetch 'OCTOKIT_TEST_GITHUB_COLLABORATOR_TOKEN', 'x' * 40
end
def test_github_client_id
ENV.fetch 'OCTOKIT_TEST_GITHUB_CLIENT_ID', 'x' * 21
end
def test_github_client_secret
ENV.fetch 'OCTOKIT_TEST_GITHUB_CLIENT_SECRET', 'x' * 40
end
def test_github_enterprise_login
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_LOGIN', 'crashoverride'
end
def test_github_enterprise_token
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_TOKEN', 'x' * 40
end
def test_github_enterprise_management_console_password
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_MANAGEMENT_CONSOLE_PASSWORD', 'Secretpa55'
end
def test_github_enterprise_management_console_endpoint
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_MANAGEMENT_CONSOLE_ENDPOINT', 'https://enterprise.github.dev:8443/'
end
def test_github_enterprise_endpoint
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_ENDPOINT', 'http://enterprise.github.dev/api/v3/'
end
def test_github_manage_ghes_endpoint
ENV.fetch 'OCTOKIT_TEST_GITHUB_MANAGE_GHES_ENDPOINT', 'http://enterprise.github.dev:8443'
end
def test_github_manage_ghes_username
ENV.fetch 'OCTOKIT_TEST_GITHUB_MANAGE_GHES_USERNAME', 'api_key'
end
def test_github_manage_ghes_password
ENV.fetch 'OCTOKIT_TEST_GITHUB_MANAGE_GHES_PASSWORD', 'passworD1'
end
def test_github_repository
ENV.fetch 'OCTOKIT_TEST_GITHUB_REPOSITORY', 'api-sandbox'
end
def test_github_repository_id
ENV.fetch('OCTOKIT_TEST_GITHUB_REPOSITORY_ID', 20_974_780).to_i
end
def test_github_org
ENV.fetch 'OCTOKIT_TEST_GITHUB_ORGANIZATION', 'api-playground'
end
def test_github_integration
ENV.fetch('OCTOKIT_TEST_GITHUB_INTEGRATION', 76_765).to_i
end
def test_github_integration_installation
ENV.fetch('OCTOKIT_TEST_GITHUB_INTEGRATION_INSTALLATION', 898_507).to_i
end
def test_github_integration_pem_key
ENV.fetch 'OCTOKIT_TEST_INTEGRATION_PEM_KEY', "#{fixture_path}/fake_integration.private-key.pem"
end
def test_github_oauth_token
ENV.fetch 'OCTOKIT_TEST_GITHUB_OAUTH_TOKEN', 'q' * 40
end
def stub_delete(url)
stub_request(:delete, github_url(url))
end
def stub_get(url)
stub_request(:get, github_url(url))
end
def stub_head(url)
stub_request(:head, github_url(url))
end
def stub_patch(url)
stub_request(:patch, github_url(url))
end
def stub_post(url)
stub_request(:post, github_url(url))
end
def stub_put(url)
stub_request(:put, github_url(url))
end
def fixture_path
File.expand_path('fixtures', __dir__)
end
def fixture(file)
File.new("#{fixture_path}/#{file}")
end
def json_response(file)
{
body: fixture(file),
headers: {
content_type: 'application/json; charset=utf-8'
}
}
end
def github_url(url)
return url if url =~ /^http/
url = File.join(Octokit.api_endpoint, url)
uri = Addressable::URI.parse(url)
uri.path.gsub!('v3//', 'v3/')
uri.to_s
end
def github_enterprise_url(url)
test_github_enterprise_endpoint + url
end
def github_management_console_url(url)
test_github_enterprise_management_console_endpoint + url
end
def github_manage_ghes_url(url)
test_github_manage_ghes_endpoint + url
end
def basic_auth_client(login: test_github_login, password: test_github_password)
Octokit::Client.new(login: login, password: password)
end
def oauth_client(access_token: test_github_token)
Octokit::Client.new(access_token: access_token)
end
def enterprise_admin_client
stack = Faraday::RackBuilder.new do |builder|
builder.request :multipart
builder.request :url_encoded
builder.adapter Faraday.default_adapter
end
client = Octokit::EnterpriseAdminClient.new \
access_token: test_github_enterprise_token,
connection_options: { ssl: { verify: false } }
client.configure do |c|
c.api_endpoint = test_github_enterprise_endpoint
c.middleware = stack
end
client
end
def http_cache_middleware_store
@http_cache_middleware_store ||= Faraday::HttpCache::MemoryStore.new
end
def oauth_client_with_http_cache_middleware(access_token: test_github_token)
stack = Faraday::RackBuilder.new do |builder|
builder.use Faraday::HttpCache, serializer: Marshal, store: http_cache_middleware_store, shared_cache: false
builder.adapter Faraday.default_adapter
end
client = oauth_client(access_token: access_token)
client.configure do |c|
c.middleware = stack
end
client
end
def enterprise_management_console_client
stack = Faraday::RackBuilder.new do |builder|
builder.request :multipart
builder.request :url_encoded
builder.adapter Faraday.default_adapter
end
client = Octokit::EnterpriseManagementConsoleClient.new \
management_console_endpoint: test_github_enterprise_management_console_endpoint,
management_console_password: test_github_enterprise_management_console_password,
connection_options: { ssl: { verify: false } }
client.configure do |c|
c.middleware = stack
end
client
end
def manage_ghes_client
stack = Faraday::RackBuilder.new do |builder|
builder.request :multipart
builder.request :url_encoded
builder.adapter Faraday.default_adapter
end
client = Octokit::ManageGHESClient.new \
manage_ghes_endpoint: test_github_manage_ghes_endpoint,
manage_ghes_username: test_github_manage_ghes_username,
manage_ghes_password: test_github_manage_ghes_password,
connection_options: { ssl: { verify: false } }
client.configure do |c|
c.middleware = stack
end
client
end
def new_jwt_token
private_pem = File.read(test_github_integration_pem_key)
private_key = OpenSSL::PKey::RSA.new(private_pem)
payload = {}.tap do |opts|
opts[:iat] = Time.now.to_i # Issued at time.
opts[:exp] = opts[:iat] + 600 # JWT expiration time is 10 minutes from issued time.
opts[:iss] = test_github_integration # Integration's GitHub identifier.
end
JWT.encode(payload, private_key, 'RS256')
end
def use_vcr_placeholder_for(text, replacement)
VCR.configure do |c|
c.define_cassette_placeholder(replacement) do
text
end
end
end
|