2021-04-29 21:17:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
2022-10-11 01:57:18 +05:30
|
|
|
require 'rake_helper'
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
RSpec.describe API::UsageDataQueries, :aggregate_failures, feature_category: :service_ping do
|
2021-04-29 21:17:54 +05:30
|
|
|
include UsageDataHelpers
|
|
|
|
|
|
|
|
let_it_be(:admin) { create(:user, admin: true) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_usage_data_connections
|
2022-03-02 08:16:31 +05:30
|
|
|
stub_database_flavor_check
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /usage_data/usage_data_queries' do
|
|
|
|
let(:endpoint) { '/usage_data/queries' }
|
|
|
|
|
|
|
|
context 'with authentication' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(usage_data_queries_api: true)
|
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'GET request permissions for admin mode' do
|
|
|
|
let(:path) { endpoint }
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'returns queries if user is admin' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(endpoint, admin, admin_mode: true)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['active_user_count']).to start_with('SELECT COUNT("users"."id") FROM "users"')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns forbidden if user is not admin' do
|
|
|
|
get api(endpoint, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without authentication' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(usage_data_queries_api: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get api(endpoint)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when feature_flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(usage_data_queries_api: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns not_found for admin' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(endpoint, admin, admin_mode: true)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns forbidden for non-admin' do
|
|
|
|
get api(endpoint, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
context 'when querying sql metrics' do
|
|
|
|
let(:file) { Rails.root.join('tmp', 'test', 'sql_metrics_queries.json') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/usage_data'
|
|
|
|
|
|
|
|
run_rake_task('gitlab:usage_data:generate_sql_metrics_queries')
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches the generated query' do
|
2023-03-04 22:38:38 +05:30
|
|
|
travel_to(Time.utc(2021, 1, 1)) do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(endpoint, admin, admin_mode: true)
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
data = Gitlab::Json.parse(File.read(file))
|
|
|
|
|
|
|
|
expect(
|
|
|
|
json_response['counts_monthly'].except('aggregated_metrics')
|
|
|
|
).to eq(data['counts_monthly'].except('aggregated_metrics'))
|
|
|
|
|
|
|
|
expect(json_response['counts']).to eq(data['counts'])
|
|
|
|
expect(json_response['active_user_count']).to eq(data['active_user_count'])
|
|
|
|
expect(json_response['usage_activity_by_stage']).to eq(data['usage_activity_by_stage'])
|
|
|
|
expect(json_response['usage_activity_by_stage_monthly']).to eq(data['usage_activity_by_stage_monthly'])
|
|
|
|
end
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|