Commit 3443e5bb authored by mo khan's avatar mo khan

Apply maintainer feedback

* match params[:detected] == 'true'
* extract test helper method
* extract `matching_policies_params` method
* invert assertion to check for absence
parent ad13cee7
...@@ -65,10 +65,14 @@ module Projects ...@@ -65,10 +65,14 @@ module Projects
render json: { errors: result[:message].as_json }, status: result.fetch(:http_status, :unprocessable_entity) render json: { errors: result[:message].as_json }, status: result.fetch(:http_status, :unprocessable_entity)
end end
def matching_policies_params
params.permit(:detected, classification: [])
end
def matching_policies_from(license_compliance) def matching_policies_from(license_compliance)
filters = params.permit(:detected, classification: []) filters = matching_policies_params
license_compliance.find_policies( license_compliance.find_policies(
detected_only: filters[:detected].present?, detected_only: filters[:detected] == 'true',
classification: filters[:classification] classification: filters[:classification]
) )
end end
......
...@@ -84,7 +84,11 @@ describe Projects::LicensesController do ...@@ -84,7 +84,11 @@ describe Projects::LicensesController do
context "when loading all policies" do context "when loading all policies" do
before do before do
get :index, params: { namespace_id: project.namespace, project_id: project }, format: :json get :index, params: {
namespace_id: project.namespace,
project_id: project,
detected: false
}, format: :json
end end
it { expect(response).to have_http_status(:ok) } it { expect(response).to have_http_status(:ok) }
......
...@@ -152,35 +152,51 @@ RSpec.describe SCA::LicenseCompliance do ...@@ -152,35 +152,51 @@ RSpec.describe SCA::LicenseCompliance do
let!(:mit_policy) { create(:software_license_policy, :denied, software_license: mit, project: project) } let!(:mit_policy) { create(:software_license_policy, :denied, software_license: mit, project: project) }
let!(:other_license_policy) { create(:software_license_policy, :allowed, software_license: other_license, project: project) } let!(:other_license_policy) { create(:software_license_policy, :allowed, software_license: other_license, project: project) }
def assert_matches(item, expected = {})
actual = expected.keys.each_with_object({}) do |attribute, memo|
memo[attribute] = item.public_send(attribute)
end
expect(actual).to eql(expected)
end
context "when searching for policies for licenses that were detected in a scan report" do context "when searching for policies for licenses that were detected in a scan report" do
let(:results) { subject.find_policies(detected_only: true) } let(:results) { subject.find_policies(detected_only: true) }
it 'excludes policies for licenses that do not appear in the latest license scan report' do it 'excludes policies for licenses that do not appear in the latest license scan report' do
expect(results.count).to eq(3) expect(results.map(&:name)).not_to include('SOFTWARE_LICENSE-2.7/example_1')
end end
it 'includes a policy for an unclassified and known license that was detected in the scan report' do it 'includes a policy for an unclassified and known license that was detected in the scan report' do
expect(results[0].id).to be_nil assert_matches(
expect(results[0].name).to eq("BSD 3-Clause \"New\" or \"Revised\" License") results[0],
expect(results[0].url).to eq("http://spdx.org/licenses/BSD-3-Clause.json") id: nil,
expect(results[0].classification).to eq("unclassified") name: "BSD 3-Clause \"New\" or \"Revised\" License",
expect(results[0].spdx_identifier).to eq("BSD-3-Clause") url: "http://spdx.org/licenses/BSD-3-Clause.json",
classification: "unclassified",
spdx_identifier: "BSD-3-Clause"
)
end end
it 'includes an entry for a denied license found in the scan report' do it 'includes an entry for a denied license found in the scan report' do
expect(results[1].id).to eq(mit_policy.id) assert_matches(
expect(results[1].name).to eq(mit.name) results[1],
expect(results[1].url).to eq("http://spdx.org/licenses/MIT.json") id: mit_policy.id,
expect(results[1].classification).to eq("denied") name: mit.name,
expect(results[1].spdx_identifier).to eq("MIT") url: "http://spdx.org/licenses/MIT.json",
classification: "denied",
spdx_identifier: "MIT"
)
end end
it 'includes an entry for an allowed license found in the scan report' do it 'includes an entry for an allowed license found in the scan report' do
expect(results[2].id).to be_nil assert_matches(
expect(results[2].name).to eq("unknown") results[2],
expect(results[2].url).to be_blank id: nil,
expect(results[2].classification).to eq("unclassified") name: 'unknown',
expect(results[2].spdx_identifier).to be_nil url: '',
classification: 'unclassified',
spdx_identifier: nil
)
end end
end end
...@@ -189,11 +205,14 @@ RSpec.describe SCA::LicenseCompliance do ...@@ -189,11 +205,14 @@ RSpec.describe SCA::LicenseCompliance do
it 'includes an entry for each `allowed` licensed' do it 'includes an entry for each `allowed` licensed' do
expect(results.count).to eq(1) expect(results.count).to eq(1)
expect(results[0].id).to eql(other_license_policy.id) assert_matches(
expect(results[0].name).to eq(other_license_policy.software_license.name) results[0],
expect(results[0].url).to be_blank id: other_license_policy.id,
expect(results[0].classification).to eq("allowed") name: other_license_policy.software_license.name,
expect(results[0].spdx_identifier).to eq(other_license_policy.software_license.spdx_identifier) url: nil,
classification: 'allowed',
spdx_identifier: other_license_policy.software_license.spdx_identifier
)
end end
end end
...@@ -202,18 +221,22 @@ RSpec.describe SCA::LicenseCompliance do ...@@ -202,18 +221,22 @@ RSpec.describe SCA::LicenseCompliance do
it 'includes an entry for each `allowed` and `denied` licensed' do it 'includes an entry for each `allowed` and `denied` licensed' do
expect(results.count).to eq(2) expect(results.count).to eq(2)
assert_matches(
expect(results[0].id).to eql(mit_policy.id) results[0],
expect(results[0].name).to eq(mit_policy.software_license.name) id: mit_policy.id,
expect(results[0].url).to be_present name: mit_policy.software_license.name,
expect(results[0].classification).to eq("denied") url: 'http://spdx.org/licenses/MIT.json',
expect(results[0].spdx_identifier).to eq(mit_policy.software_license.spdx_identifier) classification: "denied",
spdx_identifier: mit_policy.software_license.spdx_identifier
expect(results[1].id).to eql(other_license_policy.id) )
expect(results[1].name).to eq(other_license_policy.software_license.name) assert_matches(
expect(results[1].url).to be_blank results[1],
expect(results[1].classification).to eq("allowed") id: other_license_policy.id,
expect(results[1].spdx_identifier).to eq(other_license_policy.software_license.spdx_identifier) name: other_license_policy.software_license.name,
url: nil,
classification: "allowed",
spdx_identifier: other_license_policy.software_license.spdx_identifier
)
end end
end end
...@@ -222,12 +245,14 @@ RSpec.describe SCA::LicenseCompliance do ...@@ -222,12 +245,14 @@ RSpec.describe SCA::LicenseCompliance do
it 'includes an entry for each entry that was detected in the report and matches a classification' do it 'includes an entry for each entry that was detected in the report and matches a classification' do
expect(results.count).to eq(1) expect(results.count).to eq(1)
assert_matches(
expect(results[0].id).to eql(mit_policy.id) results[0],
expect(results[0].name).to eq(mit_policy.software_license.name) id: mit_policy.id,
expect(results[0].url).to be_present name: mit_policy.software_license.name,
expect(results[0].classification).to eq("denied") url: 'http://spdx.org/licenses/MIT.json',
expect(results[0].spdx_identifier).to eq(mit_policy.software_license.spdx_identifier) classification: "denied",
spdx_identifier: mit_policy.software_license.spdx_identifier
)
end 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