Commit 9406626a authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '8820_add_license_management_reports_and_parsers_followup' into 'master'

Add schema matching for API responses and guard license management report parsing

Closes #8820

See merge request gitlab-org/gitlab-ee!9072
parents 1ba3c45e d6e6b56d
......@@ -81,6 +81,10 @@ module EE
def collect_license_management_reports!(license_management_report)
each_report(::Ci::JobArtifact::LICENSE_MANAGEMENT_REPORT_FILE_TYPES) do |file_type, blob|
next if ::Feature.disabled?(:parse_license_management_reports, default_enabled: true)
next unless project.feature_available?(:license_management)
::Gitlab::Ci::Parsers.fabricate!(file_type).parse!(blob, license_management_report)
end
......
{
"type": "array",
"items": { "$ref": "software_license_policy.json" }
}
......@@ -265,6 +265,10 @@ describe Ci::Build do
let(:license_management_report) { Gitlab::Ci::Reports::LicenseManagement::Report.new }
before do
stub_licensed_features(license_management: true)
end
it { expect(license_management_report.licenses.count).to eq(0) }
context 'when build has a license management report' do
......@@ -291,6 +295,32 @@ describe Ci::Build do
expect { subject }.to raise_error(Gitlab::Ci::Parsers::LicenseManagement::LicenseManagement::LicenseManagementParserError)
end
end
context 'when Feature flag is disabled for License Management reports parsing' do
before do
stub_feature_flags(parse_license_management_reports: false)
create(:ee_ci_job_artifact, :license_management, job: job, project: job.project)
end
it 'does NOT parse license management report' do
subject
expect(license_management_report.licenses.count).to eq(0)
end
end
context 'when the license management feature is disabled' do
before do
stub_licensed_features(license_management: false)
create(:ee_ci_job_artifact, :license_management, job: job, project: job.project)
end
it 'does NOT parse license management report' do
subject
expect(license_management_report.licenses.count).to eq(0)
end
end
end
end
end
......@@ -326,6 +326,10 @@ describe Ci::Pipeline do
describe '#has_license_management_reports?' do
subject { pipeline.has_license_management_reports? }
before do
stub_licensed_features(license_management: true)
end
context 'when pipeline has builds with license_management reports' do
before do
create(:ee_ci_build, :license_management, pipeline: pipeline, project: project)
......@@ -368,6 +372,10 @@ describe Ci::Pipeline do
describe '#license_management_reports' do
subject { pipeline.license_management_report }
before do
stub_licensed_features(license_management: true)
end
context 'when pipeline has multiple builds with license management reports' do
let!(:build_1) { create(:ci_build, :success, name: 'license_management', pipeline: pipeline, project: project) }
let!(:build_2) { create(:ci_build, :success, name: 'license_management2', pipeline: pipeline, project: project) }
......
......@@ -48,11 +48,12 @@ describe API::ManagedLicenses do
end
end
context 'authorized user with proper permissions' do
context 'with an authorized user with proper permissions' do
it 'returns project managed licenses' do
get api("/projects/#{project.id}/managed_licenses", dev_user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('managed_licenses', dir: 'ee')
expect(json_response).to be_a(Array)
expect(json_response.first['id']).to eq(software_license_policy.id)
expect(json_response.first['name']).to eq(software_license_policy.name)
......@@ -60,11 +61,12 @@ describe API::ManagedLicenses do
end
end
context 'authorized user without read permissions' do
context 'with authorized user without read permissions' do
it 'returns project managed licenses to users with read permissions' do
get api("/projects/#{project.id}/managed_licenses", reporter_user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('managed_licenses', dir: 'ee')
expect(json_response).to be_a(Array)
expect(json_response.first['id']).to eq(software_license_policy.id)
expect(json_response.first['name']).to eq(software_license_policy.name)
......@@ -87,6 +89,7 @@ describe API::ManagedLicenses do
get api("/projects/#{project.id}/managed_licenses/#{software_license_policy.id}", dev_user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('software_license_policy', dir: 'ee')
expect(json_response['id']).to eq(software_license_policy.id)
expect(json_response['name']).to eq(software_license_policy.name)
expect(json_response['approval_status']).to eq(software_license_policy.approval_status)
......@@ -97,6 +100,7 @@ describe API::ManagedLicenses do
get api("/projects/#{project.id}/managed_licenses/#{escaped_name}", dev_user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('software_license_policy', dir: 'ee')
expect(json_response['id']).to eq(software_license_policy.id)
expect(json_response['name']).to eq(software_license_policy.name)
expect(json_response['approval_status']).to eq(software_license_policy.approval_status)
......@@ -114,6 +118,7 @@ describe API::ManagedLicenses do
get api("/projects/#{project.id}/managed_licenses/#{software_license_policy.id}", reporter_user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('software_license_policy', dir: 'ee')
expect(json_response['id']).to eq(software_license_policy.id)
expect(json_response['name']).to eq(software_license_policy.name)
expect(json_response['approval_status']).to eq(software_license_policy.approval_status)
......@@ -141,6 +146,7 @@ describe API::ManagedLicenses do
end.to change {project.software_license_policies.count}.by(1)
expect(response).to have_gitlab_http_status(201)
expect(response).to match_response_schema('software_license_policy', dir: 'ee')
expect(json_response).to have_key('id')
expect(json_response['name']).to eq('NEW_LICENSE_NAME')
expect(json_response['approval_status']).to eq('approved')
......@@ -209,6 +215,7 @@ describe API::ManagedLicenses do
updated_software_license_policy = project.software_license_policies.reload.first
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('software_license_policy', dir: 'ee')
# Check that response is equal to the updated object
expect(json_response['id']).to eq(initial_id)
......
......@@ -6,6 +6,10 @@ describe Ci::CompareLicenseManagementReportsService do
let(:service) { described_class.new(project) }
let(:project) { create(:project, :repository) }
before do
stub_licensed_features(license_management: true)
end
describe '#execute' do
subject { service.execute(base_pipeline, head_pipeline) }
......
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