Commit 1427ad4f authored by Peter Leitzen's avatar Peter Leitzen Committed by rpereira2

Streamline controller specs

parent e1a167ee
...@@ -25,10 +25,8 @@ class Projects::Environments::PrometheusApiController < Projects::ApplicationCon ...@@ -25,10 +25,8 @@ class Projects::Environments::PrometheusApiController < Projects::ApplicationCon
if result[:status] == :success if result[:status] == :success
render status: result[:http_status], json: result[:body] render status: result[:http_status], json: result[:body]
else else
render status: result[:http_status] || :bad_request, json: { render status: result[:http_status] || :bad_request,
status: result[:status], json: { status: result[:status], message: result[:message] }
message: result[:message]
}
end end
end end
......
...@@ -14,27 +14,27 @@ describe Projects::Environments::PrometheusApiController do ...@@ -14,27 +14,27 @@ describe Projects::Environments::PrometheusApiController do
describe 'GET #proxy' do describe 'GET #proxy' do
let(:prometheus_proxy_service) { instance_double(Prometheus::ProxyService) } let(:prometheus_proxy_service) { instance_double(Prometheus::ProxyService) }
let(:prometheus_response) { { status: :success, body: response_body } }
let(:json_response_body) { JSON.parse(response_body) }
let(:response_body) do
"{\"status\":\"success\",\"data\":{\"resultType\":\"scalar\",\"result\":[1553864609.117,\"1\"]}}"
end
context 'with valid requests' do
before do before do
allow(Prometheus::ProxyService).to receive(:new) allow(Prometheus::ProxyService).to receive(:new)
.with(environment, 'GET', 'query', anything) .with(environment, 'GET', 'query', anything)
.and_return(prometheus_proxy_service) .and_return(prometheus_proxy_service)
allow(prometheus_proxy_service).to receive(:execute) allow(prometheus_proxy_service).to receive(:execute)
.and_return(prometheus_response) .and_return(service_result)
end end
context 'with success result' do
let(:service_result) { { status: :success, body: prometheus_body } }
let(:prometheus_body) { '{"status":"success"}' }
let(:prometheus_json_body) { JSON.parse(prometheus_body) }
it 'returns prometheus response' do it 'returns prometheus response' do
get :proxy, params: environment_params get :proxy, params: environment_params
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq(json_response_body) expect(json_response).to eq(prometheus_json_body)
end end
it 'filters params' do it 'filters params' do
...@@ -43,13 +43,11 @@ describe Projects::Environments::PrometheusApiController do ...@@ -43,13 +43,11 @@ describe Projects::Environments::PrometheusApiController do
expect(Prometheus::ProxyService).to have_received(:new) expect(Prometheus::ProxyService).to have_received(:new)
.with(environment, 'GET', 'query', ActionController::Parameters.new({ 'query' => '1' }).permit!) .with(environment, 'GET', 'query', ActionController::Parameters.new({ 'query' => '1' }).permit!)
end end
context 'Prometheus::ProxyService returns nil' do
before do
allow(prometheus_proxy_service).to receive(:execute)
.and_return(nil)
end end
context 'with nil result' do
let(:service_result) { nil }
it 'returns 202 accepted' do it 'returns 202 accepted' do
get :proxy, params: environment_params get :proxy, params: environment_params
...@@ -59,13 +57,8 @@ describe Projects::Environments::PrometheusApiController do ...@@ -59,13 +57,8 @@ describe Projects::Environments::PrometheusApiController do
end end
end end
context 'Prometheus::ProxyService returns status success' do context 'with 404 result' do
let(:service_response) { { http_status: 404, status: :success, body: '{"body": "value"}' } } let(:service_result) { { http_status: 404, status: :success, body: '{"body": "value"}' } }
before do
allow(prometheus_proxy_service).to receive(:execute)
.and_return(service_response)
end
it 'returns body' do it 'returns body' do
get :proxy, params: environment_params get :proxy, params: environment_params
...@@ -75,14 +68,9 @@ describe Projects::Environments::PrometheusApiController do ...@@ -75,14 +68,9 @@ describe Projects::Environments::PrometheusApiController do
end end
end end
context 'Prometheus::ProxyService returns status error' do context 'with error result' do
before do
allow(prometheus_proxy_service).to receive(:execute)
.and_return(service_response)
end
context 'with http_status' do context 'with http_status' do
let(:service_response) do let(:service_result) do
{ http_status: :service_unavailable, status: :error, message: 'error message' } { http_status: :service_unavailable, status: :error, message: 'error message' }
end end
...@@ -96,9 +84,9 @@ describe Projects::Environments::PrometheusApiController do ...@@ -96,9 +84,9 @@ describe Projects::Environments::PrometheusApiController do
end end
context 'without http_status' do context 'without http_status' do
let(:service_response) { { status: :error, message: 'error message' } } let(:service_result) { { status: :error, message: 'error message' } }
it 'returns message' do it 'returns bad_request' do
get :proxy, params: environment_params get :proxy, params: environment_params
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
...@@ -107,7 +95,9 @@ describe Projects::Environments::PrometheusApiController do ...@@ -107,7 +95,9 @@ describe Projects::Environments::PrometheusApiController do
end end
end end
end end
end
context 'with inappropriate requests' do
context 'with anonymous user' do context 'with anonymous user' do
before do before do
sign_out(user) sign_out(user)
...@@ -132,6 +122,7 @@ describe Projects::Environments::PrometheusApiController do ...@@ -132,6 +122,7 @@ describe Projects::Environments::PrometheusApiController do
end end
end end
end end
end
private private
...@@ -142,6 +133,6 @@ describe Projects::Environments::PrometheusApiController do ...@@ -142,6 +133,6 @@ describe Projects::Environments::PrometheusApiController do
project_id: project, project_id: project,
proxy_path: 'query', proxy_path: 'query',
query: '1' query: '1'
}.merge(params) }.reverse_merge(params)
end 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