diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 18:25:58 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 18:25:58 +0000 |
commit | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (patch) | |
tree | fb69158581673816a8cd895f9d352dcb3c678b1e /spec/graphql/mutations | |
parent | d16b2e8639e99961de6ddc93909f3bb5c1445ba1 (diff) | |
download | gitlab-ce-a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4.tar.gz |
Add latest changes from gitlab-org/gitlab@14-0-stable-eev14.0.0-rc42
Diffstat (limited to 'spec/graphql/mutations')
8 files changed, 261 insertions, 34 deletions
diff --git a/spec/graphql/mutations/ci/runner/delete_spec.rb b/spec/graphql/mutations/ci/runner/delete_spec.rb new file mode 100644 index 00000000000..82873c96c3e --- /dev/null +++ b/spec/graphql/mutations/ci/runner/delete_spec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::Ci::Runner::Delete do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + let_it_be(:runner) { create(:ci_runner) } + + let(:current_ctx) { { current_user: user } } + + let(:mutation_params) do + { + id: runner.to_global_id + } + end + + specify { expect(described_class).to require_graphql_authorizations(:delete_runner) } + + describe '#resolve' do + subject do + sync(resolve(described_class, args: mutation_params, ctx: current_ctx)) + end + + context 'when the user cannot admin the runner' do + it 'raises an error' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + + context 'with invalid params' do + it 'raises an error' do + mutation_params[:id] = "invalid-id" + + expect { subject }.to raise_error(::GraphQL::CoercionError) + end + end + + context 'when required arguments are missing' do + let(:mutation_params) { {} } + + it 'raises an error' do + expect { subject }.to raise_error(ArgumentError, "missing keyword: :id") + end + end + + context 'when user can delete owned runner' do + let_it_be(:project) { create(:project, creator_id: user.id) } + let_it_be(:project_runner, reload: true) { create(:ci_runner, :project, description: 'Project runner', projects: [project]) } + + before_all do + project.add_maintainer(user) + end + + context 'with one associated project' do + it 'deletes runner' do + mutation_params[:id] = project_runner.to_global_id + + expect { subject }.to change { Ci::Runner.count }.by(-1) + expect(subject[:errors]).to be_empty + end + end + + context 'with more than one associated project' do + let_it_be(:project2) { create(:project, creator_id: user.id) } + let_it_be(:two_projects_runner) { create(:ci_runner, :project, description: 'Two projects runner', projects: [project, project2]) } + + before_all do + project2.add_maintainer(user) + end + + it 'does not delete project runner' do + mutation_params[:id] = two_projects_runner.to_global_id + + expect { subject }.not_to change { Ci::Runner.count } + expect(subject[:errors]).to contain_exactly("Runner #{two_projects_runner.to_global_id} associated with more than one project") + end + end + end + + context 'when admin can delete runner', :enable_admin_mode do + let(:admin_user) { create(:user, :admin) } + let(:current_ctx) { { current_user: admin_user } } + + it 'deletes runner' do + expect { subject }.to change { Ci::Runner.count }.by(-1) + expect(subject[:errors]).to be_empty + end + end + end +end diff --git a/spec/graphql/mutations/ci/runner/update_spec.rb b/spec/graphql/mutations/ci/runner/update_spec.rb new file mode 100644 index 00000000000..3db0d552a05 --- /dev/null +++ b/spec/graphql/mutations/ci/runner/update_spec.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::Ci::Runner::Update do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + let_it_be(:runner) { create(:ci_runner, active: true, locked: false, run_untagged: true) } + + let(:current_ctx) { { current_user: user } } + let(:mutated_runner) { subject[:runner] } + + let(:mutation_params) do + { + id: runner.to_global_id, + description: 'updated description' + } + end + + specify { expect(described_class).to require_graphql_authorizations(:update_runner) } + + describe '#resolve' do + subject do + sync(resolve(described_class, args: mutation_params, ctx: current_ctx)) + end + + context 'when the user cannot admin the runner' do + it 'raises an error' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + + context 'with invalid params' do + it 'raises an error' do + mutation_params[:id] = "invalid-id" + + expect { subject }.to raise_error(::GraphQL::CoercionError) + end + end + + context 'when required arguments are missing' do + let(:mutation_params) { {} } + + it 'raises an error' do + expect { subject }.to raise_error(ArgumentError, "missing keyword: :id") + end + end + + context 'when user can update runner', :enable_admin_mode do + let(:admin_user) { create(:user, :admin) } + let(:current_ctx) { { current_user: admin_user } } + + let(:mutation_params) do + { + id: runner.to_global_id, + description: 'updated description', + maximum_timeout: 900, + access_level: 'ref_protected', + active: false, + locked: true, + run_untagged: false, + tag_list: %w(tag1 tag2) + } + end + + context 'with valid arguments' do + it 'updates runner with correct values' do + expected_attributes = mutation_params.except(:id, :tag_list) + + subject + + expect(subject[:errors]).to be_empty + expect(subject[:runner]).to be_an_instance_of(Ci::Runner) + expect(subject[:runner]).to have_attributes(expected_attributes) + expect(subject[:runner].tag_list).to contain_exactly(*mutation_params[:tag_list]) + expect(runner.reload).to have_attributes(expected_attributes) + expect(runner.tag_list).to contain_exactly(*mutation_params[:tag_list]) + end + end + + context 'with out-of-range maximum_timeout and missing tag_list' do + it 'returns a descriptive error' do + mutation_params[:maximum_timeout] = 100 + mutation_params.delete(:tag_list) + + expect(subject[:errors]).to contain_exactly( + 'Maximum timeout needs to be at least 10 minutes', + 'Tags list can not be empty when runner is not allowed to pick untagged jobs' + ) + end + end + end + end +end diff --git a/spec/graphql/mutations/commits/create_spec.rb b/spec/graphql/mutations/commits/create_spec.rb index 152b5d87da0..097e70bada6 100644 --- a/spec/graphql/mutations/commits/create_spec.rb +++ b/spec/graphql/mutations/commits/create_spec.rb @@ -74,6 +74,10 @@ RSpec.describe Mutations::Commits::Create do expect(commit_pipeline_path).to match(%r(pipelines/sha/\w+)) end + it 'returns the content of the commit' do + expect(subject[:content]).to eq(actions.pluck(:content)) + end + it 'returns a new commit' do expect(mutated_commit).to have_attributes(message: message, project: project) expect(subject[:errors]).to be_empty @@ -166,6 +170,7 @@ RSpec.describe Mutations::Commits::Create do it 'returns a new commit' do expect(mutated_commit).to have_attributes(message: message, project: project) expect(subject[:errors]).to be_empty + expect(subject[:content]).to eq(actions.pluck(:content)) expect_to_contain_deltas([ a_hash_including(a_mode: '0', b_mode: '100644', new_file: true, new_path: 'ANOTHER_FILE.md') diff --git a/spec/graphql/mutations/design_management/upload_spec.rb b/spec/graphql/mutations/design_management/upload_spec.rb index ada88b7652c..1e585e55be4 100644 --- a/spec/graphql/mutations/design_management/upload_spec.rb +++ b/spec/graphql/mutations/design_management/upload_spec.rb @@ -105,7 +105,7 @@ RSpec.describe Mutations::DesignManagement::Upload do context "with a valid design" do it "returns the updated designs" do - expect(resolve[:errors]).to eq [] + expect(resolve[:errors]).to be_empty expect(resolve[:designs].map(&:filename)).to contain_exactly("dk.png") end end diff --git a/spec/graphql/mutations/issues/set_subscription_spec.rb b/spec/graphql/mutations/issues/set_subscription_spec.rb index 9e05a136c0b..7e2c3d93c51 100644 --- a/spec/graphql/mutations/issues/set_subscription_spec.rb +++ b/spec/graphql/mutations/issues/set_subscription_spec.rb @@ -3,8 +3,38 @@ require 'spec_helper' RSpec.describe Mutations::Issues::SetSubscription do - it_behaves_like 'a subscribeable graphql resource' do - let_it_be(:resource) { create(:issue) } - let(:permission_name) { :update_issue } + let_it_be_with_reload(:project) { create(:project) } + let_it_be_with_reload(:resource) { create(:issue, project: project) } + let_it_be(:user) { create(:user) } + + specify { expect(described_class).to require_graphql_authorizations(:update_subscription) } + + context 'when user does not have access to the project' do + it_behaves_like 'a subscribeable not accessible graphql resource' + end + + context 'when user is developer member of the project' do + before do + project.add_developer(user) + end + + it_behaves_like 'a subscribeable graphql resource' + end + + context 'when the project is public' do + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + end + + it_behaves_like 'a subscribeable graphql resource' + end + + context 'when the project is public but the issue is confidential' do + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + resource.update!(confidential: true) + end + + it_behaves_like 'a subscribeable not accessible graphql resource' end end diff --git a/spec/graphql/mutations/issues/update_spec.rb b/spec/graphql/mutations/issues/update_spec.rb index 6d6a5b94219..bd780477658 100644 --- a/spec/graphql/mutations/issues/update_spec.rb +++ b/spec/graphql/mutations/issues/update_spec.rb @@ -69,33 +69,17 @@ RSpec.describe Mutations::Issues::Update do context 'when changing state' do let_it_be_with_refind(:issue) { create(:issue, project: project, state: :opened) } - before do - mutation_params[:state_event] = state_event - end - - context 'when state_event is close' do - let_it_be(:removable_label) { create(:label, project: project, remove_on_close: true, issues: [issue]) } + it 'closes issue' do + mutation_params[:state_event] = 'close' - let(:state_event) { 'close' } - - it 'closes issue' do - expect do - subject - issue.reload - end.to change(issue, :state).from('opened').to('closed').and( - change { issue.label_ids }.from([removable_label.id]).to([]) - ) - end + expect { subject }.to change { issue.reload.state }.from('opened').to('closed') end - context 'when state_event is reopen' do - let(:state_event) { 'reopen' } - - it 'reopens issue' do - issue.close + it 'reopens issue' do + issue.close + mutation_params[:state_event] = 'reopen' - expect { subject }.to change { issue.reload.state }.from('closed').to('opened') - end + expect { subject }.to change { issue.reload.state }.from('closed').to('opened') end end diff --git a/spec/graphql/mutations/merge_requests/set_subscription_spec.rb b/spec/graphql/mutations/merge_requests/set_subscription_spec.rb index 600053637c9..377042f068c 100644 --- a/spec/graphql/mutations/merge_requests/set_subscription_spec.rb +++ b/spec/graphql/mutations/merge_requests/set_subscription_spec.rb @@ -3,8 +3,30 @@ require 'spec_helper' RSpec.describe Mutations::MergeRequests::SetSubscription do - it_behaves_like 'a subscribeable graphql resource' do - let_it_be(:resource) { create(:merge_request) } - let(:permission_name) { :update_merge_request } + let_it_be_with_reload(:project) { create(:project) } + let_it_be(:user) { create(:user) } + + let(:resource) { create(:merge_request, source_project: project, target_project: project) } + + specify { expect(described_class).to require_graphql_authorizations(:update_subscription) } + + context 'when user does not have access to the project' do + it_behaves_like 'a subscribeable not accessible graphql resource' + end + + context 'when user is developer member of the project' do + before do + project.add_developer(user) + end + + it_behaves_like 'a subscribeable graphql resource' + end + + context 'when the project is public' do + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + end + + it_behaves_like 'a subscribeable graphql resource' end end diff --git a/spec/graphql/mutations/todos/mark_all_done_spec.rb b/spec/graphql/mutations/todos/mark_all_done_spec.rb index f3b6bf52ef7..1e12d86aa18 100644 --- a/spec/graphql/mutations/todos/mark_all_done_spec.rb +++ b/spec/graphql/mutations/todos/mark_all_done_spec.rb @@ -21,26 +21,25 @@ RSpec.describe Mutations::Todos::MarkAllDone do describe '#resolve' do it 'marks all pending todos as done' do - updated_todo_ids, todos = mutation_for(current_user).resolve.values_at(:updated_ids, :todos) + todos = mutation_for(current_user).resolve[:todos] expect(todo1.reload.state).to eq('done') expect(todo2.reload.state).to eq('done') expect(todo3.reload.state).to eq('done') expect(other_user_todo.reload.state).to eq('pending') - expect(updated_todo_ids).to contain_exactly(todo1.id, todo3.id) expect(todos).to contain_exactly(todo1, todo3) end it 'behaves as expected if there are no todos for the requesting user' do - updated_todo_ids = mutation_for(user3).resolve.dig(:updated_ids) + todos = mutation_for(user3).resolve[:todos] expect(todo1.reload.state).to eq('pending') expect(todo2.reload.state).to eq('done') expect(todo3.reload.state).to eq('pending') expect(other_user_todo.reload.state).to eq('pending') - expect(updated_todo_ids).to be_empty + expect(todos).to be_empty end context 'when user is not logged in' do |