Phil Lee
2 years ago
committed by
GitHub
3 changed files with 74 additions and 0 deletions
@ -0,0 +1,33 @@ |
|||||||
|
class BulkUpload::Downloader |
||||||
|
attr_reader :bulk_upload |
||||||
|
|
||||||
|
delegate :path, to: :file |
||||||
|
|
||||||
|
def initialize(bulk_upload:) |
||||||
|
@bulk_upload = bulk_upload |
||||||
|
end |
||||||
|
|
||||||
|
def call |
||||||
|
download |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def download |
||||||
|
io = storage_service.get_file_io(bulk_upload.identifier) |
||||||
|
file.write(io.read) |
||||||
|
io.close |
||||||
|
file.close |
||||||
|
end |
||||||
|
|
||||||
|
def file |
||||||
|
@file ||= Tempfile.new |
||||||
|
end |
||||||
|
|
||||||
|
def storage_service |
||||||
|
@storage_service ||= Storage::S3Service.new( |
||||||
|
Configuration::PaasConfigurationService.new, |
||||||
|
ENV["CSV_DOWNLOAD_PAAS_INSTANCE"], |
||||||
|
) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,10 @@ |
|||||||
|
require "securerandom" |
||||||
|
|
||||||
|
FactoryBot.define do |
||||||
|
factory :bulk_upload do |
||||||
|
user |
||||||
|
log_type { BulkUpload.log_types.values.sample } |
||||||
|
year { 2022 } |
||||||
|
identifier { SecureRandom.uuid } |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,31 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe BulkUpload::Downloader do |
||||||
|
subject(:downloader) { described_class.new(bulk_upload:) } |
||||||
|
|
||||||
|
let(:bulk_upload) { build(:bulk_upload) } |
||||||
|
|
||||||
|
let(:get_file_io) do |
||||||
|
io = StringIO.new |
||||||
|
io.write("hello") |
||||||
|
io.rewind |
||||||
|
io |
||||||
|
end |
||||||
|
|
||||||
|
# rubocop:disable RSpec/PredicateMatcher |
||||||
|
describe "#call" do |
||||||
|
let(:mock_storage_service) { instance_double(Storage::S3Service, get_file_io:) } |
||||||
|
|
||||||
|
it "downloads the file as a temporary file" do |
||||||
|
allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service) |
||||||
|
|
||||||
|
downloader.call |
||||||
|
|
||||||
|
expect(mock_storage_service).to have_received(:get_file_io).with(bulk_upload.identifier) |
||||||
|
|
||||||
|
expect(File.exist?(downloader.path)).to be_truthy |
||||||
|
expect(File.read(downloader.path)).to eql("hello") |
||||||
|
end |
||||||
|
end |
||||||
|
# rubocop:enable RSpec/PredicateMatcher |
||||||
|
end |
Loading…
Reference in new issue