From c8c96c363725a81a9a21b49421c8ad34982b2606 Mon Sep 17 00:00:00 2001 From: Eliot Jordan Date: Thu, 19 Dec 2024 11:22:51 -0600 Subject: [PATCH] Use start methof with block --- app/jobs/pdf_ocr_job.rb | 6 +----- spec/jobs/pdf_ocr_job_spec.rb | 10 ++-------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/app/jobs/pdf_ocr_job.rb b/app/jobs/pdf_ocr_job.rb index a0cfe31d2..5cc47a7a9 100644 --- a/app/jobs/pdf_ocr_job.rb +++ b/app/jobs/pdf_ocr_job.rb @@ -52,12 +52,8 @@ def transfer_file(path) port = Figgy.config["illiad_sftp_port"] out_path = File.join(Figgy.config["illiad_sftp_path"], "pdf", resource.filename) - begin - sftp = Net::SFTP.start(host, user, { password: pass, port: port }) + Net::SFTP.start(host, user, { password: pass, port: port }) do |sftp| sftp.upload!(path, out_path) - ensure - sftp.close_channel - sftp.session.close end end end diff --git a/spec/jobs/pdf_ocr_job_spec.rb b/spec/jobs/pdf_ocr_job_spec.rb index e95cf2757..630d58fcb 100644 --- a/spec/jobs/pdf_ocr_job_spec.rb +++ b/spec/jobs/pdf_ocr_job_spec.rb @@ -3,16 +3,12 @@ RSpec.describe PdfOcrJob do describe "#perform" do - let(:ssh_session) { instance_double(Net::SSH::Connection::Session) } let(:sftp_session) { instance_double(Net::SFTP::Session) } let(:resource) { FactoryBot.create(:ocr_request, file: fixture_path) } before do - allow(Net::SFTP).to receive(:start).and_return(sftp_session) + allow(Net::SFTP).to receive(:start).and_yield(sftp_session) allow(sftp_session).to receive(:upload!) - allow(sftp_session).to receive(:close_channel) - allow(sftp_session).to receive(:session).and_return(ssh_session) - allow(ssh_session).to receive(:close) end context "with a valid PDF" do @@ -20,9 +16,7 @@ it "creates on OCRed PDF, uploads the file to the Illiad SFTP server, and deletes the attached PDF" do described_class.perform_now(resource: resource) - expect(sftp_session).to have_received(:upload!) - expect(sftp_session).to have_received(:close_channel) - expect(ssh_session).to have_received(:close) + expect(sftp_session).to have_received(:upload!).with(/.*/, /pdf\/sample\.pdf/) ocr_request = OcrRequest.all.first expect(ocr_request.state).to eq "Complete" expect(ocr_request.pdf.attached?).to be false