debian-mirror-gitlab/spec/helpers/packages_helper_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

256 lines
8 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe PackagesHelper, feature_category: :package_registry do
2021-09-04 01:27:46 +05:30
using RSpec::Parameterized::TableSyntax
2023-06-20 00:43:36 +05:30
include AdminModeHelper
2021-09-04 01:27:46 +05:30
let_it_be_with_reload(:project) { create(:project) }
2020-10-24 23:57:45 +05:30
let_it_be(:base_url) { "#{Gitlab.config.gitlab.url}/api/v4/" }
2021-09-04 01:27:46 +05:30
describe '#package_registry_instance_url' do
it 'returns conan instance url when registry_type is conant' do
2020-10-24 23:57:45 +05:30
url = helper.package_registry_instance_url(:conan)
expect(url).to eq("#{base_url}packages/conan")
end
it 'returns npm instance url when registry_type is npm' do
url = helper.package_registry_instance_url(:npm)
expect(url).to eq("#{base_url}packages/npm")
end
end
2021-09-04 01:27:46 +05:30
describe '#package_registry_project_url' do
2020-10-24 23:57:45 +05:30
it 'returns maven registry url when registry_type is not provided' do
url = helper.package_registry_project_url(1)
expect(url).to eq("#{base_url}projects/1/packages/maven")
end
it 'returns specified registry url when registry_type is provided' do
url = helper.package_registry_project_url(1, :npm)
expect(url).to eq("#{base_url}projects/1/packages/npm")
end
end
2021-09-04 01:27:46 +05:30
describe '#pypi_registry_url' do
2020-10-24 23:57:45 +05:30
let_it_be(:base_url_with_token) { base_url.sub('://', '://__token__:<your_personal_token>@') }
2023-05-27 22:25:52 +05:30
let_it_be(:public_project) { create(:project, :public) }
2020-10-24 23:57:45 +05:30
2023-05-27 22:25:52 +05:30
it 'returns the pypi registry url with token when project is private' do
url = helper.pypi_registry_url(project)
2020-10-24 23:57:45 +05:30
2023-05-27 22:25:52 +05:30
expect(url).to eq("#{base_url_with_token}projects/#{project.id}/packages/pypi/simple")
end
it 'returns the pypi registry url without token when project is public' do
url = helper.pypi_registry_url(public_project)
expect(url).to eq("#{base_url}projects/#{public_project.id}/packages/pypi/simple")
2020-10-24 23:57:45 +05:30
end
end
2021-09-04 01:27:46 +05:30
describe '#composer_registry_url' do
2020-10-24 23:57:45 +05:30
it 'return the composer registry url' do
url = helper.composer_registry_url(1)
expect(url).to eq("#{base_url}group/1/-/packages/composer/packages.json")
end
end
2021-09-04 01:27:46 +05:30
describe '#composer_config_repository_name' do
2021-01-03 14:25:43 +05:30
let(:host) { Gitlab.config.gitlab.host }
let(:group_id) { 1 }
2020-10-24 23:57:45 +05:30
2021-01-03 14:25:43 +05:30
it 'return global unique composer registry id' do
id = helper.composer_config_repository_name(group_id)
2020-10-24 23:57:45 +05:30
2021-01-03 14:25:43 +05:30
expect(id).to eq("#{host}/#{group_id}")
2020-10-24 23:57:45 +05:30
end
end
2021-09-04 01:27:46 +05:30
2022-06-21 17:19:12 +05:30
describe '#show_cleanup_policy_link' do
2021-09-30 23:02:18 +05:30
let_it_be(:user) { create(:user) }
2021-09-04 01:27:46 +05:30
let_it_be_with_reload(:container_repository) { create(:container_repository) }
2022-06-21 17:19:12 +05:30
subject { helper.show_cleanup_policy_link(project.reload) }
2021-09-04 01:27:46 +05:30
2022-05-07 20:08:51 +05:30
where(:com, :config_registry, :project_registry, :nil_policy, :container_repositories_exist, :expected_result) do
false | false | false | false | false | false
false | false | false | false | true | false
false | false | false | true | false | false
false | false | false | true | true | false
false | false | true | false | false | false
false | false | true | false | true | false
false | false | true | true | false | false
false | false | true | true | true | false
false | true | false | false | false | false
false | true | false | false | true | false
false | true | false | true | false | false
false | true | false | true | true | false
false | true | true | false | false | false
false | true | true | false | true | false
false | true | true | true | false | false
false | true | true | true | true | false
true | false | false | false | false | false
true | false | false | false | true | false
true | false | false | true | false | false
true | false | false | true | true | false
true | false | true | false | false | false
true | false | true | false | true | false
true | false | true | true | false | false
true | false | true | true | true | false
true | true | false | false | false | false
true | true | false | false | true | false
true | true | false | true | false | false
true | true | false | true | true | false
true | true | true | false | false | false
true | true | true | false | true | false
true | true | true | true | false | false
true | true | true | true | true | true
2021-09-04 01:27:46 +05:30
end
with_them do
before do
2021-09-30 23:02:18 +05:30
allow(helper).to receive(:current_user).and_return(user)
2021-09-04 01:27:46 +05:30
allow(Gitlab).to receive(:com?).and_return(com)
stub_config(registry: { enabled: config_registry })
2021-09-30 23:02:18 +05:30
allow(project).to receive(:feature_available?).with(:container_registry, user).and_return(project_registry)
2021-09-04 01:27:46 +05:30
project.container_expiration_policy.destroy! if nil_policy
container_repository.update!(project_id: project.id) if container_repositories_exist
end
it { is_expected.to eq(expected_result) }
end
end
2023-06-20 00:43:36 +05:30
2023-07-09 08:55:56 +05:30
describe '#show_container_registry_settings' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:admin) { create(:admin) }
before do
allow(helper).to receive(:current_user) { user }
end
subject { helper.show_container_registry_settings(project) }
context 'with container registry config enabled' do
before do
stub_config(registry: { enabled: true })
end
context 'when user has permission' do
before do
allow(Ability).to receive(:allowed?).with(user, :admin_container_image, project).and_return(true)
end
it { is_expected.to be(true) }
end
context 'when user does not have permission' do
before do
allow(Ability).to receive(:allowed?).with(user, :admin_container_image, project).and_return(false)
end
it { is_expected.to be(false) }
end
end
context 'with container registry config disabled' do
before do
stub_config(registry: { enabled: false })
end
context 'when user has permission' do
before do
allow(Ability).to receive(:allowed?).with(user, :admin_container_image, project).and_return(true)
end
it { is_expected.to be(false) }
end
context 'when user does not have permission' do
before do
allow(Ability).to receive(:allowed?).with(user, :admin_container_image, project).and_return(false)
end
it { is_expected.to be(false) }
end
end
end
2023-06-20 00:43:36 +05:30
describe '#show_group_package_registry_settings' do
let_it_be(:group) { create(:group) }
let_it_be(:user) { create(:user) }
let_it_be(:admin) { create(:admin) }
before do
allow(helper).to receive(:current_user) { user }
end
subject { helper.show_group_package_registry_settings(group) }
context 'with package registry config enabled' do
before do
stub_config(packages: { enabled: true })
end
context "with admin", :enable_admin_mode do
before do
allow(helper).to receive(:current_user) { admin }
end
it { is_expected.to be(true) }
end
context "with owner" do
before do
group.add_owner(user)
end
it { is_expected.to be(true) }
end
%i[maintainer developer reporter guest].each do |role|
context "with #{role}" do
before do
group.public_send("add_#{role}", user)
end
it { is_expected.to be(false) }
end
end
end
context 'with package registry config disabled' do
before do
stub_config(packages: { enabled: false })
end
context "with admin", :enable_admin_mode do
before do
allow(helper).to receive(:current_user) { admin }
end
it { is_expected.to be(false) }
end
%i[owner maintainer developer reporter guest].each do |role|
context "with #{role}" do
before do
group.public_send("add_#{role}", user)
end
it { is_expected.to be(false) }
end
end
end
end
2020-10-24 23:57:45 +05:30
end