Commit 49f4c684 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor packages API backend based on review

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 7578529c
......@@ -14,6 +14,7 @@ module API
def authorize_download_package!
authorize!(:read_package, user_project)
end
alias_method :authorize_read_package!, :authorize_download_package!
def authorize_create_package!
authorize!(:create_package, user_project)
......
......@@ -7,7 +7,7 @@ module API
before do
require_packages_enabled!
authorize_packages_feature!
authorize_download_package!
authorize_read_package!
end
helpers ::API::Helpers::PackagesHelpers
......@@ -17,6 +17,7 @@ module API
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get all project packages' do
detail 'This feature was introduced in GitLab 11.8'
success EE::API::Entities::Package
end
params do
......
......@@ -6,29 +6,19 @@ describe API::Packages do
let(:user) { create(:user) }
let(:project) { create(:project, :public) }
let(:package) { create(:npm_package, project: project) }
let(:params) { nil }
before do
project.add_developer(user)
stub_licensed_features(packages: true)
end
describe 'GET /projects/:id/packages' do
let(:url) { "/projects/#{project.id}/packages" }
context 'when packages feature is disabled' do
context 'packages feature enabled' do
before do
stub_licensed_features(packages: false)
end
it 'returns 403' do
get api(url, user)
expect(response).to have_gitlab_http_status(403)
stub_licensed_features(packages: true)
end
end
context 'when packages feature is enabled' do
context 'project is public' do
it 'returns 200' do
get api(url)
......@@ -38,9 +28,7 @@ describe API::Packages do
end
context 'project is private' do
before do
project.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
end
let(:project) { create(:project, :private) }
it 'returns 404 for non authenticated user' do
get api(url)
......@@ -63,36 +51,44 @@ describe API::Packages do
expect(response).to match_response_schema('public_api/v4/packages/packages', dir: 'ee')
end
end
end
context 'with pagination params' do
let(:per_page) { 2 }
let!(:package1) { create(:npm_package, project: project) }
let!(:package2) { create(:npm_package, project: project) }
let!(:package3) { create(:maven_package, project: project) }
context 'with pagination params' do
let(:per_page) { 2 }
let!(:package1) { create(:npm_package, project: project) }
let!(:package2) { create(:npm_package, project: project) }
let!(:package3) { create(:maven_package, project: project) }
before do
stub_licensed_features(packages: true)
end
before do
stub_licensed_features(packages: true)
end
context 'when viewing the first page' do
let(:expected) { [package1.id, package2.id] }
context 'when viewing the first page' do
it 'returns first 2 packages' do
get api(url, user), params: { page: 1, per_page: per_page }
it 'returns first 2 packages' do
get api(url, user), params: { page: 1, per_page: per_page }
expect_paginated_array_response([package1.id, package2.id])
end
end
expect_paginated_array_response(expected)
context 'viewing the second page' do
it 'returns the last package' do
get api(url, user), params: { page: 2, per_page: per_page }
expect_paginated_array_response([package3.id])
end
end
end
end
context 'viewing the second page' do
let(:expected) { [package3.id] }
context 'packages feature disabled' do
before do
stub_licensed_features(packages: false)
end
it 'returns the last package' do
get api(url, user), params: { page: 2, per_page: per_page }
it 'returns 403' do
get api(url, user)
expect_paginated_array_response(expected)
end
expect(response).to have_gitlab_http_status(403)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment