diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /spec/graphql/mutations | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'spec/graphql/mutations')
-rw-r--r-- | spec/graphql/mutations/design_management/move_spec.rb | 2 | ||||
-rw-r--r-- | spec/graphql/mutations/discussions/toggle_resolve_spec.rb | 4 | ||||
-rw-r--r-- | spec/graphql/mutations/issues/create_spec.rb | 146 | ||||
-rw-r--r-- | spec/graphql/mutations/issues/move_spec.rb | 41 | ||||
-rw-r--r-- | spec/graphql/mutations/issues/update_spec.rb | 17 | ||||
-rw-r--r-- | spec/graphql/mutations/todos/mark_done_spec.rb | 3 | ||||
-rw-r--r-- | spec/graphql/mutations/todos/restore_many_spec.rb | 33 | ||||
-rw-r--r-- | spec/graphql/mutations/todos/restore_spec.rb | 11 |
8 files changed, 219 insertions, 38 deletions
diff --git a/spec/graphql/mutations/design_management/move_spec.rb b/spec/graphql/mutations/design_management/move_spec.rb index 7519347d07c..d17483e69b3 100644 --- a/spec/graphql/mutations/design_management/move_spec.rb +++ b/spec/graphql/mutations/design_management/move_spec.rb @@ -29,7 +29,7 @@ RSpec.describe Mutations::DesignManagement::Move do next_design: next_design&.to_global_id }.compact - mutation.resolve(args) + mutation.resolve(**args) end shared_examples "resource not available" do diff --git a/spec/graphql/mutations/discussions/toggle_resolve_spec.rb b/spec/graphql/mutations/discussions/toggle_resolve_spec.rb index d779a2227c1..2e5d41a8f1e 100644 --- a/spec/graphql/mutations/discussions/toggle_resolve_spec.rb +++ b/spec/graphql/mutations/discussions/toggle_resolve_spec.rb @@ -50,8 +50,8 @@ RSpec.describe Mutations::Discussions::ToggleResolve do it 'raises an error' do expect { subject }.to raise_error( - Gitlab::Graphql::Errors::ArgumentError, - "#{discussion.to_global_id} is not a valid ID for Discussion." + GraphQL::CoercionError, + "\"#{discussion.to_global_id}\" does not represent an instance of Discussion" ) end end diff --git a/spec/graphql/mutations/issues/create_spec.rb b/spec/graphql/mutations/issues/create_spec.rb new file mode 100644 index 00000000000..57658f6b358 --- /dev/null +++ b/spec/graphql/mutations/issues/create_spec.rb @@ -0,0 +1,146 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::Issues::Create do + let_it_be(:project) { create(:project) } + let_it_be(:user) { create(:user) } + let_it_be(:assignee1) { create(:user) } + let_it_be(:assignee2) { create(:user) } + let_it_be(:project_label1) { create(:label, project: project) } + let_it_be(:project_label2) { create(:label, project: project) } + let_it_be(:milestone) { create(:milestone, project: project) } + let_it_be(:new_label1) { FFaker::Lorem.word } + let_it_be(:new_label2) { new_label1 + 'Extra' } + + let(:expected_attributes) do + { + title: 'new title', + description: 'new description', + confidential: true, + due_date: Date.tomorrow, + discussion_locked: true + } + end + + let(:mutation_params) do + { + project_path: project.full_path, + milestone_id: milestone.to_global_id, + labels: [project_label1.title, project_label2.title, new_label1, new_label2], + assignee_ids: [assignee1.to_global_id, assignee2.to_global_id] + }.merge(expected_attributes) + end + + let(:special_params) do + { + iid: non_existing_record_id, + created_at: 2.days.ago + } + end + + let(:mutation) { described_class.new(object: nil, context: { current_user: user }, field: nil) } + let(:mutated_issue) { subject[:issue] } + + specify { expect(described_class).to require_graphql_authorizations(:create_issue) } + + describe '#resolve' do + before do + stub_licensed_features(multiple_issue_assignees: false, issue_weights: false) + project.add_guest(assignee1) + project.add_guest(assignee2) + end + + subject { mutation.resolve(mutation_params) } + + context 'when the user does not have permission to create an issue' do + it 'raises an error' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + + context 'when the user can create an issue' do + context 'when creating an issue a developer' do + before do + project.add_developer(user) + end + + it 'creates issue with correct values' do + expect(mutated_issue).to have_attributes(expected_attributes) + expect(mutated_issue.milestone_id).to eq(milestone.id) + expect(mutated_issue.labels.pluck(:title)).to eq([project_label1.title, project_label2.title, new_label1, new_label2]) + expect(mutated_issue.assignees.pluck(:id)).to eq([assignee1.id]) + end + + context 'when passing in label_ids' do + before do + mutation_params.delete(:labels) + mutation_params.merge!(label_ids: [project_label1.to_global_id, project_label2.to_global_id]) + end + + it 'creates issue with correct values' do + expect(mutated_issue.labels.pluck(:title)).to eq([project_label1.title, project_label2.title]) + end + end + + context 'when trying to create issue with restricted params' do + before do + mutation_params.merge!(special_params) + end + + it 'ignores the special params' do + expect(mutated_issue).not_to be_like_time(special_params[:created_at]) + expect(mutated_issue.iid).not_to eq(special_params[:iid]) + end + end + end + + context 'when creating an issue as owner' do + let_it_be(:user) { project.owner } + + before do + mutation_params.merge!(special_params) + end + + it 'sets the special params' do + expect(mutated_issue.created_at).to be_like_time(special_params[:created_at]) + expect(mutated_issue.iid).to eq(special_params[:iid]) + end + end + end + end + + describe "#ready?" do + context 'when passing in both labels and label_ids' do + before do + mutation_params.merge!(label_ids: [project_label1.to_global_id, project_label2.to_global_id]) + end + + it 'raises exception when mutually exclusive params are given' do + expect { mutation.ready?(mutation_params) } + .to raise_error(Gitlab::Graphql::Errors::ArgumentError, /one and only one of/) + end + end + + context 'when passing only `discussion_to_resolve` param' do + before do + mutation_params.merge!(discussion_to_resolve: 'abc') + end + + it 'raises exception when mutually exclusive params are given' do + expect { mutation.ready?(mutation_params) } + .to raise_error(Gitlab::Graphql::Errors::ArgumentError, /to resolve a discussion please also provide `merge_request_to_resolve_discussions_of` parameter/) + end + end + + context 'when passing only `merge_request_to_resolve_discussions_of` param' do + before do + mutation_params.merge!(merge_request_to_resolve_discussions_of: 'abc') + end + + it 'raises exception when mutually exclusive params are given' do + expect { mutation.ready?(mutation_params) }.not_to raise_error + end + end + end +end diff --git a/spec/graphql/mutations/issues/move_spec.rb b/spec/graphql/mutations/issues/move_spec.rb new file mode 100644 index 00000000000..c8e9c556a3f --- /dev/null +++ b/spec/graphql/mutations/issues/move_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::Issues::Move do + let_it_be(:issue) { create(:issue) } + let_it_be(:user) { create(:user) } + let_it_be(:target_project) { create(:project) } + + subject(:mutation) { described_class.new(object: nil, context: { current_user: user }, field: nil) } + + describe '#resolve' do + subject(:resolve) { mutation.resolve(project_path: issue.project.full_path, iid: issue.iid, target_project_path: target_project.full_path) } + + it 'raises an error if the resource is not accessible to the user' do + expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + + context 'when user does not have permissions' do + before do + issue.project.add_developer(user) + end + + it 'returns error message' do + expect(resolve[:issue]).to eq(nil) + expect(resolve[:errors].first).to eq('Cannot move issue due to insufficient permissions!') + end + end + + context 'when user has sufficient permissions' do + before do + issue.project.add_developer(user) + target_project.add_developer(user) + end + + it 'moves issue' do + expect(resolve[:issue].project).to eq(target_project) + end + end + end +end diff --git a/spec/graphql/mutations/issues/update_spec.rb b/spec/graphql/mutations/issues/update_spec.rb index 15c15afd9b7..f9f4bdeb6fa 100644 --- a/spec/graphql/mutations/issues/update_spec.rb +++ b/spec/graphql/mutations/issues/update_spec.rb @@ -70,6 +70,23 @@ RSpec.describe Mutations::Issues::Update do end end + context 'when changing state' do + let_it_be_with_refind(:issue) { create(:issue, project: project, state: :opened) } + + it 'closes issue' do + mutation_params[:state_event] = 'close' + + expect { subject }.to change { issue.reload.state }.from('opened').to('closed') + end + + it 'reopens issue' do + issue.close + mutation_params[:state_event] = 'reopen' + + expect { subject }.to change { issue.reload.state }.from('closed').to('opened') + end + end + context 'when changing labels' do let_it_be(:label_1) { create(:label, project: project) } let_it_be(:label_2) { create(:label, project: project) } diff --git a/spec/graphql/mutations/todos/mark_done_spec.rb b/spec/graphql/mutations/todos/mark_done_spec.rb index 51ad3e1a5d7..b5f2ff5d044 100644 --- a/spec/graphql/mutations/todos/mark_done_spec.rb +++ b/spec/graphql/mutations/todos/mark_done_spec.rb @@ -52,7 +52,8 @@ RSpec.describe Mutations::Todos::MarkDone do end it 'ignores invalid GIDs' do - expect { mutation.resolve(id: 'invalid_gid') }.to raise_error(Gitlab::Graphql::Errors::ArgumentError) + expect { mutation.resolve(id: author.to_global_id.to_s) } + .to raise_error(::GraphQL::CoercionError) expect(todo1.reload.state).to eq('pending') expect(todo2.reload.state).to eq('done') diff --git a/spec/graphql/mutations/todos/restore_many_spec.rb b/spec/graphql/mutations/todos/restore_many_spec.rb index b3b3e057745..59995e33f2d 100644 --- a/spec/graphql/mutations/todos/restore_many_spec.rb +++ b/spec/graphql/mutations/todos/restore_many_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' RSpec.describe Mutations::Todos::RestoreMany do + include GraphqlHelpers + let_it_be(:current_user) { create(:user) } let_it_be(:author) { create(:user) } let_it_be(:other_user) { create(:user) } @@ -44,8 +46,9 @@ RSpec.describe Mutations::Todos::RestoreMany do expect_states_were_not_changed end - it 'ignores invalid GIDs' do - expect { mutation.resolve(ids: ['invalid_gid']) }.to raise_error(URI::BadURIError) + it 'raises an error with invalid or non-Todo GIDs' do + expect { mutation.resolve(ids: [author.to_global_id.to_s]) } + .to raise_error(GraphQL::CoercionError) expect_states_were_not_changed end @@ -78,38 +81,12 @@ RSpec.describe Mutations::Todos::RestoreMany do it 'fails if too many todos are requested for update' do expect { restore_mutation([todo1] * 51) }.to raise_error(Gitlab::Graphql::Errors::ArgumentError) end - - it 'does not update todos from another app' do - todo4 = create(:todo) - todo4_gid = ::URI::GID.parse("gid://otherapp/Todo/#{todo4.id}") - - result = mutation.resolve(ids: [todo4_gid.to_s]) - - expect(result[:updated_ids]).to be_empty - - expect_states_were_not_changed - end - - it 'does not update todos from another model' do - todo4 = create(:todo) - todo4_gid = ::URI::GID.parse("gid://#{GlobalID.app}/Project/#{todo4.id}") - - result = mutation.resolve(ids: [todo4_gid.to_s]) - - expect(result[:updated_ids]).to be_empty - - expect_states_were_not_changed - end end def restore_mutation(todos) mutation.resolve(ids: todos.map { |todo| global_id_of(todo) } ) end - def global_id_of(todo) - todo.to_global_id.to_s - end - def expect_states_were_not_changed expect(todo1.reload.state).to eq('done') expect(todo2.reload.state).to eq('pending') diff --git a/spec/graphql/mutations/todos/restore_spec.rb b/spec/graphql/mutations/todos/restore_spec.rb index 9043d7a44a8..22fb1bba7a8 100644 --- a/spec/graphql/mutations/todos/restore_spec.rb +++ b/spec/graphql/mutations/todos/restore_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' RSpec.describe Mutations::Todos::Restore do + include GraphqlHelpers + let_it_be(:current_user) { create(:user) } let_it_be(:author) { create(:user) } let_it_be(:other_user) { create(:user) } @@ -49,8 +51,9 @@ RSpec.describe Mutations::Todos::Restore do expect(other_user_todo.reload.state).to eq('done') end - it 'ignores invalid GIDs' do - expect { mutation.resolve(id: 'invalid_gid') }.to raise_error(Gitlab::Graphql::Errors::ArgumentError) + it 'raises error for invalid GID' do + expect { mutation.resolve(id: author.to_global_id.to_s) } + .to raise_error(::GraphQL::CoercionError) expect(todo1.reload.state).to eq('done') expect(todo2.reload.state).to eq('pending') @@ -61,8 +64,4 @@ RSpec.describe Mutations::Todos::Restore do def restore_mutation(todo) mutation.resolve(id: global_id_of(todo)) end - - def global_id_of(todo) - todo.to_global_id.to_s - end end |