Skip to content

Commit

Permalink
add kit upload macros and simplify specs
Browse files Browse the repository at this point in the history
  • Loading branch information
caitmich committed Jun 26, 2024
1 parent b2d9276 commit 4e7c20c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
33 changes: 4 additions & 29 deletions spec/jobs/kit_import_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,16 @@
require 'rails_helper'

RSpec.describe KitImportJob do
include KitUploadMacros

before do
@user = create(:user)

file = File.new(Rails.root.join('spec', 'fixtures', 'files', 'templates', 'kit.zip'))
@tmp_dir = Rails.root.join('tmp', 'rspec')
FileUtils.mkdir_p @tmp_dir

# Use a temporary file for the job instead of the original fixture
FileUtils.cp file.path, @tmp_dir
@tmp_file = File.new(@tmp_dir.join('kit.zip'))

['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
conf.save!
FileUtils.mkdir_p folder
end

allow(NoteTemplate).to receive(:pwd).and_return(
Pathname.new(Configuration.paths_templates_notes)
)
allow(Methodology).to receive(:pwd).and_return(
Pathname.new(Configuration.paths_templates_methodologies)
)
allow(ProjectTemplate).to receive(:pwd).and_return(
Pathname.new(Configuration.paths_templates_projects)
)
setup_kit_import
end

describe '#perform' do
after(:all) do
FileUtils.rm_rf(Dir.glob(Attachment.pwd + '*'))
FileUtils.rm_rf(Rails.root.join('tmp', 'rspec'))
Configuration.delete_by('name LIKE ?', 'admin:paths:%')
cleanup_kit_import
end

it 'imports kit content' do
Expand Down
37 changes: 37 additions & 0 deletions spec/support/kit_upload_macros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module KitUploadMacros
extend ActiveSupport::Concern

def setup_kit_import
file = File.new(Rails.root.join('spec', 'fixtures', 'files', 'templates', 'kit.zip'))
@tmp_dir = Rails.root.join('tmp', 'rspec')
FileUtils.mkdir_p @tmp_dir

# Use a temporary file for the job instead of the original fixture
FileUtils.cp file.path, @tmp_dir
@tmp_file = File.new(@tmp_dir.join('kit.zip'))

['methodologies', 'notes', 'plugins', 'projects', 'reports'].each do |item|
conf = Configuration.find_or_initialize_by(name: "admin:paths:templates:#{item}")
folder = @tmp_dir.join(item)
conf.value = folder
conf.save!
FileUtils.mkdir_p folder
end

allow(NoteTemplate).to receive(:pwd).and_return(
Pathname.new(Configuration.paths_templates_notes)
)
allow(Methodology).to receive(:pwd).and_return(
Pathname.new(Configuration.paths_templates_methodologies)
)
allow(ProjectTemplate).to receive(:pwd).and_return(
Pathname.new(Configuration.paths_templates_projects)
)
end

def cleanup_kit_import
FileUtils.rm_rf(Dir.glob(Attachment.pwd + '*'))
FileUtils.rm_rf(Rails.root.join('tmp', 'rspec'))
Configuration.delete_by('name LIKE ?', 'admin:paths:%')
end
end

0 comments on commit 4e7c20c

Please sign in to comment.