diff options
author | Peter Leitzen <pleitzen@gitlab.com> | 2019-04-02 19:38:49 +0300 |
---|---|---|
committer | rpereira2 <rpereira@gitlab.com> | 2019-04-04 23:42:33 +0300 |
commit | 1427ad4f5ce11d8327d9258eb3ffc62803c1a304 (patch) | |
tree | 346975722e9b8eaf9a634eb4a84f831783e1bac5 /spec/controllers | |
parent | e1a167ee234fbb8886ac1a882002b76adeb67edf (diff) |
Streamline controller specs
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/projects/environments/prometheus_api_controller_spec.rb | 157 |
1 files changed, 74 insertions, 83 deletions
diff --git a/spec/controllers/projects/environments/prometheus_api_controller_spec.rb b/spec/controllers/projects/environments/prometheus_api_controller_spec.rb index d943d006ae1..b1b68bb42d8 100644 --- a/spec/controllers/projects/environments/prometheus_api_controller_spec.rb +++ b/spec/controllers/projects/environments/prometheus_api_controller_spec.rb @@ -14,121 +14,112 @@ describe Projects::Environments::PrometheusApiController do describe 'GET #proxy' do 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 - - before do - allow(Prometheus::ProxyService).to receive(:new) - .with(environment, 'GET', 'query', anything) - .and_return(prometheus_proxy_service) + context 'with valid requests' do + before do + allow(Prometheus::ProxyService).to receive(:new) + .with(environment, 'GET', 'query', anything) + .and_return(prometheus_proxy_service) - allow(prometheus_proxy_service).to receive(:execute) - .and_return(prometheus_response) - end + allow(prometheus_proxy_service).to receive(:execute) + .and_return(service_result) + end - it 'returns prometheus response' do - get :proxy, params: environment_params + 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) } - expect(response).to have_gitlab_http_status(:ok) - expect(json_response).to eq(json_response_body) - end + it 'returns prometheus response' do + get :proxy, params: environment_params - it 'filters params' do - get :proxy, params: environment_params({ extra_param: 'dangerous value' }) + expect(response).to have_gitlab_http_status(:ok) + expect(json_response).to eq(prometheus_json_body) + end - expect(Prometheus::ProxyService).to have_received(:new) - .with(environment, 'GET', 'query', ActionController::Parameters.new({ 'query' => '1' }).permit!) - end + it 'filters params' do + get :proxy, params: environment_params({ extra_param: 'dangerous value' }) - context 'Prometheus::ProxyService returns nil' do - before do - allow(prometheus_proxy_service).to receive(:execute) - .and_return(nil) + expect(Prometheus::ProxyService).to have_received(:new) + .with(environment, 'GET', 'query', ActionController::Parameters.new({ 'query' => '1' }).permit!) + end end - it 'returns 202 accepted' do - get :proxy, params: environment_params - - expect(json_response['status']).to eq('processing') - expect(json_response['message']).to eq('Not ready yet. Try again later.') - expect(response).to have_gitlab_http_status(:accepted) - end - end + context 'with nil result' do + let(:service_result) { nil } - context 'Prometheus::ProxyService returns status success' do - let(:service_response) { { http_status: 404, status: :success, body: '{"body": "value"}' } } + it 'returns 202 accepted' do + get :proxy, params: environment_params - before do - allow(prometheus_proxy_service).to receive(:execute) - .and_return(service_response) + expect(json_response['status']).to eq('processing') + expect(json_response['message']).to eq('Not ready yet. Try again later.') + expect(response).to have_gitlab_http_status(:accepted) + end end - it 'returns body' do - get :proxy, params: environment_params + context 'with 404 result' do + let(:service_result) { { http_status: 404, status: :success, body: '{"body": "value"}' } } - expect(response).to have_gitlab_http_status(:not_found) - expect(json_response['body']).to eq('value') - end - end + it 'returns body' do + get :proxy, params: environment_params - context 'Prometheus::ProxyService returns status error' do - before do - allow(prometheus_proxy_service).to receive(:execute) - .and_return(service_response) + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['body']).to eq('value') + end end - context 'with http_status' do - let(:service_response) do - { http_status: :service_unavailable, status: :error, message: 'error message' } - end + context 'with error result' do + context 'with http_status' do + let(:service_result) do + { http_status: :service_unavailable, status: :error, message: 'error message' } + end - it 'sets the http response status code' do - get :proxy, params: environment_params + it 'sets the http response status code' do + get :proxy, params: environment_params - expect(response).to have_gitlab_http_status(:service_unavailable) - expect(json_response['status']).to eq('error') - expect(json_response['message']).to eq('error message') + expect(response).to have_gitlab_http_status(:service_unavailable) + expect(json_response['status']).to eq('error') + expect(json_response['message']).to eq('error message') + end end - end - context 'without http_status' do - let(:service_response) { { status: :error, message: 'error message' } } + context 'without http_status' do + let(:service_result) { { status: :error, message: 'error message' } } - it 'returns message' do - get :proxy, params: environment_params + it 'returns bad_request' do + get :proxy, params: environment_params - expect(response).to have_gitlab_http_status(:bad_request) - expect(json_response['status']).to eq('error') - expect(json_response['message']).to eq('error message') + expect(response).to have_gitlab_http_status(:bad_request) + expect(json_response['status']).to eq('error') + expect(json_response['message']).to eq('error message') + end end end end - context 'with anonymous user' do - before do - sign_out(user) - end + context 'with inappropriate requests' do + context 'with anonymous user' do + before do + sign_out(user) + end - it 'redirects to signin page' do - get :proxy, params: environment_params + it 'redirects to signin page' do + get :proxy, params: environment_params - expect(response).to redirect_to(new_user_session_path) + expect(response).to redirect_to(new_user_session_path) + end end - end - context 'without correct permissions' do - before do - project.team.truncate - end + context 'without correct permissions' do + before do + project.team.truncate + end - it 'returns 404' do - get :proxy, params: environment_params + it 'returns 404' do + get :proxy, params: environment_params - expect(response).to have_gitlab_http_status(:not_found) + expect(response).to have_gitlab_http_status(:not_found) + end end end end @@ -142,6 +133,6 @@ describe Projects::Environments::PrometheusApiController do project_id: project, proxy_path: 'query', query: '1' - }.merge(params) + }.reverse_merge(params) end end |