diff options
Diffstat (limited to 'spec')
17 files changed, 183 insertions, 504 deletions
diff --git a/spec/features/projects/blobs/blob_show_spec.rb b/spec/features/projects/blobs/blob_show_spec.rb index cbac5cdac39..b7e0e3fd590 100644 --- a/spec/features/projects/blobs/blob_show_spec.rb +++ b/spec/features/projects/blobs/blob_show_spec.rb @@ -589,76 +589,35 @@ RSpec.describe 'File blob', :js, feature_category: :projects do file_path: '.gitlab/dashboards/custom-dashboard.yml', file_content: file_content ).execute - end - - context 'with metrics_dashboard_exhaustive_validations feature flag off' do - before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: false) - visit_blob('.gitlab/dashboards/custom-dashboard.yml') - end - - context 'valid dashboard file' do - let(:file_content) { File.read(Rails.root.join('config/prometheus/common_metrics.yml')) } - - it 'displays an auxiliary viewer' do - aggregate_failures do - # shows that dashboard yaml is valid - expect(page).to have_content('Metrics Dashboard YAML definition is valid.') - - # shows a learn more link - expect(page).to have_link('Learn more') - end - end - end - - context 'invalid dashboard file' do - let(:file_content) { "dashboard: 'invalid'" } - - it 'displays an auxiliary viewer' do - aggregate_failures do - # shows that dashboard yaml is invalid - expect(page).to have_content('Metrics Dashboard YAML definition is invalid:') - expect(page).to have_content("panel_groups: should be an array of panel_groups objects") - # shows a learn more link - expect(page).to have_link('Learn more') - end - end - end + visit_blob('.gitlab/dashboards/custom-dashboard.yml') end - context 'with metrics_dashboard_exhaustive_validations feature flag on' do - before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: true) - visit_blob('.gitlab/dashboards/custom-dashboard.yml') - end - - context 'valid dashboard file' do - let(:file_content) { File.read(Rails.root.join('config/prometheus/common_metrics.yml')) } + context 'valid dashboard file' do + let(:file_content) { File.read(Rails.root.join('config/prometheus/common_metrics.yml')) } - it 'displays an auxiliary viewer' do - aggregate_failures do - # shows that dashboard yaml is valid - expect(page).to have_content('Metrics Dashboard YAML definition is valid.') + it 'displays an auxiliary viewer' do + aggregate_failures do + # shows that dashboard yaml is valid + expect(page).to have_content('Metrics Dashboard YAML definition is valid.') - # shows a learn more link - expect(page).to have_link('Learn more') - end + # shows a learn more link + expect(page).to have_link('Learn more') end end + end - context 'invalid dashboard file' do - let(:file_content) { "dashboard: 'invalid'" } + context 'invalid dashboard file' do + let(:file_content) { "dashboard: 'invalid'" } - it 'displays an auxiliary viewer' do - aggregate_failures do - # shows that dashboard yaml is invalid - expect(page).to have_content('Metrics Dashboard YAML definition is invalid:') - expect(page).to have_content("root is missing required keys: panel_groups") + it 'displays an auxiliary viewer' do + aggregate_failures do + # shows that dashboard yaml is invalid + expect(page).to have_content('Metrics Dashboard YAML definition is invalid:') + expect(page).to have_content("panel_groups: should be an array of panel_groups objects") - # shows a learn more link - expect(page).to have_link('Learn more') - end + # shows a learn more link + expect(page).to have_link('Learn more') end end end diff --git a/spec/frontend/projects/commit/components/branches_dropdown_spec.js b/spec/frontend/projects/commit/components/branches_dropdown_spec.js index e2848e615c3..a84dd246f5d 100644 --- a/spec/frontend/projects/commit/components/branches_dropdown_spec.js +++ b/spec/frontend/projects/commit/components/branches_dropdown_spec.js @@ -13,7 +13,7 @@ describe('BranchesDropdown', () => { let store; const spyFetchBranches = jest.fn(); - const createComponent = (term, state = { isFetching: false }) => { + const createComponent = (props, state = { isFetching: false }) => { store = new Vuex.Store({ getters: { joinedBranches: () => ['_main_', '_branch_1_', '_branch_2_'], @@ -28,7 +28,8 @@ describe('BranchesDropdown', () => { shallowMount(BranchesDropdown, { store, propsData: { - value: term, + value: props.value, + blanked: props.blanked || false, }, }), ); @@ -48,23 +49,40 @@ describe('BranchesDropdown', () => { describe('On mount', () => { beforeEach(() => { - createComponent(''); + createComponent({ value: '' }); }); it('invokes fetchBranches', () => { expect(spyFetchBranches).toHaveBeenCalled(); }); + + describe('with a value but visually blanked', () => { + beforeEach(() => { + createComponent({ value: '_main_', blanked: true }, { branch: '_main_' }); + }); + + it('renders all branches', () => { + expect(findAllDropdownItems()).toHaveLength(3); + expect(findDropdownItemByIndex(0).text()).toBe('_main_'); + expect(findDropdownItemByIndex(1).text()).toBe('_branch_1_'); + expect(findDropdownItemByIndex(2).text()).toBe('_branch_2_'); + }); + + it('selects the active branch', () => { + expect(wrapper.vm.isSelected('_main_')).toBe(true); + }); + }); }); describe('Loading states', () => { it('shows loading icon while fetching', () => { - createComponent('', { isFetching: true }); + createComponent({ value: '' }, { isFetching: true }); expect(findLoading().isVisible()).toBe(true); }); it('does not show loading icon', () => { - createComponent(''); + createComponent({ value: '' }); expect(findLoading().isVisible()).toBe(false); }); @@ -72,7 +90,7 @@ describe('BranchesDropdown', () => { describe('No branches found', () => { beforeEach(() => { - createComponent('_non_existent_branch_'); + createComponent({ value: '_non_existent_branch_' }); }); it('renders empty results message', () => { @@ -90,7 +108,7 @@ describe('BranchesDropdown', () => { describe('Search term is empty', () => { beforeEach(() => { - createComponent(''); + createComponent({ value: '' }); }); it('renders all branches when search term is empty', () => { @@ -107,7 +125,7 @@ describe('BranchesDropdown', () => { describe('When searching', () => { beforeEach(() => { - createComponent(''); + createComponent({ value: '' }); }); it('invokes fetchBranches', async () => { @@ -124,7 +142,7 @@ describe('BranchesDropdown', () => { describe('Branches found', () => { beforeEach(() => { - createComponent('_branch_1_', { branch: '_branch_1_' }); + createComponent({ value: '_branch_1_' }, { branch: '_branch_1_' }); }); it('renders only the branch searched for', () => { @@ -156,7 +174,7 @@ describe('BranchesDropdown', () => { describe('Case insensitive for search term', () => { beforeEach(() => { - createComponent('_BrAnCh_1_'); + createComponent({ value: '_BrAnCh_1_' }); }); it('renders only the branch searched for', () => { diff --git a/spec/lib/gitlab/api_authentication/sent_through_builder_spec.rb b/spec/lib/gitlab/api_authentication/sent_through_builder_spec.rb index 845e317f3aa..aa088a5d6d5 100644 --- a/spec/lib/gitlab/api_authentication/sent_through_builder_spec.rb +++ b/spec/lib/gitlab/api_authentication/sent_through_builder_spec.rb @@ -8,10 +8,10 @@ RSpec.describe Gitlab::APIAuthentication::SentThroughBuilder do let(:locators) { Array.new(3) { double } } it 'adds a strategy for each of locators x resolvers' do - strategies = locators.to_h { |l| [l, []] } + strategies = locators.index_with { [] } described_class.new(strategies, resolvers).sent_through(*locators) - expect(strategies).to eq(locators.to_h { |l| [l, resolvers] }) + expect(strategies).to eq(locators.index_with { resolvers }) end end end diff --git a/spec/lib/gitlab/conflict/file_spec.rb b/spec/lib/gitlab/conflict/file_spec.rb index 165305476d2..6ea8e6c6706 100644 --- a/spec/lib/gitlab/conflict/file_spec.rb +++ b/spec/lib/gitlab/conflict/file_spec.rb @@ -30,7 +30,7 @@ RSpec.describe Gitlab::Conflict::File do let(:section_keys) { conflict_file.sections.map { |section| section[:id] }.compact } context 'when resolving everything to the same side' do - let(:resolution_hash) { section_keys.to_h { |key| [key, 'head'] } } + let(:resolution_hash) { section_keys.index_with { 'head' } } let(:resolved_lines) { conflict_file.resolve_lines(resolution_hash) } let(:expected_lines) { conflict_file.lines.reject { |line| line.type == 'old' } } @@ -63,8 +63,8 @@ RSpec.describe Gitlab::Conflict::File do end it 'raises ResolutionError when passed a hash without resolutions for all sections' do - empty_hash = section_keys.to_h { |key| [key, nil] } - invalid_hash = section_keys.to_h { |key| [key, 'invalid'] } + empty_hash = section_keys.index_with { nil } + invalid_hash = section_keys.index_with { 'invalid' } expect { conflict_file.resolve_lines({}) } .to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError) diff --git a/spec/lib/gitlab/github_import/client_spec.rb b/spec/lib/gitlab/github_import/client_spec.rb index 625212da394..526a8721ff3 100644 --- a/spec/lib/gitlab/github_import/client_spec.rb +++ b/spec/lib/gitlab/github_import/client_spec.rb @@ -679,7 +679,7 @@ RSpec.describe Gitlab::GithubImport::Client do context 'when pagination options present' do it 'searches for repositories via expected query' do expect(client.octokit).to receive(:search_repositories).with( - expected_query, page: 2, per_page: 25 + expected_query, { page: 2, per_page: 25 } ) client.search_repos_by_name('test', { page: 2, per_page: 25 }) diff --git a/spec/lib/gitlab/import_export/model_configuration_spec.rb b/spec/lib/gitlab/import_export/model_configuration_spec.rb index 34591122a97..4f01f470ce7 100644 --- a/spec/lib/gitlab/import_export/model_configuration_spec.rb +++ b/spec/lib/gitlab/import_export/model_configuration_spec.rb @@ -23,8 +23,8 @@ RSpec.describe 'Import/Export model configuration' do # List of current models between models, in the format of # {model: [model_2, model3], ...} def setup_models - model_names.each_with_object({}) do |model_name, hash| - hash[model_name] = associations_for(relation_class_for_name(model_name)) - ['project'] + model_names.index_with do |model_name| + associations_for(relation_class_for_name(model_name)) - ['project'] end end diff --git a/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb index b753746cd8c..2699dc10b18 100644 --- a/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb @@ -768,7 +768,7 @@ RSpec.describe Gitlab::ImportExport::Project::TreeRestorer do it 'overrides project feature access levels' do access_level_keys = ProjectFeature.available_features.map { |feature| ProjectFeature.access_level_attribute(feature) } - disabled_access_levels = access_level_keys.to_h { |item| [item, 'disabled'] } + disabled_access_levels = access_level_keys.index_with { 'disabled' } project.create_import_data(data: { override_params: disabled_access_levels }) diff --git a/spec/lib/gitlab/metrics/dashboard/validator_spec.rb b/spec/lib/gitlab/metrics/dashboard/validator_spec.rb index aaa9daf8fee..fb55b736354 100644 --- a/spec/lib/gitlab/metrics/dashboard/validator_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/validator_spec.rb @@ -143,56 +143,4 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do end end end - - describe '#errors' do - context 'valid dashboard schema' do - it 'returns no errors' do - expect(described_class.errors(valid_dashboard)).to eq [] - end - - context 'with duplicate metric_ids' do - it 'returns errors' do - expect(described_class.errors(duplicate_id_dashboard)).to eq [Gitlab::Metrics::Dashboard::Validator::Errors::DuplicateMetricIds.new] - end - end - - context 'with dashboard_path and project' do - subject { described_class.errors(valid_dashboard, dashboard_path: 'test/path.yml', project: project) } - - context 'with no conflicting metric identifiers in db' do - it { is_expected.to eq [] } - end - - context 'with metric identifier present in current dashboard' do - before do - create(:prometheus_metric, - identifier: 'metric_a1', - dashboard_path: 'test/path.yml', - project: project - ) - end - - it { is_expected.to eq [] } - end - - context 'with metric identifier present in another dashboard' do - before do - create(:prometheus_metric, - identifier: 'metric_a1', - dashboard_path: 'some/other/dashboard/path.yml', - project: project - ) - end - - it { is_expected.to eq [Gitlab::Metrics::Dashboard::Validator::Errors::DuplicateMetricIds.new] } - end - end - end - - context 'invalid dashboard schema' do - it 'returns collection of validation errors' do - expect(described_class.errors(invalid_dashboard)).to all be_kind_of(Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError) - end - end - end end diff --git a/spec/lib/google_api/cloud_platform/client_spec.rb b/spec/lib/google_api/cloud_platform/client_spec.rb index 0c207161927..82ab6c089da 100644 --- a/spec/lib/google_api/cloud_platform/client_spec.rb +++ b/spec/lib/google_api/cloud_platform/client_spec.rb @@ -106,8 +106,8 @@ RSpec.describe GoogleApi::CloudPlatform::Client do let(:enable_addons) { [] } let(:addons_config) do - enable_addons.each_with_object({}) do |addon, hash| - hash[addon] = { disabled: false } + enable_addons.index_with do + { disabled: false } end end diff --git a/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb b/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb index 8d5c7ce84f6..d28fa0bbe97 100644 --- a/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb +++ b/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb @@ -14,224 +14,109 @@ RSpec.describe BlobViewer::MetricsDashboardYml do subject(:viewer) { described_class.new(blob) } - context 'with metrics_dashboard_exhaustive_validations feature flag on' do - before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: true) - end - - context 'when the definition is valid' do + context 'when the definition is valid' do + describe '#valid?' do before do - allow(Gitlab::Metrics::Dashboard::Validator).to receive(:errors).and_return([]) - end - - describe '#valid?' do - it 'calls prepare! on the viewer' do - expect(viewer).to receive(:prepare!) - - viewer.valid? - end - - it 'processes dashboard yaml and returns true', :aggregate_failures do - yml = ::Gitlab::Config::Loader::Yaml.new(data).load_raw! - - expect_next_instance_of(::Gitlab::Config::Loader::Yaml, data) do |loader| - expect(loader).to receive(:load_raw!).and_call_original - end - expect(Gitlab::Metrics::Dashboard::Validator) - .to receive(:errors) - .with(yml, dashboard_path: '.gitlab/dashboards/custom-dashboard.yml', project: project) - .and_call_original - expect(viewer.valid?).to be true - end - end - - describe '#errors' do - it 'returns empty array' do - expect(viewer.errors).to eq [] - end + allow(PerformanceMonitoring::PrometheusDashboard).to receive(:from_json) end - end - context 'when definition is invalid' do - let(:error) { ::Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError.new } - let(:data) do - <<~YAML - dashboard: - YAML - end + it 'calls prepare! on the viewer' do + expect(viewer).to receive(:prepare!) - before do - allow(Gitlab::Metrics::Dashboard::Validator).to receive(:errors).and_return([error]) + viewer.valid? end - describe '#valid?' do - it 'returns false' do - expect(viewer.valid?).to be false - end - end + it 'processes dashboard yaml and returns true', :aggregate_failures do + yml = ::Gitlab::Config::Loader::Yaml.new(data).load_raw! - describe '#errors' do - it 'returns validation errors' do - expect(viewer.errors).to eq ["Dashboard failed schema validation"] + expect_next_instance_of(::Gitlab::Config::Loader::Yaml, data) do |loader| + expect(loader).to receive(:load_raw!).and_call_original end + expect(PerformanceMonitoring::PrometheusDashboard) + .to receive(:from_json) + .with(yml) + .and_call_original + expect(viewer.valid?).to be true end end - context 'when YAML syntax is invalid' do - let(:data) do - <<~YAML - dashboard: 'empty metrics' - panel_groups: - - group: 'Group Title' - YAML - end - - describe '#valid?' do - it 'returns false' do - expect(Gitlab::Metrics::Dashboard::Validator).not_to receive(:errors) - expect(viewer.valid?).to be false - end - end - - describe '#errors' do - it 'returns validation errors' do - expect(viewer.errors).to all be_kind_of String - end - end - end - - context 'when YAML loader raises error' do - let(:data) do - <<~YAML - large yaml file - YAML - end - - before do - allow(::Gitlab::Config::Loader::Yaml).to receive(:new) - .and_raise(::Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big') - end - - it 'is invalid' do - expect(Gitlab::Metrics::Dashboard::Validator).not_to receive(:errors) - expect(viewer.valid?).to be false - end - - it 'returns validation errors' do - expect(viewer.errors).to eq ['The parsed YAML is too big'] + describe '#errors' do + it 'returns empty array' do + expect(viewer.errors).to eq [] end end end - context 'with metrics_dashboard_exhaustive_validations feature flag off' do - before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: false) + context 'when definition is invalid' do + let(:error) { ActiveModel::ValidationError.new(PerformanceMonitoring::PrometheusDashboard.new.tap(&:validate)) } + let(:data) do + <<~YAML + dashboard: + YAML end - context 'when the definition is valid' do - describe '#valid?' do - before do - allow(PerformanceMonitoring::PrometheusDashboard).to receive(:from_json) - end - - it 'calls prepare! on the viewer' do - expect(viewer).to receive(:prepare!) + describe '#valid?' do + it 'returns false' do + expect(PerformanceMonitoring::PrometheusDashboard) + .to receive(:from_json).and_raise(error) - viewer.valid? - end - - it 'processes dashboard yaml and returns true', :aggregate_failures do - yml = ::Gitlab::Config::Loader::Yaml.new(data).load_raw! - - expect_next_instance_of(::Gitlab::Config::Loader::Yaml, data) do |loader| - expect(loader).to receive(:load_raw!).and_call_original - end - expect(PerformanceMonitoring::PrometheusDashboard) - .to receive(:from_json) - .with(yml) - .and_call_original - expect(viewer.valid?).to be true - end - end - - describe '#errors' do - it 'returns empty array' do - expect(viewer.errors).to eq [] - end + expect(viewer.valid?).to be false end end - context 'when definition is invalid' do - let(:error) { ActiveModel::ValidationError.new(PerformanceMonitoring::PrometheusDashboard.new.tap(&:validate)) } - let(:data) do - <<~YAML - dashboard: - YAML - end - - describe '#valid?' do - it 'returns false' do - expect(PerformanceMonitoring::PrometheusDashboard) - .to receive(:from_json).and_raise(error) + describe '#errors' do + it 'returns validation errors' do + allow(PerformanceMonitoring::PrometheusDashboard) + .to receive(:from_json).and_raise(error) - expect(viewer.valid?).to be false - end - end - - describe '#errors' do - it 'returns validation errors' do - allow(PerformanceMonitoring::PrometheusDashboard) - .to receive(:from_json).and_raise(error) - - expect(viewer.errors).to eq error.model.errors.messages.map { |messages| messages.join(': ') } - end + expect(viewer.errors).to eq error.model.errors.messages.map { |messages| messages.join(': ') } end end + end - context 'when YAML syntax is invalid' do - let(:data) do - <<~YAML + context 'when YAML syntax is invalid' do + let(:data) do + <<~YAML dashboard: 'empty metrics' panel_groups: - group: 'Group Title' - YAML - end + YAML + end - describe '#valid?' do - it 'returns false' do - expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json) - expect(viewer.valid?).to be false - end + describe '#valid?' do + it 'returns false' do + expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json) + expect(viewer.valid?).to be false end + end - describe '#errors' do - it 'returns validation errors' do - expect(viewer.errors).to eq ["YAML syntax: (<unknown>): did not find expected key while parsing a block mapping at line 1 column 1"] - end + describe '#errors' do + it 'returns validation errors' do + expect(viewer.errors).to eq ["YAML syntax: (<unknown>): did not find expected key while parsing a block mapping at line 1 column 1"] end end + end - context 'when YAML loader raises error' do - let(:data) do - <<~YAML + context 'when YAML loader raises error' do + let(:data) do + <<~YAML large yaml file - YAML - end + YAML + end - before do - allow(::Gitlab::Config::Loader::Yaml).to( - receive(:new).and_raise(::Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big') - ) - end + before do + allow(::Gitlab::Config::Loader::Yaml).to( + receive(:new).and_raise(::Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big') + ) + end - it 'is invalid' do - expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json) - expect(viewer.valid?).to be false - end + it 'is invalid' do + expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json) + expect(viewer.valid?).to be false + end - it 'returns validation errors' do - expect(viewer.errors).to eq ["YAML syntax: The parsed YAML is too big"] - end + it 'returns validation errors' do + expect(viewer.errors).to eq ["YAML syntax: The parsed YAML is too big"] end end end diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 9579c4c2d27..dee068d9810 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -303,11 +303,11 @@ RSpec.describe Event do end def visible_to_none_except(*roles) - visible_to_none.merge(roles.to_h { |role| [role, true] }) + visible_to_none.merge(roles.index_with { true }) end def visible_to_all_except(*roles) - visible_to_all.merge(roles.to_h { |role| [role, false] }) + visible_to_all.merge(roles.index_with { false }) end shared_examples 'visibility examples' do diff --git a/spec/models/performance_monitoring/prometheus_dashboard_spec.rb b/spec/models/performance_monitoring/prometheus_dashboard_spec.rb index ee2407f21b6..21b16bdeb17 100644 --- a/spec/models/performance_monitoring/prometheus_dashboard_spec.rb +++ b/spec/models/performance_monitoring/prometheus_dashboard_spec.rb @@ -229,83 +229,37 @@ RSpec.describe PerformanceMonitoring::PrometheusDashboard do allow(Gitlab::Metrics::Dashboard::Finder).to receive(:find_raw).with(project, dashboard_path: path).and_call_original end - context 'metrics_dashboard_exhaustive_validations is on' do - before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: true) - end - - context 'when schema is valid' do - let(:dashboard_schema) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')) } + context 'when schema is valid' do + let(:dashboard_schema) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')) } - it 'returns empty array' do - expect(Gitlab::Metrics::Dashboard::Validator).to receive(:errors).with(dashboard_schema, dashboard_path: path, project: project).and_return([]) + it 'returns empty array' do + expect(described_class).to receive(:from_json).with(dashboard_schema) - expect(schema_validation_warnings).to eq [] - end - end - - context 'when schema is invalid' do - let(:dashboard_schema) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/dashboard_missing_panel_groups.yml')) } - - it 'returns array with errors messages' do - error = ::Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError.new - - expect(Gitlab::Metrics::Dashboard::Validator).to receive(:errors).with(dashboard_schema, dashboard_path: path, project: project).and_return([error]) - - expect(schema_validation_warnings).to eq [error.message] - end - end - - context 'when YAML has wrong syntax' do - let(:project) { create(:project, :repository, :custom_repo, files: { path => fixture_file('lib/gitlab/metrics/dashboard/broken_yml_syntax.yml') }) } - - subject(:schema_validation_warnings) { described_class.new(path: path, environment: environment).schema_validation_warnings } - - it 'returns array with errors messages' do - expect(Gitlab::Metrics::Dashboard::Validator).not_to receive(:errors) - - expect(schema_validation_warnings).to eq ['Invalid yaml'] - end + expect(schema_validation_warnings).to eq [] end end - context 'metrics_dashboard_exhaustive_validations is off' do - before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: false) - end - - context 'when schema is valid' do - let(:dashboard_schema) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')) } - - it 'returns empty array' do - expect(described_class).to receive(:from_json).with(dashboard_schema) - - expect(schema_validation_warnings).to eq [] - end - end - - context 'when schema is invalid' do - let(:dashboard_schema) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/dashboard_missing_panel_groups.yml')) } + context 'when schema is invalid' do + let(:dashboard_schema) { YAML.safe_load(fixture_file('lib/gitlab/metrics/dashboard/dashboard_missing_panel_groups.yml')) } - it 'returns array with errors messages' do - instance = described_class.new - instance.errors.add(:test, 'test error') + it 'returns array with errors messages' do + instance = described_class.new + instance.errors.add(:test, 'test error') - expect(described_class).to receive(:from_json).and_raise(ActiveModel::ValidationError.new(instance)) - expect(described_class.new.schema_validation_warnings).to eq ['test: test error'] - end + expect(described_class).to receive(:from_json).and_raise(ActiveModel::ValidationError.new(instance)) + expect(described_class.new.schema_validation_warnings).to eq ['test: test error'] end + end - context 'when YAML has wrong syntax' do - let(:project) { create(:project, :repository, :custom_repo, files: { path => fixture_file('lib/gitlab/metrics/dashboard/broken_yml_syntax.yml') }) } + context 'when YAML has wrong syntax' do + let(:project) { create(:project, :repository, :custom_repo, files: { path => fixture_file('lib/gitlab/metrics/dashboard/broken_yml_syntax.yml') }) } - subject(:schema_validation_warnings) { described_class.new(path: path, environment: environment).schema_validation_warnings } + subject(:schema_validation_warnings) { described_class.new(path: path, environment: environment).schema_validation_warnings } - it 'returns array with errors messages' do - expect(described_class).not_to receive(:from_json) + it 'returns array with errors messages' do + expect(described_class).not_to receive(:from_json) - expect(schema_validation_warnings).to eq ['Invalid yaml'] - end + expect(schema_validation_warnings).to eq ['Invalid yaml'] end end end diff --git a/spec/presenters/projects/security/configuration_presenter_spec.rb b/spec/presenters/projects/security/configuration_presenter_spec.rb index ca7f96b567d..4fe459a798a 100644 --- a/spec/presenters/projects/security/configuration_presenter_spec.rb +++ b/spec/presenters/projects/security/configuration_presenter_spec.rb @@ -11,7 +11,7 @@ RSpec.describe Projects::Security::ConfigurationPresenter do let(:presenter) { described_class.new(project, current_user: current_user) } before do - stub_licensed_features(licensed_scan_types.to_h { |type| [type, true] }) + stub_licensed_features(licensed_scan_types.index_with { true }) end describe '#to_html_data_attribute' do diff --git a/spec/requests/api/graphql/metrics/dashboard_query_spec.rb b/spec/requests/api/graphql/metrics/dashboard_query_spec.rb index 0d0385bf7e7..8db0844c6d7 100644 --- a/spec/requests/api/graphql/metrics/dashboard_query_spec.rb +++ b/spec/requests/api/graphql/metrics/dashboard_query_spec.rb @@ -26,156 +26,73 @@ RSpec.describe 'Getting Metrics Dashboard', feature_category: :metrics do ) end - context 'with metrics_dashboard_exhaustive_validations feature flag off' do + context 'for anonymous user' do before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: false) + post_graphql(query, current_user: current_user) end - context 'for anonymous user' do - before do - post_graphql(query, current_user: current_user) - end - - context 'requested dashboard is available' do - let(:path) { 'config/prometheus/common_metrics.yml' } - - it_behaves_like 'a working graphql query' - - it 'returns nil' do - dashboard = graphql_data.dig('project', 'environments', 'nodes') - - expect(dashboard).to be_nil - end - end - end - - context 'for user with developer access' do - before do - project.add_developer(current_user) - post_graphql(query, current_user: current_user) - end - - context 'requested dashboard is available' do - let(:path) { 'config/prometheus/common_metrics.yml' } - - it_behaves_like 'a working graphql query' - - it 'returns metrics dashboard' do - dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - - expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => nil) - end - - context 'invalid dashboard' do - let(:path) { '.gitlab/dashboards/metrics.yml' } - let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "---\ndashboard: 'test'" }) } - - it 'returns metrics dashboard' do - dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - - expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["panel_groups: should be an array of panel_groups objects"]) - end - end - - context 'empty dashboard' do - let(:path) { '.gitlab/dashboards/metrics.yml' } - let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "" }) } - - it 'returns metrics dashboard' do - dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') + context 'requested dashboard is available' do + let(:path) { 'config/prometheus/common_metrics.yml' } - expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["dashboard: can't be blank", "panel_groups: should be an array of panel_groups objects"]) - end - end - end - - context 'requested dashboard can not be found' do - let(:path) { 'config/prometheus/i_am_not_here.yml' } + it_behaves_like 'a working graphql query' - it_behaves_like 'a working graphql query' + it 'returns nil' do + dashboard = graphql_data.dig('project', 'environments', 'nodes') - it 'returns nil' do - dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - - expect(dashboard).to be_nil - end + expect(dashboard).to be_nil end end end - context 'with metrics_dashboard_exhaustive_validations feature flag on' do + context 'for user with developer access' do before do - stub_feature_flags(metrics_dashboard_exhaustive_validations: true) + project.add_developer(current_user) + post_graphql(query, current_user: current_user) end - context 'for anonymous user' do - before do - post_graphql(query, current_user: current_user) - end - - context 'requested dashboard is available' do - let(:path) { 'config/prometheus/common_metrics.yml' } - - it_behaves_like 'a working graphql query' + context 'requested dashboard is available' do + let(:path) { 'config/prometheus/common_metrics.yml' } - it 'returns nil' do - dashboard = graphql_data.dig('project', 'environments', 'nodes') + it_behaves_like 'a working graphql query' - expect(dashboard).to be_nil - end - end - end + it 'returns metrics dashboard' do + dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - context 'for user with developer access' do - before do - project.add_developer(current_user) - post_graphql(query, current_user: current_user) + expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => nil) end - context 'requested dashboard is available' do - let(:path) { 'config/prometheus/common_metrics.yml' } - - it_behaves_like 'a working graphql query' + context 'invalid dashboard' do + let(:path) { '.gitlab/dashboards/metrics.yml' } + let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "---\ndashboard: 'test'" }) } it 'returns metrics dashboard' do dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => nil) - end - - context 'invalid dashboard' do - let(:path) { '.gitlab/dashboards/metrics.yml' } - let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "---\ndashboard: 'test'" }) } - - it 'returns metrics dashboard' do - dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - - expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["root is missing required keys: panel_groups"]) - end + expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["panel_groups: should be an array of panel_groups objects"]) end + end - context 'empty dashboard' do - let(:path) { '.gitlab/dashboards/metrics.yml' } - let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "" }) } + context 'empty dashboard' do + let(:path) { '.gitlab/dashboards/metrics.yml' } + let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "" }) } - it 'returns metrics dashboard' do - dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') + it 'returns metrics dashboard' do + dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["root is missing required keys: dashboard, panel_groups"]) - end + expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["dashboard: can't be blank", "panel_groups: should be an array of panel_groups objects"]) end end + end - context 'requested dashboard can not be found' do - let(:path) { 'config/prometheus/i_am_not_here.yml' } + context 'requested dashboard can not be found' do + let(:path) { 'config/prometheus/i_am_not_here.yml' } - it_behaves_like 'a working graphql query' + it_behaves_like 'a working graphql query' - it 'returns nil' do - dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') + it 'returns nil' do + dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard') - expect(dashboard).to be_nil - end + expect(dashboard).to be_nil end end end diff --git a/spec/support/shared_contexts/services/projects/container_repository/delete_tags_service_shared_context.rb b/spec/support/shared_contexts/services/projects/container_repository/delete_tags_service_shared_context.rb index e26b8cd8b37..7db479bcfd2 100644 --- a/spec/support/shared_contexts/services/projects/container_repository/delete_tags_service_shared_context.rb +++ b/spec/support/shared_contexts/services/projects/container_repository/delete_tags_service_shared_context.rb @@ -23,7 +23,7 @@ RSpec.shared_context 'container repository delete tags service shared context' d end def stub_delete_reference_requests(tags) - tags = Array.wrap(tags).to_h { |tag| [tag, 200] } unless tags.is_a?(Hash) + tags = Array.wrap(tags).index_with { 200 } unless tags.is_a?(Hash) tags.each do |tag, status| stub_request(:delete, "http://registry.gitlab/v2/#{repository.path}/tags/reference/#{tag}") diff --git a/spec/support/shared_examples/models/concerns/sanitizable_shared_examples.rb b/spec/support/shared_examples/models/concerns/sanitizable_shared_examples.rb index ed94a71892d..aedbfe4deb3 100644 --- a/spec/support/shared_examples/models/concerns/sanitizable_shared_examples.rb +++ b/spec/support/shared_examples/models/concerns/sanitizable_shared_examples.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.shared_examples 'sanitizable' do |factory, fields| - let(:attributes) { fields.to_h { |field| [field, input] } } + let(:attributes) { fields.index_with { input } } it 'includes Sanitizable' do expect(described_class).to include(Sanitizable) diff --git a/spec/views/admin/dashboard/index.html.haml_spec.rb b/spec/views/admin/dashboard/index.html.haml_spec.rb index 6e06af92232..337964f1354 100644 --- a/spec/views/admin/dashboard/index.html.haml_spec.rb +++ b/spec/views/admin/dashboard/index.html.haml_spec.rb @@ -7,9 +7,7 @@ RSpec.describe 'admin/dashboard/index.html.haml' do include StubVersion before do - counts = Admin::DashboardController::COUNTED_ITEMS.each_with_object({}) do |item, hash| - hash[item] = 100 - end + counts = Admin::DashboardController::COUNTED_ITEMS.index_with { 100 } assign(:counts, counts) assign(:projects, create_list(:project, 1)) |