Commit 01947844 authored by Douwe Maan's avatar Douwe Maan

Merge branch '13084-add-licenses-to-dl-response' into 'master'

Add Licenses info into Dependencies response

See merge request gitlab-org/gitlab-ee!15160
parents e32aab74 df6bd040
......@@ -11,13 +11,22 @@ class DependencyEntity < Grape::Entity
expose :name, :severity
end
class LicenseEntity < Grape::Entity
expose :name, :url
end
expose :name, :packager, :version
expose :location, using: LocationEntity
expose :vulnerabilities, using: VulnerabilityEntity, if: ->(_) { can_read_vulnerabilities? }
expose :licenses, using: LicenseEntity, if: ->(_) { can_read_licenses? }
private
def can_read_vulnerabilities?
can?(request.user, :read_project_security_dashboard, request.project)
end
def can_read_licenses?
can?(request.user, :read_software_license_policy, request.project)
end
end
---
title: Add Licenses info into Dependencies response
merge_request: 15160
author:
type: added
......@@ -26,6 +26,15 @@ FactoryBot.define do
end
end
trait :with_licenses do
licenses do
[{
name: 'MIT',
url: 'http://opensource.org/licenses/mit-license'
}]
end
end
initialize_with { attributes }
end
end
......@@ -5,7 +5,8 @@
"packager",
"version",
"location",
"vulnerabilities"
"vulnerabilities",
"licenses"
],
"properties": {
"name": {
......@@ -35,6 +36,17 @@
"type": "string"
}
}
},
"licenses": {
"type": "array",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
}
}
}
},
"additionalProperties": false
......
......@@ -6,32 +6,47 @@ describe DependencyEntity do
describe '#as_json' do
subject { described_class.represent(dependency, request: request).as_json }
set(:project) { create(:project, :repository, :private) }
set(:user) { create(:user) }
let(:project) { create(:project, :repository, :private) }
let(:request) { double('request') }
let(:dependency) { build(:dependency, :with_vulnerabilities) }
let(:dependency) { build(:dependency, :with_vulnerabilities, :with_licenses) }
before do
stub_licensed_features(security_dashboard: true)
allow(request).to receive(:project).and_return(project)
allow(request).to receive(:user).and_return(user)
end
context 'with developer' do
context 'when all required features available' do
before do
project.add_developer(user)
stub_licensed_features(security_dashboard: true, license_management: true)
allow(request).to receive(:project).and_return(project)
allow(request).to receive(:user).and_return(user)
end
it do
is_expected.to eq(dependency.except(:licenses))
context 'with developer' do
before do
project.add_developer(user)
end
it { is_expected.to eq(dependency) }
end
context 'with reporter' do
let(:dependency_info) { build(:dependency, :with_licenses) }
before do
project.add_reporter(user)
end
it { is_expected.to eq(dependency_info) }
end
end
context 'with reporter' do
context 'when all required features are unavailable' do
let(:dependency_info) { build(:dependency).except(:licenses) }
before do
project.add_reporter(user)
project.add_developer(user)
end
it { is_expected.to eq(dependency_info) }
......
......@@ -6,14 +6,14 @@ describe DependencyListSerializer do
set(:project) { create(:project, :repository, :private) }
set(:user) { create(:user) }
let(:ci_build) { create(:ee_ci_build, :success) }
let(:dependencies) { [build(:dependency, :with_vulnerabilities)] }
let(:dependencies) { [build(:dependency, :with_vulnerabilities, :with_licenses)] }
let(:serializer) do
described_class.new(project: project, user: user).represent(dependencies, build: ci_build)
end
before do
stub_licensed_features(security_dashboard: true)
stub_licensed_features(security_dashboard: true, license_management: true)
project.add_developer(user)
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