diff options
Diffstat (limited to 'spec/presenters')
52 files changed, 1102 insertions, 110 deletions
diff --git a/spec/presenters/alert_management/alert_presenter_spec.rb b/spec/presenters/alert_management/alert_presenter_spec.rb new file mode 100644 index 00000000000..b1bf7029f3e --- /dev/null +++ b/spec/presenters/alert_management/alert_presenter_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AlertManagement::AlertPresenter do + let_it_be(:project) { create(:project) } + let_it_be(:generic_payload) do + { + 'title' => 'Alert title', + 'start_time' => '2020-04-27T10:10:22.265949279Z', + 'custom' => { 'param' => 73 } + } + end + let_it_be(:alert) do + create(:alert_management_alert, :with_description, :with_host, :with_service, :with_monitoring_tool, project: project, payload: generic_payload) + end + + subject(:presenter) { described_class.new(alert) } + + describe '#issue_description' do + let(:markdown_line_break) { ' ' } + + it 'returns an alert issue description' do + expect(presenter.issue_description).to eq( + <<~MARKDOWN.chomp + #### Summary + + **Start time:** #{presenter.start_time}#{markdown_line_break} + **Severity:** #{presenter.severity}#{markdown_line_break} + **Service:** #{alert.service}#{markdown_line_break} + **Monitoring tool:** #{alert.monitoring_tool}#{markdown_line_break} + **Hosts:** #{alert.hosts.join(' ')}#{markdown_line_break} + **Description:** #{alert.description} + + #### Alert Details + + **custom.param:** 73 + MARKDOWN + ) + end + end + + describe '#metrics_dashboard_url' do + it 'is not defined' do + expect(presenter.metrics_dashboard_url).to be_nil + end + end +end diff --git a/spec/presenters/alert_management/prometheus_alert_presenter_spec.rb b/spec/presenters/alert_management/prometheus_alert_presenter_spec.rb new file mode 100644 index 00000000000..95246914140 --- /dev/null +++ b/spec/presenters/alert_management/prometheus_alert_presenter_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AlertManagement::PrometheusAlertPresenter do + let_it_be(:project) { create(:project) } + let_it_be(:payload) do + { + 'annotations' => { + 'title' => 'Alert title', + 'gitlab_incident_markdown' => '**`markdown example`**', + 'custom annotation' => 'custom annotation value' + }, + 'startsAt' => '2020-04-27T10:10:22.265949279Z', + 'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1' + } + end + let(:alert) do + create(:alert_management_alert, :prometheus, project: project, payload: payload) + end + + subject(:presenter) { described_class.new(alert) } + + describe '#issue_description' do + let(:markdown_line_break) { ' ' } + + it 'returns an alert issue description' do + expect(presenter.issue_description).to eq( + <<~MARKDOWN.chomp + #### Summary + + **Start time:** #{presenter.start_time}#{markdown_line_break} + **Severity:** #{presenter.severity}#{markdown_line_break} + **full_query:** `vector(1)`#{markdown_line_break} + **Monitoring tool:** Prometheus + + #### Alert Details + + **custom annotation:** custom annotation value + + --- + + **`markdown example`** + MARKDOWN + ) + end + end + + describe '#metrics_dashboard_url' do + subject { presenter.metrics_dashboard_url } + + context 'for a non-prometheus alert' do + it { is_expected.to be_nil } + end + + context 'for a self-managed prometheus alert' do + include_context 'self-managed prometheus alert attributes' + + it { is_expected.to eq(dashboard_url_for_alert) } + end + + context 'for a gitlab-managed prometheus alert' do + include_context 'gitlab-managed prometheus alert attributes' + + it { is_expected.to eq(dashboard_url_for_alert) } + end + end +end diff --git a/spec/presenters/award_emoji_presenter_spec.rb b/spec/presenters/award_emoji_presenter_spec.rb index e2ada2a3c93..58ee985f165 100644 --- a/spec/presenters/award_emoji_presenter_spec.rb +++ b/spec/presenters/award_emoji_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe AwardEmojiPresenter do +RSpec.describe AwardEmojiPresenter do let(:emoji_name) { 'thumbsup' } let(:award_emoji) { build(:award_emoji, name: emoji_name) } let(:presenter) { described_class.new(award_emoji) } diff --git a/spec/presenters/blob_presenter_spec.rb b/spec/presenters/blob_presenter_spec.rb index ee7bfd1256d..bf926ce62b3 100644 --- a/spec/presenters/blob_presenter_spec.rb +++ b/spec/presenters/blob_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe BlobPresenter, :seed_helper do +RSpec.describe BlobPresenter, :seed_helper do let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') } let(:git_blob) do diff --git a/spec/presenters/blobs/unfold_presenter_spec.rb b/spec/presenters/blobs/unfold_presenter_spec.rb index 83004809536..4e9f83e8001 100644 --- a/spec/presenters/blobs/unfold_presenter_spec.rb +++ b/spec/presenters/blobs/unfold_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Blobs::UnfoldPresenter do +RSpec.describe Blobs::UnfoldPresenter do include FakeBlobHelpers let(:project) { create(:project, :repository) } diff --git a/spec/presenters/ci/bridge_presenter_spec.rb b/spec/presenters/ci/bridge_presenter_spec.rb index 2a4c40a7eaa..6291c3426e2 100644 --- a/spec/presenters/ci/bridge_presenter_spec.rb +++ b/spec/presenters/ci/bridge_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::BridgePresenter do +RSpec.describe Ci::BridgePresenter do let_it_be(:project) { create(:project) } let_it_be(:pipeline) { create(:ci_pipeline, project: project) } let_it_be(:bridge) { create(:ci_bridge, pipeline: pipeline, status: :failed) } diff --git a/spec/presenters/ci/build_presenter_spec.rb b/spec/presenters/ci/build_presenter_spec.rb index 9cf6eb45c63..8d302b242b3 100644 --- a/spec/presenters/ci/build_presenter_spec.rb +++ b/spec/presenters/ci/build_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::BuildPresenter do +RSpec.describe Ci::BuildPresenter do let(:project) { create(:project) } let(:pipeline) { create(:ci_pipeline, project: project) } let(:build) { create(:ci_build, pipeline: pipeline) } diff --git a/spec/presenters/ci/build_runner_presenter_spec.rb b/spec/presenters/ci/build_runner_presenter_spec.rb index de199d2bff9..ce4c5a2db7d 100644 --- a/spec/presenters/ci/build_runner_presenter_spec.rb +++ b/spec/presenters/ci/build_runner_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::BuildRunnerPresenter do +RSpec.describe Ci::BuildRunnerPresenter do let(:presenter) { described_class.new(build) } let(:archive) { { paths: ['sample.txt'] } } diff --git a/spec/presenters/ci/group_variable_presenter_spec.rb b/spec/presenters/ci/group_variable_presenter_spec.rb index 3b81a425f5b..aaa6410266e 100644 --- a/spec/presenters/ci/group_variable_presenter_spec.rb +++ b/spec/presenters/ci/group_variable_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::GroupVariablePresenter do +RSpec.describe Ci::GroupVariablePresenter do include Gitlab::Routing.url_helpers let(:group) { create(:group) } diff --git a/spec/presenters/ci/legacy_stage_presenter_spec.rb b/spec/presenters/ci/legacy_stage_presenter_spec.rb index ccf4e48de6e..5268ef0f246 100644 --- a/spec/presenters/ci/legacy_stage_presenter_spec.rb +++ b/spec/presenters/ci/legacy_stage_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::LegacyStagePresenter do +RSpec.describe Ci::LegacyStagePresenter do let(:legacy_stage) { create(:ci_stage) } let(:presenter) { described_class.new(legacy_stage) } diff --git a/spec/presenters/ci/pipeline_presenter_spec.rb b/spec/presenters/ci/pipeline_presenter_spec.rb index e8b66682b97..158daad97f5 100644 --- a/spec/presenters/ci/pipeline_presenter_spec.rb +++ b/spec/presenters/ci/pipeline_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::PipelinePresenter do +RSpec.describe Ci::PipelinePresenter do include Gitlab::Routing let(:user) { create(:user) } diff --git a/spec/presenters/ci/trigger_presenter_spec.rb b/spec/presenters/ci/trigger_presenter_spec.rb index 41cb436f928..bac1c94e0b7 100644 --- a/spec/presenters/ci/trigger_presenter_spec.rb +++ b/spec/presenters/ci/trigger_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::TriggerPresenter do +RSpec.describe Ci::TriggerPresenter do let_it_be(:user) { create(:user) } let_it_be(:project) { create(:project) } diff --git a/spec/presenters/ci/variable_presenter_spec.rb b/spec/presenters/ci/variable_presenter_spec.rb index 70cf2f539b6..30fedf78035 100644 --- a/spec/presenters/ci/variable_presenter_spec.rb +++ b/spec/presenters/ci/variable_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Ci::VariablePresenter do +RSpec.describe Ci::VariablePresenter do include Gitlab::Routing.url_helpers let(:project) { create(:project) } diff --git a/spec/presenters/clusterable_presenter_spec.rb b/spec/presenters/clusterable_presenter_spec.rb index 2c0a7f3e9b2..d19abd4e4d8 100644 --- a/spec/presenters/clusterable_presenter_spec.rb +++ b/spec/presenters/clusterable_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ClusterablePresenter do +RSpec.describe ClusterablePresenter do include Gitlab::Routing.url_helpers describe '.fabricate' do diff --git a/spec/presenters/clusters/cluster_presenter_spec.rb b/spec/presenters/clusters/cluster_presenter_spec.rb index 6a1360807b7..5b75b281297 100644 --- a/spec/presenters/clusters/cluster_presenter_spec.rb +++ b/spec/presenters/clusters/cluster_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Clusters::ClusterPresenter do +RSpec.describe Clusters::ClusterPresenter do include Gitlab::Routing.url_helpers let(:cluster) { create(:cluster, :provided_by_gcp, :project) } @@ -249,4 +249,126 @@ describe Clusters::ClusterPresenter do it { is_expected.to be_truthy } end end + + describe '#health_data' do + shared_examples 'cluster health data' do + let(:user) { create(:user) } + let(:cluster_presenter) { cluster.present(current_user: user) } + + let(:clusterable_presenter) do + ClusterablePresenter.fabricate(clusterable, current_user: user) + end + + subject { cluster_presenter.health_data(clusterable_presenter) } + + it do + is_expected.to include('clusters-path': clusterable_presenter.index_path, + 'dashboard-endpoint': clusterable_presenter.metrics_dashboard_path(cluster), + 'documentation-path': help_page_path('user/project/clusters/index', anchor: 'monitoring-your-kubernetes-cluster-ultimate'), + 'add-dashboard-documentation-path': help_page_path('user/project/integrations/prometheus.md', anchor: 'adding-a-new-dashboard-to-your-project'), + 'empty-getting-started-svg-path': match_asset_path('/assets/illustrations/monitoring/getting_started.svg'), + 'empty-loading-svg-path': match_asset_path('/assets/illustrations/monitoring/loading.svg'), + 'empty-no-data-svg-path': match_asset_path('/assets/illustrations/monitoring/no_data.svg'), + 'empty-no-data-small-svg-path': match_asset_path('illustrations/chart-empty-state-small.svg'), + 'empty-unable-to-connect-svg-path': match_asset_path('/assets/illustrations/monitoring/unable_to_connect.svg'), + 'settings-path': '', + 'project-path': '', + 'tags-path': '') + end + end + + context 'with project cluster' do + let(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:clusterable) { cluster.project } + + it_behaves_like 'cluster health data' + end + + context 'with group cluster' do + let(:cluster) { create(:cluster, :group, :provided_by_gcp) } + let(:clusterable) { cluster.group } + + it_behaves_like 'cluster health data' + end + end + + describe '#gitlab_managed_apps_logs_path' do + context 'user can read logs' do + let(:project) { cluster.project } + + before do + project.add_maintainer(user) + end + + it 'returns path to logs' do + expect(presenter.gitlab_managed_apps_logs_path).to eq k8s_project_logs_path(project, cluster_id: cluster.id, format: :json) + end + + context 'cluster has elastic stack application installed' do + before do + create(:clusters_applications_elastic_stack, :installed, cluster: cluster) + end + + it 'returns path to logs' do + expect(presenter.gitlab_managed_apps_logs_path).to eq elasticsearch_project_logs_path(project, cluster_id: cluster.id, format: :json) + end + end + end + + context 'group cluster' do + let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [group]) } + let(:group) { create(:group, name: 'Foo') } + + context 'user can read logs' do + before do + group.add_maintainer(user) + end + + context 'there are projects within group' do + let!(:project) { create(:project, namespace: group) } + + it 'returns path to logs' do + expect(presenter.gitlab_managed_apps_logs_path).to eq k8s_project_logs_path(project, cluster_id: cluster.id, format: :json) + end + end + + context 'there are no projects within group' do + it 'returns nil' do + expect(presenter.gitlab_managed_apps_logs_path).to be_nil + end + end + end + end + + context 'instance cluster' do + let(:cluster) { create(:cluster, cluster_type: :instance_type) } + let!(:project) { create(:project) } + let(:user) { create(:admin) } + + before do + project.add_maintainer(user) + stub_feature_flags(user_mode_in_session: false) + end + + context 'user can read logs' do + it 'returns path to logs' do + expect(presenter.gitlab_managed_apps_logs_path).to eq k8s_project_logs_path(project, cluster_id: cluster.id, format: :json) + end + end + end + + context 'user can NOT read logs' do + let(:cluster) { create(:cluster, cluster_type: :instance_type) } + let!(:project) { create(:project) } + + before do + project.add_developer(user) + stub_feature_flags(user_mode_in_session: false) + end + + it 'returns nil' do + expect(presenter.gitlab_managed_apps_logs_path).to be_nil + end + end + end end diff --git a/spec/presenters/commit_presenter_spec.rb b/spec/presenters/commit_presenter_spec.rb index bc749acfa3a..bc6be07f415 100644 --- a/spec/presenters/commit_presenter_spec.rb +++ b/spec/presenters/commit_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe CommitPresenter do +RSpec.describe CommitPresenter do let(:project) { create(:project, :repository) } let(:commit) { project.commit } let(:user) { create(:user) } diff --git a/spec/presenters/commit_status_presenter_spec.rb b/spec/presenters/commit_status_presenter_spec.rb index b02497d4c11..4b2441d656e 100644 --- a/spec/presenters/commit_status_presenter_spec.rb +++ b/spec/presenters/commit_status_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe CommitStatusPresenter do +RSpec.describe CommitStatusPresenter do let(:project) { create(:project) } let(:pipeline) { create(:ci_pipeline, project: project) } let(:build) { create(:ci_build, pipeline: pipeline) } diff --git a/spec/presenters/dev_ops_score/metric_presenter_spec.rb b/spec/presenters/dev_ops_score/metric_presenter_spec.rb index b6eab3f2e74..8b7b2c88578 100644 --- a/spec/presenters/dev_ops_score/metric_presenter_spec.rb +++ b/spec/presenters/dev_ops_score/metric_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe DevOpsScore::MetricPresenter do +RSpec.describe DevOpsScore::MetricPresenter do subject { described_class.new(metric) } let(:metric) { build(:dev_ops_score_metric) } diff --git a/spec/presenters/event_presenter_spec.rb b/spec/presenters/event_presenter_spec.rb index eb94d838370..2d4872ea29e 100644 --- a/spec/presenters/event_presenter_spec.rb +++ b/spec/presenters/event_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe EventPresenter do +RSpec.describe EventPresenter do include Gitlab::Routing.url_helpers let_it_be(:group) { create(:group) } diff --git a/spec/presenters/gitlab/blame_presenter_spec.rb b/spec/presenters/gitlab/blame_presenter_spec.rb index d2a173b557c..b163926154b 100644 --- a/spec/presenters/gitlab/blame_presenter_spec.rb +++ b/spec/presenters/gitlab/blame_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::BlamePresenter do +RSpec.describe Gitlab::BlamePresenter do let(:project) { create(:project, :repository) } let(:path) { 'files/ruby/popen.rb' } let(:commit) { project.commit('master') } diff --git a/spec/presenters/group_clusterable_presenter_spec.rb b/spec/presenters/group_clusterable_presenter_spec.rb index d40ca856f7b..27360201e81 100644 --- a/spec/presenters/group_clusterable_presenter_spec.rb +++ b/spec/presenters/group_clusterable_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe GroupClusterablePresenter do +RSpec.describe GroupClusterablePresenter do include Gitlab::Routing.url_helpers let(:presenter) { described_class.new(group) } @@ -94,4 +94,10 @@ describe GroupClusterablePresenter do it { is_expected.to eq(group_cluster_path(group, cluster)) } end + + describe '#metrics_dashboard_path' do + subject { presenter.metrics_dashboard_path(cluster) } + + it { is_expected.to eq(metrics_dashboard_group_cluster_path(group, cluster)) } + end end diff --git a/spec/presenters/group_member_presenter_spec.rb b/spec/presenters/group_member_presenter_spec.rb index 382b1881ab7..6bd3005fbb6 100644 --- a/spec/presenters/group_member_presenter_spec.rb +++ b/spec/presenters/group_member_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe GroupMemberPresenter do +RSpec.describe GroupMemberPresenter do let(:user) { double(:user) } let(:group) { double(:group) } let(:group_member) { double(:group_member, source: group) } diff --git a/spec/presenters/instance_clusterable_presenter_spec.rb b/spec/presenters/instance_clusterable_presenter_spec.rb index 4265e2fcb69..6968e3a4da3 100644 --- a/spec/presenters/instance_clusterable_presenter_spec.rb +++ b/spec/presenters/instance_clusterable_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe InstanceClusterablePresenter do +RSpec.describe InstanceClusterablePresenter do include Gitlab::Routing.url_helpers let(:presenter) { described_class.new(instance) } @@ -26,4 +26,10 @@ describe InstanceClusterablePresenter do it { is_expected.to eq(clear_cache_admin_cluster_path(cluster)) } end + + describe '#metrics_dashboard_path' do + subject { presenter.metrics_dashboard_path(cluster) } + + it { is_expected.to eq(metrics_dashboard_admin_cluster_path(cluster)) } + end end diff --git a/spec/presenters/issue_presenter_spec.rb b/spec/presenters/issue_presenter_spec.rb index 4a4caef9d28..f08cd0f2026 100644 --- a/spec/presenters/issue_presenter_spec.rb +++ b/spec/presenters/issue_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe IssuePresenter do +RSpec.describe IssuePresenter do include Gitlab::Routing.url_helpers let(:user) { create(:user) } diff --git a/spec/presenters/label_presenter_spec.rb b/spec/presenters/label_presenter_spec.rb index 9578d017af5..cb6e991bd8e 100644 --- a/spec/presenters/label_presenter_spec.rb +++ b/spec/presenters/label_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe LabelPresenter do +RSpec.describe LabelPresenter do include Gitlab::Routing.url_helpers let_it_be(:group) { create(:group) } diff --git a/spec/presenters/merge_request_presenter_spec.rb b/spec/presenters/merge_request_presenter_spec.rb index f184e767f8c..f1e581efd44 100644 --- a/spec/presenters/merge_request_presenter_spec.rb +++ b/spec/presenters/merge_request_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe MergeRequestPresenter do +RSpec.describe MergeRequestPresenter do let(:resource) { create(:merge_request, source_project: project) } let(:project) { create(:project) } let(:user) { create(:user) } @@ -613,4 +613,22 @@ describe MergeRequestPresenter do end end end + + describe '#api_approvals_path' do + subject { described_class.new(resource, current_user: user).api_approvals_path } + + it { is_expected.to eq(expose_path("/api/v4/projects/#{project.id}/merge_requests/#{resource.iid}/approvals")) } + end + + describe '#api_approve_path' do + subject { described_class.new(resource, current_user: user).api_approve_path } + + it { is_expected.to eq(expose_path("/api/v4/projects/#{project.id}/merge_requests/#{resource.iid}/approve")) } + end + + describe '#api_unapprove_path' do + subject { described_class.new(resource, current_user: user).api_unapprove_path } + + it { is_expected.to eq(expose_path("/api/v4/projects/#{project.id}/merge_requests/#{resource.iid}/unapprove")) } + end end diff --git a/spec/presenters/milestone_presenter_spec.rb b/spec/presenters/milestone_presenter_spec.rb index 3d7b3ad6d78..1f23bb31fda 100644 --- a/spec/presenters/milestone_presenter_spec.rb +++ b/spec/presenters/milestone_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe MilestonePresenter do +RSpec.describe MilestonePresenter do let_it_be(:user) { create(:user) } let_it_be(:group) { create(:group) } let_it_be(:milestone) { create(:milestone, group: group) } diff --git a/spec/presenters/packages/composer/packages_presenter_spec.rb b/spec/presenters/packages/composer/packages_presenter_spec.rb new file mode 100644 index 00000000000..0445a346180 --- /dev/null +++ b/spec/presenters/packages/composer/packages_presenter_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Packages::Composer::PackagesPresenter do + using RSpec::Parameterized::TableSyntax + + let_it_be(:package_name) { 'sample-project' } + let_it_be(:json) { { 'name' => package_name } } + let_it_be(:group) { create(:group) } + let_it_be(:project) { create(:project, :custom_repo, files: { 'composer.json' => json.to_json }, group: group) } + let_it_be(:package1) { create(:composer_package, :with_metadatum, project: project, name: package_name, version: '1.0.0', json: json) } + let_it_be(:package2) { create(:composer_package, :with_metadatum, project: project, name: package_name, version: '2.0.0', json: json) } + + let(:branch) { project.repository.find_branch('master') } + + let(:packages) { [package1, package2] } + let(:presenter) { described_class.new(group, packages) } + + describe '#package_versions' do + subject { presenter.package_versions } + + def expected_json(package) + { + 'dist' => { + 'reference' => branch.target, + 'shasum' => '', + 'type' => 'zip', + 'url' => "http://localhost/api/v4/projects/#{project.id}/packages/composer/archives/#{package.name}.zip?sha=#{branch.target}" + }, + 'name' => package.name, + 'uid' => package.id, + 'version' => package.version + } + end + + it 'returns the packages json' do + packages = subject['packages'][package_name] + + expect(packages['1.0.0']).to eq(expected_json(package1)) + expect(packages['2.0.0']).to eq(expected_json(package2)) + end + end + + describe '#provider' do + subject { presenter.provider} + + let(:expected_json) do + { + 'providers' => { + package_name => { + 'sha256' => /^\h+$/ + } + } + } + end + + it 'returns the provider json' do + expect(subject).to match(expected_json) + end + end + + describe '#root' do + subject { presenter.root } + + let(:expected_json) do + { + 'packages' => [], + 'provider-includes' => { 'p/%hash%.json' => { 'sha256' => /^\h+$/ } }, + 'providers-url' => "/api/v4/group/#{group.id}/-/packages/composer/%package%.json" + } + end + + it 'returns the provider json' do + expect(subject).to match(expected_json) + end + end +end diff --git a/spec/presenters/packages/conan/package_presenter_spec.rb b/spec/presenters/packages/conan/package_presenter_spec.rb new file mode 100644 index 00000000000..3bc649c5da4 --- /dev/null +++ b/spec/presenters/packages/conan/package_presenter_spec.rb @@ -0,0 +1,181 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Packages::Conan::PackagePresenter do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:conan_package_reference) { '123456789'} + + RSpec.shared_examples 'not selecting a package with the wrong type' do + context 'with a nuget package with same name and version' do + let_it_be(:wrong_package) { create(:nuget_package, name: 'wrong', version: '1.0.0', project: project) } + + let(:recipe) { "#{wrong_package.name}/#{wrong_package.version}" } + + it { is_expected.to be_empty } + end + end + + describe '#recipe_urls' do + subject { described_class.new(recipe, user, project).recipe_urls } + + context 'no existing package' do + let(:recipe) { "my-pkg/v1.0.0/#{project.full_path}/stable" } + + it { is_expected.to be_empty } + end + + it_behaves_like 'not selecting a package with the wrong type' + + context 'existing package' do + let(:package) { create(:conan_package, project: project) } + let(:recipe) { package.conan_recipe } + + let(:expected_result) do + { + "conanfile.py" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/export/conanfile.py", + "conanmanifest.txt" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/export/conanmanifest.txt" + } + end + + it { is_expected.to eq(expected_result) } + end + end + + describe '#recipe_snapshot' do + subject { described_class.new(recipe, user, project).recipe_snapshot } + + context 'no existing package' do + let(:recipe) { "my-pkg/v1.0.0/#{project.full_path}/stable" } + + it { is_expected.to be_empty } + end + + it_behaves_like 'not selecting a package with the wrong type' + + context 'existing package' do + let(:package) { create(:conan_package, project: project) } + let(:recipe) { package.conan_recipe } + + let(:expected_result) do + { + "conanfile.py" => '12345abcde', + "conanmanifest.txt" => '12345abcde' + } + end + + it { is_expected.to eq(expected_result) } + end + end + + describe '#package_urls' do + let(:reference) { conan_package_reference } + + subject do + described_class.new( + recipe, user, project, conan_package_reference: reference + ).package_urls + end + + context 'no existing package' do + let(:recipe) { "my-pkg/v1.0.0/#{project.full_path}/stable" } + + it { is_expected.to be_empty } + end + + it_behaves_like 'not selecting a package with the wrong type' + + context 'existing package' do + let(:package) { create(:conan_package, project: project) } + let(:recipe) { package.conan_recipe } + + let(:expected_result) do + { + "conaninfo.txt" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/package/#{conan_package_reference}/0/conaninfo.txt", + "conanmanifest.txt" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/package/#{conan_package_reference}/0/conanmanifest.txt", + "conan_package.tgz" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/package/#{conan_package_reference}/0/conan_package.tgz" + } + end + + it { is_expected.to eq(expected_result) } + + context 'multiple packages with different references' do + let(:info_file) { create(:conan_package_file, :conan_package_info, package: package) } + let(:manifest_file) { create(:conan_package_file, :conan_package_manifest, package: package) } + let(:package_file) { create(:conan_package_file, :conan_package, package: package) } + let(:alternative_reference) { 'abcdefghi' } + + before do + [info_file, manifest_file, package_file].each do |file| + file.conan_file_metadatum.conan_package_reference = alternative_reference + file.save + end + end + + it { is_expected.to eq(expected_result) } + + context 'requesting the alternative reference' do + let(:reference) { alternative_reference } + + let(:expected_result) do + { + "conaninfo.txt" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/package/#{alternative_reference}/0/conaninfo.txt", + "conanmanifest.txt" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/package/#{alternative_reference}/0/conanmanifest.txt", + "conan_package.tgz" => "#{Settings.build_base_gitlab_url}/api/v4/packages/conan/v1/files/#{package.conan_recipe_path}/0/package/#{alternative_reference}/0/conan_package.tgz" + } + end + + it { is_expected.to eq(expected_result) } + end + + it 'returns empty if the reference does not exist' do + result = described_class.new( + recipe, user, project, conan_package_reference: 'doesnotexist' + ).package_urls + + expect(result).to eq({}) + end + end + end + end + + describe '#package_snapshot' do + let(:reference) { conan_package_reference } + + subject do + described_class.new( + recipe, user, project, conan_package_reference: reference + ).package_snapshot + end + + context 'no existing package' do + let(:recipe) { "my-pkg/v1.0.0/#{project.full_path}/stable" } + + it { is_expected.to be_empty } + end + + it_behaves_like 'not selecting a package with the wrong type' + + context 'existing package' do + let(:package) { create(:conan_package, project: project) } + let(:recipe) { package.conan_recipe } + + let(:expected_result) do + { + "conaninfo.txt" => '12345abcde', + "conanmanifest.txt" => '12345abcde', + "conan_package.tgz" => '12345abcde' + } + end + + it { is_expected.to eq(expected_result) } + + context 'when requested with invalid reference' do + let(:reference) { 'invalid' } + + it { is_expected.to eq({}) } + end + end + end +end diff --git a/spec/presenters/packages/detail/package_presenter_spec.rb b/spec/presenters/packages/detail/package_presenter_spec.rb new file mode 100644 index 00000000000..34582957364 --- /dev/null +++ b/spec/presenters/packages/detail/package_presenter_spec.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Packages::Detail::PackagePresenter do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project, creator: user) } + let_it_be(:package) { create(:npm_package, :with_build, project: project) } + let(:presenter) { described_class.new(package) } + + let_it_be(:user_info) { { name: user.name, avatar_url: user.avatar_url } } + let!(:expected_package_files) do + npm_file = package.package_files.first + [{ + created_at: npm_file.created_at, + download_path: npm_file.download_path, + file_name: npm_file.file_name, + size: npm_file.size + }] + end + let(:pipeline_info) do + pipeline = package.build_info.pipeline + { + created_at: pipeline.created_at, + id: pipeline.id, + sha: pipeline.sha, + ref: pipeline.ref, + git_commit_message: pipeline.git_commit_message, + user: user_info, + project: { + name: pipeline.project.name, + web_url: pipeline.project.web_url + } + } + end + let!(:dependency_links) { [] } + let!(:expected_package_details) do + { + id: package.id, + created_at: package.created_at, + name: package.name, + package_files: expected_package_files, + package_type: package.package_type, + project_id: package.project_id, + tags: package.tags.as_json, + updated_at: package.updated_at, + version: package.version, + dependency_links: dependency_links + } + end + + context 'detail_view' do + context 'with build_info' do + let_it_be(:package) { create(:npm_package, :with_build, project: project) } + let(:expected_package_details) { super().merge(pipeline: pipeline_info) } + + it 'returns details with pipeline' do + expect(presenter.detail_view).to eq expected_package_details + end + end + + context 'without build info' do + let_it_be(:package) { create(:npm_package, project: project) } + + it 'returns details without pipeline' do + expect(presenter.detail_view).to eq expected_package_details + end + end + + context 'with nuget_metadatum' do + let_it_be(:package) { create(:nuget_package, project: project) } + let_it_be(:nuget_metadatum) { create(:nuget_metadatum, package: package) } + let(:expected_package_details) { super().merge(nuget_metadatum: nuget_metadatum) } + + it 'returns nuget_metadatum' do + expect(presenter.detail_view).to eq expected_package_details + end + end + + context 'with dependency_links' do + let_it_be(:package) { create(:nuget_package, project: project) } + let_it_be(:dependency_link) { create(:packages_dependency_link, package: package) } + let_it_be(:nuget_dependency) { create(:nuget_dependency_link_metadatum, dependency_link: dependency_link) } + let_it_be(:expected_link) do + { + name: dependency_link.dependency.name, + version_pattern: dependency_link.dependency.version_pattern, + target_framework: nuget_dependency.target_framework + } + end + let_it_be(:dependency_links) { [expected_link] } + + it 'returns the correct dependency link' do + expect(presenter.detail_view).to eq expected_package_details + end + end + end +end diff --git a/spec/presenters/packages/npm/package_presenter_spec.rb b/spec/presenters/packages/npm/package_presenter_spec.rb new file mode 100644 index 00000000000..0e8cda5bafd --- /dev/null +++ b/spec/presenters/packages/npm/package_presenter_spec.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Packages::Npm::PackagePresenter do + let_it_be(:project) { create(:project) } + let_it_be(:package_name) { "@#{project.root_namespace.path}/test" } + let!(:package1) { create(:npm_package, version: '1.0.4', project: project, name: package_name) } + let!(:package2) { create(:npm_package, version: '1.0.6', project: project, name: package_name) } + let!(:latest_package) { create(:npm_package, version: '1.0.11', project: project, name: package_name) } + let(:packages) { project.packages.npm.with_name(package_name).last_of_each_version } + let(:presenter) { described_class.new(package_name, packages) } + + describe '#versions' do + subject { presenter.versions } + + context 'for packages without dependencies' do + it { is_expected.to be_a(Hash) } + it { expect(subject[package1.version]).to match_schema('public_api/v4/packages/npm_package_version') } + it { expect(subject[package2.version]).to match_schema('public_api/v4/packages/npm_package_version') } + + described_class::NPM_VALID_DEPENDENCY_TYPES.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 + end + + context 'for packages with dependencies' do + described_class::NPM_VALID_DEPENDENCY_TYPES.each do |dependency_type| + let!("package_dependency_link_for_#{dependency_type}") { create(:packages_dependency_link, package: package1, dependency_type: dependency_type) } + end + + it { is_expected.to be_a(Hash) } + it { expect(subject[package1.version]).to match_schema('public_api/v4/packages/npm_package_version') } + it { expect(subject[package2.version]).to match_schema('public_api/v4/packages/npm_package_version') } + described_class::NPM_VALID_DEPENDENCY_TYPES.each do |dependency_type| + it { expect(subject.dig(package1.version, dependency_type.to_s)).to be_any } + end + end + end + + describe '#dist_tags' do + subject { presenter.dist_tags } + + context 'for packages without tags' do + it { is_expected.to be_a(Hash) } + it { expect(subject["latest"]).to eq(latest_package.version) } + end + + context 'for packages with tags' do + let!(:package_tag1) { create(:packages_tag, package: package1, name: 'release_a') } + let!(:package_tag2) { create(:packages_tag, package: package1, name: 'test_release') } + let!(:package_tag3) { create(:packages_tag, package: package2, name: 'release_b') } + let!(:package_tag4) { create(:packages_tag, package: latest_package, name: 'release_c') } + let!(:package_tag5) { create(:packages_tag, package: latest_package, name: 'latest') } + + it { is_expected.to be_a(Hash) } + it { expect(subject[package_tag1.name]).to eq(package1.version) } + it { expect(subject[package_tag2.name]).to eq(package1.version) } + it { expect(subject[package_tag3.name]).to eq(package2.version) } + it { expect(subject[package_tag4.name]).to eq(latest_package.version) } + it { expect(subject[package_tag5.name]).to eq(latest_package.version) } + end + end +end diff --git a/spec/presenters/packages/nuget/package_metadata_presenter_spec.rb b/spec/presenters/packages/nuget/package_metadata_presenter_spec.rb new file mode 100644 index 00000000000..d5e7b23d785 --- /dev/null +++ b/spec/presenters/packages/nuget/package_metadata_presenter_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Nuget::PackageMetadataPresenter do + include_context 'with expected presenters dependency groups' + + let_it_be(:package) { create(:nuget_package, :with_metadatum) } + let_it_be(:tag1) { create(:packages_tag, name: 'tag1', package: package) } + let_it_be(:tag2) { create(:packages_tag, name: 'tag2', package: package) } + let_it_be(:presenter) { described_class.new(package) } + + describe '#json_url' do + let_it_be(:expected_suffix) { "/api/v4/projects/#{package.project_id}/packages/nuget/metadata/#{package.name}/#{package.version}.json" } + + subject { presenter.json_url } + + it { is_expected.to end_with(expected_suffix) } + end + + describe '#archive_url' do + let_it_be(:expected_suffix) { "/api/v4/projects/#{package.project_id}/packages/nuget/download/#{package.name}/#{package.version}/#{package.package_files.last.file_name}" } + + subject { presenter.archive_url } + + it { is_expected.to end_with(expected_suffix) } + end + + describe '#catalog_entry' do + subject { presenter.catalog_entry } + + before do + create_dependencies_for(package) + end + + it 'returns an entry structure' do + entry = subject + + expect(entry).to be_a Hash + %i[json_url archive_url].each { |field| expect(entry[field]).not_to be_blank } + %i[authors summary].each { |field| expect(entry[field]).to be_blank } + expect(entry[:dependency_groups]).to eq expected_dependency_groups(package.project_id, package.name, package.version) + expect(entry[:package_name]).to eq package.name + expect(entry[:package_version]).to eq package.version + expect(entry[:tags].split(::Packages::Tag::NUGET_TAGS_SEPARATOR)).to contain_exactly('tag1', 'tag2') + + %i[project_url license_url icon_url].each do |field| + expect(entry.dig(:metadatum, field)).to eq(package.nuget_metadatum.send(field)) + end + end + end +end diff --git a/spec/presenters/packages/nuget/packages_metadata_presenter_spec.rb b/spec/presenters/packages/nuget/packages_metadata_presenter_spec.rb new file mode 100644 index 00000000000..b2bcdf8f03d --- /dev/null +++ b/spec/presenters/packages/nuget/packages_metadata_presenter_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Nuget::PackagesMetadataPresenter do + include_context 'with expected presenters dependency groups' + + let_it_be(:project) { create(:project) } + let_it_be(:packages) { create_list(:nuget_package, 5, :with_metadatum, name: 'Dummy.Package', project: project) } + let_it_be(:presenter) { described_class.new(packages) } + + describe '#count' do + subject { presenter.count } + + it {is_expected.to eq 1} + end + + describe '#items' do + let(:tag_names) { %w(tag1 tag2) } + + subject { presenter.items } + + before do + packages.each do |pkg| + tag_names.each { |tag| create(:packages_tag, package: pkg, name: tag) } + + create_dependencies_for(pkg) + end + end + + it 'returns an array' do + items = subject + + expect(items).to be_a Array + expect(items.size).to eq 1 + end + + it 'returns a summary structure' do + item = subject.first + + expect(item).to be_a Hash + %i[json_url lower_version upper_version].each { |field| expect(item[field]).not_to be_blank } + expect(item[:packages_count]).to eq packages.count + expect(item[:packages]).to be_a Array + expect(item[:packages].size).to eq packages.count + end + + it 'returns the catalog entries' do + item = subject.first + + item[:packages].each do |pkg| + expect(pkg).to be_a Hash + %i[json_url archive_url catalog_entry].each { |field| expect(pkg[field]).not_to be_blank } + catalog_entry = pkg[:catalog_entry] + %i[json_url archive_url package_name package_version].each { |field| expect(catalog_entry[field]).not_to be_blank } + %i[authors summary].each { |field| expect(catalog_entry[field]).to be_blank } + expect(catalog_entry[:dependency_groups]).to eq(expected_dependency_groups(project.id, catalog_entry[:package_name], catalog_entry[:package_version])) + expect(catalog_entry[:tags].split(::Packages::Tag::NUGET_TAGS_SEPARATOR)).to contain_exactly('tag1', 'tag2') + + %i[project_url license_url icon_url].each do |field| + expect(catalog_entry.dig(:metadatum, field)).not_to be_blank + end + end + end + end +end diff --git a/spec/presenters/packages/nuget/packages_versions_presenter_spec.rb b/spec/presenters/packages/nuget/packages_versions_presenter_spec.rb new file mode 100644 index 00000000000..36aa28243a4 --- /dev/null +++ b/spec/presenters/packages/nuget/packages_versions_presenter_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Nuget::PackagesVersionsPresenter do + let_it_be(:packages) { create_list(:nuget_package, 5) } + let_it_be(:presenter) { described_class.new(::Packages::Package.all) } + + describe '#versions' do + subject { presenter.versions } + + it { is_expected.to match_array(packages.map(&:version).sort) } + end +end diff --git a/spec/presenters/packages/nuget/search_results_presenter_spec.rb b/spec/presenters/packages/nuget/search_results_presenter_spec.rb new file mode 100644 index 00000000000..29ec8579dc1 --- /dev/null +++ b/spec/presenters/packages/nuget/search_results_presenter_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Packages::Nuget::SearchResultsPresenter do + let_it_be(:project) { create(:project) } + let_it_be(:package_a) { create(:nuget_package, :with_metadatum, project: project, name: 'DummyPackageA') } + let_it_be(:tag1) { create(:packages_tag, package: package_a, name: 'tag1') } + let_it_be(:tag2) { create(:packages_tag, package: package_a, name: 'tag2') } + let_it_be(:packages_b) { create_list(:nuget_package, 5, project: project, name: 'DummyPackageB') } + let_it_be(:packages_c) { create_list(:nuget_package, 5, project: project, name: 'DummyPackageC') } + let_it_be(:search_results) { OpenStruct.new(total_count: 3, results: [package_a, packages_b, packages_c].flatten) } + let_it_be(:presenter) { described_class.new(search_results) } + let(:total_count) { presenter.total_count } + let(:data) { presenter.data } + + describe '#total_count' do + it 'expects to have 3 total elements' do + expect(total_count).to eq(3) + end + end + + describe '#data' do + it 'returns the proper data structure' do + expect(data.size).to eq 3 + pkg_a, pkg_b, pkg_c = data + expect_package_result(pkg_a, package_a.name, [package_a.version], %w(tag1 tag2), with_metadatum: true) + expect_package_result(pkg_b, packages_b.first.name, packages_b.map(&:version)) + expect_package_result(pkg_c, packages_c.first.name, packages_c.map(&:version)) + end + + # rubocop:disable Metrics/AbcSize + def expect_package_result(package_json, name, versions, tags = [], with_metadatum: false) + expect(package_json[:type]).to eq 'Package' + expect(package_json[:authors]).to be_blank + expect(package_json[:name]).to eq(name) + expect(package_json[:summary]).to be_blank + expect(package_json[:total_downloads]).to eq 0 + expect(package_json[:verified]).to be + expect(package_json[:version]).to eq VersionSorter.sort(versions).last # rubocop: disable Style/UnneededSort + versions.zip(package_json[:versions]).each do |version, version_json| + expect(version_json[:json_url]).to end_with("#{version}.json") + expect(version_json[:downloads]).to eq 0 + expect(version_json[:version]).to eq version + end + + if tags.any? + expect(package_json[:tags].split(::Packages::Tag::NUGET_TAGS_SEPARATOR)).to contain_exactly(*tags) + else + expect(package_json[:tags]).to be_blank + end + + %i[project_url license_url icon_url].each do |field| + expect(package_json.dig(:metadatum, field)).to with_metadatum ? be_present : be_blank + end + end + # rubocop:enable Metrics/AbcSize + end +end diff --git a/spec/presenters/packages/nuget/service_index_presenter_spec.rb b/spec/presenters/packages/nuget/service_index_presenter_spec.rb new file mode 100644 index 00000000000..19ef890e19f --- /dev/null +++ b/spec/presenters/packages/nuget/service_index_presenter_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Packages::Nuget::ServiceIndexPresenter do + let_it_be(:project) { create(:project) } + let_it_be(:presenter) { described_class.new(project) } + + describe '#version' do + subject { presenter.version } + + it { is_expected.to eq '3.0.0' } + end + + describe '#resources' do + subject { presenter.resources } + + it 'has valid resources' do + expect(subject.size).to eq 8 + subject.each do |resource| + %i[@id @type comment].each do |field| + expect(resource).to have_key(field) + expect(resource[field]).to be_a(String) + end + end + end + end +end diff --git a/spec/presenters/packages/pypi/package_presenter_spec.rb b/spec/presenters/packages/pypi/package_presenter_spec.rb new file mode 100644 index 00000000000..e4d234a4688 --- /dev/null +++ b/spec/presenters/packages/pypi/package_presenter_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Packages::Pypi::PackagePresenter do + using RSpec::Parameterized::TableSyntax + + let_it_be(:project) { create(:project) } + let_it_be(:package_name) { 'sample-project' } + let_it_be(:package1) { create(:pypi_package, project: project, name: package_name, version: '1.0.0') } + let_it_be(:package2) { create(:pypi_package, project: project, name: package_name, version: '2.0.0') } + + let(:packages) { [package1, package2] } + let(:presenter) { described_class.new(packages, project) } + + describe '#body' do + subject { presenter.body} + + shared_examples_for "pypi package presenter" do + let(:file) { package.package_files.first } + let(:filename) { file.file_name } + let(:expected_file) { "<a href=\"http://localhost/api/v4/projects/#{project.id}/packages/pypi/files/#{file.file_sha256}/#{filename}#sha256=#{file.file_sha256}\" data-requires-python=\"#{expected_python_version}\">#{filename}</a><br>" } + + before do + package.pypi_metadatum.required_python = python_version + end + + it { is_expected.to include expected_file } + end + + it_behaves_like "pypi package presenter" do + let(:python_version) { '>=2.7' } + let(:expected_python_version) { '>=2.7' } + let(:package) { package1 } + end + + it_behaves_like "pypi package presenter" do + let(:python_version) { '"><script>alert(1)</script>' } + let(:expected_python_version) { '"><script>alert(1)</script>' } + let(:package) { package1 } + end + + it_behaves_like "pypi package presenter" do + let(:python_version) { '>=2.7, !=3.0' } + let(:expected_python_version) { '>=2.7, !=3.0' } + let(:package) { package2 } + end + end +end diff --git a/spec/presenters/pages_domain_presenter_spec.rb b/spec/presenters/pages_domain_presenter_spec.rb index 30ce59b7bfb..731279ce5b9 100644 --- a/spec/presenters/pages_domain_presenter_spec.rb +++ b/spec/presenters/pages_domain_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe PagesDomainPresenter do +RSpec.describe PagesDomainPresenter do using RSpec::Parameterized::TableSyntax include LetsEncryptHelpers diff --git a/spec/presenters/project_clusterable_presenter_spec.rb b/spec/presenters/project_clusterable_presenter_spec.rb index b3dad4abde5..b518c63f0ca 100644 --- a/spec/presenters/project_clusterable_presenter_spec.rb +++ b/spec/presenters/project_clusterable_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ProjectClusterablePresenter do +RSpec.describe ProjectClusterablePresenter do include Gitlab::Routing.url_helpers let(:presenter) { described_class.new(project) } @@ -94,4 +94,10 @@ describe ProjectClusterablePresenter do it { is_expected.to eq(project_cluster_path(project, cluster)) } end + + describe '#metrics_dashboard_path' do + subject { presenter.metrics_dashboard_path(cluster) } + + it { is_expected.to eq(metrics_dashboard_project_cluster_path(project, cluster)) } + end end diff --git a/spec/presenters/project_hook_presenter_spec.rb b/spec/presenters/project_hook_presenter_spec.rb index 773e8ccf51e..061ec38ae34 100644 --- a/spec/presenters/project_hook_presenter_spec.rb +++ b/spec/presenters/project_hook_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ProjectHookPresenter do +RSpec.describe ProjectHookPresenter do let(:web_hook_log) { create(:web_hook_log) } let(:project) { web_hook_log.web_hook.project } let(:web_hook) { web_hook_log.web_hook } diff --git a/spec/presenters/project_member_presenter_spec.rb b/spec/presenters/project_member_presenter_spec.rb index 743c89fc7c2..ad45a23c183 100644 --- a/spec/presenters/project_member_presenter_spec.rb +++ b/spec/presenters/project_member_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ProjectMemberPresenter do +RSpec.describe ProjectMemberPresenter do let(:user) { double(:user) } let(:project) { double(:project) } let(:project_member) { double(:project_member, source: project) } diff --git a/spec/presenters/project_presenter_spec.rb b/spec/presenters/project_presenter_spec.rb index 65ae85ea78f..eb1ff628d14 100644 --- a/spec/presenters/project_presenter_spec.rb +++ b/spec/presenters/project_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ProjectPresenter do +RSpec.describe ProjectPresenter do let(:user) { create(:user) } let(:project) { create(:project) } let(:presenter) { described_class.new(project, current_user: user) } diff --git a/spec/presenters/projects/import_export/project_export_presenter_spec.rb b/spec/presenters/projects/import_export/project_export_presenter_spec.rb index 052ca36974a..8463d01d95b 100644 --- a/spec/presenters/projects/import_export/project_export_presenter_spec.rb +++ b/spec/presenters/projects/import_export/project_export_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Projects::ImportExport::ProjectExportPresenter do +RSpec.describe Projects::ImportExport::ProjectExportPresenter do let_it_be(:group) { create(:group) } let_it_be(:project) { create(:project, group: group) } let_it_be(:user) { create(:user) } diff --git a/spec/presenters/projects/prometheus/alert_presenter_spec.rb b/spec/presenters/projects/prometheus/alert_presenter_spec.rb index 8ee5a4d7b3f..89c5438b074 100644 --- a/spec/presenters/projects/prometheus/alert_presenter_spec.rb +++ b/spec/presenters/projects/prometheus/alert_presenter_spec.rb @@ -2,7 +2,9 @@ require 'spec_helper' -describe Projects::Prometheus::AlertPresenter do +RSpec.describe Projects::Prometheus::AlertPresenter do + include Gitlab::Routing.url_helpers + let_it_be(:project, reload: true) { create(:project) } let(:presenter) { described_class.new(alert) } @@ -14,7 +16,7 @@ describe Projects::Prometheus::AlertPresenter do let(:metric_id) { gitlab_alert.prometheus_metric_id } let(:alert) do - create(:alerting_alert, project: project, metric_id: metric_id) + create(:alerting_alert, project: project, metric_id: metric_id, payload: payload) end end @@ -171,7 +173,7 @@ describe Projects::Prometheus::AlertPresenter do **Start time:** #{presenter.start_time}#{markdown_line_break} **full_query:** `avg(metric) > 1.0` - [](#{url}) + [](#{presenter.metrics_dashboard_url}) MARKDOWN end @@ -180,68 +182,30 @@ describe Projects::Prometheus::AlertPresenter do Timecop.freeze(starts_at) { example.run } end + before do + payload.delete('startsAt') + end + it { is_expected.to eq(expected_markdown) } end context 'with a starting time available' do - before do - payload['startsAt'] = starts_at - end - it { is_expected.to eq(expected_markdown) } end end context 'for gitlab-managed prometheus alerts' do - let(:gitlab_alert) { create(:prometheus_alert, project: project) } - let(:metric_id) { gitlab_alert.prometheus_metric_id } - let(:env_id) { gitlab_alert.environment_id } + include_context 'gitlab-managed prometheus alert attributes' - before do - payload['labels'] = { 'gitlab_alert_id' => metric_id } + let(:alert) do + create(:alerting_alert, project: project, metric_id: prometheus_metric_id, payload: payload) end - let(:url) { "http://localhost/#{project.full_path}/prometheus/alerts/#{metric_id}/metrics_dashboard?end=2018-03-12T09%3A36%3A00Z&environment_id=#{env_id}&start=2018-03-12T08%3A36%3A00Z" } - it_behaves_like 'markdown with metrics embed' end context 'for alerts from a self-managed prometheus' do - let!(:environment) { create(:environment, project: project, name: 'production') } - let(:url) { "http://localhost/#{project.full_path}/-/environments/#{environment.id}/metrics_dashboard?embed_json=#{CGI.escape(embed_content.to_json)}&end=2018-03-12T09%3A36%3A00Z&start=2018-03-12T08%3A36%3A00Z" } - - let(:title) { 'title' } - let(:y_label) { 'y_label' } - let(:query) { 'avg(metric) > 1.0' } - let(:embed_content) do - { - panel_groups: [{ - panels: [{ - type: 'line-graph', - title: title, - y_label: y_label, - metrics: [{ query_range: query }] - }] - }] - } - end - - before do - # Setup embed time range - payload['startsAt'] = starts_at - - # Setup query - payload['generatorURL'] = "http://host?g0.expr=#{CGI.escape(query)}" - - # Setup environment - payload['labels'] ||= {} - payload['labels']['gitlab_environment_name'] = 'production' - - # Setup chart title & axis labels - payload['annotations'] ||= {} - payload['annotations']['title'] = 'title' - payload['annotations']['gitlab_y_label'] = 'y_label' - end + include_context 'self-managed prometheus alert attributes' it_behaves_like 'markdown with metrics embed' @@ -359,10 +323,7 @@ describe Projects::Prometheus::AlertPresenter do end describe '#performance_dashboard_link' do - let(:expected_link) do - Gitlab::Routing.url_helpers - .metrics_project_environment_url(project, alert.environment) - end + let(:expected_link) { metrics_project_environment_url(project, alert.environment) } subject { presenter.performance_dashboard_link } @@ -370,10 +331,7 @@ describe Projects::Prometheus::AlertPresenter do end describe '#incident_issues_link' do - let(:expected_link) do - Gitlab::Routing.url_helpers - .project_issues_url(project, label_name: described_class::INCIDENT_LABEL_NAME) - end + let(:expected_link) { project_issues_url(project, label_name: described_class::INCIDENT_LABEL_NAME) } subject { presenter.incident_issues_link } @@ -413,13 +371,35 @@ describe Projects::Prometheus::AlertPresenter do end describe '#performance_dashboard_link' do - let(:expected_link) do - Gitlab::Routing.url_helpers.metrics_project_environments_url(project) - end + let(:expected_link) { metrics_project_environments_url(project) } subject { presenter.performance_dashboard_link } it { is_expected.to eq(expected_link) } end end + + describe '#metrics_dashboard_url' do + subject { presenter.metrics_dashboard_url } + + context 'for a non-prometheus alert' do + it { is_expected.to be_nil } + end + + context 'for a self-managed prometheus alert' do + include_context 'self-managed prometheus alert attributes' + + let(:prometheus_payload) { payload } + + it { is_expected.to eq(dashboard_url_for_alert) } + end + + context 'for a gitlab-managed prometheus alert' do + include_context 'gitlab-managed prometheus alert attributes' + + let(:prometheus_payload) { payload } + + it { is_expected.to eq(dashboard_url_for_alert) } + end + end end diff --git a/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb b/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb index b9cb60e414f..7a679a03b53 100644 --- a/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb +++ b/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Projects::Settings::DeployKeysPresenter do +RSpec.describe Projects::Settings::DeployKeysPresenter do let(:project) { create(:project) } let(:user) { create(:user) } diff --git a/spec/presenters/release_presenter_spec.rb b/spec/presenters/release_presenter_spec.rb index d1f023b8760..5577b3ad2e8 100644 --- a/spec/presenters/release_presenter_spec.rb +++ b/spec/presenters/release_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ReleasePresenter do +RSpec.describe ReleasePresenter do include Gitlab::Routing.url_helpers let_it_be(:project) { create(:project, :repository) } @@ -112,4 +112,36 @@ describe ReleasePresenter do it { is_expected.to be_nil } end end + + describe '#assets_count' do + subject { presenter.assets_count } + + it 'returns the number of assets associated to the release' do + is_expected.to be release.assets_count + end + + context 'when a user is not allowed to download release sources' do + let(:presenter) { described_class.new(release, current_user: guest) } + + it 'returns the number of all non-source assets associated to the release' do + is_expected.to be release.assets_count(except: [:sources]) + end + end + end + + describe '#name' do + subject { presenter.name } + + 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 diff --git a/spec/presenters/sentry_error_presenter_spec.rb b/spec/presenters/sentry_error_presenter_spec.rb index 5f3f1d33b86..af9e7c8a2b2 100644 --- a/spec/presenters/sentry_error_presenter_spec.rb +++ b/spec/presenters/sentry_error_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe SentryErrorPresenter do +RSpec.describe SentryErrorPresenter do let(:error) { build(:detailed_error_tracking_error) } let(:presenter) { described_class.new(error) } diff --git a/spec/presenters/service_hook_presenter_spec.rb b/spec/presenters/service_hook_presenter_spec.rb index bea57768e3e..adef34a882b 100644 --- a/spec/presenters/service_hook_presenter_spec.rb +++ b/spec/presenters/service_hook_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ServiceHookPresenter do +RSpec.describe ServiceHookPresenter do let(:web_hook_log) { create(:web_hook_log, web_hook: service_hook) } let(:service_hook) { create(:service_hook, service: service) } let(:service) { create(:drone_ci_service, project: project) } diff --git a/spec/presenters/snippet_blob_presenter_spec.rb b/spec/presenters/snippet_blob_presenter_spec.rb index eb7621cc591..7464c0ac15b 100644 --- a/spec/presenters/snippet_blob_presenter_spec.rb +++ b/spec/presenters/snippet_blob_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe SnippetBlobPresenter do +RSpec.describe SnippetBlobPresenter do describe '#rich_data' do before do allow_next_instance_of(described_class) do |instance| @@ -109,22 +109,38 @@ describe SnippetBlobPresenter do end describe '#raw_path' do - subject { described_class.new(snippet.blob).raw_path } + let_it_be(:project) { create(:project) } + let_it_be(:user) { create(:user) } + let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user) } + let_it_be(:project_snippet) { create(:project_snippet, :repository, project: project, author: user) } - context 'with ProjectSnippet' do - let!(:project) { create(:project) } - let(:snippet) { create(:project_snippet, project: project) } + before do + project.add_developer(user) + end + + subject { described_class.new(snippet.blobs.first, current_user: user).raw_path } - it 'returns the raw path' do - expect(subject).to eq "/#{snippet.project.full_path}/snippets/#{snippet.id}/raw" + it_behaves_like 'snippet blob raw path' + + context 'with snippet_multiple_files feature disabled' do + before do + stub_feature_flags(snippet_multiple_files: false) end - end - context 'with PersonalSnippet' do - let(:snippet) { create(:personal_snippet) } + context 'with ProjectSnippet' do + let(:snippet) { project_snippet } - it 'returns the raw path' do - expect(subject).to eq "/snippets/#{snippet.id}/raw" + it 'returns the raw path' do + expect(subject).to eq "/#{snippet.project.full_path}/snippets/#{snippet.id}/raw" + end + end + + context 'with PersonalSnippet' do + let(:snippet) { personal_snippet } + + it 'returns the raw path' do + expect(subject).to eq "/snippets/#{snippet.id}/raw" + end end end end diff --git a/spec/presenters/snippet_presenter_spec.rb b/spec/presenters/snippet_presenter_spec.rb index 423e9edc219..98c291bdd02 100644 --- a/spec/presenters/snippet_presenter_spec.rb +++ b/spec/presenters/snippet_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe SnippetPresenter do +RSpec.describe SnippetPresenter do include Gitlab::Routing.url_helpers let_it_be(:user) { create(:user) } diff --git a/spec/presenters/tree_entry_presenter_spec.rb b/spec/presenters/tree_entry_presenter_spec.rb index 0c29fe3e5ff..d29a7a6ab04 100644 --- a/spec/presenters/tree_entry_presenter_spec.rb +++ b/spec/presenters/tree_entry_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe TreeEntryPresenter do +RSpec.describe TreeEntryPresenter do include Gitlab::Routing.url_helpers let(:project) { create(:project, :repository) } diff --git a/spec/presenters/web_hook_log_presenter_spec.rb b/spec/presenters/web_hook_log_presenter_spec.rb index 8812a0ba594..68c8c6e2a1b 100644 --- a/spec/presenters/web_hook_log_presenter_spec.rb +++ b/spec/presenters/web_hook_log_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe WebHookLogPresenter do +RSpec.describe WebHookLogPresenter do include Gitlab::Routing.url_helpers describe '#details_path' do |