diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /spec/requests/api/graphql/mutations | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'spec/requests/api/graphql/mutations')
7 files changed, 336 insertions, 3 deletions
diff --git a/spec/requests/api/graphql/mutations/ci/job_cancel_spec.rb b/spec/requests/api/graphql/mutations/ci/job_cancel_spec.rb new file mode 100644 index 00000000000..ee0f0a9bccb --- /dev/null +++ b/spec/requests/api/graphql/mutations/ci/job_cancel_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe "JobCancel" do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:pipeline) { create(:ci_pipeline, project: project, user: user) } + let_it_be(:job) { create(:ci_build, pipeline: pipeline, name: 'build') } + + let(:mutation) do + variables = { + id: job.to_global_id.to_s + } + graphql_mutation(:job_cancel, variables, + <<-QL + errors + job { + id + } + QL + ) + end + + let(:mutation_response) { graphql_mutation_response(:job_cancel) } + + it 'returns an error if the user is not allowed to cancel the job' do + project.add_developer(user) + post_graphql_mutation(mutation, current_user: user) + + expect(graphql_errors).not_to be_empty + end + + it 'cancels a job' do + job_id = ::Gitlab::GlobalId.build(job, id: job.id).to_s + project.add_maintainer(user) + post_graphql_mutation(mutation, current_user: user) + + expect(response).to have_gitlab_http_status(:success) + expect(mutation_response['job']['id']).to eq(job_id) + expect(job.reload.status).to eq('canceled') + end +end diff --git a/spec/requests/api/graphql/mutations/ci/job_unschedule_spec.rb b/spec/requests/api/graphql/mutations/ci/job_unschedule_spec.rb new file mode 100644 index 00000000000..4ddc019a2b5 --- /dev/null +++ b/spec/requests/api/graphql/mutations/ci/job_unschedule_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'JobUnschedule' do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:pipeline) { create(:ci_pipeline, project: project, user: user) } + let_it_be(:job) { create(:ci_build, :scheduled, pipeline: pipeline, name: 'build') } + + let(:mutation) do + variables = { + id: job.to_global_id.to_s + } + graphql_mutation(:job_unschedule, variables, + <<-QL + errors + job { + id + } + QL + ) + end + + let(:mutation_response) { graphql_mutation_response(:job_unschedule) } + + it 'returns an error if the user is not allowed to unschedule the job' do + project.add_developer(user) + + post_graphql_mutation(mutation, current_user: user) + + expect(graphql_errors).not_to be_empty + expect(job.reload.status).to eq('scheduled') + end + + it 'unschedules a job' do + project.add_maintainer(user) + + job_id = ::Gitlab::GlobalId.build(job, id: job.id).to_s + post_graphql_mutation(mutation, current_user: user) + + expect(response).to have_gitlab_http_status(:success) + expect(mutation_response['job']['id']).to eq(job_id) + expect(job.reload.status).to eq('manual') + end +end diff --git a/spec/requests/api/graphql/mutations/groups/update_spec.rb b/spec/requests/api/graphql/mutations/groups/update_spec.rb new file mode 100644 index 00000000000..b9dfb8e37ab --- /dev/null +++ b/spec/requests/api/graphql/mutations/groups/update_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'GroupUpdate' do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + let_it_be_with_reload(:group) { create(:group) } + + let(:variables) do + { + full_path: group.full_path, + shared_runners_setting: 'DISABLED_WITH_OVERRIDE' + } + end + + let(:mutation) { graphql_mutation(:group_update, variables) } + + context 'when unauthorized' do + shared_examples 'unauthorized' do + it 'returns an error' do + post_graphql_mutation(mutation, current_user: user) + + expect(graphql_errors).not_to be_empty + end + end + + context 'when not a group member' do + it_behaves_like 'unauthorized' + end + + context 'when a non-admin group member' do + before do + group.add_developer(user) + end + + it_behaves_like 'unauthorized' + end + end + + context 'when authorized' do + before do + group.add_owner(user) + end + + it 'updates shared runners settings' do + post_graphql_mutation(mutation, current_user: user) + + expect(response).to have_gitlab_http_status(:success) + expect(graphql_errors).to be_nil + expect(group.reload.shared_runners_setting).to eq(variables[:shared_runners_setting].downcase) + end + + context 'when bad arguments are provided' do + let(:variables) { { full_path: '', shared_runners_setting: 'INVALID' } } + + it 'returns the errors' do + post_graphql_mutation(mutation, current_user: user) + + expect(graphql_errors).not_to be_empty + expect(group.reload.shared_runners_setting).to eq('enabled') + end + end + end +end diff --git a/spec/requests/api/graphql/mutations/issues/set_due_date_spec.rb b/spec/requests/api/graphql/mutations/issues/set_due_date_spec.rb index ea5be9f9852..72e47a98373 100644 --- a/spec/requests/api/graphql/mutations/issues/set_due_date_spec.rb +++ b/spec/requests/api/graphql/mutations/issues/set_due_date_spec.rb @@ -68,7 +68,7 @@ RSpec.describe 'Setting Due Date of an issue' do it 'returns an error' do post_graphql_mutation(mutation, current_user: current_user) - expect(graphql_errors).to include(a_hash_including('message' => /Argument dueDate must be provided/)) + expect(graphql_errors).to include(a_hash_including('message' => /Arguments must be provided: dueDate/)) end end diff --git a/spec/requests/api/graphql/mutations/issues/update_spec.rb b/spec/requests/api/graphql/mutations/issues/update_spec.rb index b3e1ab62e54..c3aaf090703 100644 --- a/spec/requests/api/graphql/mutations/issues/update_spec.rb +++ b/spec/requests/api/graphql/mutations/issues/update_spec.rb @@ -8,6 +8,8 @@ RSpec.describe 'Update of an existing issue' do let_it_be(:current_user) { create(:user) } let_it_be(:project) { create(:project, :public) } let_it_be(:issue) { create(:issue, project: project) } + let_it_be(:label1) { create(:label, project: project) } + let_it_be(:label2) { create(:label, project: project) } let(:input) do { @@ -20,7 +22,9 @@ RSpec.describe 'Update of an existing issue' do } end - let(:mutation) { graphql_mutation(:update_issue, input.merge(project_path: project.full_path, locked: true)) } + let(:extra_params) { { project_path: project.full_path, locked: true } } + let(:input_params) { input.merge(extra_params) } + let(:mutation) { graphql_mutation(:update_issue, input_params) } let(:mutation_response) { graphql_mutation_response(:update_issue) } context 'the user is not allowed to update issue' do @@ -39,5 +43,82 @@ RSpec.describe 'Update of an existing issue' do expect(mutation_response['issue']).to include(input) expect(mutation_response['issue']).to include('discussionLocked' => true) end + + context 'setting labels' do + let(:mutation) do + graphql_mutation(:update_issue, input_params) do + <<~QL + issue { + labels { + nodes { + id + } + } + } + errors + QL + end + end + + context 'reset labels' do + let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.id, label2.id] }) } + + it 'resets labels' do + post_graphql_mutation(mutation, current_user: current_user) + + expect(response).to have_gitlab_http_status(:success) + expect(json_response['errors']).to be_nil + expect(mutation_response['issue']['labels']).to include({ "nodes" => [{ "id" => label1.to_global_id.to_s }, { "id" => label2.to_global_id.to_s }] }) + end + + context 'reset labels and add labels' do + let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.id], addLabelIds: [label2.id] }) } + + it 'returns error for mutually exclusive arguments' do + post_graphql_mutation(mutation, current_user: current_user) + + expect(response).to have_gitlab_http_status(:success) + expect(json_response['errors'].first['message']).to eq('labelIds is mutually exclusive with any of addLabelIds or removeLabelIds') + expect(mutation_response).to be_nil + end + end + + context 'reset labels and remove labels' do + let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.id], removeLabelIds: [label2.id] }) } + + it 'returns error for mutually exclusive arguments' do + post_graphql_mutation(mutation, current_user: current_user) + + expect(response).to have_gitlab_http_status(:success) + expect(json_response['errors'].first['message']).to eq('labelIds is mutually exclusive with any of addLabelIds or removeLabelIds') + expect(mutation_response).to be_nil + end + end + + context 'with global label ids' do + let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.to_global_id.to_s, label2.to_global_id.to_s] }) } + + it 'resets labels' do + post_graphql_mutation(mutation, current_user: current_user) + + expect(response).to have_gitlab_http_status(:success) + expect(json_response['errors']).to be_nil + expect(mutation_response['issue']['labels']).to include({ "nodes" => [{ "id" => label1.to_global_id.to_s }, { "id" => label2.to_global_id.to_s }] }) + end + end + end + + context 'add and remove labels' do + let(:input_params) { input.merge(extra_params).merge({ addLabelIds: [label1.id], removeLabelIds: [label2.id] }) } + + it 'returns error for mutually exclusive arguments' do + post_graphql_mutation(mutation, current_user: current_user) + + expect(response).to have_gitlab_http_status(:success) + expect(json_response['errors']).to be_nil + expect(mutation_response['issue']['labels']).to include({ "nodes" => [{ "id" => label1.to_global_id.to_s }] }) + end + end + end end end diff --git a/spec/requests/api/graphql/mutations/packages/destroy_file_spec.rb b/spec/requests/api/graphql/mutations/packages/destroy_file_spec.rb new file mode 100644 index 00000000000..7be629f8f4b --- /dev/null +++ b/spec/requests/api/graphql/mutations/packages/destroy_file_spec.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'Destroying a package file' do + using RSpec::Parameterized::TableSyntax + + include GraphqlHelpers + + let_it_be_with_reload(:package) { create(:maven_package) } + let_it_be(:user) { create(:user) } + + let(:project) { package.project } + let(:id) { package.package_files.first.to_global_id.to_s } + + let(:query) do + <<~GQL + errors + GQL + end + + let(:params) { { id: id } } + let(:mutation) { graphql_mutation(:destroy_package_file, params, query) } + let(:mutation_response) { graphql_mutation_response(:destroyPackageFile) } + + shared_examples 'destroying the package file' do + it 'destroy the package file' do + expect { mutation_request }.to change { ::Packages::PackageFile.count }.by(-1) + end + + it_behaves_like 'returning response status', :success + end + + shared_examples 'denying the mutation request' do + it 'does not destroy the package file' do + expect(::Packages::PackageFile) + .not_to receive(:destroy) + + expect { mutation_request }.not_to change { ::Packages::PackageFile.count } + + expect(mutation_response).to be_nil + end + + it_behaves_like 'returning response status', :success + end + + describe 'post graphql mutation' do + subject(:mutation_request) { post_graphql_mutation(mutation, current_user: user) } + + context 'with valid id' do + where(:user_role, :shared_examples_name) do + :maintainer | 'destroying the package file' + :developer | 'denying the mutation request' + :reporter | 'denying the mutation request' + :guest | 'denying the mutation request' + :anonymous | 'denying the mutation request' + end + + with_them do + before do + project.send("add_#{user_role}", user) unless user_role == :anonymous + end + + it_behaves_like params[:shared_examples_name] + end + end + + context 'with invalid id' do + let(:params) { { id: 'gid://gitlab/Packages::PackageFile/5555' } } + + it_behaves_like 'denying the mutation request' + end + + context 'when an error occures' do + let(:error_messages) { ['some error'] } + + before do + project.add_maintainer(user) + end + + it 'returns the errors in the response' do + allow_next_found_instance_of(::Packages::PackageFile) do |package_file| + allow(package_file).to receive(:destroy).and_return(false) + allow(package_file).to receive_message_chain(:errors, :full_messages).and_return(error_messages) + end + + mutation_request + + expect(mutation_response['errors']).to eq(error_messages) + end + end + end +end diff --git a/spec/requests/api/graphql/mutations/snippets/mark_as_spam_spec.rb b/spec/requests/api/graphql/mutations/snippets/mark_as_spam_spec.rb index 43d846cb297..77fd6cddc09 100644 --- a/spec/requests/api/graphql/mutations/snippets/mark_as_spam_spec.rb +++ b/spec/requests/api/graphql/mutations/snippets/mark_as_spam_spec.rb @@ -58,7 +58,7 @@ RSpec.describe 'Mark snippet as spam' do end it 'marks snippet as spam' do - expect_next(Spam::MarkAsSpamService, target: snippet) + expect_next(Spam::AkismetMarkAsSpamService, target: snippet) .to receive(:execute).and_return(true) post_graphql_mutation(mutation, current_user: current_user) |