Commit c8804c89 authored by Adriel Santiago's avatar Adriel Santiago Committed by Sean McGivern

Helper method to check custom metrics availability

- Add a key in metrics_data that will indicate if the
custom_prometheus_metrics license is available.
- Also add specs for environments_helper.
parent 2df4221e
......@@ -4,6 +4,13 @@ module EE
module Projects
module Prometheus
module MetricsController
extend ActiveSupport::Concern
prepended do
before_action :check_custom_metrics_license!,
only: [:validate_query, :index, :create, :update, :edit, :destroy]
end
def validate_query
respond_to do |format|
format.json do
......@@ -81,6 +88,10 @@ module EE
private
def check_custom_metrics_license!
render_404 unless project.feature_available?(:custom_prometheus_metrics)
end
def update_metrics_service(metric)
::Projects::Prometheus::Metrics::UpdateService.new(metric, metrics_params)
end
......
......@@ -26,8 +26,15 @@ module EE
super.merge(ee_environments_folder_list_view_data)
end
def custom_metrics_available?(project)
project.feature_available?(:custom_prometheus_metrics) && can?(current_user, :admin_project, project)
end
def metrics_data(project, environment)
ee_metrics_data = {
"custom-metrics-path" => project_prometheus_metrics_path(project),
"validate-query-path" => validate_query_project_prometheus_metrics_path(project),
"custom-metrics-available" => "#{custom_metrics_available?(project)}",
"alerts-endpoint" => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json),
"prometheus-alerts-available" => "#{can?(current_user, :read_prometheus_alerts, project)}"
}
......
......@@ -81,6 +81,7 @@ class License < ApplicationRecord
operations_dashboard
dependency_proxy
metrics_reports
custom_prometheus_metrics
]
EEP_FEATURES.freeze
......
......@@ -12,6 +12,20 @@ describe Projects::Prometheus::MetricsController do
project.add_maintainer(user)
sign_in(user)
stub_licensed_features(custom_prometheus_metrics: true)
end
shared_context 'not found' do
before do
stub_licensed_features(custom_prometheus_metrics: false)
end
it 'renders 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
describe 'POST #validate_query' do
......@@ -40,9 +54,21 @@ describe Projects::Prometheus::MetricsController do
expect(response).to have_gitlab_http_status(202)
end
end
context 'without premium license' do
subject { post :validate_query, params: project_params(format: :json, query: query) }
it_behaves_like 'not found'
end
end
describe 'GET #index' do
context 'without premium license' do
subject { get :index, params: project_params(format: :json) }
it_behaves_like 'not found'
end
context 'with custom metric present' do
let!(:prometheus_metric) { create(:prometheus_metric, project: project) }
......@@ -65,6 +91,14 @@ describe Projects::Prometheus::MetricsController do
end
describe 'POST #create' do
context 'without premium license' do
let(:metric) { { prometheus_metric: { title: 'title' } } }
subject { post :create, params: project_params(metric) }
it_behaves_like 'not found'
end
context 'metric is valid' do
let(:valid_metric) { { prometheus_metric: { title: 'title', query: 'query', group: 'business', y_label: 'label', unit: 'u', legend: 'legend' } } }
......@@ -90,6 +124,14 @@ describe Projects::Prometheus::MetricsController do
end
describe 'DELETE #destroy' do
context 'without premium license' do
let(:metric) { create(:prometheus_metric, project: project) }
subject { delete :destroy, params: project_params(id: metric.id) }
it_behaves_like 'not found'
end
context 'format html' do
let!(:metric) { create(:prometheus_metric, project: project) }
......
......@@ -9,6 +9,8 @@ describe 'Prometheus custom metrics', :js do
project.add_maintainer(user)
sign_in(user)
stub_licensed_features(custom_prometheus_metrics: true)
visit(project_settings_integrations_path(project))
click_link('Prometheus')
......
# frozen_string_literal: true
require 'spec_helper'
describe EnvironmentsHelper do
let(:environment) { create(:environment) }
let(:project) { environment.project }
let(:user) { create(:user) }
describe '#metrics_data' do
subject { helper.metrics_data(project, environment) }
before do
allow(helper).to receive(:current_user).and_return(user)
allow(helper).to receive(:can?)
.with(user, :read_prometheus_alerts, project)
.and_return(true)
allow(helper).to receive(:can?)
.with(user, :admin_project, project)
.and_return(true)
end
it 'contains all keys' do
expect(subject).to include(
'settings-path' => edit_project_service_path(project, 'prometheus'),
'clusters-path' => project_clusters_path(project),
'current-environment-name': environment.name,
'documentation-path' => help_page_path('administration/monitoring/prometheus/index.md'),
'empty-getting-started-svg-path' => match_asset_path('/assets/illustrations/monitoring/getting_started.svg'),
'empty-loading-svg-path' => match_asset_path('/assets/illustrations/monitoring/loading.svg'),
'empty-no-data-svg-path' => match_asset_path('/assets/illustrations/monitoring/no_data.svg'),
'empty-unable-to-connect-svg-path' => match_asset_path('/assets/illustrations/monitoring/unable_to_connect.svg'),
'metrics-endpoint' => additional_metrics_project_environment_path(project, environment, format: :json),
'deployment-endpoint' => project_environment_deployments_path(project, environment, format: :json),
'environments-endpoint': project_environments_path(project, format: :json),
'project-path' => project_path(project),
'tags-path' => project_tags_path(project),
'has-metrics' => "#{environment.has_metrics?}",
'custom-metrics-path' => project_prometheus_metrics_path(project),
'validate-query-path' => validate_query_project_prometheus_metrics_path(project),
'custom-metrics-available' => 'false',
'alerts-endpoint' => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json),
'prometheus-alerts-available' => 'true'
)
expect(subject.keys).to contain_exactly(
'settings-path', 'clusters-path', :'current-environment-name', 'documentation-path',
'empty-getting-started-svg-path', 'empty-loading-svg-path', 'empty-no-data-svg-path',
'empty-unable-to-connect-svg-path', 'metrics-endpoint', 'deployment-endpoint',
:'environments-endpoint', 'project-path', 'tags-path', 'has-metrics', 'custom-metrics-path',
'validate-query-path', 'custom-metrics-available', 'alerts-endpoint', 'prometheus-alerts-available'
)
end
end
describe '#custom_metrics_available?' do
subject { helper.custom_metrics_available?(project) }
before do
project.add_maintainer(user)
stub_licensed_features(custom_prometheus_metrics: true)
allow(helper).to receive(:current_user).and_return(user)
allow(helper).to receive(:can?)
.with(user, :admin_project, project)
.and_return(true)
end
it 'returns true' do
expect(subject).to eq(true)
end
end
end
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