summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/grafana_api_controller_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/controllers/projects/grafana_api_controller_spec.rb')
-rw-r--r--spec/controllers/projects/grafana_api_controller_spec.rb71
1 files changed, 71 insertions, 0 deletions
diff --git a/spec/controllers/projects/grafana_api_controller_spec.rb b/spec/controllers/projects/grafana_api_controller_spec.rb
index 352a364295b..0ef96514961 100644
--- a/spec/controllers/projects/grafana_api_controller_spec.rb
+++ b/spec/controllers/projects/grafana_api_controller_spec.rb
@@ -94,4 +94,75 @@ describe Projects::GrafanaApiController do
end
end
end
+
+ describe 'GET #metrics_dashboard' do
+ let(:service_result) { { status: :success, dashboard: '{}' } }
+ let(:params) do
+ {
+ format: :json,
+ embedded: true,
+ grafana_url: 'https://grafana.example.com',
+ namespace_id: project.namespace.full_path,
+ project_id: project.name
+ }
+ end
+
+ before do
+ allow(Gitlab::Metrics::Dashboard::Finder)
+ .to receive(:find)
+ .and_return(service_result)
+ end
+
+ context 'when the result is still processing' do
+ let(:service_result) { nil }
+
+ it 'returns 204 no content' do
+ get :metrics_dashboard, params: params
+
+ expect(response).to have_gitlab_http_status(:no_content)
+ end
+ end
+
+ context 'when the result was successful' do
+ it 'returns the dashboard response' do
+ get :metrics_dashboard, params: params
+
+ expect(response).to have_gitlab_http_status(:ok)
+ expect(json_response).to eq({
+ 'dashboard' => '{}',
+ 'status' => 'success'
+ })
+ end
+ end
+
+ context 'when an error has occurred' do
+ shared_examples_for 'error response' do |http_status|
+ it "returns #{http_status}" do
+ get :metrics_dashboard, params: params
+
+ expect(response).to have_gitlab_http_status(http_status)
+ expect(json_response['status']).to eq('error')
+ expect(json_response['message']).to eq('error message')
+ end
+ end
+
+ context 'with an error accessing grafana' do
+ let(:service_result) do
+ {
+ http_status: :service_unavailable,
+ status: :error,
+ message: 'error message'
+ }
+ end
+
+ it_behaves_like 'error response', :service_unavailable
+ end
+
+ context 'with a processing error' do
+ let(:service_result) { { status: :error, message: 'error message' } }
+
+ it_behaves_like 'error response', :bad_request
+ end
+ end
+ end
end