diff options
Diffstat (limited to 'spec/support/helpers')
-rw-r--r-- | spec/support/helpers/cycle_analytics_helpers.rb | 26 | ||||
-rw-r--r-- | spec/support/helpers/features/members_table_helpers.rb | 26 | ||||
-rw-r--r-- | spec/support/helpers/features/notes_helpers.rb | 4 | ||||
-rw-r--r-- | spec/support/helpers/graphql_helpers.rb | 47 | ||||
-rw-r--r-- | spec/support/helpers/next_found_instance_of.rb | 16 | ||||
-rw-r--r-- | spec/support/helpers/search_settings_helpers.rb | 5 | ||||
-rw-r--r-- | spec/support/helpers/seed_helper.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/smime_helper.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/sorting_helper.rb | 31 | ||||
-rw-r--r-- | spec/support/helpers/stub_configuration.rb | 6 | ||||
-rw-r--r-- | spec/support/helpers/stub_object_storage.rb | 7 | ||||
-rw-r--r-- | spec/support/helpers/test_env.rb | 4 | ||||
-rw-r--r-- | spec/support/helpers/wait_for_requests.rb | 2 |
13 files changed, 154 insertions, 24 deletions
diff --git a/spec/support/helpers/cycle_analytics_helpers.rb b/spec/support/helpers/cycle_analytics_helpers.rb index 6d3ac699a7c..a90cbbf3bd3 100644 --- a/spec/support/helpers/cycle_analytics_helpers.rb +++ b/spec/support/helpers/cycle_analytics_helpers.rb @@ -3,6 +3,32 @@ module CycleAnalyticsHelpers include GitHelpers + def wait_for_stages_to_load + expect(page).to have_selector '.js-stage-table' + wait_for_requests + end + + def select_group(target_group) + visit group_analytics_cycle_analytics_path(target_group) + + wait_for_stages_to_load + end + + def toggle_dropdown(field) + page.within("[data-testid='#{field}']") do + find('.dropdown-toggle').click + + wait_for_requests + + expect(find('.dropdown-menu')).to have_selector('.dropdown-item') + end + end + + def select_dropdown_option_by_value(name, value, elem = '.dropdown-item') + toggle_dropdown name + page.find("[data-testid='#{name}'] .dropdown-menu").find("#{elem}[value='#{value}']").click + end + def create_commit_referencing_issue(issue, branch_name: generate(:branch)) project.repository.add_branch(user, branch_name, 'master') create_commit("Commit for ##{issue.iid}", issue.project, user, branch_name) diff --git a/spec/support/helpers/features/members_table_helpers.rb b/spec/support/helpers/features/members_table_helpers.rb index 5394e370900..4a0e218ed3e 100644 --- a/spec/support/helpers/features/members_table_helpers.rb +++ b/spec/support/helpers/features/members_table_helpers.rb @@ -30,6 +30,32 @@ module Spec def invite_users_form page.find('[data-testid="invite-users-form"]') end + + def find_row(name) + page.within(members_table) do + page.find('tbody > tr', text: name) + end + end + + def find_member_row(user) + find_row(user.name) + end + + def find_invited_member_row(email) + find_row(email) + end + + def find_group_row(group) + find_row(group.full_name) + end + + def fill_in_filtered_search(label, with:) + page.within '[data-testid="members-filtered-search-bar"]' do + find_field(label).click + find('input').native.send_keys(with) + click_button 'Search' + end + end end end end diff --git a/spec/support/helpers/features/notes_helpers.rb b/spec/support/helpers/features/notes_helpers.rb index 8c27f81930d..f8252254531 100644 --- a/spec/support/helpers/features/notes_helpers.rb +++ b/spec/support/helpers/features/notes_helpers.rb @@ -21,6 +21,8 @@ module Spec find(".js-comment-submit-button").click end end + + wait_for_requests end def edit_note(note_text_to_edit, new_note_text) @@ -29,6 +31,8 @@ module Spec fill_in('note[note]', with: new_note_text) find('.js-comment-button').click end + + wait_for_requests end def preview_note(text) diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb index 35c298a4d48..46d0c13dc18 100644 --- a/spec/support/helpers/graphql_helpers.rb +++ b/spec/support/helpers/graphql_helpers.rb @@ -125,11 +125,15 @@ module GraphqlHelpers end def graphql_query_for(name, attributes = {}, fields = nil) - <<~QUERY - { - #{query_graphql_field(name, attributes, fields)} - } - QUERY + type = GitlabSchema.types['Query'].fields[GraphqlHelpers.fieldnamerize(name)]&.type + wrap_query(query_graphql_field(name, attributes, fields, type)) + end + + def wrap_query(query) + q = query.to_s + return q if q.starts_with?('{') + + "{ #{q} }" end def graphql_mutation(name, input, fields = nil, &block) @@ -219,12 +223,13 @@ module GraphqlHelpers "#{namerized}#{field_params}" end - def query_graphql_field(name, attributes = {}, fields = nil) + def query_graphql_field(name, attributes = {}, fields = nil, type = nil) + type ||= name.to_s.classify attributes, fields = [nil, attributes] if fields.nil? && !attributes.is_a?(Hash) field = field_with_params(name, attributes) - field + wrap_fields(fields || all_graphql_fields_for(name.to_s.classify)).to_s + field + wrap_fields(fields || all_graphql_fields_for(type)).to_s end def page_info_selection @@ -237,6 +242,10 @@ module GraphqlHelpers query_graphql_path([[name, args], node_selection], fields) end + def query_graphql_fragment(name) + "... on #{name} { #{all_graphql_fields_for(name)} }" + end + # e.g: # query_graphql_path(%i[foo bar baz], all_graphql_fields_for('Baz')) # => foo { bar { baz { x y z } } } @@ -277,8 +286,8 @@ module GraphqlHelpers allow_high_graphql_recursion allow_high_graphql_transaction_threshold - type = GitlabSchema.types[class_name.to_s] - return "" unless type + type = class_name.respond_to?(:kind) ? class_name : GitlabSchema.types[class_name.to_s] + raise "#{class_name} is not a known type in the GitlabSchema" unless type # We can't guess arguments, so skip fields that require them skip = ->(name, field) { excluded.include?(name) || required_arguments?(field) } @@ -287,7 +296,7 @@ module GraphqlHelpers end def with_signature(variables, query) - %Q[query(#{variables.map(&:sig).join(', ')}) #{query}] + %Q[query(#{variables.map(&:sig).join(', ')}) #{wrap_query(query)}] end def var(type) @@ -305,6 +314,10 @@ module GraphqlHelpers def post_graphql(query, current_user: nil, variables: nil, headers: {}) params = { query: query, variables: serialize_variables(variables) } post api('/', current_user, version: 'graphql'), params: params, headers: headers + + if graphql_errors # Errors are acceptable, but not this one: + expect(graphql_errors).not_to include(a_hash_including('message' => 'Internal server error')) + end end def post_graphql_mutation(mutation, current_user: nil) @@ -374,10 +387,8 @@ module GraphqlHelpers end # Raises an error if no data is found - def graphql_data(body = json_response) - # Note that `json_response` is defined as `let(:json_response)` and - # therefore, in a spec with multiple queries, will only contain data - # from the _first_ query, not subsequent ones + # NB: We use fresh_response_data to support tests that make multiple requests. + def graphql_data(body = fresh_response_data) body['data'] || (raise NoData, graphql_errors(body)) end @@ -510,8 +521,12 @@ module GraphqlHelpers end end - def global_id_of(model) - model.to_global_id.to_s + def global_id_of(model, id: nil, model_name: nil) + if id || model_name + ::Gitlab::GlobalId.build(model, id: id, model_name: model_name).to_s + else + model.to_global_id.to_s + end end def missing_required_argument(path, argument) diff --git a/spec/support/helpers/next_found_instance_of.rb b/spec/support/helpers/next_found_instance_of.rb index ff34fcdd1d3..feb63f90211 100644 --- a/spec/support/helpers/next_found_instance_of.rb +++ b/spec/support/helpers/next_found_instance_of.rb @@ -6,7 +6,7 @@ module NextFoundInstanceOf def expect_next_found_instance_of(klass) check_if_active_record!(klass) - stub_allocate(expect(klass)) do |expectation| + stub_allocate(expect(klass), klass) do |expectation| yield(expectation) end end @@ -14,7 +14,7 @@ module NextFoundInstanceOf def allow_next_found_instance_of(klass) check_if_active_record!(klass) - stub_allocate(allow(klass)) do |allowance| + stub_allocate(allow(klass), klass) do |allowance| yield(allowance) end end @@ -25,9 +25,17 @@ module NextFoundInstanceOf raise ArgumentError.new(ERROR_MESSAGE) unless klass < ActiveRecord::Base end - def stub_allocate(target) + def stub_allocate(target, klass) target.to receive(:allocate).and_wrap_original do |method| - method.call.tap { |allocation| yield(allocation) } + method.call.tap do |allocation| + # ActiveRecord::Core.allocate returns a frozen object: + # https://github.com/rails/rails/blob/291a3d2ef29a3842d1156ada7526f4ee60dd2b59/activerecord/lib/active_record/core.rb#L620 + # It's unexpected behavior and probably a bug in Rails + # Let's work it around by setting the attributes to default to unfreeze the object for now + allocation.instance_variable_set(:@attributes, klass._default_attributes) + + yield(allocation) + end end end end diff --git a/spec/support/helpers/search_settings_helpers.rb b/spec/support/helpers/search_settings_helpers.rb new file mode 100644 index 00000000000..838f897bff5 --- /dev/null +++ b/spec/support/helpers/search_settings_helpers.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module SearchHelpers + self::INPUT_PLACEHOLDER = 'Search settings' +end diff --git a/spec/support/helpers/seed_helper.rb b/spec/support/helpers/seed_helper.rb index 90d7f60fdeb..f65993efa05 100644 --- a/spec/support/helpers/seed_helper.rb +++ b/spec/support/helpers/seed_helper.rb @@ -4,7 +4,7 @@ require_relative 'test_env' # This file is specific to specs in spec/lib/gitlab/git/ -SEED_STORAGE_PATH = TestEnv.repos_path +SEED_STORAGE_PATH = Gitlab::GitalyClient::StorageSettings.allow_disk_access { TestEnv.repos_path } TEST_REPO_PATH = 'gitlab-git-test.git' TEST_NORMAL_REPO_PATH = 'not-bare-repo.git' TEST_MUTABLE_REPO_PATH = 'mutable-repo.git' diff --git a/spec/support/helpers/smime_helper.rb b/spec/support/helpers/smime_helper.rb index 261aef9518e..fa16c433c6b 100644 --- a/spec/support/helpers/smime_helper.rb +++ b/spec/support/helpers/smime_helper.rb @@ -52,7 +52,7 @@ module SmimeHelper cert.add_extension(extension_factory.create_extension('extendedKeyUsage', 'clientAuth,emailProtection', false)) end - cert.sign(signed_by&.fetch(:key, nil) || key, OpenSSL::Digest::SHA256.new) + cert.sign(signed_by&.fetch(:key, nil) || key, OpenSSL::Digest.new('SHA256')) { key: key, cert: cert } end diff --git a/spec/support/helpers/sorting_helper.rb b/spec/support/helpers/sorting_helper.rb index 3801d25fb63..f19f8c12928 100644 --- a/spec/support/helpers/sorting_helper.rb +++ b/spec/support/helpers/sorting_helper.rb @@ -17,4 +17,35 @@ module SortingHelper click_link value end end + + def nils_last(value) + NilsLast.new(value) + end + + class NilsLast + include Comparable + + attr_reader :value + delegate :==, :eql?, :hash, to: :value + + def initialize(value) + @value = value + @reverse = false + end + + def <=>(other) + return unless other.is_a?(self.class) + return 0 if value.nil? && other.value.nil? + return 1 if value.nil? + return -1 if other.value.nil? + + int = value <=> other.value + @reverse ? -int : int + end + + def -@ + @reverse = true + self + end + end end diff --git a/spec/support/helpers/stub_configuration.rb b/spec/support/helpers/stub_configuration.rb index 3b733a2e57a..9851a3de9e9 100644 --- a/spec/support/helpers/stub_configuration.rb +++ b/spec/support/helpers/stub_configuration.rb @@ -121,6 +121,12 @@ module StubConfiguration allow(::Gitlab.config.packages).to receive_messages(to_settings(messages)) end + def stub_maintenance_mode_setting(value) + allow(Gitlab::CurrentSettings).to receive(:current_application_settings?).and_return(true) + + stub_application_setting(maintenance_mode: value) + end + private # Modifies stubbed messages to also stub possible predicate versions diff --git a/spec/support/helpers/stub_object_storage.rb b/spec/support/helpers/stub_object_storage.rb index dc54a21d0fa..0d0ac171baa 100644 --- a/spec/support/helpers/stub_object_storage.rb +++ b/spec/support/helpers/stub_object_storage.rb @@ -85,6 +85,13 @@ module StubObjectStorage **params) end + def stub_composer_cache_object_storage(**params) + stub_object_storage_uploader(config: Gitlab.config.packages.object_store, + uploader: ::Packages::Composer::CacheUploader, + remote_directory: 'packages', + **params) + end + def stub_uploads_object_storage(uploader = described_class, **params) stub_object_storage_uploader(config: Gitlab.config.uploads.object_store, uploader: uploader, diff --git a/spec/support/helpers/test_env.rb b/spec/support/helpers/test_env.rb index cb25f5f9429..2d71662b0eb 100644 --- a/spec/support/helpers/test_env.rb +++ b/spec/support/helpers/test_env.rb @@ -149,7 +149,9 @@ module TestEnv end end - FileUtils.mkdir_p(repos_path) + FileUtils.mkdir_p( + Gitlab::GitalyClient::StorageSettings.allow_disk_access { TestEnv.repos_path } + ) FileUtils.mkdir_p(SECOND_STORAGE_PATH) FileUtils.mkdir_p(backup_path) FileUtils.mkdir_p(pages_path) diff --git a/spec/support/helpers/wait_for_requests.rb b/spec/support/helpers/wait_for_requests.rb index 43060e571a9..8fd9bb47053 100644 --- a/spec/support/helpers/wait_for_requests.rb +++ b/spec/support/helpers/wait_for_requests.rb @@ -52,6 +52,6 @@ module WaitForRequests end def finished_all_ajax_requests? - Capybara.page.evaluate_script('window.pendingRequests || window.pendingRailsUJSRequests || 0').zero? # rubocop:disable Style/NumericPredicate + Capybara.page.evaluate_script('window.pendingRequests || window.pendingApolloRequests || window.pendingRailsUJSRequests || 0').zero? # rubocop:disable Style/NumericPredicate end end |