-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SIMPLE_FORMS] feat: add logic to properly upload files to S3 (#18572)
* add logic to properly upload files to S3 * minor tweaks * update CODEOWNERS file * add test coverage * update CODEOWNERS file for spec file
- Loading branch information
Showing
4 changed files
with
88 additions
and
8 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/uploaders/veteran_facing_forms_remediation_uploader.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
class VeteranFacingFormsRemediationUploader < CarrierWave::Uploader::Base | ||
include SetAWSConfig | ||
include UploaderVirusScan | ||
|
||
def size_range | ||
(1.byte)...(100_000_000.bytes) | ||
end | ||
|
||
# All the same files allowed by benefits intake, with the | ||
# addition of json for metadata and csv for manifest | ||
def extension_allowlist | ||
%w[bmp csv gif jpeg jpg json pdf png tif tiff txt] | ||
end | ||
|
||
def initialize(benefits_intake_uuid, directory) | ||
raise 'The benefits_intake_uuid is missing.' if benefits_intake_uuid.blank? | ||
|
||
super | ||
@benefits_intake_uuid = benefits_intake_uuid | ||
@directory = directory | ||
|
||
set_storage_options! | ||
end | ||
|
||
def store_dir | ||
raise 'The s3 directory is missing.' if @directory.blank? | ||
|
||
@directory | ||
end | ||
|
||
def set_storage_options! | ||
# TODO: update this to vff specific S3 bucket once it has been created | ||
s3_settings = Settings.reports.aws | ||
# defaults to CarrierWave::Storage::File if not AWS unless a real aws_access_key_id is set | ||
if s3_settings.aws_access_key_id.present? | ||
set_aws_config( | ||
s3_settings.aws_access_key_id, | ||
s3_settings.aws_secret_access_key, | ||
s3_settings.region, | ||
s3_settings.bucket | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
spec/uploaders/veteran_facing_forms_remediation_uploader_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe VeteranFacingFormsRemediationUploader do | ||
subject { described_class.new(benefits_intake_uuid, directory) } | ||
|
||
let(:benefits_intake_uuid) { SecureRandom.uuid } | ||
let(:directory) { '/some/path' } | ||
|
||
it 'allows image, pdf, json, csv, and text files' do | ||
expect(subject.extension_allowlist).to match_array %w[bmp csv gif jpeg jpg json pdf png tif tiff txt] | ||
end | ||
|
||
it 'returns a store directory containing benefits_intake_uuid' do | ||
expect(subject.store_dir).to eq(directory) | ||
end | ||
|
||
it 'throws an error if no benefits_intake_uuid is given' do | ||
expect { described_class.new(nil, directory) }.to raise_error(RuntimeError, 'The benefits_intake_uuid is missing.') | ||
end | ||
|
||
it 'throws an error if no directory is given' do | ||
malformed_uploader = described_class.new(benefits_intake_uuid, nil) | ||
expect { malformed_uploader.store_dir }.to raise_error(RuntimeError, 'The s3 directory is missing.') | ||
end | ||
end |