diff options
author | Brett Walker <brett@digitalmoksha.com> | 2018-01-23 10:23:23 +0000 |
---|---|---|
committer | Grzegorz Bizon <grzegorz@gitlab.com> | 2018-01-23 10:23:23 +0000 |
commit | b8b9e9eb8e3f285bc3040f3dc425deeada585aae (patch) | |
tree | 87934fbe6b2b2b66be25888152bcbcd4af82e970 /qa/spec | |
parent | fcfda3ef82992e5f56573dffb56b2a5896c24aff (diff) | |
download | gitlab-ce-b8b9e9eb8e3f285bc3040f3dc425deeada585aae.tar.gz |
Add ability to drive the API in QA specs
Diffstat (limited to 'qa/spec')
-rw-r--r-- | qa/spec/runtime/api_client_spec.rb | 30 | ||||
-rw-r--r-- | qa/spec/runtime/api_request_spec.rb | 42 | ||||
-rw-r--r-- | qa/spec/runtime/env_spec.rb | 8 | ||||
-rw-r--r-- | qa/spec/spec_helper.rb | 2 | ||||
-rw-r--r-- | qa/spec/support/stub_env.rb | 38 |
5 files changed, 113 insertions, 7 deletions
diff --git a/qa/spec/runtime/api_client_spec.rb b/qa/spec/runtime/api_client_spec.rb new file mode 100644 index 00000000000..d497d8839b8 --- /dev/null +++ b/qa/spec/runtime/api_client_spec.rb @@ -0,0 +1,30 @@ +describe QA::Runtime::API::Client do + include Support::StubENV + + describe 'initialization' do + it 'defaults to :gitlab address' do + expect(described_class.new.address).to eq :gitlab + end + + it 'uses specified address' do + client = described_class.new('http:///example.com') + + expect(client.address).to eq 'http:///example.com' + end + end + + describe '#get_personal_access_token' do + it 'returns specified token from env' do + stub_env('PERSONAL_ACCESS_TOKEN', 'a_token') + + expect(described_class.new.get_personal_access_token).to eq 'a_token' + end + + it 'returns a created token' do + allow_any_instance_of(described_class) + .to receive(:create_personal_access_token).and_return('created_token') + + expect(described_class.new.get_personal_access_token).to eq 'created_token' + end + end +end diff --git a/qa/spec/runtime/api_request_spec.rb b/qa/spec/runtime/api_request_spec.rb new file mode 100644 index 00000000000..9a1ed8a7a46 --- /dev/null +++ b/qa/spec/runtime/api_request_spec.rb @@ -0,0 +1,42 @@ +describe QA::Runtime::API::Request do + include Support::StubENV + + before do + stub_env('PERSONAL_ACCESS_TOKEN', 'a_token') + end + + let(:client) { QA::Runtime::API::Client.new('http://example.com') } + let(:request) { described_class.new(client, '/users') } + + describe '#url' do + it 'returns the full api request url' do + expect(request.url).to eq 'http://example.com/api/v4/users?private_token=a_token' + end + end + + describe '#request_path' do + it 'prepends the api path' do + expect(request.request_path('/users')).to eq '/api/v4/users' + end + + it 'adds the personal access token' do + expect(request.request_path('/users', personal_access_token: 'token')) + .to eq '/api/v4/users?private_token=token' + end + + it 'adds the oauth access token' do + expect(request.request_path('/users', oauth_access_token: 'otoken')) + .to eq '/api/v4/users?access_token=otoken' + end + + it 'respects query parameters' do + expect(request.request_path('/users?page=1')).to eq '/api/v4/users?page=1' + expect(request.request_path('/users?page=1', personal_access_token: 'token')) + .to eq '/api/v4/users?page=1&private_token=token' + end + + it 'uses a different api version' do + expect(request.request_path('/users', version: 'v3')).to eq '/api/v3/users' + end + end +end diff --git a/qa/spec/runtime/env_spec.rb b/qa/spec/runtime/env_spec.rb index 57a72a04507..103573db6be 100644 --- a/qa/spec/runtime/env_spec.rb +++ b/qa/spec/runtime/env_spec.rb @@ -1,7 +1,5 @@ describe QA::Runtime::Env do - before do - allow(ENV).to receive(:[]).and_call_original - end + include Support::StubENV describe '.chrome_headless?' do context 'when there is an env variable set' do @@ -57,8 +55,4 @@ describe QA::Runtime::Env do end end end - - def stub_env(name, value) - allow(ENV).to receive(:[]).with(name).and_return(value) - end end diff --git a/qa/spec/spec_helper.rb b/qa/spec/spec_helper.rb index 64d06ef6558..c2c6cf95406 100644 --- a/qa/spec/spec_helper.rb +++ b/qa/spec/spec_helper.rb @@ -1,5 +1,7 @@ require_relative '../qa' +Dir[File.join(__dir__, 'support', '**', '*.rb')].each { |f| require f } + RSpec.configure do |config| config.expect_with :rspec do |expectations| expectations.include_chain_clauses_in_custom_matcher_descriptions = true diff --git a/qa/spec/support/stub_env.rb b/qa/spec/support/stub_env.rb new file mode 100644 index 00000000000..bc8f3a5e22e --- /dev/null +++ b/qa/spec/support/stub_env.rb @@ -0,0 +1,38 @@ +# Inspired by https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb +module Support + module StubENV + def stub_env(key_or_hash, value = nil) + init_stub unless env_stubbed? + + if key_or_hash.is_a? Hash + key_or_hash.each { |k, v| add_stubbed_value(k, v) } + else + add_stubbed_value key_or_hash, value + end + end + + private + + STUBBED_KEY = '__STUBBED__'.freeze + + def add_stubbed_value(key, value) + allow(ENV).to receive(:[]).with(key).and_return(value) + allow(ENV).to receive(:key?).with(key).and_return(true) + allow(ENV).to receive(:fetch).with(key).and_return(value) + allow(ENV).to receive(:fetch).with(key, anything()) do |_, default_val| + value || default_val + end + end + + def env_stubbed? + ENV[STUBBED_KEY] + end + + def init_stub + allow(ENV).to receive(:[]).and_call_original + allow(ENV).to receive(:key?).and_call_original + allow(ENV).to receive(:fetch).and_call_original + add_stubbed_value(STUBBED_KEY, true) + end + end +end |