diff options
Diffstat (limited to 'spec/graphql/mutations/issues')
-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 |
3 files changed, 204 insertions, 0 deletions
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) } |