diff options
Diffstat (limited to 'spec/support/shared_examples/graphql')
5 files changed, 184 insertions, 0 deletions
diff --git a/spec/support/shared_examples/graphql/design_fields_shared_examples.rb b/spec/support/shared_examples/graphql/design_fields_shared_examples.rb index 029d7e677da..ef7086234c4 100644 --- a/spec/support/shared_examples/graphql/design_fields_shared_examples.rb +++ b/spec/support/shared_examples/graphql/design_fields_shared_examples.rb @@ -35,6 +35,7 @@ RSpec.shared_examples 'a GraphQL type with design fields' do object = GitlabSchema.sync_lazy(GitlabSchema.object_from_id(object_id)) object_type.authorized_new(object, query.context) end + let(:instance_b) do object_b = GitlabSchema.sync_lazy(GitlabSchema.object_from_id(object_id_b)) object_type.authorized_new(object_b, query.context) diff --git a/spec/support/shared_examples/graphql/mutations/resolves_subscription_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/resolves_subscription_shared_examples.rb new file mode 100644 index 00000000000..ebba312e895 --- /dev/null +++ b/spec/support/shared_examples/graphql/mutations/resolves_subscription_shared_examples.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.shared_examples 'a subscribeable graphql resource' do + let(:project) { resource.project } + let_it_be(:user) { create(:user) } + + subject(:mutation) { described_class.new(object: nil, context: { current_user: user }, field: nil) } + + specify { expect(described_class).to require_graphql_authorizations(permission_name) } + + describe '#resolve' do + let(:subscribe) { true } + let(:mutated_resource) { subject[resource.class.name.underscore.to_sym] } + + subject { mutation.resolve(project_path: resource.project.full_path, iid: resource.iid, subscribed_state: subscribe) } + + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + + context 'when the user can update the resource' do + before do + resource.project.add_developer(user) + end + + it 'subscribes to the resource' do + expect(mutated_resource).to eq(resource) + expect(mutated_resource.subscribed?(user, project)).to eq(true) + expect(subject[:errors]).to be_empty + end + + context 'when passing subscribe as false' do + let(:subscribe) { false } + + it 'unsubscribes from the discussion' do + resource.subscribe(user, project) + + expect(mutated_resource.subscribed?(user, project)).to eq(false) + end + end + end + end +end diff --git a/spec/support/shared_examples/graphql/mutations/set_assignees_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/set_assignees_shared_examples.rb new file mode 100644 index 00000000000..cfa12171b7e --- /dev/null +++ b/spec/support/shared_examples/graphql/mutations/set_assignees_shared_examples.rb @@ -0,0 +1,126 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.shared_examples 'an assignable resource' do + let_it_be(:user) { create(:user) } + + subject(:mutation) { described_class.new(object: nil, context: { current_user: user }, field: nil) } + + describe '#resolve' do + let_it_be(:assignee) { create(:user) } + let_it_be(:assignee2) { create(:user) } + let(:assignee_usernames) { [assignee.username] } + let(:mutated_resource) { subject[resource.class.name.underscore.to_sym] } + + subject { mutation.resolve(project_path: resource.project.full_path, iid: resource.iid, assignee_usernames: assignee_usernames) } + + before do + resource.project.add_developer(assignee) + resource.project.add_developer(assignee2) + end + + it 'raises an error if the resource is not accessible to the user' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + + context 'when the user can update the resource' do + before do + resource.project.add_developer(user) + end + + it 'replaces the assignee' do + resource.assignees = [assignee2] + resource.save! + + expect(mutated_resource).to eq(resource) + expect(mutated_resource.assignees).to contain_exactly(assignee) + expect(subject[:errors]).to be_empty + end + + it 'returns errors when resource could not be updated' do + allow(resource).to receive(:errors_on_object).and_return(['foo']) + + expect(subject[:errors]).not_to match_array(['foo']) + end + + context 'when passing an empty assignee list' do + let(:assignee_usernames) { [] } + + before do + resource.assignees = [assignee] + resource.save! + end + + it 'removes all assignees' do + expect(mutated_resource).to eq(resource) + expect(mutated_resource.assignees).to eq([]) + expect(subject[:errors]).to be_empty + end + end + + context 'when passing "append" as true' do + subject do + mutation.resolve( + project_path: resource.project.full_path, + iid: resource.iid, + assignee_usernames: assignee_usernames, + operation_mode: Types::MutationOperationModeEnum.enum[:append] + ) + end + + before do + resource.assignees = [assignee2] + resource.save! + + # In CE, APPEND is a NOOP as you can't have multiple assignees + # We test multiple assignment in EE specs + if resource.is_a?(MergeRequest) + stub_licensed_features(multiple_merge_request_assignees: false) + else + stub_licensed_features(multiple_issue_assignees: false) + end + end + + it 'is a NO-OP in FOSS' do + expect(mutated_resource).to eq(resource) + expect(mutated_resource.assignees).to contain_exactly(assignee2) + expect(subject[:errors]).to be_empty + end + end + + context 'when passing "remove" as true' do + before do + resource.assignees = [assignee] + resource.save! + end + + it 'removes named assignee' do + mutated_resource = mutation.resolve( + project_path: resource.project.full_path, + iid: resource.iid, + assignee_usernames: assignee_usernames, + operation_mode: Types::MutationOperationModeEnum.enum[:remove] + )[resource.class.name.underscore.to_sym] + + expect(mutated_resource).to eq(resource) + expect(mutated_resource.assignees).to eq([]) + expect(subject[:errors]).to be_empty + end + + it 'does not remove unnamed assignee' do + mutated_resource = mutation.resolve( + project_path: resource.project.full_path, + iid: resource.iid, + assignee_usernames: [assignee2.username], + operation_mode: Types::MutationOperationModeEnum.enum[:remove] + )[resource.class.name.underscore.to_sym] + + expect(mutated_resource).to eq(resource) + expect(mutated_resource.assignees).to contain_exactly(assignee) + expect(subject[:errors]).to be_empty + end + end + end + end +end diff --git a/spec/support/shared_examples/graphql/notes_on_noteables_shared_examples.rb b/spec/support/shared_examples/graphql/notes_on_noteables_shared_examples.rb index e1dd98814f1..41b7da07d2d 100644 --- a/spec/support/shared_examples/graphql/notes_on_noteables_shared_examples.rb +++ b/spec/support/shared_examples/graphql/notes_on_noteables_shared_examples.rb @@ -8,6 +8,7 @@ RSpec.shared_context 'exposing regular notes on a noteable in GraphQL' do noteable: noteable, project: (noteable.project if noteable.respond_to?(:project))) end + let(:user) { note.author } context 'for regular notes' do diff --git a/spec/support/shared_examples/graphql/projects/merge_request_n_plus_one_query_examples.rb b/spec/support/shared_examples/graphql/projects/merge_request_n_plus_one_query_examples.rb new file mode 100644 index 00000000000..397e22ace28 --- /dev/null +++ b/spec/support/shared_examples/graphql/projects/merge_request_n_plus_one_query_examples.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true +shared_examples 'N+1 query check' do + it 'prevents N+1 queries' do + execute_query # "warm up" to prevent undeterministic counts + + control_count = ActiveRecord::QueryRecorder.new { execute_query }.count + + search_params[:iids] << extra_iid_for_second_query + expect { execute_query }.not_to exceed_query_limit(control_count) + end +end |