diff options
Diffstat (limited to 'spec/presenters/packages/nuget')
5 files changed, 219 insertions, 0 deletions
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 |