Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload nil tracked item #429

Merged
merged 8 commits into from
Nov 3, 2016
7 changes: 5 additions & 2 deletions app/services/disability_claim_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ def request_decision(claim)
end

# upload file to s3 and enqueue job to upload to EVSS
def upload_document(file_body, disability_claim_document)
def upload_document(file, disability_claim_document)
uploader = DisabilityClaimDocumentUploader.new(@user.uuid, disability_claim_document.tracked_item_id)
uploader.store!(file_body)
uploader.store!(file)
# the uploader sanitizes the filename before storing, so set our doc to match
# TODO: set this directly on the model, need to modify common/model/base to update attributes hash
disability_claim_document.attributes[:file_name] = uploader.filename
DisabilityClaim::DocumentUpload.perform_async(auth_headers, @user.uuid, disability_claim_document.to_h)
end

Expand Down
4 changes: 3 additions & 1 deletion app/uploaders/disability_claim_document_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def initialize(user_uuid, tracked_item_id)
end

def store_dir
"disability_claim_documents/#{@user_uuid}/#{@tracked_item_id}"
store_dir = "disability_claim_documents/#{@user_uuid}"
store_dir += "/#{@tracked_item_id}" if @tracked_item_id
store_dir
end

def extension_white_list
Expand Down
7 changes: 6 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
require 'webmock/rspec'
require 'support/factory_girl'
require 'support/serializer_spec_helper'
require 'support/carrierwave_spec_helper'
require 'support/xml_matchers'
require 'support/api_schema_matcher'
require 'support/validation_helpers'
Expand Down Expand Up @@ -44,6 +43,8 @@
Sidekiq::Testing.fake!
Sidekiq::Logging.logger = nil

CarrierWave.root = "#{Rails.root}/spec/support/uploads/"

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
Expand Down Expand Up @@ -88,6 +89,10 @@

config.include StatsD::Instrument::Matchers

config.before(:each) do
Sidekiq::Worker.clear_all
end

# clean up carrierwave uploads
# https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Cleanup-after-your-Rspec-tests
config.after(:all) do
Expand Down
23 changes: 23 additions & 0 deletions spec/services/disability_claim_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@
end
end

describe '#upload_document' do
let(:tempfile) do
f = Tempfile.new(['file with spaces', '.txt'])
f.write('test')
f.rewind
f
end
let(:document) { DisabilityClaimDocument.new(tracked_item_id: 1) }

it 'enqueues a job' do
expect do
subject.upload_document(tempfile, document)
end.to change(DisabilityClaim::DocumentUpload.jobs, :size).by(1)
end

it 'updates document with sanitized filename' do
subject.upload_document(tempfile, document)
job = DisabilityClaim::DocumentUpload.jobs.last
doc_args = job['args'].last
expect(doc_args['file_name']).to match(/file_with_spaces.*\.txt/)
end
end

# TODO: (AJM) add these tests back when turning breakers back on (post testing)
# :nocov:
context 'when EVSS client has an outage' do
Expand Down
19 changes: 0 additions & 19 deletions spec/support/carrierwave_spec_helper.rb

This file was deleted.

12 changes: 12 additions & 0 deletions spec/uploaders/disability_claim_document_uploader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
end
end

describe '#store_dir' do
it 'omits the tracked item id if it is nil' do
subject = described_class.new('1234abc', nil)
expect(subject.store_dir).to eq('disability_claim_documents/1234abc')
end

it 'includes the uuid and tracked item id' do
subject = described_class.new('1234abc', '13')
expect(subject.store_dir).to eq('disability_claim_documents/1234abc/13')
end
end

describe '#store!' do
it 'raises an error when the file is larger than 25 megabytes' do
file = double(size: 25.megabytes + 1)
Expand Down