2019-08-05 16:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe ContainerRepositoriesFinder do
|
2019-11-04 10:07:36 -05:00
|
|
|
let_it_be(:reporter) { create(:user) }
|
|
|
|
let_it_be(:guest) { create(:user) }
|
|
|
|
|
2020-04-27 20:09:33 -04:00
|
|
|
let_it_be(:group) { create(:group) }
|
2021-07-15 08:09:01 -04:00
|
|
|
let_it_be(:project) { create(:project, :public, group: group) }
|
2020-04-27 20:09:33 -04:00
|
|
|
let_it_be(:project_repository) { create(:container_repository, name: 'my_image', project: project) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2020-04-27 20:09:33 -04:00
|
|
|
let(:params) { {} }
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
before do
|
2021-07-15 08:09:01 -04:00
|
|
|
project.project_feature.update!(container_registry_access_level: ProjectFeature::PRIVATE)
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
group.add_reporter(reporter)
|
|
|
|
project.add_reporter(reporter)
|
|
|
|
end
|
|
|
|
|
2020-04-27 20:09:33 -04:00
|
|
|
shared_examples 'with name search' do
|
|
|
|
let_it_be(:not_searched_repository) do
|
|
|
|
create(:container_repository, name: 'foo_bar_baz', project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
%w[my_image my_imag _image _imag].each do |name|
|
|
|
|
context "with name set to #{name}" do
|
|
|
|
let(:params) { { name: name } }
|
|
|
|
|
2022-08-26 14:12:09 -04:00
|
|
|
it { is_expected.to contain_exactly(project_repository) }
|
2020-04-27 20:09:33 -04:00
|
|
|
|
2022-08-26 14:12:09 -04:00
|
|
|
it { is_expected.not_to include(not_searched_repository) }
|
2020-04-27 20:09:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-09 13:09:59 -05:00
|
|
|
shared_examples 'with sorting' do
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, group: group) }
|
|
|
|
let_it_be(:sort_repository) do
|
|
|
|
create(:container_repository, name: 'bar', project: project, created_at: 1.day.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:sort_repository2) do
|
|
|
|
create(:container_repository, name: 'foo', project: project, created_at: 1.hour.ago, updated_at: 1.hour.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
[:created_desc, :updated_asc, :name_desc].each do |order|
|
|
|
|
context "with sort set to #{order}" do
|
|
|
|
let(:params) { { sort: order } }
|
|
|
|
|
2022-08-26 14:12:09 -04:00
|
|
|
it { is_expected.to eq([sort_repository2, sort_repository]) }
|
2021-02-09 13:09:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[:created_asc, :updated_desc, :name_asc].each do |order|
|
|
|
|
context "with sort set to #{order}" do
|
|
|
|
let(:params) { { sort: order } }
|
|
|
|
|
2022-08-26 14:12:09 -04:00
|
|
|
it { is_expected.to eq([sort_repository, sort_repository2]) }
|
2021-02-09 13:09:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-05 16:00:50 -04:00
|
|
|
describe '#execute' do
|
2019-11-04 10:07:36 -05:00
|
|
|
context 'with authorized user' do
|
2020-04-27 20:09:33 -04:00
|
|
|
subject { described_class.new(user: reporter, subject: subject_object, params: params).execute }
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
context 'when subject_type is group' do
|
|
|
|
let(:subject_object) { group }
|
|
|
|
let(:other_project) { create(:project, group: group) }
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
let(:other_repository) do
|
|
|
|
create(:container_repository, name: 'test_repository2', project: other_project)
|
|
|
|
end
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
it { is_expected.to match_array([project_repository, other_repository]) }
|
2020-04-27 20:09:33 -04:00
|
|
|
|
|
|
|
it_behaves_like 'with name search'
|
2021-02-09 13:09:59 -05:00
|
|
|
it_behaves_like 'with sorting'
|
2021-07-15 08:09:01 -04:00
|
|
|
|
|
|
|
context 'when project has container registry disabled' do
|
|
|
|
before do
|
|
|
|
project.project_feature.update!(container_registry_access_level: ProjectFeature::DISABLED)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to match_array([other_repository]) }
|
|
|
|
end
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
context 'when subject_type is project' do
|
|
|
|
let(:subject_object) { project }
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
it { is_expected.to match_array([project_repository]) }
|
2020-04-27 20:09:33 -04:00
|
|
|
|
|
|
|
it_behaves_like 'with name search'
|
2021-02-09 13:09:59 -05:00
|
|
|
it_behaves_like 'with sorting'
|
2021-07-15 08:09:01 -04:00
|
|
|
|
|
|
|
context 'when project has container registry disabled' do
|
|
|
|
before do
|
|
|
|
project.project_feature.update!(container_registry_access_level: ProjectFeature::DISABLED)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
2019-11-04 10:07:36 -05:00
|
|
|
end
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
context 'with invalid subject_type' do
|
|
|
|
let(:subject_object) { "invalid type" }
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
it { expect { subject }.to raise_exception('invalid subject_type') }
|
|
|
|
end
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
context 'with unauthorized user' do
|
2021-07-15 08:09:01 -04:00
|
|
|
subject { described_class.new(user: guest, subject: subject_type).execute }
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2021-07-15 08:09:01 -04:00
|
|
|
context 'when subject_type is group' do
|
|
|
|
let(:subject_type) { group }
|
|
|
|
|
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when subject_type is project' do
|
|
|
|
let(:subject_type) { project }
|
|
|
|
|
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|