require 'spec_helper' module Wicoris::Postman describe Copier do let(:job) { double('job') } let(:copier) { Copier.new(job, {}) } describe '#run' do before do job.stub(:letter).and_return('/tmp/foo.pdf') copier.stub(:destination).and_return('/export') end context 'without noop' do let(:copier) { Copier.new(job, {}) } it 'copies the letter to destination' do copier.should_receive(:source).and_return('/chunky.pdf') copier.should_receive(:destination).and_return('/bacon.pdf') FileUtils.should_receive(:cp). with('/chunky.pdf', '/bacon.pdf', :noop => false) copier.run end end context 'with noop' do let(:copier) { Copier.new(job, :noop => true) } it 'does not copy the letter to destination' do FileUtils.should_receive(:cp). with('/tmp/foo.pdf', '/export', :noop => true) copier.run end end end describe '#source' do it 'returns the letter' do job.should_receive(:letter).and_return('/chunky/bacon.pdf') expect(copier.send(:source)).to eq '/chunky/bacon.pdf' end end describe '#fingerprint' do it 'returns the first 4 bytes of the source hexdigest' do copier.should_receive(:source).and_return('/chunky/bacon.pdf') File.should_receive(:read).with('/chunky/bacon.pdf'). and_return('content') Digest::MD5.should_receive(:hexdigest).with('content'). and_return('cafebabedeadbeef') expect(copier.send(:fingerprint)).to eq 'cafe' end end describe '#destination' do it 'returns full path from output filename' do copier.should_receive(:outdir).and_return('/chunky') copier.should_receive(:filename).and_return('bacon.pdf') expect(copier.send(:destination)).to eq '/chunky/bacon.pdf' end end describe '#outdir' do let(:copier) { Copier.new(job, {:outdir => '/chunky'}) } before do [:exists?, :directory?, :writable?].each do |method| File.stub(method).and_return(true) end end it 'returns the output dir' do expect(copier.send(:outdir)).to eq '/chunky' end it 'raises when directory does not exist' do File.should_receive(:exists?).and_return(false) expect { copier.send(:outdir) }.to raise_error( %r{output directory does not exist: /chunky}i) end it 'raises when not a directory' do File.should_receive(:directory?).and_return(false) expect { copier.send(:outdir) }.to raise_error( %r{output directory is no directory: '/chunky'}i) end it 'raises when not writable' do File.should_receive(:writable?).and_return(false) expect { copier.send(:outdir) }.to raise_error( %r{output directory not writable: '/chunky'}i) end context 'without outdir' do let(:copier) { Copier.new(job, {:outdir => nil}) } it 'raises an error' do expect { copier.send(:outdir) }.to raise_error( %r{No output directory given}i) end end end describe '#filename' do it 'returns a filename based on its components and suffix' do copier.stub(:filename_components). and_return %w(chunky bacon 42) copier.stub(:suffix).and_return('.foo') expect(copier.send(:filename)).to eq 'chunky_bacon_42.foo' end it 'raises on missing filename component' do copier.stub(:filename_components). and_return ['chunky', nil, 'bacon'] expect{ copier.send(:filename) }.to raise_error( /missing patient demographics/i) end it 'raises on empty filename component' do copier.stub(:filename_components). and_return ['chunky', '', 'bacon'] expect{ copier.send(:filename) }.to raise_error( /missing patient demographics/i) end end describe '#filename_components' do it 'orders the components by most relevant patient demographics' do job.should_receive(:patient_first_name).and_return('Chuck') job.should_receive(:patient_last_name).and_return('Norris') job.should_receive(:patient_date_of_birth).and_return('1940-03-10') copier.should_receive(:fingerprint).and_return('abcd') expect(copier.send(:filename_components)).to eq( %w(Norris Chuck 1940-03-10 abcd)) end end describe '#suffix' do it 'returns the filename suffix' do expect(copier.send(:suffix)).to eq '.pdf' end end end end