Commit b35bb456 authored by Tetiana Chupryna's avatar Tetiana Chupryna Committed by James Lopez

Add Licenses list controller

Add route and additional service for fetching
parent c1d17c55
......@@ -19,7 +19,7 @@ module Projects
def collect_dependencies
found_dependencies = report_service.able_to_fetch? ? service.execute : []
::Gitlab::DependenciesCollection.new(found_dependencies)
::Gitlab::ItemsCollection.new(found_dependencies)
end
def authorize_read_dependency_list!
......
# frozen_string_literal: true
module Projects
module Security
class LicensesController < Projects::ApplicationController
before_action :authorize_read_licenses_list!
def index
respond_to do |format|
format.json do
render json: serializer.represent(licenses, build: report_service.build)
end
end
end
private
def licenses
found_licenses = report_service.able_to_fetch? ? service.execute : []
::Gitlab::ItemsCollection.new(found_licenses)
end
def report_service
@report_service ||= ::Security::ReportFetchService.new(project, ::Ci::JobArtifact.license_management_reports)
end
def serializer
::LicensesListSerializer.new(project: project, user: current_user)
.with_pagination(request, response)
end
def service
::Security::LicensesListService.new(pipeline: report_service.pipeline)
end
end
end
end
# frozen_string_literal: true
module Security
class LicensesListService
# @param pipeline [Ci::Pipeline]
def initialize(pipeline:)
@pipeline = pipeline
end
def execute
pipeline.license_management_report.licenses
end
private
attr_reader :pipeline
end
end
......@@ -87,6 +87,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
namespace :security do
resources :dependencies, only: [:index]
resources :licenses, only: [:index]
resources :vulnerabilities, only: [:index] do
collection do
get :summary
......
# frozen_string_literal: true
module Gitlab
class DependenciesCollection
class ItemsCollection
include Enumerable
def initialize(dependencies)
@collection = dependencies
def initialize(items)
@collection = items
end
def each
......
# frozen_string_literal: true
require 'spec_helper'
describe Projects::Security::LicensesController do
describe "GET index.json" do
let_it_be(:project) { create(:project, :repository, :private) }
let_it_be(:user) { create(:user) }
let(:params) { { namespace_id: project.namespace, project_id: project } }
let(:get_licenses) { get :index, params: params, format: :json }
before do
sign_in(user)
end
context 'with authorized user' do
before do
project.add_guest(user)
end
context 'when feature is available' do
before do
stub_licensed_features(licenses_list: true, license_management: true)
end
context 'with existing report' do
let!(:pipeline) { create(:ee_ci_pipeline, :with_license_management_report, project: project) }
before do
get_licenses
end
it 'returns success code' do
expect(response).to have_gitlab_http_status(200)
end
it 'returns a hash with licenses' do
expect(json_response).to be_a(Hash)
expect(json_response['licenses'].length).to eq(4)
end
it 'returns status ok' do
expect(json_response['report']['status']).to eq('ok')
end
context 'with pagination params' do
let(:params) { { namespace_id: project.namespace, project_id: project, per_page: 3, page: 2 } }
it 'return only 1 license' do
expect(json_response['licenses'].length).to eq(1)
end
end
end
context 'without existing report' do
let!(:pipeline) { create(:ee_ci_pipeline, :with_dependency_list_report, project: project) }
before do
get_licenses
end
it 'returns status job_not_set_up' do
expect(json_response['report']['status']).to eq('job_not_set_up')
end
end
end
context 'when feature is not available' do
before do
get_licenses
end
it 'returns 404' do
expect(response).to have_gitlab_http_status(404)
end
end
end
context 'with unauthorized user' do
before do
stub_licensed_features(licenses_list: true, license_management: true)
get_licenses
end
it 'returns 404' do
expect(response).to have_gitlab_http_status(404)
end
end
end
end
......@@ -2,9 +2,9 @@
require 'spec_helper'
describe Gitlab::DependenciesCollection do
let(:collection) { described_class.new(fake_dependencies) }
let(:fake_dependencies) { Array.new(42, :dependency) }
describe Gitlab::ItemsCollection do
let(:collection) { described_class.new(items) }
let(:items) { Array.new(42, :dependency) }
it 'responds to each' do
expect(collection).to respond_to(:each)
......
# frozen_string_literal: true
require 'spec_helper'
describe Security::LicensesListService do
describe '#execute' do
let!(:pipeline) { create(:ee_ci_pipeline, :with_license_management_report) }
subject { described_class.new(pipeline: pipeline).execute }
before do
stub_licensed_features(license_management: true)
end
it 'returns array of Licenses' do
is_expected.to be_an(Array)
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