-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
477c5cf
commit 39f1b7b
Showing
14 changed files
with
413 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module V2 | ||
# API for Reports (rule results) | ||
class ReportsController < ApplicationController | ||
def index | ||
render_json reports | ||
end | ||
permission_for_action :index, Rbac::REPORT_READ | ||
|
||
def show | ||
render_json report | ||
end | ||
permission_for_action :show, Rbac::REPORT_READ | ||
|
||
private | ||
|
||
def reports | ||
@reports ||= authorize(fetch_collection) | ||
end | ||
|
||
def report | ||
@report ||= authorize(expand_resource.find(permitted_params[:id])) | ||
end | ||
|
||
def resource | ||
V2::Report | ||
end | ||
|
||
def serializer | ||
V2::ReportSerializer | ||
end | ||
|
||
def extra_fields | ||
%i[account_id] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module V2 | ||
# Policies for accessing Reports | ||
class ReportPolicy < V2::ApplicationPolicy | ||
def index? | ||
true # FIXME: this is handled in scoping | ||
end | ||
|
||
def show? | ||
match_account? | ||
end | ||
|
||
# Only show Reports in our user account | ||
class Scope < V2::ApplicationPolicy::Scope | ||
def resolve | ||
return scope.where('1=0') if user&.account_id.blank? | ||
|
||
scope.where(account_id: user.account_id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module V2 | ||
# JSON serialization for Reports | ||
class ReportSerializer < V2::ApplicationSerializer | ||
attributes :title, :description, :business_objective, :compliance_threshold | ||
|
||
derived_attribute :os_major_version, security_guide: [:os_major_version] | ||
derived_attribute :profile_title, profile: [:title] | ||
derived_attribute :ref_id, profile: [:ref_id] | ||
|
||
aggregated_attribute :system_count, :systems, V2::Report::SYSTEM_COUNT | ||
# aggregated_attribute :test_result_system_count, :test_results, V2::Report::TEST_RESULT_SYSTEM_COUNT | ||
# aggregated_attribute :compliant_system_count, :test_results, V2::Report::COMPLIANT_SYSTEM_COUNT | ||
# aggregated_attribute :unsupported_system_count, :test_results, V2::Report::UNSUPPORTED_SYSTEM_COUNT | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe V2::ReportsController do | ||
before do | ||
stub_rbac_permissions( | ||
Rbac::INVENTORY_HOSTS_READ, | ||
Rbac::SYSTEM_READ, | ||
Rbac::REPORT_READ | ||
) | ||
end | ||
|
||
let(:attributes) do | ||
{ | ||
title: :title, | ||
os_major_version: :os_major_version, | ||
ref_id: :ref_id, | ||
description: :description, | ||
profile_title: :profile_title, | ||
business_objective: :business_objective, | ||
system_count: -> { 3 }, | ||
# TODO: compliant_system_count: -> { 0 }, | ||
# TODO: test_result_system_count: -> { 0 }, | ||
# TODO: unsupported_system_count: -> { 0 }, | ||
compliance_threshold: :compliance_threshold | ||
} | ||
end | ||
|
||
let(:current_user) { FactoryBot.create(:v2_user) } | ||
let(:rbac_allowed?) { true } | ||
|
||
before do | ||
request.headers['X-RH-IDENTITY'] = current_user.account.identity_header.raw | ||
allow(StrongerParameters::InvalidValue).to receive(:new) { |value, _| value.to_sym } | ||
allow(controller).to receive(:rbac_allowed?).and_return(rbac_allowed?) | ||
end | ||
|
||
context '/reports' do | ||
describe 'GET index' do | ||
let(:attributes) do | ||
{ | ||
title: :title, | ||
os_major_version: :os_major_version, | ||
ref_id: :ref_id, | ||
description: :description, | ||
profile_title: :profile_title, | ||
business_objective: :business_objective, | ||
system_count: -> { 2 }, | ||
# TODO: compliant_system_count: -> { 0 }, | ||
# TODO: test_result_system_count: -> { 0 }, | ||
# TODO: unsupported_system_count: -> { 0 }, | ||
compliance_threshold: :compliance_threshold | ||
} | ||
end | ||
let(:extra_params) { { account: current_user.account } } | ||
let(:parents) { nil } | ||
|
||
let(:items) do | ||
FactoryBot.create_list( | ||
:v2_report, item_count, | ||
os_major_version: 8, | ||
supports_minors: [0, 1], | ||
account: current_user.account | ||
).sort_by(&:id) | ||
end | ||
|
||
it_behaves_like 'collection' | ||
include_examples 'with metadata' | ||
it_behaves_like 'paginable' | ||
it_behaves_like 'sortable' | ||
it_behaves_like 'searchable' | ||
end | ||
|
||
describe 'GET show' do | ||
let(:attributes) do | ||
{ | ||
title: :title, | ||
os_major_version: :os_major_version, | ||
ref_id: :ref_id, | ||
description: :description, | ||
profile_title: :profile_title, | ||
business_objective: :business_objective, | ||
system_count: -> { 3 }, | ||
# TODO: compliant_system_count: -> { 0 }, | ||
# TODO: test_result_system_count: -> { 0 }, | ||
# TODO: unsupported_system_count: -> { 0 }, | ||
compliance_threshold: :compliance_threshold | ||
} | ||
end | ||
let(:extra_params) { { account: current_user.account, id: item.id } } | ||
let(:parent) { nil } | ||
|
||
let(:item) do | ||
FactoryBot.create( | ||
:v2_report, | ||
os_major_version: 9, | ||
supports_minors: [0, 1, 2], | ||
account: current_user.account | ||
) | ||
end | ||
|
||
it_behaves_like 'individual' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :v2_report, class: 'V2::Report' do | ||
to_create do |instance, context| | ||
policy = FactoryBot.create( | ||
:v2_policy, | ||
:for_tailoring, | ||
title: context.title, | ||
os_major_version: context.os_major_version, | ||
compliance_threshold: context.compliance_threshold, | ||
business_objective: context.business_objective, | ||
account: context.account, | ||
supports_minors: context.supports_minors | ||
) | ||
|
||
policy.os_minor_versions.each do |version| | ||
FactoryBot.create( | ||
:v2_test_result, | ||
account: context.account, | ||
tailoring: FactoryBot.create( | ||
:v2_tailoring, | ||
policy: policy, | ||
os_minor_version: version | ||
) | ||
) | ||
end | ||
|
||
instance.id = policy.id | ||
instance.attributes = V2::Policy.find(instance.id).attributes | ||
instance.reload | ||
end | ||
|
||
transient do | ||
os_major_version { 7 } | ||
business_objective { 'safety' } | ||
compliance_threshold { 90 } | ||
title { 'aaa' } | ||
supports_minors { [0] } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :v2_test_result, class: 'V2::TestResult' do | ||
tailoring { association :v2_tailoring } | ||
system { association :system, account: account } | ||
start_time { 5.minutes.ago } | ||
end_time { 1.minute.ago } | ||
score { SecureRandom.rand(98) + 1 } | ||
supported { true } | ||
|
||
transient do | ||
account { FactoryBot.create(:v2_account) } | ||
end | ||
end | ||
end |
Oops, something went wrong.