diff options
Diffstat (limited to 'spec/lib/api')
-rw-r--r-- | spec/lib/api/entities/branch_spec.rb | 28 | ||||
-rw-r--r-- | spec/lib/api/entities/design_management/design_spec.rb | 19 | ||||
-rw-r--r-- | spec/lib/api/entities/project_repository_storage_move_spec.rb | 21 | ||||
-rw-r--r-- | spec/lib/api/entities/snippet_spec.rb | 94 | ||||
-rw-r--r-- | spec/lib/api/helpers/pagination_strategies_spec.rb | 77 |
5 files changed, 231 insertions, 8 deletions
diff --git a/spec/lib/api/entities/branch_spec.rb b/spec/lib/api/entities/branch_spec.rb new file mode 100644 index 00000000000..604f56c0cb2 --- /dev/null +++ b/spec/lib/api/entities/branch_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe API::Entities::Branch do + describe '#as_json' do + subject { entity.as_json } + + let(:project) { create(:project, :public, :repository) } + let(:repository) { project.repository } + let(:branch) { repository.find_branch('master') } + let(:entity) { described_class.new(branch, project: project) } + + it 'includes basic fields', :aggregate_failures do + is_expected.to include( + name: 'master', + commit: a_kind_of(Hash), + merged: false, + protected: false, + developers_can_push: false, + developers_can_merge: false, + can_push: false, + default: true, + web_url: Gitlab::Routing.url_helpers.project_tree_url(project, 'master') + ) + end + end +end diff --git a/spec/lib/api/entities/design_management/design_spec.rb b/spec/lib/api/entities/design_management/design_spec.rb new file mode 100644 index 00000000000..50ca3b43c6a --- /dev/null +++ b/spec/lib/api/entities/design_management/design_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe API::Entities::DesignManagement::Design do + let_it_be(:design) { create(:design) } + let(:entity) { described_class.new(design, request: double) } + + subject { entity.as_json } + + it 'has the correct attributes' do + expect(subject).to eq({ + id: design.id, + project_id: design.project_id, + filename: design.filename, + image_url: ::Gitlab::UrlBuilder.build(design) + }) + end +end diff --git a/spec/lib/api/entities/project_repository_storage_move_spec.rb b/spec/lib/api/entities/project_repository_storage_move_spec.rb new file mode 100644 index 00000000000..1c38c8231d4 --- /dev/null +++ b/spec/lib/api/entities/project_repository_storage_move_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe API::Entities::ProjectRepositoryStorageMove do + describe '#as_json' do + subject { entity.as_json } + + let(:storage_move) { build(:project_repository_storage_move, :scheduled, destination_storage_name: 'test_second_storage') } + let(:entity) { described_class.new(storage_move) } + + it 'includes basic fields' do + is_expected.to include( + state: 'scheduled', + source_storage_name: 'default', + destination_storage_name: 'test_second_storage', + project: a_kind_of(Hash) + ) + end + end +end diff --git a/spec/lib/api/entities/snippet_spec.rb b/spec/lib/api/entities/snippet_spec.rb new file mode 100644 index 00000000000..dada0942e49 --- /dev/null +++ b/spec/lib/api/entities/snippet_spec.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ::API::Entities::Snippet do + 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, author: user) } + + let(:entity) { described_class.new(snippet) } + + subject { entity.as_json } + + shared_examples 'common attributes' do + it { expect(subject[:id]).to eq snippet.id } + it { expect(subject[:title]).to eq snippet.title } + it { expect(subject[:description]).to eq snippet.description } + it { expect(subject[:updated_at]).to eq snippet.updated_at } + it { expect(subject[:created_at]).to eq snippet.created_at } + it { expect(subject[:project_id]).to eq snippet.project_id } + it { expect(subject[:visibility]).to eq snippet.visibility } + it { expect(subject).to include(:author) } + + describe 'file_name' do + it 'returns attribute from repository' do + expect(subject[:file_name]).to eq snippet.blobs.first.path + end + + context 'when repository is empty' do + it 'returns attribute from db' do + allow(snippet.repository).to receive(:empty?).and_return(true) + + expect(subject[:file_name]).to eq snippet.file_name + end + end + end + + describe 'ssh_url_to_repo' do + it 'returns attribute' do + expect(subject[:ssh_url_to_repo]).to eq snippet.ssh_url_to_repo + end + + context 'when repository does not exist' do + it 'does not include attribute' do + allow(snippet).to receive(:repository_exists?).and_return(false) + + expect(subject).not_to include(:ssh_url_to_repo) + end + end + end + + describe 'http_url_to_repo' do + it 'returns attribute' do + expect(subject[:http_url_to_repo]).to eq snippet.http_url_to_repo + end + + context 'when repository does not exist' do + it 'does not include attribute' do + allow(snippet).to receive(:repository_exists?).and_return(false) + + expect(subject).not_to include(:http_url_to_repo) + end + end + end + end + + context 'with PersonalSnippet' do + let(:snippet) { personal_snippet } + + it_behaves_like 'common attributes' + + it 'returns snippet web_url attribute' do + expect(subject[:web_url]).to match("/snippets/#{snippet.id}") + end + + it 'returns snippet raw_url attribute' do + expect(subject[:raw_url]).to match("/snippets/#{snippet.id}/raw") + end + end + + context 'with ProjectSnippet' do + let(:snippet) { project_snippet } + + it_behaves_like 'common attributes' + + it 'returns snippet web_url attribute' do + expect(subject[:web_url]).to match("#{snippet.project.full_path}/snippets/#{snippet.id}") + end + + it 'returns snippet raw_url attribute' do + expect(subject[:raw_url]).to match("#{snippet.project.full_path}/snippets/#{snippet.id}/raw") + end + end +end diff --git a/spec/lib/api/helpers/pagination_strategies_spec.rb b/spec/lib/api/helpers/pagination_strategies_spec.rb index a418c09a824..eaa71159714 100644 --- a/spec/lib/api/helpers/pagination_strategies_spec.rb +++ b/spec/lib/api/helpers/pagination_strategies_spec.rb @@ -6,7 +6,7 @@ describe API::Helpers::PaginationStrategies do subject { Class.new.include(described_class).new } let(:expected_result) { double("result") } - let(:relation) { double("relation") } + let(:relation) { double("relation", klass: "SomeClass") } let(:params) { {} } before do @@ -17,18 +17,18 @@ describe API::Helpers::PaginationStrategies do let(:paginator) { double("paginator", paginate: expected_result, finalize: nil) } before do - allow(subject).to receive(:paginator).with(relation).and_return(paginator) + allow(subject).to receive(:paginator).with(relation, nil).and_return(paginator) end it 'yields paginated relation' do - expect { |b| subject.paginate_with_strategies(relation, &b) }.to yield_with_args(expected_result) + expect { |b| subject.paginate_with_strategies(relation, nil, &b) }.to yield_with_args(expected_result) end it 'calls #finalize with first value returned from block' do return_value = double expect(paginator).to receive(:finalize).with(return_value) - subject.paginate_with_strategies(relation) do |records| + subject.paginate_with_strategies(relation, nil) do |records| some_options = {} [return_value, some_options] end @@ -37,7 +37,7 @@ describe API::Helpers::PaginationStrategies do it 'returns whatever the block returns' do return_value = [double, double] - result = subject.paginate_with_strategies(relation) do |records| + result = subject.paginate_with_strategies(relation, nil) do |records| return_value end @@ -47,16 +47,77 @@ describe API::Helpers::PaginationStrategies do describe '#paginator' do context 'offset pagination' do + let(:plan_limits) { Plan.default.actual_limits } + let(:offset_limit) { plan_limits.offset_pagination_limit } let(:paginator) { double("paginator") } before do allow(subject).to receive(:keyset_pagination_enabled?).and_return(false) end - it 'delegates to OffsetPagination' do - expect(Gitlab::Pagination::OffsetPagination).to receive(:new).with(subject).and_return(paginator) + context 'when keyset pagination is available for the relation' do + before do + allow(Gitlab::Pagination::Keyset).to receive(:available_for_type?).and_return(true) + end + + context 'when a request scope is given' do + let(:params) { { per_page: 100, page: offset_limit / 100 + 1 } } + let(:request_scope) { double("scope", actual_limits: plan_limits) } + + context 'when the scope limit is exceeded' do + it 'renders a 405 error' do + expect(subject).to receive(:error!).with(/maximum allowed offset/, 405) + + subject.paginator(relation, request_scope) + end + end + + context 'when the scope limit is not exceeded' do + let(:params) { { per_page: 100, page: offset_limit / 100 } } + + it 'delegates to OffsetPagination' do + expect(Gitlab::Pagination::OffsetPagination).to receive(:new).with(subject).and_return(paginator) + + expect(subject.paginator(relation, request_scope)).to eq(paginator) + end + end + end + + context 'when a request scope is not given' do + context 'when the default limits are exceeded' do + let(:params) { { per_page: 100, page: offset_limit / 100 + 1 } } + + it 'renders a 405 error' do + expect(subject).to receive(:error!).with(/maximum allowed offset/, 405) + + subject.paginator(relation) + end + end - expect(subject.paginator(relation)).to eq(paginator) + context 'when the default limits are not exceeded' do + let(:params) { { per_page: 100, page: offset_limit / 100 } } + + it 'delegates to OffsetPagination' do + expect(Gitlab::Pagination::OffsetPagination).to receive(:new).with(subject).and_return(paginator) + + expect(subject.paginator(relation)).to eq(paginator) + end + end + end + end + + context 'when keyset pagination is not available for the relation' do + let(:params) { { per_page: 100, page: offset_limit / 100 + 1 } } + + before do + allow(Gitlab::Pagination::Keyset).to receive(:available_for_type?).and_return(false) + end + + it 'delegates to OffsetPagination' do + expect(Gitlab::Pagination::OffsetPagination).to receive(:new).with(subject).and_return(paginator) + + expect(subject.paginator(relation)).to eq(paginator) + end end end |