diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 16:37:47 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 16:37:47 +0300 |
commit | aee0a117a889461ce8ced6fcf73207fe017f1d99 (patch) | |
tree | 891d9ef189227a8445d83f35c1b0fc99573f4380 /spec/presenters | |
parent | 8d46af3258650d305f53b819eabf7ab18d22f59e (diff) |
Add latest changes from gitlab-org/gitlab@14-6-stable-eev14.6.0-rc42
Diffstat (limited to 'spec/presenters')
-rw-r--r-- | spec/presenters/blob_presenter_spec.rb | 36 | ||||
-rw-r--r-- | spec/presenters/ci/pipeline_presenter_spec.rb | 24 | ||||
-rw-r--r-- | spec/presenters/merge_request_presenter_spec.rb | 24 | ||||
-rw-r--r-- | spec/presenters/packages/npm/package_presenter_spec.rb | 21 | ||||
-rw-r--r-- | spec/presenters/projects/security/configuration_presenter_spec.rb | 301 |
5 files changed, 386 insertions, 20 deletions
diff --git a/spec/presenters/blob_presenter_spec.rb b/spec/presenters/blob_presenter_spec.rb index 28e18708eab..8c0347b3c8d 100644 --- a/spec/presenters/blob_presenter_spec.rb +++ b/spec/presenters/blob_presenter_spec.rb @@ -31,6 +31,28 @@ RSpec.describe BlobPresenter do it { expect(presenter.replace_path).to eq("/#{project.full_path}/-/create/#{blob.commit_id}/#{blob.path}") } end + describe '#can_current_user_push_to_branch' do + let(:branch_exists) { true } + + before do + allow(project.repository).to receive(:branch_exists?).with(blob.commit_id).and_return(branch_exists) + end + + it { expect(presenter.can_current_user_push_to_branch?).to eq(true) } + + context 'current_user is nil' do + let(:user) { nil } + + it { expect(presenter.can_current_user_push_to_branch?).to eq(false) } + end + + context 'branch does not exist' do + let(:branch_exists) { false } + + it { expect(presenter.can_current_user_push_to_branch?).to eq(false) } + end + end + describe '#pipeline_editor_path' do context 'when blob is .gitlab-ci.yml' do before do @@ -45,6 +67,10 @@ RSpec.describe BlobPresenter do end end + describe '#code_owners' do + it { expect(presenter.code_owners).to match_array([]) } + end + describe '#ide_edit_path' do it { expect(presenter.ide_edit_path).to eq("/-/ide/project/#{project.full_path}/edit/HEAD/-/files/ruby/regex.rb") } end @@ -133,27 +159,25 @@ RSpec.describe BlobPresenter do presenter.highlight end end - end - describe '#highlight_transformed' do context 'when blob is ipynb' do let(:blob) { repository.blob_at('f6b7a707', 'files/ipython/markdown-table.ipynb') } let(:git_blob) { blob.__getobj__ } before do - allow(git_blob).to receive(:transformed_for_diff).and_return(true) + allow(Gitlab::Diff::CustomDiff).to receive(:transformed_for_diff?).and_return(true) end it 'uses md as the transformed language' do expect(Gitlab::Highlight).to receive(:highlight).with('files/ipython/markdown-table.ipynb', anything, plain: nil, language: 'md') - presenter.highlight_transformed + presenter.highlight end it 'transforms the blob' do expect(Gitlab::Highlight).to receive(:highlight).with('files/ipython/markdown-table.ipynb', include("%%"), plain: nil, language: 'md') - presenter.highlight_transformed + presenter.highlight end end @@ -171,7 +195,7 @@ RSpec.describe BlobPresenter do it 'does not transform the file' do expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: 'ruby') - presenter.highlight_transformed + presenter.highlight end end end diff --git a/spec/presenters/ci/pipeline_presenter_spec.rb b/spec/presenters/ci/pipeline_presenter_spec.rb index 2d3c0d85eda..a278d4dad83 100644 --- a/spec/presenters/ci/pipeline_presenter_spec.rb +++ b/spec/presenters/ci/pipeline_presenter_spec.rb @@ -122,6 +122,30 @@ RSpec.describe Ci::PipelinePresenter do end end + describe '#coverage' do + subject { presenter.coverage } + + context 'when pipeline has coverage' do + before do + allow(pipeline).to receive(:coverage).and_return(35.0) + end + + it 'formats coverage into 2 decimal points' do + expect(subject).to eq('35.00') + end + end + + context 'when pipeline does not have coverage' do + before do + allow(pipeline).to receive(:coverage).and_return(nil) + end + + it 'returns nil' do + expect(subject).to be_nil + end + end + end + describe '#ref_text' do subject { presenter.ref_text } diff --git a/spec/presenters/merge_request_presenter_spec.rb b/spec/presenters/merge_request_presenter_spec.rb index b3ec184d08c..60296cca058 100644 --- a/spec/presenters/merge_request_presenter_spec.rb +++ b/spec/presenters/merge_request_presenter_spec.rb @@ -632,4 +632,28 @@ RSpec.describe MergeRequestPresenter do it { is_expected.to eq(expose_path("/api/v4/projects/#{project.id}/merge_requests/#{resource.iid}/unapprove")) } end + + describe '#pipeline_coverage_delta' do + subject { described_class.new(resource, current_user: user).pipeline_coverage_delta } + + context 'when merge request has pipeline coverage delta' do + before do + allow(resource).to receive(:pipeline_coverage_delta).and_return(35.0) + end + + it 'formats coverage into 2 decimal points' do + expect(subject).to eq('35.00') + end + end + + context 'when merge request does not have pipeline coverage delta' do + before do + allow(resource).to receive(:pipeline_coverage_delta).and_return(nil) + end + + it 'returns nil' do + expect(subject).to be_nil + end + end + end end diff --git a/spec/presenters/packages/npm/package_presenter_spec.rb b/spec/presenters/packages/npm/package_presenter_spec.rb index 49046492ab4..3b6dfcd20b8 100644 --- a/spec/presenters/packages/npm/package_presenter_spec.rb +++ b/spec/presenters/packages/npm/package_presenter_spec.rb @@ -32,22 +32,15 @@ RSpec.describe ::Packages::Npm::PackagePresenter do } end - let(:presenter) { described_class.new(package_name, packages, include_metadata: include_metadata) } + let(:presenter) { described_class.new(package_name, packages) } subject { presenter.versions } - where(:has_dependencies, :has_metadatum, :include_metadata) do - true | true | true - false | true | true - true | false | true - false | false | true - - # TODO : to remove along with packages_npm_abbreviated_metadata - # See https://gitlab.com/gitlab-org/gitlab/-/issues/344827 - true | true | false - false | true | false - true | false | false - false | false | false + where(:has_dependencies, :has_metadatum) do + true | true + false | true + true | false + false | false end with_them do @@ -80,7 +73,7 @@ RSpec.describe ::Packages::Npm::PackagePresenter do context 'metadatum' do ::Packages::Npm::PackagePresenter::PACKAGE_JSON_ALLOWED_FIELDS.each do |metadata_field| - if params[:has_metadatum] && params[:include_metadata] + if params[:has_metadatum] it { expect(subject.dig(package1.version, metadata_field)).not_to be nil } else it { expect(subject.dig(package1.version, metadata_field)).to be nil } diff --git a/spec/presenters/projects/security/configuration_presenter_spec.rb b/spec/presenters/projects/security/configuration_presenter_spec.rb new file mode 100644 index 00000000000..836753d0483 --- /dev/null +++ b/spec/presenters/projects/security/configuration_presenter_spec.rb @@ -0,0 +1,301 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Projects::Security::ConfigurationPresenter do + include Gitlab::Routing.url_helpers + using RSpec::Parameterized::TableSyntax + + let(:project_with_repo) { create(:project, :repository) } + let(:project_with_no_repo) { create(:project) } + let(:current_user) { create(:user) } + let(:presenter) { described_class.new(project, current_user: current_user) } + + before do + stub_licensed_features(licensed_scan_types.to_h { |type| [type, true] }) + + stub_feature_flags(corpus_management: false) + end + + describe '#to_html_data_attribute' do + subject(:html_data) { presenter.to_html_data_attribute } + + context 'when latest default branch pipeline`s source is not auto devops' do + let(:project) { project_with_repo } + + let(:pipeline) do + create( + :ci_pipeline, + project: project, + ref: project.default_branch, + sha: project.commit.sha + ) + end + + let!(:build_sast) { create(:ci_build, :sast, pipeline: pipeline) } + let!(:build_dast) { create(:ci_build, :dast, pipeline: pipeline) } + let!(:build_license_scanning) { create(:ci_build, :license_scanning, pipeline: pipeline) } + + it 'includes links to auto devops and secure product docs' do + expect(html_data[:auto_devops_help_page_path]).to eq(help_page_path('topics/autodevops/index')) + expect(html_data[:help_page_path]).to eq(help_page_path('user/application_security/index')) + end + + it 'returns info that Auto DevOps is not enabled' do + expect(html_data[:auto_devops_enabled]).to eq(false) + expect(html_data[:auto_devops_path]).to eq(project_settings_ci_cd_path(project, anchor: 'autodevops-settings')) + end + + it 'includes a link to the latest pipeline' do + expect(html_data[:latest_pipeline_path]).to eq(project_pipeline_path(project, pipeline)) + end + + it 'has stubs for autofix' do + expect(html_data.keys).to include(:can_toggle_auto_fix_settings, :auto_fix_enabled, :auto_fix_user_path) + end + + context "while retrieving information about user's ability to enable auto_devops" do + where(:is_admin, :archived, :feature_available, :result) do + true | true | true | false + false | true | true | false + true | false | true | true + false | false | true | false + true | true | false | false + false | true | false | false + true | false | false | false + false | false | false | false + end + + with_them do + before do + allow_next_instance_of(described_class) do |presenter| + allow(presenter).to receive(:can?).and_return(is_admin) + allow(presenter).to receive(:archived?).and_return(archived) + allow(presenter).to receive(:feature_available?).and_return(feature_available) + end + end + + it 'includes can_enable_auto_devops' do + expect(html_data[:can_enable_auto_devops]).to eq(result) + end + end + end + + it 'includes feature information' do + feature = Gitlab::Json.parse(html_data[:features]).find { |scan| scan['type'] == 'sast' } + + expect(feature['type']).to eq('sast') + expect(feature['configured']).to eq(true) + expect(feature['configuration_path']).to eq(project_security_configuration_sast_path(project)) + expect(feature['available']).to eq(true) + end + + context 'when checking features configured status' do + let(:features) { Gitlab::Json.parse(html_data[:features]) } + + where(:type, :configured) do + :dast | true + :dast_profiles | true + :sast | true + :sast_iac | false + :container_scanning | false + :cluster_image_scanning | false + :dependency_scanning | false + :license_scanning | true + :secret_detection | false + :coverage_fuzzing | false + :api_fuzzing | false + :corpus_management | true + end + + with_them do + it 'returns proper configuration status' do + feature = features.find { |scan| scan['type'] == type.to_s } + + expect(feature['configured']).to eq(configured) + end + end + end + + context 'when the job has more than one report' do + let(:features) { Gitlab::Json.parse(html_data[:features]) } + + let!(:artifacts) do + { artifacts: { reports: { other_job: ['gl-other-report.json'], sast: ['gl-sast-report.json'] } } } + end + + let!(:complicated_job) { build_stubbed(:ci_build, options: artifacts) } + + before do + allow_next_instance_of(::Security::SecurityJobsFinder) do |finder| + allow(finder).to receive(:execute).and_return([complicated_job]) + end + end + + where(:type, :configured) do + :dast | false + :dast_profiles | true + :sast | true + :sast_iac | false + :container_scanning | false + :cluster_image_scanning | false + :dependency_scanning | false + :license_scanning | true + :secret_detection | false + :coverage_fuzzing | false + :api_fuzzing | false + :corpus_management | true + end + + with_them do + it 'properly detects security jobs' do + feature = features.find { |scan| scan['type'] == type.to_s } + + expect(feature['configured']).to eq(configured) + end + end + end + + it 'includes a link to the latest pipeline' do + expect(subject[:latest_pipeline_path]).to eq(project_pipeline_path(project, pipeline)) + end + + context "while retrieving information about gitlab ci file" do + context 'when a .gitlab-ci.yml file exists' do + let!(:ci_config) do + project.repository.create_file( + project.creator, + Gitlab::FileDetector::PATTERNS[:gitlab_ci], + 'contents go here', + message: 'test', + branch_name: 'master') + end + + it 'expects gitlab_ci_present to be true' do + expect(html_data[:gitlab_ci_present]).to eq(true) + end + end + + context 'when a .gitlab-ci.yml file does not exist' do + it 'expects gitlab_ci_present to be false if the file is not present' do + expect(html_data[:gitlab_ci_present]).to eq(false) + end + end + end + + it 'includes the path to gitlab_ci history' do + expect(subject[:gitlab_ci_history_path]).to eq(project_blame_path(project, 'master/.gitlab-ci.yml')) + end + end + + context 'when the project is empty' do + let(:project) { project_with_no_repo } + + it 'includes a blank gitlab_ci history path' do + expect(html_data[:gitlab_ci_history_path]).to eq('') + end + end + + context 'when the project has no default branch set' do + let(:project) { project_with_repo } + + it 'includes the path to gitlab_ci history' do + allow(project).to receive(:default_branch).and_return(nil) + + expect(html_data[:gitlab_ci_history_path]).to eq(project_blame_path(project, 'master/.gitlab-ci.yml')) + end + end + + context "when the latest default branch pipeline's source is auto devops" do + let(:project) { project_with_repo } + + let(:pipeline) do + create( + :ci_pipeline, + :auto_devops_source, + project: project, + ref: project.default_branch, + sha: project.commit.sha + ) + end + + let!(:build_sast) { create(:ci_build, :sast, pipeline: pipeline, status: 'success') } + let!(:build_dast) { create(:ci_build, :dast, pipeline: pipeline, status: 'success') } + let!(:ci_build) { create(:ci_build, :secret_detection, pipeline: pipeline, status: 'pending') } + + it 'reports that auto devops is enabled' do + expect(html_data[:auto_devops_enabled]).to be_truthy + end + + context 'when gathering feature data' do + let(:features) { Gitlab::Json.parse(html_data[:features]) } + + where(:type, :configured) do + :dast | true + :dast_profiles | true + :sast | true + :sast_iac | false + :container_scanning | false + :cluster_image_scanning | false + :dependency_scanning | false + :license_scanning | false + :secret_detection | true + :coverage_fuzzing | false + :api_fuzzing | false + :corpus_management | true + end + + with_them do + it 'reports that all scanners are configured for which latest pipeline has builds' do + feature = features.find { |scan| scan['type'] == type.to_s } + + expect(feature['configured']).to eq(configured) + end + end + end + end + + context 'when the project has no default branch pipeline' do + let(:project) { project_with_repo } + + it 'reports that auto devops is disabled' do + expect(html_data[:auto_devops_enabled]).to be_falsy + end + + it 'includes a link to CI pipeline docs' do + expect(html_data[:latest_pipeline_path]).to eq(help_page_path('ci/pipelines')) + end + + context 'when gathering feature data' do + let(:features) { Gitlab::Json.parse(html_data[:features]) } + + where(:type, :configured) do + :dast | false + :dast_profiles | true + :sast | false + :sast_iac | false + :container_scanning | false + :cluster_image_scanning | false + :dependency_scanning | false + :license_scanning | false + :secret_detection | false + :coverage_fuzzing | false + :api_fuzzing | false + :corpus_management | true + end + + with_them do + it 'reports all security jobs as unconfigured with exception of "fake" jobs' do + feature = features.find { |scan| scan['type'] == type.to_s } + + expect(feature['configured']).to eq(configured) + end + end + end + end + + def licensed_scan_types + ::Security::SecurityJobsFinder.allowed_job_types + ::Security::LicenseComplianceJobsFinder.allowed_job_types - [:cluster_image_scanning] + end + end +end |