diff options
Diffstat (limited to 'spec/support/helpers')
22 files changed, 162 insertions, 76 deletions
diff --git a/spec/support/helpers/bare_repo_operations.rb b/spec/support/helpers/bare_repo_operations.rb index 099610f087d..98fa13db6c2 100644 --- a/spec/support/helpers/bare_repo_operations.rb +++ b/spec/support/helpers/bare_repo_operations.rb @@ -44,7 +44,7 @@ class BareRepoOperations yield stdin if block_given? end - unless status.zero? + unless status == 0 if allow_failure return [] else diff --git a/spec/support/helpers/cycle_analytics_helpers.rb b/spec/support/helpers/cycle_analytics_helpers.rb index d101b092e7d..f4343b8b783 100644 --- a/spec/support/helpers/cycle_analytics_helpers.rb +++ b/spec/support/helpers/cycle_analytics_helpers.rb @@ -37,7 +37,7 @@ module CycleAnalyticsHelpers end def create_cycle(user, project, issue, mr, milestone, pipeline) - issue.update(milestone: milestone) + issue.update!(milestone: milestone) pipeline.run ci_build = create(:ci_build, pipeline: pipeline, status: :success, author: user) diff --git a/spec/support/helpers/design_management_test_helpers.rb b/spec/support/helpers/design_management_test_helpers.rb index 1daa92e8ad4..db217250b17 100644 --- a/spec/support/helpers/design_management_test_helpers.rb +++ b/spec/support/helpers/design_management_test_helpers.rb @@ -35,9 +35,9 @@ module DesignManagementTestHelpers def act_on_designs(designs, &block) issue = designs.first.issue - version = build(:design_version, :empty, issue: issue).tap { |v| v.save(validate: false) } + version = build(:design_version, :empty, issue: issue).tap { |v| v.save!(validate: false) } designs.each do |d| - yield.create(design: d, version: version) + yield.create!(design: d, version: version) end version end diff --git a/spec/support/helpers/filtered_search_helpers.rb b/spec/support/helpers/filtered_search_helpers.rb index 1847a8f8a06..d203ff60cc9 100644 --- a/spec/support/helpers/filtered_search_helpers.rb +++ b/spec/support/helpers/filtered_search_helpers.rb @@ -13,7 +13,7 @@ module FilteredSearchHelpers search = "#{search_term} " end - filtered_search.set(search) + filtered_search.set(search, rapid: false) if submit # Wait for the lazy author/assignee tokens that diff --git a/spec/support/helpers/http_basic_auth_helpers.rb b/spec/support/helpers/http_basic_auth_helpers.rb index c0b24b3dfa4..bc34e073f9f 100644 --- a/spec/support/helpers/http_basic_auth_helpers.rb +++ b/spec/support/helpers/http_basic_auth_helpers.rb @@ -8,19 +8,22 @@ module HttpBasicAuthHelpers end def job_basic_auth_header(job) - basic_auth_header(Ci::Build::CI_REGISTRY_USER, job.token) + basic_auth_header(::Gitlab::Auth::CI_JOB_USER, job.token) end def client_basic_auth_header(client) basic_auth_header(client.uid, client.secret) end + def build_auth_headers(value) + { 'HTTP_AUTHORIZATION' => value } + end + + def build_token_auth_header(token) + build_auth_headers("Bearer #{token}") + end + def basic_auth_header(username, password) - { - 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials( - username, - password - ) - } + build_auth_headers(ActionController::HttpAuthentication::Basic.encode_credentials(username, password)) end end diff --git a/spec/support/helpers/jira_service_helper.rb b/spec/support/helpers/jira_service_helper.rb index 9072c41fe66..4895bc3ba15 100644 --- a/spec/support/helpers/jira_service_helper.rb +++ b/spec/support/helpers/jira_service_helper.rb @@ -10,7 +10,7 @@ module JiraServiceHelper password = 'my-secret-password' jira_issue_transition_id = '1' - jira_tracker.update( + jira_tracker.update!( url: url, username: username, password: password, jira_issue_transition_id: jira_issue_transition_id, active: true ) diff --git a/spec/support/helpers/login_helpers.rb b/spec/support/helpers/login_helpers.rb index 92f6d673255..1118cfcf7ac 100644 --- a/spec/support/helpers/login_helpers.rb +++ b/spec/support/helpers/login_helpers.rb @@ -40,7 +40,7 @@ module LoginHelpers if user_or_role.is_a?(User) user_or_role else - create(user_or_role) + create(user_or_role) # rubocop:disable Rails/SaveBang end gitlab_sign_in_with(user, **kwargs) diff --git a/spec/support/helpers/memory_usage_helper.rb b/spec/support/helpers/memory_usage_helper.rb index 984ea8cc571..aa7b3bae83a 100644 --- a/spec/support/helpers/memory_usage_helper.rb +++ b/spec/support/helpers/memory_usage_helper.rb @@ -21,7 +21,7 @@ module MemoryUsageHelper def get_memory_usage output, status = Gitlab::Popen.popen(%w(free -m)) - abort "`free -m` return code is #{status}: #{output}" unless status.zero? + abort "`free -m` return code is #{status}: #{output}" unless status == 0 result = output.split("\n")[1].split(" ")[1..-1] attrs = %i(m_total m_used m_free m_shared m_buffers_cache m_available).freeze diff --git a/spec/support/helpers/metrics_dashboard_helpers.rb b/spec/support/helpers/metrics_dashboard_helpers.rb index b2dd8ead7dd..7168079fead 100644 --- a/spec/support/helpers/metrics_dashboard_helpers.rb +++ b/spec/support/helpers/metrics_dashboard_helpers.rb @@ -1,16 +1,22 @@ # frozen_string_literal: true module MetricsDashboardHelpers - def project_with_dashboard(dashboard_path, dashboard_yml = nil) - dashboard_yml ||= fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') - - create(:project, :custom_repo, files: { dashboard_path => dashboard_yml }) + # @param dashboards [Hash<string, string>] - Should contain a hash where + # each key is the path to a dashboard in the repository and each value is + # the dashboard content. + # Ex: { '.gitlab/dashboards/dashboard1.yml' => fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') } + def project_with_dashboards(dashboards, project_params = {}) + create(:project, :custom_repo, **project_params, files: dashboards) end - def project_with_dashboard_namespace(dashboard_path, dashboard_yml = nil) + def project_with_dashboard(dashboard_path, dashboard_yml = nil, project_params = {}) dashboard_yml ||= fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') - create(:project, :custom_repo, namespace: namespace, path: 'monitor-project', files: { dashboard_path => dashboard_yml }) + project_with_dashboards({ dashboard_path => dashboard_yml }, project_params) + end + + def project_with_dashboard_namespace(dashboard_path, dashboard_yml = nil, project_params = {}) + project_with_dashboard(dashboard_path, dashboard_yml, project_params.reverse_merge(path: 'monitor-project')) end def delete_project_dashboard(project, user, dashboard_path) diff --git a/spec/support/helpers/metrics_dashboard_url_helpers.rb b/spec/support/helpers/metrics_dashboard_url_helpers.rb index cb9f58753a3..58b3d1e4d1d 100644 --- a/spec/support/helpers/metrics_dashboard_url_helpers.rb +++ b/spec/support/helpers/metrics_dashboard_url_helpers.rb @@ -13,4 +13,14 @@ module MetricsDashboardUrlHelpers Gitlab::Metrics::Dashboard::Url.clear_memoization(method_name) end end + + def stub_gitlab_domain + allow_any_instance_of(Banzai::Filter::InlineEmbedsFilter) + .to receive(:gitlab_domain) + .and_return(urls.root_url.chomp('/')) + + allow(Gitlab::Metrics::Dashboard::Url) + .to receive(:gitlab_domain) + .and_return(urls.root_url.chomp('/')) + end end diff --git a/spec/support/helpers/navbar_structure_helper.rb b/spec/support/helpers/navbar_structure_helper.rb index cfb1b185560..c7aa2ffe536 100644 --- a/spec/support/helpers/navbar_structure_helper.rb +++ b/spec/support/helpers/navbar_structure_helper.rb @@ -18,4 +18,22 @@ module NavbarStructureHelper index = hash[:nav_sub_items].find_index(before_sub_nav_item_name) hash[:nav_sub_items].insert(index + 1, new_sub_nav_item_name) end + + def insert_package_nav(within) + insert_after_nav_item( + within, + new_nav_item: { + nav_item: _('Packages & Registries'), + nav_sub_items: [_('Package Registry')] + } + ) + end + + def insert_container_nav(within) + insert_after_sub_nav_item( + _('Package Registry'), + within: _('Packages & Registries'), + new_sub_nav_item_name: _('Container Registry') + ) + end end diff --git a/spec/support/helpers/notification_helpers.rb b/spec/support/helpers/notification_helpers.rb index 887d68de4e1..aee57b452fe 100644 --- a/spec/support/helpers/notification_helpers.rb +++ b/spec/support/helpers/notification_helpers.rb @@ -12,7 +12,7 @@ module NotificationHelpers def create_global_setting_for(user, level) setting = user.global_notification_setting setting.level = level - setting.save + setting.save! user end @@ -27,7 +27,7 @@ module NotificationHelpers def create_notification_setting(user, resource, level) setting = user.notification_settings_for(resource) setting.level = level - setting.save + setting.save! end # Create custom notifications diff --git a/spec/support/helpers/packages_manager_api_spec_helper.rb b/spec/support/helpers/packages_manager_api_spec_helper.rb index e5a690e1680..34e92c0595c 100644 --- a/spec/support/helpers/packages_manager_api_spec_helper.rb +++ b/spec/support/helpers/packages_manager_api_spec_helper.rb @@ -1,18 +1,6 @@ # frozen_string_literal: true module PackagesManagerApiSpecHelpers - def build_auth_headers(value) - { 'HTTP_AUTHORIZATION' => value } - end - - def build_basic_auth_header(username, password) - build_auth_headers(ActionController::HttpAuthentication::Basic.encode_credentials(username, password)) - end - - def build_token_auth_header(token) - build_auth_headers("Bearer #{token}") - end - def build_jwt(personal_access_token, secret: jwt_secret, user_id: nil) JSONWebToken::HMACToken.new(secret).tap do |jwt| jwt['access_token'] = personal_access_token.id diff --git a/spec/support/helpers/require_migration.rb b/spec/support/helpers/require_migration.rb new file mode 100644 index 00000000000..d3f192a4142 --- /dev/null +++ b/spec/support/helpers/require_migration.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'find' + +class RequireMigration + MIGRATION_FOLDERS = %w(db/migrate db/post_migrate ee/db/geo/migrate ee/db/geo/post_migrate).freeze + SPEC_FILE_PATTERN = /.+\/(?<file_name>.+)_spec\.rb/.freeze + + class << self + def require_migration!(file_name) + file_paths = search_migration_file(file_name) + + require file_paths.first + end + + def search_migration_file(file_name) + MIGRATION_FOLDERS.flat_map do |path| + migration_path = Rails.root.join(path).to_s + + Find.find(migration_path).grep(/\d+_#{file_name}\.rb/) + end + end + end +end + +def require_migration!(file_name = nil) + location_info = caller_locations.first.path.match(RequireMigration::SPEC_FILE_PATTERN) + file_name ||= location_info[:file_name] + + RequireMigration.require_migration!(file_name) +end diff --git a/spec/support/helpers/stub_configuration.rb b/spec/support/helpers/stub_configuration.rb index e19f230d8df..3b733a2e57a 100644 --- a/spec/support/helpers/stub_configuration.rb +++ b/spec/support/helpers/stub_configuration.rb @@ -33,8 +33,8 @@ module StubConfiguration allow(Gitlab.config).to receive_messages(to_settings(messages)) end - def stub_default_url_options(host: "localhost", protocol: "http") - url_options = { host: host, protocol: protocol } + def stub_default_url_options(host: "localhost", protocol: "http", script_name: nil) + url_options = { host: host, protocol: protocol, script_name: script_name } allow(Rails.application.routes).to receive(:default_url_options).and_return(url_options) end diff --git a/spec/support/helpers/stub_feature_flags.rb b/spec/support/helpers/stub_feature_flags.rb index 696148cacaf..792a1c21c31 100644 --- a/spec/support/helpers/stub_feature_flags.rb +++ b/spec/support/helpers/stub_feature_flags.rb @@ -1,6 +1,11 @@ # frozen_string_literal: true module StubFeatureFlags + def self.included(base) + # Extend Feature class with methods that can stub feature flags. + Feature.prepend(StubbedFeature) + end + class StubFeatureGate attr_reader :flipper_id @@ -9,28 +14,14 @@ module StubFeatureFlags end end + # Ensure feature flags are stubbed and reset. def stub_all_feature_flags - adapter = Flipper::Adapters::Memory.new - flipper = Flipper.new(adapter) - - allow(Feature).to receive(:flipper).and_return(flipper) - - # All new requested flags are enabled by default - allow(Feature).to receive(:enabled?).and_wrap_original do |m, *args| - feature_flag = m.call(*args) - - # If feature flag is not persisted we mark the feature flag as enabled - # We do `m.call` as we want to validate the execution of method arguments - # and a feature flag state if it is not persisted - unless Feature.persisted_name?(args.first) - # TODO: this is hack to support `promo_feature_available?` - # We enable all feature flags by default unless they are `promo_` - # Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/218667 - feature_flag = true unless args.first.to_s.start_with?('promo_') - end + Feature.stub = true + Feature.reset_flipper + end - feature_flag - end + def unstub_all_feature_flags + Feature.stub = false end # Stub Feature flags with `flag_name: true/false` diff --git a/spec/support/helpers/stub_object_storage.rb b/spec/support/helpers/stub_object_storage.rb index 6056359d026..8a52a614821 100644 --- a/spec/support/helpers/stub_object_storage.rb +++ b/spec/support/helpers/stub_object_storage.rb @@ -1,13 +1,6 @@ # frozen_string_literal: true module StubObjectStorage - def stub_packages_object_storage(**params) - stub_object_storage_uploader(config: ::Gitlab.config.packages.object_store, - uploader: ::Packages::PackageFileUploader, - remote_directory: 'packages', - **params) - end - def stub_dependency_proxy_object_storage(**params) stub_object_storage_uploader(config: ::Gitlab.config.dependency_proxy.object_store, uploader: ::DependencyProxy::FileUploader, @@ -44,7 +37,7 @@ module StubObjectStorage Fog.mock! ::Fog::Storage.new(connection_params).tap do |connection| - connection.directories.create(key: remote_directory) + connection.directories.create(key: remote_directory) # rubocop:disable Rails/SaveBang # Cleanup remaining files connection.directories.each do |directory| @@ -54,9 +47,9 @@ module StubObjectStorage end end - def stub_artifacts_object_storage(**params) + def stub_artifacts_object_storage(uploader = JobArtifactUploader, **params) stub_object_storage_uploader(config: Gitlab.config.artifacts.object_store, - uploader: JobArtifactUploader, + uploader: uploader, remote_directory: 'artifacts', **params) end diff --git a/spec/support/helpers/stubbed_feature.rb b/spec/support/helpers/stubbed_feature.rb new file mode 100644 index 00000000000..e78efcf6b75 --- /dev/null +++ b/spec/support/helpers/stubbed_feature.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +# Extend the Feature class with the ability to stub feature flags. +module StubbedFeature + extend ActiveSupport::Concern + + class_methods do + # Turn stubbed feature flags on or off. + def stub=(stub) + @stub = stub + end + + def stub? + @stub.nil? ? true : @stub + end + + # Wipe any previously set feature flags. + def reset_flipper + @flipper = nil + end + + # Replace #flipper method with the optional stubbed/unstubbed version. + def flipper + if stub? + @flipper ||= Flipper.new(Flipper::Adapters::Memory.new) + else + super + end + end + + # Replace #enabled? method with the optional stubbed/unstubbed version. + def enabled?(*args) + feature_flag = super(*args) + return feature_flag unless stub? + + # If feature flag is not persisted we mark the feature flag as enabled + # We do `m.call` as we want to validate the execution of method arguments + # and a feature flag state if it is not persisted + unless Feature.persisted_name?(args.first) + # TODO: this is hack to support `promo_feature_available?` + # We enable all feature flags by default unless they are `promo_` + # Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/218667 + feature_flag = true unless args.first.to_s.start_with?('promo_') + end + + feature_flag + end + end +end diff --git a/spec/support/helpers/test_env.rb b/spec/support/helpers/test_env.rb index f787aedf7aa..7dae960410d 100644 --- a/spec/support/helpers/test_env.rb +++ b/spec/support/helpers/test_env.rb @@ -6,8 +6,6 @@ module TestEnv ComponentFailedToInstallError = Class.new(StandardError) - SHA_REGEX = /\A[0-9a-f]{5,40}\z/i.freeze - # When developing the seed repository, comment out the branch you will modify. BRANCH_SHA = { 'signed-commits' => '6101e87', @@ -78,7 +76,7 @@ module TestEnv 'png-lfs' => 'fe42f41', 'sha-starting-with-large-number' => '8426165', 'invalid-utf8-diff-paths' => '99e4853', - 'compare-with-merge-head-source' => 'b5f4399', + 'compare-with-merge-head-source' => 'f20a03d', 'compare-with-merge-head-target' => '2f1e176' }.freeze @@ -524,7 +522,7 @@ module TestEnv def component_matches_git_sha?(component_folder, expected_version) # Not a git SHA, so return early - return false unless expected_version =~ SHA_REGEX + return false unless expected_version =~ ::Gitlab::Git::COMMIT_ID sha, exit_status = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} rev-parse HEAD), component_folder) return false if exit_status != 0 diff --git a/spec/support/helpers/trigger_helpers.rb b/spec/support/helpers/trigger_helpers.rb index 67c62cf4869..dd6d8ff5bb5 100644 --- a/spec/support/helpers/trigger_helpers.rb +++ b/spec/support/helpers/trigger_helpers.rb @@ -28,8 +28,6 @@ module TriggerHelpers expect(timing).to eq(expected_timing.to_s) expect(events).to match_array(Array.wrap(expected_events)) - # TODO: Update CREATE TRIGGER syntax to use EXECUTE FUNCTION - # https://gitlab.com/gitlab-org/gitlab/-/issues/227089 expect(definition).to match(%r{execute (?:procedure|function) #{fn_name}()}) end diff --git a/spec/support/helpers/usage_data_helpers.rb b/spec/support/helpers/usage_data_helpers.rb index a4f40a4af0a..fab775dd404 100644 --- a/spec/support/helpers/usage_data_helpers.rb +++ b/spec/support/helpers/usage_data_helpers.rb @@ -119,6 +119,7 @@ module UsageDataHelpers projects_with_terraform_states pages_domains protected_branches + protected_branches_except_default releases remote_mirrors snippets diff --git a/spec/support/helpers/wait_for_requests.rb b/spec/support/helpers/wait_for_requests.rb index 52d1c59ab03..2cfd47634ca 100644 --- a/spec/support/helpers/wait_for_requests.rb +++ b/spec/support/helpers/wait_for_requests.rb @@ -42,7 +42,7 @@ module WaitForRequests private def finished_all_rack_requests? - Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? + Gitlab::Testing::RequestBlockerMiddleware.num_active_requests == 0 end def finished_all_js_requests? @@ -53,12 +53,12 @@ module WaitForRequests end def finished_all_axios_requests? - Capybara.page.evaluate_script('window.pendingRequests || 0').zero? + Capybara.page.evaluate_script('window.pendingRequests || 0').zero? # rubocop:disable Style/NumericPredicate end def finished_all_ajax_requests? return true if Capybara.page.evaluate_script('typeof jQuery === "undefined"') - Capybara.page.evaluate_script('jQuery.active').zero? + Capybara.page.evaluate_script('jQuery.active').zero? # rubocop:disable Style/NumericPredicate end end |