/spec/lib/gitlab/legacy_github_import/client_spec.rb

https://gitlab.com/abuhazim/gitlab-foss · Ruby · 120 lines · 92 code · 27 blank · 1 comment · 0 complexity · d3478d1c7075c05aa334f4bd83fd57a7 MD5 · raw file

  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Gitlab::LegacyGithubImport::Client do
  4. let(:token) { '123456' }
  5. let(:github_provider) { Settingslogic.new('app_id' => 'asd123', 'app_secret' => 'asd123', 'name' => 'github', 'args' => { 'client_options' => {} }) }
  6. let(:wait_for_rate_limit_reset) { true }
  7. subject(:client) { described_class.new(token, wait_for_rate_limit_reset: wait_for_rate_limit_reset) }
  8. before do
  9. allow(Gitlab.config.omniauth).to receive(:providers).and_return([github_provider])
  10. end
  11. it 'convert OAuth2 client options to symbols' do
  12. expect(client.client.options.keys).to all(be_kind_of(Symbol))
  13. end
  14. it 'does not crash (e.g. Settingslogic::MissingSetting) when verify_ssl config is not present' do
  15. expect { client.api }.not_to raise_error
  16. end
  17. context 'when config is missing' do
  18. before do
  19. allow(Gitlab.config.omniauth).to receive(:providers).and_return([])
  20. end
  21. it 'is still possible to get an Octokit client' do
  22. expect { client.api }.not_to raise_error
  23. end
  24. it 'is not be possible to get an OAuth2 client' do
  25. expect { client.client }.to raise_error(Projects::ImportService::Error)
  26. end
  27. end
  28. context 'allow SSL verification to be configurable on API' do
  29. before do
  30. github_provider['verify_ssl'] = false
  31. end
  32. it 'uses supplied value' do
  33. expect(client.client.options[:connection_opts][:ssl]).to eq({ verify: false })
  34. expect(client.api.connection_options[:ssl]).to eq({ verify: false })
  35. end
  36. end
  37. describe '#api_endpoint' do
  38. context 'when provider does not specity an API endpoint' do
  39. it 'uses GitHub root API endpoint' do
  40. expect(client.api.api_endpoint).to eq 'https://api.github.com/'
  41. end
  42. end
  43. context 'when provider specify a custom API endpoint' do
  44. before do
  45. github_provider['args']['client_options']['site'] = 'https://github.company.com/'
  46. end
  47. it 'uses the custom API endpoint' do
  48. expect(OmniAuth::Strategies::GitHub).not_to receive(:default_options)
  49. expect(client.api.api_endpoint).to eq 'https://github.company.com/'
  50. end
  51. end
  52. context 'when given a host' do
  53. subject(:client) { described_class.new(token, host: 'https://try.gitea.io/') }
  54. it 'builds a endpoint with the given host and the default API version' do
  55. expect(client.api.api_endpoint).to eq 'https://try.gitea.io/api/v3/'
  56. end
  57. end
  58. context 'when given an API version' do
  59. subject(:client) { described_class.new(token, api_version: 'v3') }
  60. it 'does not use the API version without a host' do
  61. expect(client.api.api_endpoint).to eq 'https://api.github.com/'
  62. end
  63. end
  64. context 'when given a host and version' do
  65. subject(:client) { described_class.new(token, host: 'https://try.gitea.io/', api_version: 'v3') }
  66. it 'builds a endpoint with the given options' do
  67. expect(client.api.api_endpoint).to eq 'https://try.gitea.io/api/v3/'
  68. end
  69. context 'and hostname' do
  70. subject(:client) { described_class.new(token, host: 'https://167.99.148.217/', api_version: 'v1', hostname: 'try.gitea.io') }
  71. it 'builds a endpoint with the given options' do
  72. expect(client.api.connection_options.dig(:headers, :host)).to eq 'try.gitea.io'
  73. expect(client.api.api_endpoint).to eq 'https://167.99.148.217/api/v1/'
  74. end
  75. end
  76. end
  77. end
  78. context 'github rate limit' do
  79. it 'does not raise error when rate limit is disabled' do
  80. stub_request(:get, /api.github.com/)
  81. allow(client.api).to receive(:rate_limit!).and_raise(Octokit::NotFound)
  82. expect { client.repos }.not_to raise_error
  83. end
  84. context 'when wait for rate limit is disabled' do
  85. let(:wait_for_rate_limit_reset) { false }
  86. it 'raises the error limit error when requested' do
  87. stub_request(:get, /api.github.com/)
  88. allow(client.api).to receive(:repos).and_raise(Octokit::TooManyRequests)
  89. expect { client.repos }.to raise_error(Octokit::TooManyRequests)
  90. end
  91. end
  92. end
  93. end