Commit ed80ac22 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Add api for packages index

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent d571d7be
---
title: Add API endpoint for project packages
merge_request: 9259
author:
type: added
# frozen_string_literal: true
module API
class Packages < Grape::API
include PaginationParams
before do
require_packages_enabled!
authorize_packages_feature!
authorize_download_package!
end
helpers ::API::Helpers::PackagesHelpers
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get all project packages' do
success EE::API::Entities::Package
end
params do
use :pagination
end
get ':id/packages' do
packages = user_project.packages
present paginate(packages), with: EE::API::Entities::Package
end
end
end
end
......@@ -22,6 +22,7 @@ module EE
mount ::API::ProjectPushRule
mount ::API::MavenPackages
mount ::API::NpmPackages
mount ::API::Packages
end
end
end
......
......@@ -499,6 +499,13 @@ module EE
expose :name
expose :versions
end
class Package < Grape::Entity
expose :id
expose :name
expose :version
expose :package_type
end
end
end
end
{
"type": "array",
"items": {
"type": "object",
"required" : ["name", "version", "packages_type"],
"properties" : {
"name": { "type": "string" },
"version": { "type": "string" },
"packages_type": { "type": "string" }
}
}
}
# frozen_string_literal: true
require 'spec_helper'
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
before do
stub_licensed_features(packages: false)
end
it 'returns 403' do
get api(url, user)
expect(response).to have_gitlab_http_status(403)
end
end
context 'when packages feature is enabled' do
context 'project is public' do
it 'returns 200' do
get api(url)
expect(response).to have_gitlab_http_status(200)
end
end
context 'project is private' do
before do
project.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
end
it 'returns 404 for non authenticated user' do
get api(url)
expect(response).to have_gitlab_http_status(404)
end
it 'returns 404 for a user without access to the project' do
project.team.truncate
get api(url, user)
expect(response).to have_gitlab_http_status(404)
end
it 'returns 200 and valid response schema' do
get api(url, user)
expect(response).to have_gitlab_http_status(200)
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) }
before do
stub_licensed_features(packages: true)
end
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 }
expect_paginated_array_response(expected)
end
end
context 'viewing the second page' do
let(:expected) { [package3.id] }
it 'returns the last package' do
get api(url, user), params: { page: 2, per_page: per_page }
expect_paginated_array_response(expected)
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