diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-26 18:08:56 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-26 18:08:56 +0300 |
commit | 17ab40ca089e1aef61a83f77ab6df62a72f6ce06 (patch) | |
tree | 8eb149293eee90ec2750b6ac5e46a111a806424e /spec/controllers | |
parent | 66d4203791a01fdedf668a78818a229ea2c07aad (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/projects/performance_monitoring/dashboards_controller_spec.rb | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/spec/controllers/projects/performance_monitoring/dashboards_controller_spec.rb b/spec/controllers/projects/performance_monitoring/dashboards_controller_spec.rb index 1c29b68dc24..6a53e8f3dbf 100644 --- a/spec/controllers/projects/performance_monitoring/dashboards_controller_spec.rb +++ b/spec/controllers/projects/performance_monitoring/dashboards_controller_spec.rb @@ -129,4 +129,130 @@ describe Projects::PerformanceMonitoring::DashboardsController do end end end + + describe 'PUT #update' do + context 'authenticated user' do + before do + sign_in(user) + end + + let(:file_content) do + { + "dashboard" => "Dashboard Title", + "panel_groups" => [{ + "group" => "Group Title", + "panels" => [{ + "type" => "area-chart", + "title" => "Chart Title", + "y_label" => "Y-Axis", + "metrics" => [{ + "id" => "metric_of_ages", + "unit" => "count", + "label" => "Metric of Ages", + "query_range" => "http_requests_total" + }] + }] + }] + } + end + + let(:params) do + { + namespace_id: namespace, + project_id: project, + dashboard: dashboard, + file_name: file_name, + file_content: file_content, + commit_message: commit_message, + branch: branch_name, + format: :json + } + end + + context 'project with repository feature' do + context 'with rights to push to the repository' do + before do + project.add_maintainer(user) + end + + context 'valid parameters' do + context 'request format json' do + let(:update_dashboard_service_params) { params.except(:namespace_id, :project_id, :format) } + + let(:update_dashboard_service_results) do + { + status: :success, + http_status: :created, + dashboard: { + path: ".gitlab/dashboards/custom_dashboard.yml", + display_name: "custom_dashboard.yml", + default: false, + system_dashboard: false + } + } + end + + let(:update_dashboard_service) { instance_double(::Metrics::Dashboard::UpdateDashboardService, execute: update_dashboard_service_results) } + + it 'returns path to new file' do + allow(controller).to receive(:repository).and_return(repository) + allow(repository).to receive(:find_branch).and_return(branch) + allow(::Metrics::Dashboard::UpdateDashboardService).to receive(:new).with(project, user, update_dashboard_service_params).and_return(update_dashboard_service) + + put :update, params: params + + expect(response).to have_gitlab_http_status :created + expect(response).to set_flash[:notice].to eq("Your dashboard has been updated. You can <a href=\"/-/ide/project/#{namespace.path}/#{project.name}/edit/#{branch_name}/-/.gitlab/dashboards/#{file_name}\">edit it here</a>.") + expect(json_response).to eq('status' => 'success', 'dashboard' => { 'default' => false, 'display_name' => "custom_dashboard.yml", 'path' => ".gitlab/dashboards/#{file_name}", 'system_dashboard' => false }) + end + + context 'UpdateDashboardService failure' do + it 'returns json with failure message' do + allow(::Metrics::Dashboard::UpdateDashboardService).to receive(:new).and_return(double(execute: { status: :error, message: 'something went wrong', http_status: :bad_request })) + + put :update, params: params + + expect(response).to have_gitlab_http_status :bad_request + expect(json_response).to eq('error' => 'something went wrong') + end + end + end + end + + context 'missing branch' do + let(:branch_name) { nil } + + it 'raises responds with :bad_request status code and error message' do + put :update, params: params + + expect(response).to have_gitlab_http_status :bad_request + expect(json_response).to eq('error' => "Request parameter branch is missing.") + end + end + end + + context 'without rights to push to repository' do + before do + project.add_guest(user) + end + + it 'responds with :forbidden status code' do + put :update, params: params + + expect(response).to have_gitlab_http_status :forbidden + end + end + end + + context 'project without repository feature' do + let!(:project) { create(:project, name: 'dashboard-project', namespace: namespace) } + + it 'responds with :not_found status code' do + put :update, params: params + + expect(response).to have_gitlab_http_status :not_found + end + end + end + end end |