Skip to content

Commit

Permalink
extract file naming to helper, remove plugins step from job and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
caitmich committed Jun 26, 2024
1 parent a1a47e4 commit b2d9276
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
50 changes: 25 additions & 25 deletions app/jobs/kit_import_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class KitImportJob < ApplicationJob
'html_export' => ['html.erb'],
'word' => ['docm', 'docx']
}
TEMPLATE_TYPES = %w{ methodologies notes plugins projects }
TEMPLATE_TYPES = %w{ methodologies notes projects reports }

queue_as :dradis_upload

Expand All @@ -17,7 +17,6 @@ def perform(file_or_folder, logger:, user_id: nil)
@current_user = user_id ? User.find(user_id) : User.first
@logger = logger
@project = nil
@report_templates_dir = Configuration.paths_templates_reports
@templates_dirs = TEMPLATE_TYPES.map do |template_type|
[
template_type,
Expand All @@ -31,14 +30,14 @@ def perform(file_or_folder, logger:, user_id: nil)

import_methodology_templates
import_note_templates
import_plugin_templates
import_project_package
import_project_templates
import_report_template_files

if defined?(Dradis::Pro)
import_report_template_properties
import_rules
import_mappings

assign_project_rtp
end
Expand All @@ -49,7 +48,7 @@ def perform(file_or_folder, logger:, user_id: nil)
end

private
attr_reader :current_user, :logger, :report_templates_dir, :templates_dirs, :working_dir
attr_reader :current_user, :logger, :templates_dirs, :working_dir

def assign_project_rtp
logger.info { 'Assigning RTP to project...' }
Expand All @@ -69,6 +68,12 @@ def copy_kit_to_working_dir(source)
end
end

def import_mappings
logger.info { 'Adding Mappings...' }
mappings_seed = "#{working_dir}/kit/mappings_seed.rb"
load mappings_seed if File.exist?(mappings_seed)
end

def import_methodology_templates
logger.info { 'Copying methodology templates...' }
import_templates('methodologies')
Expand Down Expand Up @@ -113,11 +118,6 @@ def import_project_package
logger.info { " - New Project #{@project.id} created." }
end

def import_plugin_templates
logger.info { 'Copying Plugin Manager templates...' }
import_templates('plugins')
end

def import_project_templates
logger.info { 'Copying project templates...' }
import_templates('projects')
Expand All @@ -126,13 +126,13 @@ def import_project_templates
def import_report_template_files
logger.info { 'Copying report template files...' }

FileUtils.mkdir_p report_templates_dir
FileUtils.mkdir_p templates_dirs['reports']
%w{
excel
html_export
word
}.each do |plugin|
dest = "#{report_templates_dir}/#{plugin}/"
dest = "#{templates_dirs['reports']}/#{plugin}/"
temp_plugin_path = "#{working_dir}/kit/templates/reports/#{plugin}/*"

# Only allow certain file extensions
Expand All @@ -149,7 +149,7 @@ def import_report_template_properties
logger.info { 'Adding properties to report template files...' }

Dradis::Plugins.with_feature(:rtp).each do |plugin|
Dir.glob(File.join(report_templates_dir, plugin.plugin_name.to_s, '*')) do |template|
Dir.glob(File.join(templates_dirs['reports'], plugin.plugin_name.to_s, '*')) do |template|
basename = File.basename(template, '.*')
reports_dir = "#{working_dir}/kit/templates/reports"
default_properties = "#{reports_dir}/#{plugin.plugin_name}/#{basename}.rb"
Expand Down Expand Up @@ -181,24 +181,24 @@ def import_rules

def import_templates(template_type)
kit_template_dir = "#{working_dir}/kit/templates/#{template_type}"
destination = templates_dirs[template_type]
return unless Dir.exist?(kit_template_dir)
template_pwd = templates_dirs[template_type]

if template_type == 'plugins'
FileUtils.cp_r("#{kit_template_dir}/.", template_pwd)
else
Dir["#{kit_template_dir}/*"].each do |file|
return unless File.file?(file)
file_name = NamingService.name_file(
original_filename: File.basename(file),
pathname: template_pwd
)

FileUtils.cp(file, "#{template_pwd}/#{file_name}")
end
Dir["#{kit_template_dir}/*"].each do |file|
return unless File.file?(file)

file_name = name_file(File.basename(file), destination)
FileUtils.cp(file, "#{destination}/#{file_name}")
end
end

def name_file(original_filename, pathname)
NamingService.name_file(
original_filename: original_filename,
pathname: pathname
)
end

def unzip(file)
logger.info { 'Extracting zip file...' }

Expand Down
Binary file modified spec/fixtures/files/templates/kit.zip
Binary file not shown.
7 changes: 2 additions & 5 deletions spec/jobs/kit_import_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe KitImportJob do
before do
@user = create(:user)
@user = create(:user)

file = File.new(Rails.root.join('spec', 'fixtures', 'files', 'templates', 'kit.zip'))
@tmp_dir = Rails.root.join('tmp', 'rspec')
Expand All @@ -14,7 +14,7 @@
FileUtils.cp file.path, @tmp_dir
@tmp_file = File.new(@tmp_dir.join('kit.zip'))

['methodologies', 'notes', 'plugins', 'projects', 'reports'].each do |item|
['methodologies', 'notes', 'projects', 'reports'].each do |item|
conf = Configuration.find_or_initialize_by(name: "admin:paths:templates:#{item}")
folder = @tmp_dir.join(item)
conf.value = folder
Expand Down Expand Up @@ -55,9 +55,6 @@
# project template
expect(ProjectTemplate.find_template('dradis-template-welcome')).to_not be_nil

# plugin templates
expect(Dir[Configuration.paths_templates_plugins + '/nessus/*']).to_not be_empty

# report template files
expect(File.exists?(Rails.root.join('tmp', 'rspec', 'reports', 'word', 'dradis_welcome_template.v0.5.docm'))).to eq true
expect(File.exists?(Rails.root.join('tmp', 'rspec', 'reports', 'excel', 'dradis_template-excel-simple.v1.3.xlsx'))).to eq true
Expand Down

0 comments on commit b2d9276

Please sign in to comment.