diff options
Diffstat (limited to 'spec/services/submit_usage_ping_service_spec.rb')
-rw-r--r-- | spec/services/submit_usage_ping_service_spec.rb | 123 |
1 files changed, 104 insertions, 19 deletions
diff --git a/spec/services/submit_usage_ping_service_spec.rb b/spec/services/submit_usage_ping_service_spec.rb index 4885ef99c13..450af68d383 100644 --- a/spec/services/submit_usage_ping_service_spec.rb +++ b/spec/services/submit_usage_ping_service_spec.rb @@ -49,17 +49,22 @@ RSpec.describe SubmitUsagePingService do let(:with_conv_index_params) { { conv_index: score_params[:score] } } let(:without_dev_ops_score_params) { { dev_ops_score: {} } } - context 'when usage ping is disabled' do - before do - stub_application_setting(usage_ping_enabled: false) - end + shared_examples 'does not run' do + it do + expect(Gitlab::HTTP).not_to receive(:post) + expect(Gitlab::UsageData).not_to receive(:data) - it 'does not run' do - expect(HTTParty).not_to receive(:post) + subject.execute + end + end - result = subject.execute + shared_examples 'does not send a blank usage ping payload' do + it do + expect(Gitlab::HTTP).not_to receive(:post) - expect(result).to eq false + expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error| + expect(error.message).to include('Usage data is blank') + end end end @@ -75,33 +80,47 @@ RSpec.describe SubmitUsagePingService do end end + context 'when usage ping is disabled' do + before do + stub_application_setting(usage_ping_enabled: false) + end + + it_behaves_like 'does not run' + end + context 'when usage ping is enabled' do before do stub_usage_data_connections stub_application_setting(usage_ping_enabled: true) end + context 'and user requires usage stats consent' do + before do + allow(User).to receive(:single_user).and_return(double(:user, requires_usage_stats_consent?: true)) + end + + it_behaves_like 'does not run' + end + it 'sends a POST request' do - response = stub_response(without_dev_ops_score_params) + response = stub_response(body: without_dev_ops_score_params) subject.execute expect(response).to have_been_requested end - it 'refreshes usage data statistics before submitting' do - stub_response(without_dev_ops_score_params) + it 'forces a refresh of usage data statistics before submitting' do + stub_response(body: without_dev_ops_score_params) - expect(Gitlab::UsageData).to receive(:to_json) - .with(force_refresh: true) - .and_call_original + expect(Gitlab::UsageData).to receive(:data).with(force_refresh: true).and_call_original subject.execute end context 'when conv_index data is passed' do before do - stub_response(with_conv_index_params) + stub_response(body: with_conv_index_params) end it_behaves_like 'saves DevOps score data from the response' @@ -109,18 +128,84 @@ RSpec.describe SubmitUsagePingService do context 'when DevOps score data is passed' do before do - stub_response(with_dev_ops_score_params) + stub_response(body: with_dev_ops_score_params) end it_behaves_like 'saves DevOps score data from the response' end + + context 'with save_raw_usage_data feature enabled' do + before do + stub_response(body: with_dev_ops_score_params) + stub_feature_flags(save_raw_usage_data: true) + end + + it 'creates a raw_usage_data record' do + expect { subject.execute }.to change(RawUsageData, :count).by(1) + end + + it 'saves the correct payload' do + recorded_at = Time.current + usage_data = { uuid: 'uuid', recorded_at: recorded_at } + + expect(Gitlab::UsageData).to receive(:data).with(force_refresh: true).and_return(usage_data) + + subject.execute + + raw_usage_data = RawUsageData.find_by(recorded_at: recorded_at) + + expect(raw_usage_data.recorded_at).to be_like_time(recorded_at) + expect(raw_usage_data.payload.to_json).to eq(usage_data.to_json) + end + end + + context 'with save_raw_usage_data feature disabled' do + before do + stub_response(body: with_dev_ops_score_params) + end + + it 'does not create a raw_usage_data record' do + stub_feature_flags(save_raw_usage_data: false) + + expect { subject.execute }.to change(RawUsageData, :count).by(0) + end + end + + context 'and usage ping response has unsuccessful status' do + before do + stub_response(body: nil, status: 504) + end + + it 'raises an exception' do + expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error| + expect(error.message).to include('Unsuccessful response code: 504') + end + end + end + + context 'and usage data is empty string' do + before do + allow(Gitlab::UsageData).to receive(:data).and_return({}) + end + + it_behaves_like 'does not send a blank usage ping payload' + end + + context 'and usage data is nil' do + before do + allow(Gitlab::UsageData).to receive(:data).and_return(nil) + end + + it_behaves_like 'does not send a blank usage ping payload' + end end - def stub_response(body) - stub_full_request('https://version.gitlab.com/usage_data', method: :post) + def stub_response(body:, status: 201) + stub_full_request(SubmitUsagePingService::STAGING_URL, method: :post) .to_return( headers: { 'Content-Type' => 'application/json' }, - body: body.to_json + body: body.to_json, + status: status ) end end |