Commit a5177ac3 authored by Piotr Skorupa's avatar Piotr Skorupa

Add usage_data_id validation from version app response

parent 25c14848
......@@ -35,7 +35,13 @@ class SubmitUsagePingService
raise SubmissionError.new("Unsuccessful response code: #{response.code}") unless response.success?
raw_usage_data.update_version_metadata!(usage_data_id: response.dig('conv_index', 'usage_data_id'))
version_usage_data_id = response.dig('conv_index', 'usage_data_id') || response.dig('dev_ops_score', 'usage_data_id')
unless version_usage_data_id.is_a?(Integer) && version_usage_data_id > 0
raise SubmissionError.new("Invalid usage_data_id in response: #{version_usage_data_id}")
end
raw_usage_data.update_version_metadata!(usage_data_id: version_usage_data_id)
store_metrics(response)
end
......
......@@ -6,6 +6,7 @@ RSpec.describe SubmitUsagePingService do
include StubRequests
include UsageDataHelpers
let(:usage_data_id) { 31643 }
let(:score_params) do
{
score: {
......@@ -40,14 +41,15 @@ RSpec.describe SubmitUsagePingService do
leader_service_desk_issues: 15.8,
instance_service_desk_issues: 15.1,
usage_data_id: usage_data_id,
non_existing_column: 'value'
}
}
end
let(:with_dev_ops_score_params) { { dev_ops_score: score_params[:score] } }
let(:with_conv_index_params) { { conv_index: score_params[:score].merge(usage_data_id: 31643) } }
let(:without_dev_ops_score_params) { { dev_ops_score: {} } }
let(:with_conv_index_params) { { conv_index: score_params[:score] } }
shared_examples 'does not run' do
it do
......@@ -103,7 +105,7 @@ RSpec.describe SubmitUsagePingService do
end
it 'sends a POST request' do
response = stub_response(body: without_dev_ops_score_params)
response = stub_response(body: with_dev_ops_score_params)
subject.execute
......@@ -111,7 +113,7 @@ RSpec.describe SubmitUsagePingService do
end
it 'forces a refresh of usage data statistics before submitting' do
stub_response(body: without_dev_ops_score_params)
stub_response(body: with_dev_ops_score_params)
expect(Gitlab::UsageData).to receive(:data).with(force_refresh: true).and_call_original
......@@ -139,6 +141,20 @@ RSpec.describe SubmitUsagePingService do
end
end
context 'when version app usage_data_id is invalid' do
let(:usage_data_id) { -1000 }
before do
stub_response(body: with_conv_index_params)
end
it 'raises an exception' do
expect { subject.execute }.to raise_error(described_class::SubmissionError) do |error|
expect(error.message).to include('Invalid usage_data_id in response: -1000')
end
end
end
context 'when DevOps report data is passed' do
before do
stub_response(body: with_dev_ops_score_params)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment