diff options
Diffstat (limited to 'spec/support/shared_examples/graphql/mutations')
4 files changed, 194 insertions, 50 deletions
diff --git a/spec/support/shared_examples/graphql/mutations/boards_list_create_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/boards_list_create_shared_examples.rb new file mode 100644 index 00000000000..b096a5e17c0 --- /dev/null +++ b/spec/support/shared_examples/graphql/mutations/boards_list_create_shared_examples.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.shared_examples 'board lists create mutation' do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + + let(:mutation) { described_class.new(object: nil, context: { current_user: user }, field: nil) } + let(:list_create_params) { {} } + + subject { mutation.resolve(board_id: board.to_global_id.to_s, **list_create_params) } + + describe '#ready?' do + it 'raises an error if required arguments are missing' do + expect { mutation.ready?(board_id: 'some id') } + .to raise_error(Gitlab::Graphql::Errors::ArgumentError, /one and only one of/) + end + + it 'raises an error if too many required arguments are specified' do + expect { mutation.ready?(board_id: 'some id', backlog: true, label_id: 'some label') } + .to raise_error(Gitlab::Graphql::Errors::ArgumentError, /one and only one of/) + end + end + + describe '#resolve' do + context 'with proper permissions' do + before_all do + group.add_reporter(user) + end + + describe 'backlog list' do + let(:list_create_params) { { backlog: true } } + + it 'creates one and only one backlog' do + expect { subject }.to change { board.lists.backlog.count }.by(1) + expect(board.lists.backlog.first.list_type).to eq 'backlog' + + backlog_id = board.lists.backlog.first.id + + expect { subject }.not_to change { board.lists.backlog.count } + expect(board.lists.backlog.last.id).to eq backlog_id + end + end + + describe 'label list' do + let_it_be(:dev_label) do + create(:group_label, title: 'Development', color: '#FFAABB', group: group) + end + + let(:list_create_params) { { label_id: dev_label.to_global_id.to_s } } + + it 'creates a new label board list' do + expect { subject }.to change { board.lists.count }.by(1) + + new_list = subject[:list] + + expect(new_list.title).to eq dev_label.title + expect(new_list.position).to eq 0 + end + + context 'when label not found' do + let(:list_create_params) { { label_id: "gid://gitlab/Label/#{non_existing_record_id}" } } + + it 'returns an error' do + expect(subject[:errors]).to include 'Label not found' + end + end + end + end + + context 'without proper permissions' do + before_all do + group.add_guest(user) + end + + it 'raises an error' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end +end diff --git a/spec/support/shared_examples/graphql/mutations/can_mutate_spammable_examples.rb b/spec/support/shared_examples/graphql/mutations/can_mutate_spammable_examples.rb new file mode 100644 index 00000000000..d294f034d2e --- /dev/null +++ b/spec/support/shared_examples/graphql/mutations/can_mutate_spammable_examples.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.shared_examples 'a mutation which can mutate a spammable' do + describe "#additional_spam_params" do + it 'passes additional spam params to the service' do + args = [ + anything, + anything, + hash_including( + api: true, + request: instance_of(ActionDispatch::Request), + captcha_response: captcha_response, + spam_log_id: spam_log_id + ) + ] + expect(service).to receive(:new).with(*args).and_call_original + + subject + end + end + + describe "#with_spam_action_fields" do + it 'resolves with spam action fields' do + subject + + # NOTE: We do not need to assert on the specific values of spam action fields here, we only need + # to verify that #with_spam_action_fields was invoked and that the fields are present in the + # response. The specific behavior of #with_spam_action_fields is covered in the + # CanMutateSpammable unit tests. + expect(mutation_response.keys) + .to include('spam', 'spamLogId', 'needsCaptchaResponse', 'captchaSiteKey') + end + end +end diff --git a/spec/support/shared_examples/graphql/mutations/http_integrations_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/http_integrations_shared_examples.rb index 0338eb43f8d..4468af1a603 100644 --- a/spec/support/shared_examples/graphql/mutations/http_integrations_shared_examples.rb +++ b/spec/support/shared_examples/graphql/mutations/http_integrations_shared_examples.rb @@ -17,3 +17,78 @@ RSpec.shared_examples 'creating a new HTTP integration' do expect(integration_response['apiUrl']).to eq(nil) end end + +RSpec.shared_examples 'updating an existing HTTP integration' do + it 'updates the integration' do + post_graphql_mutation(mutation, current_user: current_user) + + integration_response = mutation_response['integration'] + + expect(response).to have_gitlab_http_status(:success) + expect(integration_response['id']).to eq(GitlabSchema.id_from_object(integration).to_s) + expect(integration_response['name']).to eq('Modified Name') + expect(integration_response['active']).to be_falsey + expect(integration_response['url']).to include('modified-name') + end +end + +RSpec.shared_examples 'validating the payload_example' do + context 'with invalid payloadExample attribute' do + let(:payload_example) { 'not a JSON' } + + it 'responds with errors' do + post_graphql_mutation(mutation, current_user: current_user) + + expect_graphql_errors_to_include(/was provided invalid value for payloadExample \(Invalid JSON string/) + end + end + + it 'validates the payload_example size' do + allow(::Gitlab::Utils::DeepSize) + .to receive(:new) + .with(Gitlab::Json.parse(payload_example)) + .and_return(double(valid?: false)) + + post_graphql_mutation(mutation, current_user: current_user) + + expect_graphql_errors_to_include(/payloadExample JSON is too big/) + end +end + +RSpec.shared_examples 'validating the payload_attribute_mappings' do + context 'with invalid payloadAttributeMapping attribute does not contain fieldName' do + let(:payload_attribute_mappings) do + [{ path: %w[alert name], type: 'STRING' }] + end + + it 'responds with errors' do + post_graphql_mutation(mutation, current_user: current_user) + + expect_graphql_errors_to_include(/was provided invalid value for payloadAttributeMappings\.0\.fieldName \(Expected value to not be null/) + end + end + + context 'with invalid payloadAttributeMapping attribute does not contain path' do + let(:payload_attribute_mappings) do + [{ fieldName: 'TITLE', type: 'STRING' }] + end + + it 'responds with errors' do + post_graphql_mutation(mutation, current_user: current_user) + + expect_graphql_errors_to_include(/was provided invalid value for payloadAttributeMappings\.0\.path \(Expected value to not be null/) + end + end + + context 'with invalid payloadAttributeMapping attribute does not contain type' do + let(:payload_attribute_mappings) do + [{ fieldName: 'TITLE', path: %w[alert name] }] + end + + it 'responds with errors' do + post_graphql_mutation(mutation, current_user: current_user) + + expect_graphql_errors_to_include(/was provided invalid value for payloadAttributeMappings\.0\.type \(Expected value to not be null/) + end + end +end diff --git a/spec/support/shared_examples/graphql/mutations/spammable_mutation_fields_examples.rb b/spec/support/shared_examples/graphql/mutations/spammable_mutation_fields_examples.rb deleted file mode 100644 index 8678b23ad31..00000000000 --- a/spec/support/shared_examples/graphql/mutations/spammable_mutation_fields_examples.rb +++ /dev/null @@ -1,50 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.shared_examples 'spam flag is present' do - specify :aggregate_failures do - subject - - expect(mutation_response).to have_key('spam') - expect(mutation_response['spam']).to be_falsey - end -end - -RSpec.shared_examples 'can raise spam flag' do - it 'spam parameters are passed to the service' do - args = [anything, anything, hash_including(api: true, request: instance_of(ActionDispatch::Request))] - expect(service).to receive(:new).with(*args).and_call_original - - subject - end - - context 'when the snippet is detected as spam' do - it 'raises spam flag' do - allow_next_instance_of(service) do |instance| - allow(instance).to receive(:spam_check) do |snippet, user, _| - snippet.spam! - end - end - - subject - - expect(mutation_response['spam']).to be true - expect(mutation_response['errors']).to include("Your snippet has been recognized as spam and has been discarded.") - end - end - - context 'when :snippet_spam flag is disabled' do - before do - stub_feature_flags(snippet_spam: false) - end - - it 'request parameter is not passed to the service' do - expect(service).to receive(:new) - .with(anything, anything, hash_not_including(request: instance_of(ActionDispatch::Request))) - .and_call_original - - subject - end - end -end |