diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
commit | 311b0269b4eb9839fa63f80c8d7a58f32b8138a0 (patch) | |
tree | 07e7870bca8aed6d61fdcc810731c50d2c40af47 /spec/presenters | |
parent | 27909cef6c4170ed9205afa7426b8d3de47cbb0c (diff) |
Add latest changes from gitlab-org/gitlab@14-5-stable-eev14.5.0-rc42
Diffstat (limited to 'spec/presenters')
-rw-r--r-- | spec/presenters/award_emoji_presenter_spec.rb | 9 | ||||
-rw-r--r-- | spec/presenters/blob_presenter_spec.rb | 55 | ||||
-rw-r--r-- | spec/presenters/ci/build_runner_presenter_spec.rb | 57 | ||||
-rw-r--r-- | spec/presenters/packages/npm/package_presenter_spec.rb | 88 | ||||
-rw-r--r-- | spec/presenters/project_presenter_spec.rb | 74 | ||||
-rw-r--r-- | spec/presenters/release_presenter_spec.rb | 14 |
6 files changed, 154 insertions, 143 deletions
diff --git a/spec/presenters/award_emoji_presenter_spec.rb b/spec/presenters/award_emoji_presenter_spec.rb index 58ee985f165..a23196282a2 100644 --- a/spec/presenters/award_emoji_presenter_spec.rb +++ b/spec/presenters/award_emoji_presenter_spec.rb @@ -6,21 +6,22 @@ RSpec.describe AwardEmojiPresenter do let(:emoji_name) { 'thumbsup' } let(:award_emoji) { build(:award_emoji, name: emoji_name) } let(:presenter) { described_class.new(award_emoji) } + let(:emoji) { TanukiEmoji.find_by_alpha_code(emoji_name) } describe '#description' do - it { expect(presenter.description).to eq Gitlab::Emoji.emojis[emoji_name]['description'] } + it { expect(presenter.description).to eq emoji.description } end describe '#unicode' do - it { expect(presenter.unicode).to eq Gitlab::Emoji.emojis[emoji_name]['unicode'] } + it { expect(presenter.unicode).to eq emoji.hex } end describe '#unicode_version' do - it { expect(presenter.unicode_version).to eq Gitlab::Emoji.emoji_unicode_version(emoji_name) } + it { expect(presenter.unicode_version).to eq('6.0') } end describe '#emoji' do - it { expect(presenter.emoji).to eq Gitlab::Emoji.emojis[emoji_name]['moji'] } + it { expect(presenter.emoji).to eq emoji.codepoints } end describe 'when presenting an award emoji with an invalid name' do diff --git a/spec/presenters/blob_presenter_spec.rb b/spec/presenters/blob_presenter_spec.rb index 466a2b55e76..28e18708eab 100644 --- a/spec/presenters/blob_presenter_spec.rb +++ b/spec/presenters/blob_presenter_spec.rb @@ -31,6 +31,20 @@ RSpec.describe BlobPresenter do it { expect(presenter.replace_path).to eq("/#{project.full_path}/-/create/#{blob.commit_id}/#{blob.path}") } end + describe '#pipeline_editor_path' do + context 'when blob is .gitlab-ci.yml' do + before do + project.repository.create_file(user, '.gitlab-ci.yml', '', + message: 'Add a ci file', + branch_name: 'main') + end + + let(:blob) { repository.blob_at('main', '.gitlab-ci.yml') } + + it { expect(presenter.pipeline_editor_path).to eq("/#{project.full_path}/-/ci/editor?branch_name=#{blob.commit_id}") } + end + 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 @@ -121,6 +135,47 @@ RSpec.describe BlobPresenter do 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) + 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 + 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 + end + end + + context 'when blob is other file type' do + let(:git_blob) { blob.__getobj__ } + + before do + allow(git_blob) + .to receive(:data) + .and_return("line one\nline two\nline 3") + + allow(blob).to receive(:language_from_gitattributes).and_return('ruby') + end + + 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 + end + end + end + describe '#raw_plain_data' do let(:blob) { repository.blob_at('HEAD', file) } diff --git a/spec/presenters/ci/build_runner_presenter_spec.rb b/spec/presenters/ci/build_runner_presenter_spec.rb index 4422773fec6..b8d0b093a24 100644 --- a/spec/presenters/ci/build_runner_presenter_spec.rb +++ b/spec/presenters/ci/build_runner_presenter_spec.rb @@ -259,12 +259,7 @@ RSpec.describe Ci::BuildRunnerPresenter do describe '#runner_variables' do subject { presenter.runner_variables } - let_it_be(:project_with_flag_disabled) { create(:project, :repository) } - let_it_be(:project_with_flag_enabled) { create(:project, :repository) } - - before do - stub_feature_flags(variable_inside_variable: [project_with_flag_enabled]) - end + let_it_be(:project) { create(:project, :repository) } shared_examples 'returns an array with the expected variables' do it 'returns an array' do @@ -276,21 +271,11 @@ RSpec.describe Ci::BuildRunnerPresenter do end end - context 'when FF :variable_inside_variable is disabled' do - let(:sha) { project_with_flag_disabled.repository.commit.sha } - let(:pipeline) { create(:ci_pipeline, sha: sha, project: project_with_flag_disabled) } - let(:build) { create(:ci_build, pipeline: pipeline) } - - it_behaves_like 'returns an array with the expected variables' - end + let(:sha) { project.repository.commit.sha } + let(:pipeline) { create(:ci_pipeline, sha: sha, project: project) } + let(:build) { create(:ci_build, pipeline: pipeline) } - context 'when FF :variable_inside_variable is enabled' do - let(:sha) { project_with_flag_enabled.repository.commit.sha } - let(:pipeline) { create(:ci_pipeline, sha: sha, project: project_with_flag_enabled) } - let(:build) { create(:ci_build, pipeline: pipeline) } - - it_behaves_like 'returns an array with the expected variables' - end + it_behaves_like 'returns an array with the expected variables' end describe '#runner_variables subset' do @@ -305,32 +290,12 @@ RSpec.describe Ci::BuildRunnerPresenter do create(:ci_pipeline_variable, key: 'C', value: 'value', pipeline: build.pipeline) end - context 'when FF :variable_inside_variable is disabled' do - before do - stub_feature_flags(variable_inside_variable: false) - end - - it 'returns non-expanded variables' do - is_expected.to eq [ - { key: 'A', value: 'refA-$B', public: false, masked: false }, - { key: 'B', value: 'refB-$C-$D', public: false, masked: false }, - { key: 'C', value: 'value', public: false, masked: false } - ] - end - end - - context 'when FF :variable_inside_variable is enabled' do - before do - stub_feature_flags(variable_inside_variable: [build.project]) - end - - it 'returns expanded and sorted variables' do - is_expected.to eq [ - { key: 'C', value: 'value', public: false, masked: false }, - { key: 'B', value: 'refB-value-$D', public: false, masked: false }, - { key: 'A', value: 'refA-refB-value-$D', public: false, masked: false } - ] - end + it 'returns expanded and sorted variables' do + is_expected.to eq [ + { key: 'C', value: 'value', public: false, masked: false }, + { key: 'B', value: 'refB-value-$D', public: false, masked: false }, + { key: 'A', value: 'refA-refB-value-$D', public: false, masked: false } + ] end end end diff --git a/spec/presenters/packages/npm/package_presenter_spec.rb b/spec/presenters/packages/npm/package_presenter_spec.rb index 65f69d4056b..49046492ab4 100644 --- a/spec/presenters/packages/npm/package_presenter_spec.rb +++ b/spec/presenters/packages/npm/package_presenter_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' RSpec.describe ::Packages::Npm::PackagePresenter do + using RSpec::Parameterized::TableSyntax + let_it_be(:project) { create(:project) } let_it_be(:package_name) { "@#{project.root_namespace.path}/test" } let_it_be(:package1) { create(:npm_package, version: '2.0.4', project: project, name: package_name) } @@ -13,42 +15,88 @@ RSpec.describe ::Packages::Npm::PackagePresenter do let(:presenter) { described_class.new(package_name, packages) } describe '#versions' do - subject { presenter.versions } + let_it_be('package_json') do + { + 'name': package_name, + 'version': '2.0.4', + 'deprecated': 'warning!', + 'bin': './cli.js', + 'directories': ['lib'], + 'engines': { 'npm': '^7.5.6' }, + '_hasShrinkwrap': false, + 'dist': { + 'tarball': 'http://localhost/tarball.tgz', + 'shasum': '1234567890' + }, + 'custom_field': 'foo_bar' + } + end - context 'for packages without dependencies' do - it { is_expected.to be_a(Hash) } - it { expect(subject[package1.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } - it { expect(subject[package2.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } + let(:presenter) { described_class.new(package_name, packages, include_metadata: include_metadata) } - ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| - it { expect(subject.dig(package1.version, dependency_type)).to be nil } - it { expect(subject.dig(package2.version, dependency_type)).to be nil } - end + subject { presenter.versions } - it 'avoids N+1 database queries' do - check_n_plus_one(:versions) do - create_list(:npm_package, 5, project: project, name: package_name) + 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 + end + + with_them do + if params[:has_dependencies] + ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| + let_it_be("package_dependency_link_for_#{dependency_type}") { create(:packages_dependency_link, package: package1, dependency_type: dependency_type) } end end - end - context 'for packages with dependencies' do - ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| - let_it_be("package_dependency_link_for_#{dependency_type}") { create(:packages_dependency_link, package: package1, dependency_type: dependency_type) } + if params[:has_metadatum] + let_it_be('package_metadatadum') { create(:npm_metadatum, package: package1, package_json: package_json) } end it { is_expected.to be_a(Hash) } it { expect(subject[package1.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } it { expect(subject[package2.version].with_indifferent_access).to match_schema('public_api/v4/packages/npm_package_version') } - ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| - it { expect(subject.dig(package1.version, dependency_type.to_s)).to be_any } + it { expect(subject[package1.version]['custom_field']).to be_blank } + + context 'dependencies' do + ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| + if params[:has_dependencies] + it { expect(subject.dig(package1.version, dependency_type.to_s)).to be_any } + else + it { expect(subject.dig(package1.version, dependency_type)).to be nil } + end + + it { expect(subject.dig(package2.version, dependency_type)).to be nil } + end + end + + context 'metadatum' do + ::Packages::Npm::PackagePresenter::PACKAGE_JSON_ALLOWED_FIELDS.each do |metadata_field| + if params[:has_metadatum] && params[:include_metadata] + it { expect(subject.dig(package1.version, metadata_field)).not_to be nil } + else + it { expect(subject.dig(package1.version, metadata_field)).to be nil } + end + + it { expect(subject.dig(package2.version, metadata_field)).to be nil } + end end it 'avoids N+1 database queries' do check_n_plus_one(:versions) do create_list(:npm_package, 5, project: project, name: package_name).each do |npm_package| - ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| - create(:packages_dependency_link, package: npm_package, dependency_type: dependency_type) + if has_dependencies + ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type| + create(:packages_dependency_link, package: npm_package, dependency_type: dependency_type) + end end end end diff --git a/spec/presenters/project_presenter_spec.rb b/spec/presenters/project_presenter_spec.rb index 5f789f59908..27b777dec5f 100644 --- a/spec/presenters/project_presenter_spec.rb +++ b/spec/presenters/project_presenter_spec.rb @@ -567,44 +567,27 @@ RSpec.describe ProjectPresenter do end describe '#upload_anchor_data' do - context 'with empty_repo_upload enabled' do + context 'when a user can push to the default branch' do before do - stub_experiments(empty_repo_upload: :candidate) - end - - context 'user can push to branch' do - before do - project.add_developer(user) - end - - it 'returns upload_anchor_data' do - expect(presenter.upload_anchor_data).to have_attributes( - is_link: false, - label: a_string_including('Upload file'), - data: { - "can_push_code" => "true", - "original_branch" => "master", - "path" => "/#{project.full_path}/-/create/master", - "project_path" => project.full_path, - "target_branch" => "master" - } - ) - end + project.add_developer(user) end - context 'user cannot push to branch' do - it 'returns nil' do - expect(presenter.upload_anchor_data).to be_nil - end + it 'returns upload_anchor_data' do + expect(presenter.upload_anchor_data).to have_attributes( + is_link: false, + label: a_string_including('Upload file'), + data: { + "can_push_code" => "true", + "original_branch" => "master", + "path" => "/#{project.full_path}/-/create/master", + "project_path" => project.full_path, + "target_branch" => "master" + } + ) end end - context 'with empty_repo_upload disabled' do - before do - stub_experiments(empty_repo_upload: :control) - project.add_developer(user) - end - + context 'when the user cannot push to default branch' do it 'returns nil' do expect(presenter.upload_anchor_data).to be_nil end @@ -666,7 +649,6 @@ RSpec.describe ProjectPresenter do context 'for a developer' do before do project.add_developer(user) - stub_experiments(empty_repo_upload: :candidate) end it 'orders the items correctly' do @@ -680,16 +662,6 @@ RSpec.describe ProjectPresenter do a_string_including('CI/CD') ) end - - context 'when not in the upload experiment' do - before do - stub_experiments(empty_repo_upload: :control) - end - - it 'does not include upload button' do - expect(empty_repo_statistics_buttons.map(&:label)).not_to start_with(a_string_including('Upload')) - end - end end end @@ -781,20 +753,4 @@ RSpec.describe ProjectPresenter do it { is_expected.to match(/code_quality_walkthrough=true.*template=Code-Quality/) } end - - describe 'empty_repo_upload_experiment?' do - subject { presenter.empty_repo_upload_experiment? } - - it 'returns false when upload_anchor_data is nil' do - allow(presenter).to receive(:upload_anchor_data).and_return(nil) - - expect(subject).to be false - end - - it 'returns true when upload_anchor_data exists' do - allow(presenter).to receive(:upload_anchor_data).and_return(true) - - expect(subject).to be true - end - end end diff --git a/spec/presenters/release_presenter_spec.rb b/spec/presenters/release_presenter_spec.rb index b2e7b684644..925a69ca92d 100644 --- a/spec/presenters/release_presenter_spec.rb +++ b/spec/presenters/release_presenter_spec.rb @@ -63,12 +63,6 @@ RSpec.describe ReleasePresenter do it 'returns its own url' do is_expected.to eq(project_release_url(project, release)) end - - context 'when user is guest' do - let(:user) { guest } - - it { is_expected.to be_nil } - end end describe '#opened_merge_requests_url' do @@ -147,13 +141,5 @@ RSpec.describe ReleasePresenter do it 'returns the release name' do is_expected.to eq release.name end - - context "when a user is not allowed to access any repository information" do - let(:presenter) { described_class.new(release, current_user: guest) } - - it 'returns a replacement name to avoid potentially leaking tag information' do - is_expected.to eq "Release-#{release.id}" - end - end end end |