Commit d6e6b56d authored by Gilbert Roulot's avatar Gilbert Roulot Committed by Kamil Trzciński

Add schema matching for API responses

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