Skip to content

Commit

Permalink
core logic test for export strategies
Browse files Browse the repository at this point in the history
Signed-off-by: Vanessa Fotso <vfotso@mitre.org>
  • Loading branch information
vanessuniq committed Dec 15, 2022
1 parent a57f87e commit 8338b62
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/helpers/export_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def export_excel(project)
srg_rule: %i[disa_rule_descriptions rule_descriptions checks]
}]
).each do |component|
worksheet = workbook.add_worksheet("#{component[:name]} V#{component[:version]}R#{component[:release]}")
worksheet_name = "#{component[:name]}-V#{component[:version]}R#{component[:release]}-#{component[:id]}"
worksheet = workbook.add_worksheet(worksheet_name)
worksheet.auto_width = true
worksheet.append_row(ExportConstants::DISA_EXPORT_HEADERS)
last_row_num = 0
Expand Down
24 changes: 24 additions & 0 deletions spec/factories/components.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

FactoryBot.define do
factory :component do
project { create(:project) }
based_on { create(:security_requirements_guide) }

prefix { 'ABCD-00' }
name { FFaker::Name.name }
admin_name { generate(:name) }
admin_email { generate(:email) }
advanced_fields { false }
version { generate(:version) }
release { generate(:release) }
title { 'Fake title' }
description { 'Fake description' }

trait :released_component do
released { true }
end

factory :released_component, traits: [:released_component]
end
end
7 changes: 7 additions & 0 deletions spec/factories/projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :project do
name { generate(:name) }
end
end
13 changes: 13 additions & 0 deletions spec/factories/security_requirements_guides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

XML_FILE = File.read('./spec/fixtures/files/U_Web_Server_V2R3_Manual-xccdf.xml')

FactoryBot.define do
factory :security_requirements_guide do
srg_id { FFaker::Name.name.underscore }
title { FFaker::Name.name }
version { "V#{rand(0..9)}R#{rand(0..9)}" }
xml { XML_FILE }
release_date { Time.zone.today }
end
end
3 changes: 3 additions & 0 deletions spec/factories/sequences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
sequence(:name) { |n| "John Doe#{n}" }
sequence(:email) { |n| "user#{n}@example.org" }
sequence(:password) { |n| "12345678#{n}" }
sequence(:version) { |n| n }
sequence(:release) { |n| n }
sequence(:rule_id) { |n| n.to_s.rjust(6, '0') }
end
126 changes: 126 additions & 0 deletions spec/helpers/export_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ExportHelper, type: :helper do
include ExportHelper

before(:all) do
@component = FactoryBot.create(:component)
@released_component = FactoryBot.create(:released_component)
@project = @component.project
@project_with_realeased_comp = @released_component.project
end

describe '#export_excel' do
before(:all) do
@workbook = export_excel(@project)
@workbook_release = export_excel(@project_with_realeased_comp)

[@workbook, @workbook_release].each_with_index do |item, index|
file_name = ''
if index == 0
file_name = "./#{@project.name}.xlsx"
File.binwrite(file_name, item.read_string)
@xlsx = Roo::Spreadsheet.open(file_name)
else
file_name = "./#{@project_with_realeased_comp.name}.xlsx"
File.binwrite(file_name, item.read_string)
@xlsx_release = Roo::Spreadsheet.open(file_name)
end
File.delete(file_name)
end
end

context 'in all scenarios' do
it 'creates an excel format of a given project' do
expect(@workbook).to be_present
expect(@workbook.filename).to end_with 'xlsx'
end
end

context 'when project has released component' do
it 'creates an excel file with the # of sheets == # of released components' do
expect(@xlsx_release.sheets.size).to eq @project_with_realeased_comp.components.where(released: true).size
end

it 'creates an excel file with correct format for worksheet name' do
sheet_name = "#{@released_component.name}-V#{@released_component.version}"
sheet_name += "R#{@released_component.release}-#{@released_component.id}"
expect(@xlsx_release.sheets).to include(sheet_name)
end
end

context 'when project has no released component' do
it 'creates an empty spreadsheet' do
expect(@xlsx.sheets.size).to eq 1
expect(@xlsx.sheets.first).to eq 'Sheet1'
end
end
end

describe '#export_xccdf' do
before(:all) do
@file_name = "./#{@project.name}.zip"
File.binwrite(@file_name, export_xccdf(@project).string)
@zip = Zip::File.open(@file_name)
end

after(:all) do
File.delete(@file_name)
end

it 'creates a zip file containing all components of a project in xccdf format' do
expect(@zip.size).to eq @project.components.size
# check the content of each file is valid xml
errors = []
@zip.each do |xml|
xml.get_input_stream { |io| errors << Nokogiri::XML(io.read).errors }
end
expect(errors).to all(be_empty)
end

it 'creates a zip file containing xccdf files with correct name format' do
expected_names = @project.components.map do |comp|
version = comp.version ? "V#{comp.version}" : ''
release = comp.release ? "R#{comp.release}" : ''
title = (comp.title || "#{comp.name} STIG Readiness Guide")
"U_#{title.tr(' ', '_')}_#{version}#{release}-xccdf.xml"
end
expect(@zip.map(&:name).sort).to eq expected_names.sort
end
end

describe '#export_inspec_project' do
before(:all) do
@file_name = "./#{@project.name}.zip"
File.binwrite(@file_name, export_inspec_project(@project).string)
@zip = Zip::File.open(@file_name)
end

after(:all) do
File.delete(@file_name)
end

it 'creates a zip file containing all components of a project in YAML format' do
expect(@zip.size).to eq @project.components.size
# ensure files are valid yaml
@zip.each do |yml|
content = nil
yml.get_input_stream { |io| content = io.read }
expect { YAML.parse(content) }.to_not raise_error
end
end

it 'creates a zip file containing yaml files with correct name format' do
expected_names = @project.components.map do |comp|
version = comp.version ? "V#{comp.version}" : ''
release = comp.release ? "R#{comp.release}" : ''

"#{comp.name.tr(' ', '-')}-#{version}#{release}-stig-baseline/inspec.yml"
end

expect(@zip.map(&:name).sort).to eq expected_names.sort
end
end
end

0 comments on commit 8338b62

Please sign in to comment.