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 ...@@ -14,6 +14,7 @@ module API
def authorize_download_package! def authorize_download_package!
authorize!(:read_package, user_project) authorize!(:read_package, user_project)
end end
alias_method :authorize_read_package!, :authorize_download_package!
def authorize_create_package! def authorize_create_package!
authorize!(:create_package, user_project) authorize!(:create_package, user_project)
......
...@@ -7,7 +7,7 @@ module API ...@@ -7,7 +7,7 @@ module API
before do before do
require_packages_enabled! require_packages_enabled!
authorize_packages_feature! authorize_packages_feature!
authorize_download_package! authorize_read_package!
end end
helpers ::API::Helpers::PackagesHelpers helpers ::API::Helpers::PackagesHelpers
...@@ -17,6 +17,7 @@ module API ...@@ -17,6 +17,7 @@ module API
end end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get all project packages' do desc 'Get all project packages' do
detail 'This feature was introduced in GitLab 11.8'
success EE::API::Entities::Package success EE::API::Entities::Package
end end
params do params do
......
...@@ -6,29 +6,19 @@ describe API::Packages do ...@@ -6,29 +6,19 @@ describe API::Packages do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :public) } let(:project) { create(:project, :public) }
let(:package) { create(:npm_package, project: project) } let(:package) { create(:npm_package, project: project) }
let(:params) { nil }
before do before do
project.add_developer(user) project.add_developer(user)
stub_licensed_features(packages: true)
end end
describe 'GET /projects/:id/packages' do describe 'GET /projects/:id/packages' do
let(:url) { "/projects/#{project.id}/packages" } let(:url) { "/projects/#{project.id}/packages" }
context 'when packages feature is disabled' do context 'packages feature enabled' do
before do before do
stub_licensed_features(packages: false) stub_licensed_features(packages: true)
end
it 'returns 403' do
get api(url, user)
expect(response).to have_gitlab_http_status(403)
end end
end
context 'when packages feature is enabled' do
context 'project is public' do context 'project is public' do
it 'returns 200' do it 'returns 200' do
get api(url) get api(url)
...@@ -38,9 +28,7 @@ describe API::Packages do ...@@ -38,9 +28,7 @@ describe API::Packages do
end end
context 'project is private' do context 'project is private' do
before do let(:project) { create(:project, :private) }
project.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
end
it 'returns 404 for non authenticated user' do it 'returns 404 for non authenticated user' do
get api(url) get api(url)
...@@ -63,36 +51,44 @@ describe API::Packages do ...@@ -63,36 +51,44 @@ describe API::Packages do
expect(response).to match_response_schema('public_api/v4/packages/packages', dir: 'ee') expect(response).to match_response_schema('public_api/v4/packages/packages', dir: 'ee')
end end
end end
end
context 'with pagination params' do context 'with pagination params' do
let(:per_page) { 2 } let(:per_page) { 2 }
let!(:package1) { create(:npm_package, project: project) } let!(:package1) { create(:npm_package, project: project) }
let!(:package2) { create(:npm_package, project: project) } let!(:package2) { create(:npm_package, project: project) }
let!(:package3) { create(:maven_package, project: project) } let!(:package3) { create(:maven_package, project: project) }
before do before do
stub_licensed_features(packages: true) stub_licensed_features(packages: true)
end end
context 'when viewing the first page' do context 'when viewing the first page' do
let(:expected) { [package1.id, package2.id] } it 'returns first 2 packages' do
get api(url, user), params: { page: 1, per_page: per_page }
it 'returns first 2 packages' do expect_paginated_array_response([package1.id, package2.id])
get api(url, user), params: { page: 1, per_page: per_page } 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 end
end
context 'viewing the second page' do context 'packages feature disabled' do
let(:expected) { [package3.id] } before do
stub_licensed_features(packages: false)
end
it 'returns the last package' do it 'returns 403' do
get api(url, user), params: { page: 2, per_page: per_page } get api(url, user)
expect_paginated_array_response(expected) expect(response).to have_gitlab_http_status(403)
end
end end
end 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