diff options
Diffstat (limited to 'spec/helpers')
35 files changed, 728 insertions, 253 deletions
diff --git a/spec/helpers/admin/user_actions_helper_spec.rb b/spec/helpers/admin/user_actions_helper_spec.rb new file mode 100644 index 00000000000..7ccd9a4fe3e --- /dev/null +++ b/spec/helpers/admin/user_actions_helper_spec.rb @@ -0,0 +1,105 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Admin::UserActionsHelper do + describe '#admin_actions' do + let_it_be(:current_user) { build(:user) } + + subject { helper.admin_actions(user) } + + before do + allow(helper).to receive(:current_user).and_return(current_user) + allow(helper).to receive(:can?).with(current_user, :destroy_user, user).and_return(true) + end + + context 'the user is a bot' do + let_it_be(:user) { build(:user, :bot) } + + it { is_expected.to be_empty } + end + + context 'the current user and user are the same' do + let_it_be(:user) { build(:user) } + let_it_be(:current_user) { user } + + it { is_expected.to contain_exactly("edit") } + end + + context 'the user is a standard user' do + let_it_be(:user) { create(:user) } + + it { is_expected.to contain_exactly("edit", "block", "deactivate", "delete", "delete_with_contributions") } + end + + context 'the user is an admin user' do + let_it_be(:user) { create(:user, :admin) } + + it { is_expected.to contain_exactly("edit", "block", "deactivate", "delete", "delete_with_contributions") } + end + + context 'the user is blocked by LDAP' do + let_it_be(:user) { create(:omniauth_user, :ldap_blocked) } + + it { is_expected.to contain_exactly("edit", "ldap", "delete", "delete_with_contributions") } + end + + context 'the user is blocked pending approval' do + let_it_be(:user) { create(:user, :blocked_pending_approval) } + + it { is_expected.to contain_exactly("edit", "approve", "reject") } + end + + context 'the user is blocked' do + let_it_be(:user) { create(:user, :blocked) } + + it { is_expected.to contain_exactly("edit", "unblock", "delete", "delete_with_contributions") } + end + + context 'the user is deactivated' do + let_it_be(:user) { create(:user, :deactivated) } + + it { is_expected.to contain_exactly("edit", "block", "activate", "delete", "delete_with_contributions") } + end + + context 'the user is locked' do + let_it_be(:user) { create(:user) } + + before do + user.lock_access! + end + + it { + is_expected.to contain_exactly( + "edit", + "block", + "deactivate", + "unlock", + "delete", + "delete_with_contributions" + ) + } + end + + context 'the current_user does not have permission to delete the user' do + let_it_be(:user) { build(:user) } + + before do + allow(helper).to receive(:can?).with(current_user, :destroy_user, user).and_return(false) + end + + it { is_expected.to contain_exactly("edit", "block", "deactivate") } + end + + context 'the user is a sole owner of a group' do + let_it_be(:group) { create(:group) } + let_it_be(:user) { create(:user) } + + before do + group.add_owner(user) + end + + it { is_expected.to contain_exactly("edit", "block", "deactivate") } + end + end +end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index a557e9e04da..c7470f31ad8 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -116,9 +116,9 @@ RSpec.describe ApplicationHelper do Time.use_zone('UTC') { example.run } end - def element(*arguments) + def element(**arguments) @time = Time.zone.parse('2015-07-02 08:23') - element = helper.time_ago_with_tooltip(@time, *arguments) + element = helper.time_ago_with_tooltip(@time, **arguments) Nokogiri::HTML::DocumentFragment.parse(element).first_element_child end diff --git a/spec/helpers/auth_helper_spec.rb b/spec/helpers/auth_helper_spec.rb index b4cea7fb695..00c4a1880de 100644 --- a/spec/helpers/auth_helper_spec.rb +++ b/spec/helpers/auth_helper_spec.rb @@ -99,6 +99,22 @@ RSpec.describe AuthHelper do end end + describe 'experiment_enabled_button_based_providers' do + it 'returns the intersection set of github & google_oauth2 with enabled providers' do + allow(helper).to receive(:enabled_button_based_providers) { %w(twitter github google_oauth2) } + + expect(helper.experiment_enabled_button_based_providers).to eq(%w(github google_oauth2)) + + allow(helper).to receive(:enabled_button_based_providers) { %w(google_oauth2 bitbucket) } + + expect(helper.experiment_enabled_button_based_providers).to eq(%w(google_oauth2)) + + allow(helper).to receive(:enabled_button_based_providers) { %w(bitbucket) } + + expect(helper.experiment_enabled_button_based_providers).to be_empty + end + end + describe 'button_based_providers_enabled?' do before do allow(helper).to receive(:auth_providers) { [:twitter, :github] } diff --git a/spec/helpers/blob_helper_spec.rb b/spec/helpers/blob_helper_spec.rb index cafe4c4275e..764c582e987 100644 --- a/spec/helpers/blob_helper_spec.rb +++ b/spec/helpers/blob_helper_spec.rb @@ -236,53 +236,41 @@ RSpec.describe BlobHelper do let(:data) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) } let(:blob) { fake_blob(path: Gitlab::FileDetector::PATTERNS[:gitlab_ci], data: data) } - context 'feature enabled' do - it 'is true' do - expect(helper.show_suggest_pipeline_creation_celebration?).to be_truthy - end + it 'is true' do + expect(helper.show_suggest_pipeline_creation_celebration?).to be_truthy + end - context 'file is invalid format' do - let(:data) { 'foo' } + context 'file is invalid format' do + let(:data) { 'foo' } - it 'is false' do - expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey - end + it 'is false' do + expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey end + end - context 'does not use the default ci config' do - before do - project.ci_config_path = 'something_bad' - end - - it 'is false' do - expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey - end + context 'does not use the default ci config' do + before do + project.ci_config_path = 'something_bad' end - context 'does not have the needed cookie' do - before do - helper.request.cookies.delete "suggest_gitlab_ci_yml_commit_#{project.id}" - end - - it 'is false' do - expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey - end + it 'is false' do + expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey end + end - context 'blob does not have auxiliary view' do - before do - allow(blob).to receive(:auxiliary_viewer).and_return(nil) - end + context 'does not have the needed cookie' do + before do + helper.request.cookies.delete "suggest_gitlab_ci_yml_commit_#{project.id}" + end - it 'is false' do - expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey - end + it 'is false' do + expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey end end - context 'feature disabled' do + context 'blob does not have auxiliary view' do before do - stub_feature_flags(suggest_pipeline: false) + allow(blob).to receive(:auxiliary_viewer).and_return(nil) end it 'is false' do @@ -294,10 +282,8 @@ RSpec.describe BlobHelper do context 'when file is not a pipeline config file' do let(:blob) { fake_blob(path: 'LICENSE') } - context 'feature enabled' do - it 'is false' do - expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey - end + it 'is false' do + expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey end end end diff --git a/spec/helpers/button_helper_spec.rb b/spec/helpers/button_helper_spec.rb index 6a5cb73281e..ecb9c98b1bf 100644 --- a/spec/helpers/button_helper_spec.rb +++ b/spec/helpers/button_helper_spec.rb @@ -89,7 +89,7 @@ RSpec.describe ButtonHelper do it 'shows a warning on the dropdown description' do description = element.search('.dropdown-menu-inner-content').first - expect(description.inner_text).to eq "You won't be able to pull or push project code via SSH until you add an SSH key to your profile" + expect(description.inner_text).to eq "You won't be able to pull or push repositories via SSH until you add an SSH key to your profile" end end diff --git a/spec/helpers/calendar_helper_spec.rb b/spec/helpers/calendar_helper_spec.rb index ceed4191ef4..08993dd1dd0 100644 --- a/spec/helpers/calendar_helper_spec.rb +++ b/spec/helpers/calendar_helper_spec.rb @@ -18,5 +18,14 @@ RSpec.describe CalendarHelper do expect(helper.calendar_url_options[:feed_token]).to be_nil end end + + context 'when feed token disabled' do + it "does not have a feed_token" do + current_user = create(:user) + allow(helper).to receive(:current_user).and_return(current_user) + allow(Gitlab::CurrentSettings).to receive(:disable_feed_token).and_return(true) + expect(helper.calendar_url_options[:feed_token]).to be_nil + end + end end end diff --git a/spec/helpers/ci/pipeline_schedules_helper_spec.rb b/spec/helpers/ci/pipeline_schedules_helper_spec.rb deleted file mode 100644 index 2a81c2a44a0..00000000000 --- a/spec/helpers/ci/pipeline_schedules_helper_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Ci::PipelineSchedulesHelper, :aggregate_failures do - describe '#timezone_data' do - subject { helper.timezone_data } - - it 'matches schema' do - expect(subject).not_to be_empty - subject.each_with_index do |timzone_hash, i| - expect(timzone_hash.keys).to contain_exactly(:name, :offset, :identifier), "Failed at index #{i}" - end - end - - it 'formats for display' do - first_timezone = ActiveSupport::TimeZone.all[0] - - expect(subject[0][:name]).to eq(first_timezone.name) - expect(subject[0][:offset]).to eq(first_timezone.now.utc_offset) - expect(subject[0][:identifier]).to eq(first_timezone.tzinfo.identifier) - end - end -end diff --git a/spec/helpers/ci/runners_helper_spec.rb b/spec/helpers/ci/runners_helper_spec.rb index 38caae91ef2..6e41afac4ee 100644 --- a/spec/helpers/ci/runners_helper_spec.rb +++ b/spec/helpers/ci/runners_helper_spec.rb @@ -74,4 +74,57 @@ RSpec.describe Ci::RunnersHelper do expect(data[:parent_shared_runners_availability]).to eq('enabled') end end + + describe '#toggle_shared_runners_settings_data' do + let_it_be(:group) { create(:group) } + let(:project_with_runners) { create(:project, namespace: group, shared_runners_enabled: true) } + let(:project_without_runners) { create(:project, namespace: group, shared_runners_enabled: false) } + + context 'when project has runners' do + it 'returns the correct value for is_enabled' do + data = toggle_shared_runners_settings_data(project_with_runners) + expect(data[:is_enabled]).to eq("true") + end + end + + context 'when project does not have runners' do + it 'returns the correct value for is_enabled' do + data = toggle_shared_runners_settings_data(project_without_runners) + expect(data[:is_enabled]).to eq("false") + end + end + + context 'for all projects' do + it 'returns the update path for toggling the shared runners setting' do + data = toggle_shared_runners_settings_data(project_with_runners) + expect(data[:update_path]).to eq(toggle_shared_runners_project_runners_path(project_with_runners)) + end + + it 'returns false for is_disabled_and_unoverridable when project has no group' do + project = create(:project) + + data = toggle_shared_runners_settings_data(project) + expect(data[:is_disabled_and_unoverridable]).to eq("false") + end + + using RSpec::Parameterized::TableSyntax + + where(:shared_runners_setting, :is_disabled_and_unoverridable) do + 'enabled' | "false" + 'disabled_with_override' | "false" + 'disabled_and_unoverridable' | "true" + end + + with_them do + it 'returns the override runner status for project with group' do + group = create(:group) + project = create(:project, group: group) + allow(group).to receive(:shared_runners_setting).and_return(shared_runners_setting) + + data = toggle_shared_runners_settings_data(project) + expect(data[:is_disabled_and_unoverridable]).to eq(is_disabled_and_unoverridable) + end + end + end + end end diff --git a/spec/helpers/clusters_helper_spec.rb b/spec/helpers/clusters_helper_spec.rb index 6b08b6515cf..8c738141063 100644 --- a/spec/helpers/clusters_helper_spec.rb +++ b/spec/helpers/clusters_helper_spec.rb @@ -131,7 +131,7 @@ RSpec.describe ClustersHelper do context 'other values' do let(:cluster_type) { 'not_supported' } - it 'Diplays generic cluster and reports error' do + it 'diplays generic cluster and reports error' do expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with( an_instance_of(ArgumentError), cluster_error: { error: 'Cluster Type Missing', cluster_type: 'not_supported' } diff --git a/spec/helpers/defer_script_tag_helper_spec.rb b/spec/helpers/defer_script_tag_helper_spec.rb deleted file mode 100644 index 14317e353ab..00000000000 --- a/spec/helpers/defer_script_tag_helper_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe DeferScriptTagHelper do - describe 'script tag' do - script_url = 'test.js' - - it 'returns an script tag with defer=true' do - expect(javascript_include_tag(script_url).to_s) - .to eq "<script src=\"/javascripts/#{script_url}\" defer=\"defer\"></script>" - end - end -end diff --git a/spec/helpers/diff_helper_spec.rb b/spec/helpers/diff_helper_spec.rb index c085c3bdbd9..3580959fde0 100644 --- a/spec/helpers/diff_helper_spec.rb +++ b/spec/helpers/diff_helper_spec.rb @@ -358,30 +358,4 @@ RSpec.describe DiffHelper do expect(diff_file_path_text(diff_file, max: 10)).to eq("...open.rb") end end - - describe 'unified_diff_lines_view_type' do - before do - controller.params[:view] = 'parallel' - end - - describe 'unified diffs enabled' do - before do - stub_feature_flags(unified_diff_lines: true) - end - - it 'returns inline view' do - expect(helper.unified_diff_lines_view_type(project)).to eq 'inline' - end - end - - describe 'unified diffs disabled' do - before do - stub_feature_flags(unified_diff_lines: false) - end - - it 'returns parallel view' do - expect(helper.unified_diff_lines_view_type(project)).to eq :parallel - end - end - end end diff --git a/spec/helpers/emails_helper_spec.rb b/spec/helpers/emails_helper_spec.rb index ef8b342a3f6..58ed5901d45 100644 --- a/spec/helpers/emails_helper_spec.rb +++ b/spec/helpers/emails_helper_spec.rb @@ -296,7 +296,7 @@ RSpec.describe EmailsHelper do end with_them do - it 'Produces the right List-Id' do + it 'produces the right List-Id' do project = double("project") allow(project).to receive(:full_path).and_return(full_path) allow(project).to receive(:id).and_return(12345) diff --git a/spec/helpers/gitlab_script_tag_helper_spec.rb b/spec/helpers/gitlab_script_tag_helper_spec.rb new file mode 100644 index 00000000000..37413b9b1c2 --- /dev/null +++ b/spec/helpers/gitlab_script_tag_helper_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe GitlabScriptTagHelper do + before do + allow(helper).to receive(:content_security_policy_nonce).and_return('noncevalue') + end + + describe 'external script tag' do + let(:script_url) { 'test.js' } + + it 'returns a script tag with defer=true and a nonce' do + expect(helper.javascript_include_tag(script_url).to_s) + .to eq "<script src=\"/javascripts/#{script_url}\" defer=\"defer\" nonce=\"noncevalue\"></script>" + end + end + + describe 'inline script tag' do + let(:tag_with_nonce) {"<script nonce=\"noncevalue\">\n//<![CDATA[\nalert(1)\n//]]>\n</script>"} + let(:tag_with_nonce_and_type) {"<script type=\"application/javascript\" nonce=\"noncevalue\">\n//<![CDATA[\nalert(1)\n//]]>\n</script>"} + + it 'returns a script tag with a nonce using block syntax' do + expect(helper.javascript_tag { 'alert(1)' }.to_s).to eq tag_with_nonce + end + + it 'returns a script tag with a nonce using block syntax with options' do + expect(helper.javascript_tag(type: 'application/javascript') { 'alert(1)' }.to_s).to eq tag_with_nonce_and_type + end + + it 'returns a script tag with a nonce using argument syntax' do + expect(helper.javascript_tag('alert(1)').to_s).to eq tag_with_nonce + end + + it 'returns a script tag with a nonce using argument syntax with options' do + expect(helper.javascript_tag( 'alert(1)', type: 'application/javascript').to_s).to eq tag_with_nonce_and_type + end + + # This scenario does not really make sense, but it's supported so we test it + it 'returns a script tag with a nonce using argument and block syntax with options' do + expect(helper.javascript_tag( '// ignored', type: 'application/javascript') { 'alert(1)' }.to_s).to eq tag_with_nonce_and_type + end + end +end diff --git a/spec/helpers/groups/group_members_helper_spec.rb b/spec/helpers/groups/group_members_helper_spec.rb index bb92445cb19..222cca43860 100644 --- a/spec/helpers/groups/group_members_helper_spec.rb +++ b/spec/helpers/groups/group_members_helper_spec.rb @@ -74,13 +74,15 @@ RSpec.describe Groups::GroupMembersHelper do before do allow(helper).to receive(:group_group_member_path).with(group, ':id').and_return('/groups/foo-bar/-/group_members/:id') + allow(helper).to receive(:can?).with(current_user, :admin_group_member, group).and_return(true) end it 'returns expected hash' do expect(helper.group_members_list_data_attributes(group, present_members([group_member]))).to include({ members: helper.members_data_json(group, present_members([group_member])), member_path: '/groups/foo-bar/-/group_members/:id', - group_id: group.id + group_id: group.id, + can_manage_members: 'true' }) end end diff --git a/spec/helpers/icons_helper_spec.rb b/spec/helpers/icons_helper_spec.rb index c05b2b206cc..4784d0aff26 100644 --- a/spec/helpers/icons_helper_spec.rb +++ b/spec/helpers/icons_helper_spec.rb @@ -5,21 +5,6 @@ require 'spec_helper' RSpec.describe IconsHelper do let(:icons_path) { ActionController::Base.helpers.image_path("icons.svg") } - describe 'icon' do - it 'returns aria-hidden by default' do - star = icon('star') - - expect(star['aria-hidden']).to eq 'aria-hidden' - end - - it 'does not return aria-hidden if aria-label is set' do - up = icon('up', 'aria-label' => 'up') - - expect(up['aria-hidden']).to be_nil - expect(up['aria-label']).to eq 'aria-label' - end - end - describe 'sprite_icon_path' do it 'returns relative path' do expect(sprite_icon_path).to eq(icons_path) @@ -86,7 +71,7 @@ RSpec.describe IconsHelper do it 'does not raise in production mode' do stub_rails_env('production') - expect(File).not_to receive(:read) + expect_file_not_to_read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')) expect { sprite_icon(non_existing) }.not_to raise_error end diff --git a/spec/helpers/issuables_helper_spec.rb b/spec/helpers/issuables_helper_spec.rb index 0e3752f220e..57845904d32 100644 --- a/spec/helpers/issuables_helper_spec.rb +++ b/spec/helpers/issuables_helper_spec.rb @@ -44,6 +44,30 @@ RSpec.describe IssuablesHelper do end end + describe '#issuable_meta' do + let(:user) { create(:user) } + + let_it_be(:project) { create(:project) } + + describe 'author status' do + let(:issuable) { build(:merge_request, source_project: project, author: user, created_at: '2020-01-30') } + + it 'displays an emoji if the user status is set' do + user.status = UserStatus.new(message: 'lol') + content = helper.issuable_meta(issuable, project) + expect(content).to match('<span class="user-status-emoji has-tooltip" title="lol" data-html="true" data-placement="top">') + expect(content).to match('<gl-emoji title="speech balloon" data-name="speech_balloon" data-unicode-version="6.0">') + end + + it 'does not displays an emoji if the user status is not set' do + user.status = UserStatus.new + content = helper.issuable_meta(issuable, project) + expect(content).not_to match('class="user-status-emoji has-tooltip"') + expect(content).not_to match('gl-emoji') + end + end + end + describe '#issuables_state_counter_text' do let(:user) { create(:user) } @@ -194,7 +218,7 @@ RSpec.describe IssuablesHelper do assign(:project, issue.project) end - it 'sets sentryIssueIdentifier to nil with no sentry issue ' do + it 'sets sentryIssueIdentifier to nil with no sentry issue' do expect(helper.issuable_initial_data(issue)[:sentryIssueIdentifier]) .to be_nil end diff --git a/spec/helpers/markup_helper_spec.rb b/spec/helpers/markup_helper_spec.rb index 6c5855eeb91..45e8a2e7e1a 100644 --- a/spec/helpers/markup_helper_spec.rb +++ b/spec/helpers/markup_helper_spec.rb @@ -316,6 +316,7 @@ RSpec.describe MarkupHelper do describe '#render_wiki_content' do let(:wiki) { double('WikiPage', path: "file.#{extension}") } let(:wiki_repository) { double('Repository') } + let(:content) { 'wiki content' } let(:context) do { pipeline: :wiki, project: project, wiki: wiki, @@ -325,9 +326,11 @@ RSpec.describe MarkupHelper do end before do - expect(wiki).to receive(:content).and_return('wiki content') + expect(wiki).to receive(:content).and_return(content) expect(wiki).to receive(:slug).and_return('nested/page') expect(wiki).to receive(:repository).and_return(wiki_repository) + allow(wiki).to receive(:container).and_return(project) + helper.instance_variable_set(:@wiki, wiki) end @@ -339,6 +342,19 @@ RSpec.describe MarkupHelper do helper.render_wiki_content(wiki) end + + context 'when context has labels' do + let_it_be(:label) { create(:label, title: 'Bug', project: project) } + + let(:content) { '~Bug' } + + it 'renders label' do + result = helper.render_wiki_content(wiki) + doc = Nokogiri::HTML.parse(result) + + expect(doc.css('.gl-label-link')).not_to be_empty + end + end end context 'when file is Asciidoc' do diff --git a/spec/helpers/merge_requests_helper_spec.rb b/spec/helpers/merge_requests_helper_spec.rb index 153dc19335b..377e2c43a72 100644 --- a/spec/helpers/merge_requests_helper_spec.rb +++ b/spec/helpers/merge_requests_helper_spec.rb @@ -6,26 +6,6 @@ RSpec.describe MergeRequestsHelper do include ActionView::Helpers::UrlHelper include ProjectForksHelper - describe 'ci_build_details_path' do - let(:project) { create(:project) } - let(:merge_request) { MergeRequest.new } - let(:ci_service) { CiService.new } - let(:last_commit) { Ci::Pipeline.new({}) } - - before do - allow(merge_request).to receive(:source_project).and_return(project) - allow(merge_request).to receive(:last_commit).and_return(last_commit) - allow(project).to receive(:ci_service).and_return(ci_service) - allow(last_commit).to receive(:sha).and_return('12d65c') - end - - it 'does not include api credentials in a link' do - allow(ci_service) - .to receive(:build_page).and_return("http://secretuser:secretpass@jenkins.example.com:8888/job/test1/scm/bySHA1/12d65c") - expect(helper.ci_build_details_path(merge_request)).not_to match("secret") - end - end - describe '#state_name_with_icon' do using RSpec::Parameterized::TableSyntax diff --git a/spec/helpers/nav_helper_spec.rb b/spec/helpers/nav_helper_spec.rb index b1c9485e5a1..c4795a814ba 100644 --- a/spec/helpers/nav_helper_spec.rb +++ b/spec/helpers/nav_helper_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe NavHelper, :do_not_mock_admin_mode do +RSpec.describe NavHelper do describe '#header_links' do include_context 'custom session' diff --git a/spec/helpers/notifications_helper_spec.rb b/spec/helpers/notifications_helper_spec.rb index 5e37e56c612..555cffba614 100644 --- a/spec/helpers/notifications_helper_spec.rb +++ b/spec/helpers/notifications_helper_spec.rb @@ -20,10 +20,19 @@ RSpec.describe NotificationsHelper do end describe '#notification_event_name' do - it { expect(notification_event_name(:success_pipeline)).to match('Successful pipeline') } - it { expect(notification_event_name(:failed_pipeline)).to match('Failed pipeline') } - it { expect(notification_event_name(:fixed_pipeline)).to match('Fixed pipeline') } - it { expect(notification_event_name(:moved_project)).to match('Moved project') } + context 'for success_pipeline' do + it 'returns the custom name' do + expect(FastGettext).to receive(:cached_find).with('NotificationEvent|Successful pipeline') + expect(notification_event_name(:success_pipeline)).to eq('Successful pipeline') + end + end + + context 'for everything else' do + it 'returns a humanized name' do + expect(FastGettext).to receive(:cached_find).with('NotificationEvent|Failed pipeline') + expect(notification_event_name(:failed_pipeline)).to eq('Failed pipeline') + end + end end describe '#notification_icon_level' do diff --git a/spec/helpers/operations_helper_spec.rb b/spec/helpers/operations_helper_spec.rb index 09f9bba8f9e..801d5de79b1 100644 --- a/spec/helpers/operations_helper_spec.rb +++ b/spec/helpers/operations_helper_spec.rb @@ -21,20 +21,15 @@ RSpec.describe OperationsHelper do end context 'initial service configuration' do - let_it_be(:alerts_service) { AlertsService.new(project: project) } let_it_be(:prometheus_service) { PrometheusService.new(project: project) } before do - allow(project).to receive(:find_or_initialize_service).with('alerts').and_return(alerts_service) + allow(project).to receive(:find_or_initialize_service).and_call_original allow(project).to receive(:find_or_initialize_service).with('prometheus').and_return(prometheus_service) end it 'returns the correct values' do expect(subject).to eq( - 'activated' => 'false', - 'url' => alerts_service.url, - 'authorization_key' => nil, - 'form_path' => project_service_path(project, alerts_service), 'alerts_setup_url' => help_page_path('operations/incident_management/alert_integrations.md', anchor: 'generic-http-endpoint'), 'alerts_usage_url' => project_alert_management_index_path(project), 'prometheus_form_path' => project_service_path(project, prometheus_service), @@ -104,33 +99,6 @@ RSpec.describe OperationsHelper do end end end - - context 'with generic alerts service configured' do - let_it_be(:alerts_service) { create(:alerts_service, project: project) } - - context 'with generic alerts enabled' do - it 'returns the correct values' do - expect(subject).to include( - 'activated' => 'true', - 'authorization_key' => alerts_service.token, - 'url' => alerts_service.url - ) - end - end - - context 'with generic alerts disabled' do - before do - alerts_service.update!(active: false) - end - - it 'returns the correct values' do - expect(subject).to include( - 'activated' => 'false', - 'authorization_key' => alerts_service.token - ) - end - end - end end describe '#operations_settings_data' do diff --git a/spec/helpers/projects/alert_management_helper_spec.rb b/spec/helpers/projects/alert_management_helper_spec.rb index f6d0c9ca49a..fd35c1ecab8 100644 --- a/spec/helpers/projects/alert_management_helper_spec.rb +++ b/spec/helpers/projects/alert_management_helper_spec.rb @@ -39,28 +39,6 @@ RSpec.describe Projects::AlertManagementHelper do end end - context 'with alerts service' do - let_it_be(:alerts_service) { create(:alerts_service, project: project) } - - context 'when alerts service is active' do - it 'enables alert management' do - expect(data).to include( - 'alert-management-enabled' => 'true' - ) - end - end - - context 'when alerts service is inactive' do - it 'disables alert management' do - alerts_service.update!(active: false) - - expect(data).to include( - 'alert-management-enabled' => 'false' - ) - end - end - end - context 'with prometheus service' do let_it_be(:prometheus_service) { create(:prometheus_service, project: project) } @@ -105,6 +83,16 @@ RSpec.describe Projects::AlertManagementHelper do end end + context 'with an alert' do + let_it_be(:alert) { create(:alert_management_alert, project: project) } + + it 'enables alert management' do + expect(data).to include( + 'alert-management-enabled' => 'true' + ) + end + end + context 'when user does not have requisite enablement permissions' do let(:user_can_enable_alert_management) { false } diff --git a/spec/helpers/projects/terraform_helper_spec.rb b/spec/helpers/projects/terraform_helper_spec.rb index de363c42d21..70b08f4139b 100644 --- a/spec/helpers/projects/terraform_helper_spec.rb +++ b/spec/helpers/projects/terraform_helper_spec.rb @@ -5,10 +5,11 @@ require 'spec_helper' RSpec.describe Projects::TerraformHelper do describe '#js_terraform_list_data' do let_it_be(:project) { create(:project) } + let(:current_user) { project.creator } - subject { helper.js_terraform_list_data(project) } + subject { helper.js_terraform_list_data(current_user, project) } - it 'displays image path' do + it 'includes image path' do image_path = ActionController::Base.helpers.image_path( 'illustrations/empty-state/empty-serverless-lg.svg' ) @@ -16,8 +17,28 @@ RSpec.describe Projects::TerraformHelper do expect(subject[:empty_state_image]).to eq(image_path) end - it 'displays project path' do + it 'includes project path' do expect(subject[:project_path]).to eq(project.full_path) end + + it 'indicates the user is a terraform admin' do + expect(subject[:terraform_admin]).to eq(true) + end + + context 'when current_user is not a terraform admin' do + let(:current_user) { create(:user) } + + it 'indicates the user is not an admin' do + expect(subject[:terraform_admin]).to eq(false) + end + end + + context 'when current_user is missing' do + let(:current_user) { nil } + + it 'indicates the user is not an admin' do + expect(subject[:terraform_admin]).to be_nil + end + end end end diff --git a/spec/helpers/projects_helper_spec.rb b/spec/helpers/projects_helper_spec.rb index 9635a6f9c82..d28d5ecda1b 100644 --- a/spec/helpers/projects_helper_spec.rb +++ b/spec/helpers/projects_helper_spec.rb @@ -416,6 +416,7 @@ RSpec.describe ProjectsHelper do describe '#get_project_nav_tabs' do before do + allow(helper).to receive(:current_user).and_return(user) allow(helper).to receive(:can?) { true } end @@ -488,24 +489,37 @@ RSpec.describe ProjectsHelper do describe '#can_view_operations_tab?' do before do allow(helper).to receive(:current_user).and_return(user) + allow(helper).to receive(:can?).and_return(false) end subject { helper.send(:can_view_operations_tab?, user, project) } - [ - :metrics_dashboard, - :read_alert_management_alert, - :read_environment, - :read_issue, - :read_sentry_issue, - :read_cluster - ].each do |ability| + where(:ability) do + [ + :metrics_dashboard, + :read_alert_management_alert, + :read_environment, + :read_issue, + :read_sentry_issue, + :read_cluster + ] + end + + with_them do it 'includes operations tab' do - allow(helper).to receive(:can?).and_return(false) allow(helper).to receive(:can?).with(user, ability, project).and_return(true) is_expected.to be(true) end + + context 'when operations feature is disabled' do + it 'does not include operations tab' do + allow(helper).to receive(:can?).with(user, ability, project).and_return(true) + project.project_feature.update_attribute(:operations_access_level, ProjectFeature::DISABLED) + + is_expected.to be(false) + end + end end end @@ -1010,4 +1024,34 @@ RSpec.describe ProjectsHelper do subject end end + + describe '#project_permissions_settings' do + context 'with no project_setting associated' do + it 'includes a value for edit commit messages' do + settings = project_permissions_settings(project) + + expect(settings[:allowEditingCommitMessages]).to be_falsy + end + end + + context 'when commits are allowed to be edited' do + it 'includes the edit commit message value' do + project.create_project_setting(allow_editing_commit_messages: true) + + settings = project_permissions_settings(project) + + expect(settings[:allowEditingCommitMessages]).to be_truthy + end + end + + context 'when commits are not allowed to be edited' do + it 'returns false to the edit commit message value' do + project.create_project_setting(allow_editing_commit_messages: false) + + settings = project_permissions_settings(project) + + expect(settings[:allowEditingCommitMessages]).to be_falsy + end + end + end end diff --git a/spec/helpers/rss_helper_spec.rb b/spec/helpers/rss_helper_spec.rb index c7eb33dc6f7..05f6ebb6c1b 100644 --- a/spec/helpers/rss_helper_spec.rb +++ b/spec/helpers/rss_helper_spec.rb @@ -18,5 +18,14 @@ RSpec.describe RssHelper do expect(helper.rss_url_options[:feed_token]).to be_nil end end + + context 'when feed_token disabled' do + it "does not have a feed_token" do + current_user = create(:user) + allow(helper).to receive(:current_user).and_return(current_user) + allow(Gitlab::CurrentSettings).to receive(:disable_feed_token).and_return(true) + expect(helper.rss_url_options[:feed_token]).to be_nil + end + end end end diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb index 34af3ce7e5e..2cb9d66ac63 100644 --- a/spec/helpers/search_helper_spec.rb +++ b/spec/helpers/search_helper_spec.rb @@ -104,6 +104,37 @@ RSpec.describe SearchHelper do }) end + it 'includes the users recently viewed issues with the exact same name', :aggregate_failures do + recent_issues = instance_double(::Gitlab::Search::RecentIssues) + expect(::Gitlab::Search::RecentIssues).to receive(:new).with(user: user).and_return(recent_issues) + project1 = create(:project, namespace: user.namespace) + project2 = create(:project, namespace: user.namespace) + issue1 = create(:issue, title: 'issue same_name', project: project1) + issue2 = create(:issue, title: 'issue same_name', project: project2) + + expect(recent_issues).to receive(:search).with('the search term').and_return(Issue.id_in_ordered([issue1.id, issue2.id])) + + results = search_autocomplete_opts("the search term") + + expect(results.count).to eq(2) + + expect(results[0]).to include({ + category: 'Recent issues', + id: issue1.id, + label: 'issue same_name', + url: Gitlab::Routing.url_helpers.project_issue_path(issue1.project, issue1), + avatar_url: '' # This project didn't have an avatar so set this to '' + }) + + expect(results[1]).to include({ + category: 'Recent issues', + id: issue2.id, + label: 'issue same_name', + url: Gitlab::Routing.url_helpers.project_issue_path(issue2.project, issue2), + avatar_url: '' # This project didn't have an avatar so set this to '' + }) + end + it 'includes the users recently viewed merge requests', :aggregate_failures do recent_merge_requests = instance_double(::Gitlab::Search::RecentMergeRequests) expect(::Gitlab::Search::RecentMergeRequests).to receive(:new).with(user: user).and_return(recent_merge_requests) @@ -502,11 +533,11 @@ RSpec.describe SearchHelper do using RSpec::Parameterized::TableSyntax where(:description, :expected) do - 'test' | '<span class="gl-text-black-normal gl-font-weight-bold">test</span>' - '<span style="color: blue;">this test should not be blue</span>' | '<span>this <span class="gl-text-black-normal gl-font-weight-bold">test</span> should not be blue</span>' - '<a href="#" onclick="alert(\'XSS\')">Click Me test</a>' | '<a href="#">Click Me <span class="gl-text-black-normal gl-font-weight-bold">test</span></a>' - '<script type="text/javascript">alert(\'Another XSS\');</script> test' | ' <span class="gl-text-black-normal gl-font-weight-bold">test</span>' - 'Lorem test ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec.' | 'Lorem <span class="gl-text-black-normal gl-font-weight-bold">test</span> ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Don...' + 'test' | '<span class="gl-text-gray-900 gl-font-weight-bold">test</span>' + '<span style="color: blue;">this test should not be blue</span>' | '<span>this <span class="gl-text-gray-900 gl-font-weight-bold">test</span> should not be blue</span>' + '<a href="#" onclick="alert(\'XSS\')">Click Me test</a>' | '<a href="#">Click Me <span class="gl-text-gray-900 gl-font-weight-bold">test</span></a>' + '<script type="text/javascript">alert(\'Another XSS\');</script> test' | ' <span class="gl-text-gray-900 gl-font-weight-bold">test</span>' + 'Lorem test ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec.' | 'Lorem <span class="gl-text-gray-900 gl-font-weight-bold">test</span> ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Don...' end with_them do diff --git a/spec/helpers/services_helper_spec.rb b/spec/helpers/services_helper_spec.rb index d6b48b3d565..650642f8982 100644 --- a/spec/helpers/services_helper_spec.rb +++ b/spec/helpers/services_helper_spec.rb @@ -28,35 +28,72 @@ RSpec.describe ServicesHelper do end end - describe '#group_level_integrations?' do - subject { helper.group_level_integrations? } + describe '#scoped_reset_integration_path' do + let(:integration) { build_stubbed(:jira_service) } + let(:group) { nil } + + subject { helper.scoped_reset_integration_path(integration, group: group) } context 'when no group is present' do - it { is_expected.to eq(false) } + it 'returns instance-level path' do + is_expected.to eq(reset_admin_application_settings_integration_path(integration)) + end end context 'when group is present' do let(:group) { build_stubbed(:group) } - before do - assign(:group, group) + it 'returns group-level path' do + is_expected.to eq(reset_group_settings_integration_path(group, integration)) end + end + end + + describe '#reset_integration?' do + let(:group) { nil } + + subject { helper.reset_integration?(integration, group: group) } - context 'when `group_level_integrations` is not enabled' do + context 'when integration is existing record' do + let_it_be(:integration) { create(:jira_service) } + + context 'when `reset_integrations` is not enabled' do it 'returns false' do - stub_feature_flags(group_level_integrations: false) + stub_feature_flags(reset_integrations: false) is_expected.to eq(false) end end - context 'when `group_level_integrations` is enabled for the group' do + context 'when `reset_integrations` is enabled' do + it 'returns true' do + stub_feature_flags(reset_integrations: true) + + is_expected.to eq(true) + end + end + + context 'when `reset_integrations` is enabled for a group' do + let(:group) { build_stubbed(:group) } + it 'returns true' do - stub_feature_flags(group_level_integrations: group) + stub_feature_flags(reset_integrations: group) is_expected.to eq(true) end end end + + context 'when integration is a new record' do + let_it_be(:integration) { build(:jira_service) } + + context 'when `reset_integrations` is enabled' do + it 'returns false' do + stub_feature_flags(reset_integrations: true) + + is_expected.to eq(false) + end + end + end end end diff --git a/spec/helpers/sorting_helper_spec.rb b/spec/helpers/sorting_helper_spec.rb index 1c300aea2df..2d581dfba37 100644 --- a/spec/helpers/sorting_helper_spec.rb +++ b/spec/helpers/sorting_helper_spec.rb @@ -77,6 +77,7 @@ RSpec.describe SortingHelper do sort_value_latest_activity => sort_title_latest_activity, sort_value_recently_created => sort_title_created_date, sort_value_name => sort_title_name, + sort_value_name_desc => sort_title_name_desc, sort_value_stars_desc => sort_title_stars } end diff --git a/spec/helpers/storage_helper_spec.rb b/spec/helpers/storage_helper_spec.rb index 255ec2a41b4..2cec7203fe1 100644 --- a/spec/helpers/storage_helper_spec.rb +++ b/spec/helpers/storage_helper_spec.rb @@ -32,10 +32,12 @@ RSpec.describe StorageHelper do wiki_size: 10.bytes, lfs_objects_size: 20.gigabytes, build_artifacts_size: 30.megabytes, - snippets_size: 40.megabytes)) + snippets_size: 40.megabytes, + packages_size: 12.megabytes, + uploads_size: 15.megabytes)) end - let(:message) { 'Repository: 10 KB / Wikis: 10 Bytes / Build Artifacts: 30 MB / LFS: 20 GB / Snippets: 40 MB' } + let(:message) { 'Repository: 10 KB / Wikis: 10 Bytes / Build Artifacts: 30 MB / LFS: 20 GB / Snippets: 40 MB / Packages: 12 MB / Uploads: 15 MB' } it 'works on ProjectStatistics' do expect(helper.storage_counters_details(project.statistics)).to eq(message) diff --git a/spec/helpers/time_zone_helper_spec.rb b/spec/helpers/time_zone_helper_spec.rb new file mode 100644 index 00000000000..391e9bd38ed --- /dev/null +++ b/spec/helpers/time_zone_helper_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe TimeZoneHelper, :aggregate_failures do + describe '#timezone_data' do + context 'with short format' do + subject(:timezone_data) { helper.timezone_data } + + it 'matches schema' do + expect(timezone_data).not_to be_empty + + timezone_data.each_with_index do |timezone_hash, i| + expect(timezone_hash.keys).to contain_exactly( + :identifier, + :name, + :offset + ), "Failed at index #{i}" + end + end + + it 'formats for display' do + tz = ActiveSupport::TimeZone.all[0] + + expect(timezone_data[0]).to eq( + identifier: tz.tzinfo.identifier, + name: tz.name, + offset: tz.now.utc_offset + ) + end + end + + context 'with full format' do + subject(:timezone_data) { helper.timezone_data(format: :full) } + + it 'matches schema' do + expect(timezone_data).not_to be_empty + + timezone_data.each_with_index do |timezone_hash, i| + expect(timezone_hash.keys).to contain_exactly( + :identifier, + :name, + :abbr, + :offset, + :formatted_offset + ), "Failed at index #{i}" + end + end + + it 'formats for display' do + tz = ActiveSupport::TimeZone.all[0] + + expect(timezone_data[0]).to eq( + identifier: tz.tzinfo.identifier, + name: tz.name, + abbr: tz.tzinfo.strftime('%Z'), + offset: tz.now.utc_offset, + formatted_offset: tz.now.formatted_offset + ) + end + end + + context 'with unknown format' do + subject(:timezone_data) { helper.timezone_data(format: :unknown) } + + it 'raises an exception' do + expect { timezone_data }.to raise_error ArgumentError, 'Invalid format :unknown. Valid formats are :short, :full.' + end + end + end +end diff --git a/spec/helpers/tree_helper_spec.rb b/spec/helpers/tree_helper_spec.rb index 620bf248d7b..136ec07e73d 100644 --- a/spec/helpers/tree_helper_spec.rb +++ b/spec/helpers/tree_helper_spec.rb @@ -216,6 +216,24 @@ RSpec.describe TreeHelper do web_ide_url: "/-/ide/project/#{project.full_path}/edit/#{sha}/-/#{@path}" ) end + + it 'does not load blob from repository again' do + blob + + expect(repository).not_to receive(:blob_at) + + subject + end + end + + context 'nil blob is passed' do + let(:blob) { nil } + + it 'does not load blob from repository' do + expect(repository).not_to receive(:blob_at) + + subject + end end context 'user does not have write access but a personal fork exists' do diff --git a/spec/helpers/user_callouts_helper_spec.rb b/spec/helpers/user_callouts_helper_spec.rb index 4ab3be877b4..250aedda906 100644 --- a/spec/helpers/user_callouts_helper_spec.rb +++ b/spec/helpers/user_callouts_helper_spec.rb @@ -167,8 +167,20 @@ RSpec.describe UserCalloutsHelper do subject { helper.show_registration_enabled_user_callout? } + context 'when on gitlab.com' do + before do + allow(::Gitlab).to receive(:com?).and_return(true) + allow(helper).to receive(:current_user).and_return(admin) + stub_application_setting(signup_enabled: true) + allow(helper).to receive(:user_dismissed?).with(described_class::REGISTRATION_ENABLED_CALLOUT) { false } + end + + it { is_expected.to be false } + end + context 'when `current_user` is not an admin' do before do + allow(::Gitlab).to receive(:com?).and_return(false) allow(helper).to receive(:current_user).and_return(user) stub_application_setting(signup_enabled: true) allow(helper).to receive(:user_dismissed?).with(described_class::REGISTRATION_ENABLED_CALLOUT) { false } @@ -179,6 +191,7 @@ RSpec.describe UserCalloutsHelper do context 'when signup is disabled' do before do + allow(::Gitlab).to receive(:com?).and_return(false) allow(helper).to receive(:current_user).and_return(admin) stub_application_setting(signup_enabled: false) allow(helper).to receive(:user_dismissed?).with(described_class::REGISTRATION_ENABLED_CALLOUT) { false } @@ -189,6 +202,7 @@ RSpec.describe UserCalloutsHelper do context 'when user has dismissed callout' do before do + allow(::Gitlab).to receive(:com?).and_return(false) allow(helper).to receive(:current_user).and_return(admin) stub_application_setting(signup_enabled: true) allow(helper).to receive(:user_dismissed?).with(described_class::REGISTRATION_ENABLED_CALLOUT) { true } @@ -197,8 +211,9 @@ RSpec.describe UserCalloutsHelper do it { is_expected.to be false } end - context 'when `current_user` is an admin, signup is enabled, and user has not dismissed callout' do + context 'when not gitlab.com, `current_user` is an admin, signup is enabled, and user has not dismissed callout' do before do + allow(::Gitlab).to receive(:com?).and_return(false) allow(helper).to receive(:current_user).and_return(admin) stub_application_setting(signup_enabled: true) allow(helper).to receive(:user_dismissed?).with(described_class::REGISTRATION_ENABLED_CALLOUT) { false } diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb index 9ebbf975903..c92c6e6e78e 100644 --- a/spec/helpers/users_helper_spec.rb +++ b/spec/helpers/users_helper_spec.rb @@ -272,4 +272,82 @@ RSpec.describe UsersHelper do end end end + + describe '#user_display_name' do + subject { helper.user_display_name(user) } + + before do + stub_current_user(nil) + end + + context 'for a confirmed user' do + let(:user) { create(:user) } + + before do + stub_profile_permission_allowed(true) + end + + it { is_expected.to eq(user.name) } + end + + context 'for an unconfirmed user' do + let(:user) { create(:user, :unconfirmed) } + + before do + stub_profile_permission_allowed(false) + end + + it { is_expected.to eq('Unconfirmed user') } + + context 'when current user is an admin' do + before do + admin_user = create(:admin) + stub_current_user(admin_user) + stub_profile_permission_allowed(true, admin_user) + end + + it { is_expected.to eq(user.name) } + end + + context 'when the current user is self' do + before do + stub_current_user(user) + stub_profile_permission_allowed(true, user) + end + + it { is_expected.to eq(user.name) } + end + end + + context 'for a blocked user' do + let(:user) { create(:user, :blocked) } + + it { is_expected.to eq('Blocked user') } + end + + def stub_current_user(user) + allow(helper).to receive(:current_user).and_return(user) + end + + def stub_profile_permission_allowed(allowed, current_user = nil) + allow(helper).to receive(:can?).with(current_user, :read_user_profile, user).and_return(allowed) + end + end + + describe '#admin_users_data_attributes' do + subject(:data) { helper.admin_users_data_attributes([user]) } + + it 'users matches the serialized json' do + entity = double + expect_next_instance_of(Admin::UserSerializer) do |instance| + expect(instance).to receive(:represent).with([user]).and_return(entity) + end + expect(entity).to receive(:to_json).and_return("{\"username\":\"admin\"}") + expect(data[:users]).to eq "{\"username\":\"admin\"}" + end + + it 'paths matches the schema' do + expect(data[:paths]).to match_schema('entities/admin_users_data_attributes_paths') + end + end end diff --git a/spec/helpers/visibility_level_helper_spec.rb b/spec/helpers/visibility_level_helper_spec.rb index cd1fc70bbc1..86b0693af92 100644 --- a/spec/helpers/visibility_level_helper_spec.rb +++ b/spec/helpers/visibility_level_helper_spec.rb @@ -284,4 +284,34 @@ RSpec.describe VisibilityLevelHelper do it { is_expected.to eq(expected) } end end + + describe '#visibility_level_options' do + let(:user) { build(:user) } + + before do + allow(helper).to receive(:current_user).and_return(user) + end + + it 'returns the desired mapping' do + expected_options = [ + { + level: 0, + label: 'Private', + description: 'The group and its projects can only be viewed by members.' + }, + { + level: 10, + label: 'Internal', + description: 'The group and any internal projects can be viewed by any logged in user except external users.' + }, + { + level: 20, + label: 'Public', + description: 'The group and any public projects can be viewed without any authentication.' + } + ] + + expect(helper.visibility_level_options(group)).to eq expected_options + end + end end diff --git a/spec/helpers/whats_new_helper_spec.rb b/spec/helpers/whats_new_helper_spec.rb index 1c8684de75c..017826921ff 100644 --- a/spec/helpers/whats_new_helper_spec.rb +++ b/spec/helpers/whats_new_helper_spec.rb @@ -3,22 +3,22 @@ require 'spec_helper' RSpec.describe WhatsNewHelper do - let(:fixture_dir_glob) { Dir.glob(File.join('spec', 'fixtures', 'whats_new', '*.yml')) } - describe '#whats_new_storage_key' do subject { helper.whats_new_storage_key } context 'when version exist' do + let(:release_item) { double(:item) } + before do - allow(Dir).to receive(:glob).with(Rails.root.join('data', 'whats_new', '*.yml')).and_return(fixture_dir_glob) + allow(ReleaseHighlight).to receive(:versions).and_return([84.0]) end - it { is_expected.to eq('display-whats-new-notification-01.05') } + it { is_expected.to eq('display-whats-new-notification-84.0') } end - context 'when recent release items do NOT exist' do + context 'when most recent release highlights do NOT exist' do before do - allow(helper).to receive(:whats_new_release_items).and_return(nil) + allow(ReleaseHighlight).to receive(:versions).and_return(nil) end it { is_expected.to be_nil } @@ -30,31 +30,28 @@ RSpec.describe WhatsNewHelper do context 'when recent release items exist' do it 'returns the count from the most recent file' do - expect(Dir).to receive(:glob).with(Rails.root.join('data', 'whats_new', '*.yml')).and_return(fixture_dir_glob) + allow(ReleaseHighlight).to receive(:most_recent_item_count).and_return(1) expect(subject).to eq(1) end end context 'when recent release items do NOT exist' do - before do - allow(YAML).to receive(:safe_load).and_raise + it 'returns nil' do + allow(ReleaseHighlight).to receive(:most_recent_item_count).and_return(nil) - expect(Gitlab::ErrorTracking).to receive(:track_exception) - end - - it 'fails gracefully and logs an error' do expect(subject).to be_nil end end end - # Testing this important private method here because the request spec required multiple confusing mocks and felt wrong and overcomplicated - describe '#whats_new_items_cache_key' do - it 'returns a key containing the most recent file name and page parameter' do - allow(Dir).to receive(:glob).with(Rails.root.join('data', 'whats_new', '*.yml')).and_return(fixture_dir_glob) + describe '#whats_new_versions' do + let(:versions) { [84.0] } + + it 'returns ReleaseHighlight.versions' do + expect(ReleaseHighlight).to receive(:versions).and_return(versions) - expect(helper.send(:whats_new_items_cache_key, 2)).to eq('whats_new:release_items:file-20201225_01_05:page-2') + expect(helper.whats_new_versions).to eq(versions) end end end |