require 'spec_helper' module Wicoris::Postman describe Job do let(:job) { Job.new('example.json') } before do FileUtils.stub(:rm) end describe '#json' do it 'parses and returns the JSON file' do job.should_receive(:json_file_content).and_return('content') JSON.should_receive(:parse).with('content').once.and_return('json') 2.times { expect(job.send(:json)).to eq 'json' } end end describe '#json_file_content' do it 'returns the file content' do File.should_receive(:read).with('example.json').and_return('content') expect(job.send(:json_file_content)).to eq 'content' end it 'converts Mac Roman encoding to UTF-8' do File.stub(:read).and_return("Schl\237ter") expect(job.send(:json_file_content)).to eq 'Schlüter' end end describe '#letter' do before do File.stub(:exists?).and_return(true) end it 'returns the letter path' do job.stub(:json).and_return('file' => '/tmp/foo.pdf') expect(job.letter).to eq '/tmp/foo.pdf' end it 'raises when letter is missing' do File.should_receive(:exists?).and_return(false) job.stub(:file).and_return('chunky') expect { job.send(:letter) }.to raise_error /letter does not exist.*chunky/i end end describe '#method_missing' do context 'with existing JSON attribute' do it 'returns it' do job.stub(:json).and_return('chunky' => 'bacon') expect(job.chunky).to eq 'bacon' end end context 'without matching JSON attribute' do it 'returns it' do job.stub(:json).and_return({}) expect { job.chunky }.to raise_error /undefined method/i end end end describe '#process' do let(:logger) { double('logger') } let(:patient) { 'Norris, Chuck (1940-03-10)' } before do job.stub(:logger).and_return(logger) job.stub(:patient).and_return(patient) logger.stub(:info) #todo: remove end context 'without errors' do it 'logs a successful delivery' do job.stub(:deliver) logger.should_receive(:info). with :message => 'Letter delivered :-D', :patient => patient, :job => 'example.json' job.process end end context 'with errors' do it 'logs a failed delivery' do job.stub(:deliver).and_return { fail 'BOOOM!' } logger.should_receive(:error). with :message => 'Letter not delivered ;-(', :patient => patient, :reason => 'BOOOM!', :job => 'example.json' logger.should_receive(:debug) job.process end end context 'when fax' do it 'faxes the letter' do job.should_receive(:json).and_return('type' => 'fax') fax_machine = double('fax_machine') FaxMachine.should_receive(:new).with(job, {}).and_return(fax_machine) fax_machine.should_receive(:run).ordered job.process end end context 'when copy' do it 'copies the letter' do job.should_receive(:json).and_return('type' => 'copy') copier = double('copier') Copier.should_receive(:new).with(job, {}).and_return(copier) copier.should_receive(:run).ordered job.process end end end describe '#patient' do it 'returns the patient display name' do job.stub(:patient_first_name).and_return('Chuck') job.stub(:patient_last_name).and_return('Norris') job.stub(:patient_date_of_birth).and_return('1940-03-10') expect(job.patient).to eq 'Norris, Chuck (1940-03-10)' end it 'returns unknown when patient infos are missing' do expect(job.patient).to eq 'Unknown' end end describe '#patient_first_name' do it 'returns the patients first name' do job.stub(:json).and_return('patient_first_name' => 'Chuck') expect(job.patient_first_name).to eq 'Chuck' end end describe '#patient_last_name' do it 'returns the patients last name' do job.stub(:json).and_return('patient_last_name' => 'Norris') expect(job.patient_last_name).to eq 'Norris' end end describe '#patient_date_of_birth' do it 'returns the patients date of birth' do job.stub(:json).and_return('patient_date_of_birth' => '1940-03-10') expect(job.patient_date_of_birth).to eq '1940-03-10' end end describe '#to_s' do it 'returns the JSON file path' do expect(job.to_s).to eq 'example.json' end end describe '#clear!' do before do job.stub(:json).and_return('') end it 'deletes the json file' do FileUtils.should_receive(:rm).with('example.json', :noop => false) job.clear! end context 'with noop' do let(:job) { Job.new('example.json', :noop => true) } it 'does not delete the json file' do FileUtils.should_receive(:rm).with('example.json', :noop => true) job.clear! end end context 'without parsable JSON' do let(:logger) { double('logger', :warn => true) } before do job.stub(:json). and_return { raise JSON::ParserError, 'Shitty JSON' } job.stub(:logger).and_return(logger) end it 'does not delete the JSON file' do FileUtils.should_not_receive(:rm) job.clear! end it 'logs a warning' do logger.should_receive(:warn).with({ :message => 'Refused to delete non-JSON file.', :json_file => 'example.json' }) job.clear! end end end end end