diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /spec/lib/api | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) | |
download | gitlab-ce-85dc423f7090da0a52c73eb66faf22ddb20efff9.tar.gz |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'spec/lib/api')
-rw-r--r-- | spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb | 127 | ||||
-rw-r--r-- | spec/lib/api/helpers_spec.rb | 58 |
2 files changed, 101 insertions, 84 deletions
diff --git a/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb b/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb index 73b67f9e61c..3c40859da21 100644 --- a/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb +++ b/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb @@ -3,126 +3,87 @@ require 'spec_helper' RSpec.describe API::Helpers::PackagesManagerClientsHelpers do + include HttpBasicAuthHelpers + let_it_be(:personal_access_token) { create(:personal_access_token) } let_it_be(:username) { personal_access_token.user.username } let_it_be(:helper) { Class.new.include(described_class).new } let(:password) { personal_access_token.token } - describe '#find_job_from_http_basic_auth' do - let_it_be(:user) { personal_access_token.user } - - let(:job) { create(:ci_build, user: user, status: :running) } - let(:password) { job.token } - let(:headers) { { Authorization: basic_http_auth(username, password) } } - - subject { helper.find_job_from_http_basic_auth } - - before do - allow(helper).to receive(:headers).and_return(headers&.with_indifferent_access) - end - - context 'with a valid Authorization header' do - it { is_expected.to eq job } + let(:env) do + { + 'rack.input' => '' + } + end - context 'when the job is not running' do - before do - job.update!(status: :failed) - end + let(:request) { ActionDispatch::Request.new(env) } - it { is_expected.to be nil } - end - end + before do + allow(helper).to receive(:request).and_return(request) + end + shared_examples 'invalid auth header' do context 'with an invalid Authorization header' do - where(:headers) do - [ - [{ Authorization: 'Invalid' }], - [{}], - [nil] - ] + before do + env.merge!(build_auth_headers('Invalid')) end - with_them do - it { is_expected.to be nil } - end - end - - context 'with an unknown Authorization header' do - let(:password) { 'Unknown' } - it { is_expected.to be nil } end end - describe '#find_deploy_token_from_http_basic_auth' do - let_it_be(:deploy_token) { create(:deploy_token) } - let(:token) { deploy_token.token } - let(:headers) { { Authorization: basic_http_auth(deploy_token.username, token) } } - - subject { helper.find_deploy_token_from_http_basic_auth } - - before do - allow(helper).to receive(:headers).and_return(headers&.with_indifferent_access) - end - + shared_examples 'valid auth header' do context 'with a valid Authorization header' do - it { is_expected.to eq deploy_token } - end - - context 'with an invalid Authorization header' do - where(:headers) do - [ - [{ Authorization: 'Invalid' }], - [{}], - [nil] - ] + before do + env.merge!(basic_auth_header(username, password)) end - with_them do + context 'with an unknown password' do + let(:password) { 'Unknown' } + it { is_expected.to be nil } end - end - - context 'with an invalid token' do - let(:token) { 'Unknown' } - it { is_expected.to be nil } + it { is_expected.to eq expected_result } end end - describe '#uploaded_package_file' do - let_it_be(:params) { {} } + describe '#find_job_from_http_basic_auth' do + let_it_be(:user) { personal_access_token.user } + let(:job) { create(:ci_build, user: user, status: :running) } + let(:password) { job.token } - subject { helper.uploaded_package_file } + subject { helper.find_job_from_http_basic_auth } - before do - allow(helper).to receive(:params).and_return(params) + it_behaves_like 'valid auth header' do + let(:expected_result) { job } end - context 'with valid uploaded package file' do - let_it_be(:uploaded_file) { Object.new } + it_behaves_like 'invalid auth header' + context 'when the job is not running' do before do - allow(UploadedFile).to receive(:from_params).and_return(uploaded_file) + job.update!(status: :failed) end - it { is_expected.to be uploaded_file } + it_behaves_like 'valid auth header' do + let(:expected_result) { nil } + end end + end - context 'with invalid uploaded package file' do - before do - allow(UploadedFile).to receive(:from_params).and_return(nil) - end + describe '#find_deploy_token_from_http_basic_auth' do + let_it_be(:deploy_token) { create(:deploy_token) } + let(:token) { deploy_token.token } + let(:username) { deploy_token.username } + let(:password) { token } - it 'fails with bad_request!' do - expect(helper).to receive(:bad_request!) + subject { helper.find_deploy_token_from_http_basic_auth } - expect(subject).to be nil - end + it_behaves_like 'valid auth header' do + let(:expected_result) { deploy_token } end - end - def basic_http_auth(username, password) - ActionController::HttpAuthentication::Basic.encode_credentials(username, password) + it_behaves_like 'invalid auth header' end end diff --git a/spec/lib/api/helpers_spec.rb b/spec/lib/api/helpers_spec.rb index d0fe9163c6e..51a45dff6a4 100644 --- a/spec/lib/api/helpers_spec.rb +++ b/spec/lib/api/helpers_spec.rb @@ -183,12 +183,68 @@ RSpec.describe API::Helpers do end it "logs an exception" do - expect(Rails.logger).to receive(:warn).with(/Tracking event failed/) + expect(Gitlab::AppLogger).to receive(:warn).with(/Tracking event failed/) subject.track_event('my_event', category: nil) end end + describe '#increment_unique_values' do + let(:value) { '9f302fea-f828-4ca9-aef4-e10bd723c0b3' } + let(:event_name) { 'my_event' } + let(:unknown_event) { 'unknown' } + let(:feature) { "usage_data_#{event_name}" } + + context 'with feature enabled' do + before do + stub_feature_flags(feature => true) + end + + it 'tracks redis hll event' do + stub_application_setting(usage_ping_enabled: true) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(value, event_name) + + subject.increment_unique_values(event_name, value) + end + + it 'does not track event usage ping is not enabled' do + stub_application_setting(usage_ping_enabled: false) + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + subject.increment_unique_values(event_name, value) + end + + it 'logs an exception for unknown event' do + stub_application_setting(usage_ping_enabled: true) + + expect(Gitlab::AppLogger).to receive(:warn).with("Redis tracking event failed for event: #{unknown_event}, message: Unknown event #{unknown_event}") + + subject.increment_unique_values(unknown_event, value) + end + + it 'does not track event for nil values' do + stub_application_setting(usage_ping_enabled: true) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + subject.increment_unique_values(unknown_event, nil) + end + end + + context 'with feature disabled' do + before do + stub_feature_flags(feature => false) + end + + it 'does not track event' do + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + subject.increment_unique_values(event_name, value) + end + end + end + describe '#order_options_with_tie_breaker' do subject { Class.new.include(described_class).new.order_options_with_tie_breaker } |