diff --git a/app/views/logs/bulk_upload.html.erb b/app/views/logs/bulk_upload.html.erb
index 0f1b544f7..6d06af7a3 100644
--- a/app/views/logs/bulk_upload.html.erb
+++ b/app/views/logs/bulk_upload.html.erb
@@ -1,6 +1,6 @@
<% content_for :title, "Bulk upload" %>
-<%= form_for @bulk_upload, url: bulk_upload_lettings_logs_path, method: "post" do |f| %>
+<%= form_for @bulk_upload, scope: :bulk_upload, url: bulk_upload_lettings_logs_path, method: "post" do |f| %>
<%= f.govuk_error_summary %>
<%= f.govuk_file_field :lettings_log_bulk_upload,
diff --git a/config/initializers/feature_toggle.rb b/config/initializers/feature_toggle.rb
index 8fc2cd7d4..b315ff723 100644
--- a/config/initializers/feature_toggle.rb
+++ b/config/initializers/feature_toggle.rb
@@ -26,4 +26,8 @@ class FeatureToggle
def self.bulk_upload_logs?
!Rails.env.production?
end
+
+ def self.upload_enabled?
+ !Rails.env.development?
+ end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 6c2d7db46..4f6f6c5b5 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -49,6 +49,16 @@ en:
attributes:
year:
blank: You must select a collection period to upload for
+ forms/bulk_upload_lettings/upload_your_file:
+ attributes:
+ file:
+ blank: Select which file to upload
+ not_csv: Your file must be in CSV format
+ forms/bulk_upload_sales/upload_your_file:
+ attributes:
+ file:
+ blank: Select which file to upload
+ not_csv: Your file must be in CSV format
activerecord:
errors:
diff --git a/db/migrate/20221128130843_create_bulk_uploads.rb b/db/migrate/20221128130843_create_bulk_uploads.rb
new file mode 100644
index 000000000..adb3385b5
--- /dev/null
+++ b/db/migrate/20221128130843_create_bulk_uploads.rb
@@ -0,0 +1,14 @@
+class CreateBulkUploads < ActiveRecord::Migration[7.0]
+ def change
+ create_table :bulk_uploads do |t|
+ t.references :user
+ t.text :log_type, null: false
+ t.integer :year, null: false
+ t.uuid :identifier, null: false
+
+ t.timestamps
+ end
+
+ add_index :bulk_uploads, :identifier, unique: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 70daf01b5..4f7d267ce 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -14,6 +14,17 @@ ActiveRecord::Schema[7.0].define(version: 2022_12_06_081127) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
+ create_table "bulk_uploads", force: :cascade do |t|
+ t.bigint "user_id"
+ t.text "log_type", null: false
+ t.integer "year", null: false
+ t.uuid "identifier", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["identifier"], name: "index_bulk_uploads_on_identifier", unique: true
+ t.index ["user_id"], name: "index_bulk_uploads_on_user_id"
+ end
+
create_table "data_protection_confirmations", force: :cascade do |t|
t.bigint "organisation_id"
t.bigint "data_protection_officer_id"
diff --git a/spec/features/bulk_upload_lettings_logs_spec.rb b/spec/features/bulk_upload_lettings_logs_spec.rb
index e9a05b07d..3df49fe44 100644
--- a/spec/features/bulk_upload_lettings_logs_spec.rb
+++ b/spec/features/bulk_upload_lettings_logs_spec.rb
@@ -3,13 +3,26 @@ require "rails_helper"
RSpec.describe "Bulk upload lettings log" do
let(:user) { create(:user) }
+ let(:stub_file_upload) do
+ vcap_services = { "aws-s3-bucket" => {} }
+ mock_storage_service = instance_double("S3Service")
+
+ allow(ENV).to receive(:[])
+ allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services.to_json)
+
+ allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service)
+ allow(mock_storage_service).to receive(:write_file)
+ end
+
before do
+ stub_file_upload
sign_in user
end
+ # rubocop:disable RSpec/AnyInstance
context "when during crossover period" do
it "shows journey with year option" do
- Timecop.freeze(2023, 6, 1) do
+ Timecop.freeze(2022, 6, 1) do
visit("/lettings-logs")
expect(page).to have_link("Upload lettings logs in bulk")
click_link("Upload lettings logs in bulk")
@@ -30,9 +43,30 @@ RSpec.describe "Bulk upload lettings log" do
click_button("Continue")
expect(page).to have_content("Upload your file")
+ click_button("Upload")
+
+ allow_any_instance_of(Forms::BulkUploadLettings::UploadYourFile).to receive(:`).and_return("not a csv")
+
+ expect(page).to have_content("Select which file to upload")
+ attach_file "file", file_fixture("2021_22_lettings_bulk_upload.xlsx")
+ click_button("Upload")
+
+ allow_any_instance_of(Forms::BulkUploadLettings::UploadYourFile).to receive(:`).and_return("text/csv")
+
+ expect(page).to have_content("Your file must be in CSV format")
+ attach_file "file", file_fixture("blank_bulk_upload_sales.csv")
+ expect {
+ click_button("Upload")
+ }.to change(BulkUpload, :count).by(1)
+
+ expect(page).to have_content("Once this is done")
+ click_link("Back")
+
+ expect(page).to have_content("Upload lettings logs in bulk")
end
end
end
+ # rubocop:enable RSpec/AnyInstance
context "when not it crossover period" do
it "shows journey with year option" do
diff --git a/spec/features/bulk_upload_sales_logs_spec.rb b/spec/features/bulk_upload_sales_logs_spec.rb
index 67187ff78..b69b6e237 100644
--- a/spec/features/bulk_upload_sales_logs_spec.rb
+++ b/spec/features/bulk_upload_sales_logs_spec.rb
@@ -3,13 +3,26 @@ require "rails_helper"
RSpec.describe "Bulk upload sales log" do
let(:user) { create(:user) }
+ let(:stub_file_upload) do
+ vcap_services = { "aws-s3-bucket" => {} }
+ mock_storage_service = instance_double("S3Service")
+
+ allow(ENV).to receive(:[])
+ allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services.to_json)
+
+ allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service)
+ allow(mock_storage_service).to receive(:write_file)
+ end
+
before do
+ stub_file_upload
sign_in user
end
+ # rubocop:disable RSpec/AnyInstance
context "when during crossover period" do
it "shows journey with year option" do
- Timecop.freeze(2023, 6, 1) do
+ Timecop.freeze(2023, 5, 1) do
visit("/sales-logs")
expect(page).to have_link("Upload sales logs in bulk")
click_link("Upload sales logs in bulk")
@@ -30,9 +43,30 @@ RSpec.describe "Bulk upload sales log" do
click_button("Continue")
expect(page).to have_content("Upload your file")
+ click_button("Upload")
+
+ allow_any_instance_of(Forms::BulkUploadSales::UploadYourFile).to receive(:`).and_return("not a csv")
+
+ expect(page).to have_content("Select which file to upload")
+ attach_file "file", file_fixture("2021_22_lettings_bulk_upload.xlsx")
+ click_button("Upload")
+
+ allow_any_instance_of(Forms::BulkUploadSales::UploadYourFile).to receive(:`).and_return("text/csv")
+
+ expect(page).to have_content("Your file must be in CSV format")
+ attach_file "file", file_fixture("blank_bulk_upload_sales.csv")
+ expect {
+ click_button("Upload")
+ }.to change(BulkUpload, :count).by(1)
+
+ expect(page).to have_content("Once this is done")
+ click_link("Back")
+
+ expect(page).to have_content("Upload sales logs in bulk")
end
end
end
+ # rubocop:enable RSpec/AnyInstance
context "when not it crossover period" do
it "shows journey with year option" do
diff --git a/spec/fixtures/files/blank_bulk_upload_sales.csv b/spec/fixtures/files/blank_bulk_upload_sales.csv
new file mode 100644
index 000000000..4771d9fc9
--- /dev/null
+++ b/spec/fixtures/files/blank_bulk_upload_sales.csv
@@ -0,0 +1,118 @@
+Question,What is the purchaser code?,What is the day of the sale completion date? - DD,What is the month of the sale completion date? - MM,What is the year of the sale completion date? - YY,[BLANK],Was the buyer interviewed for any of the answers you will provide on this log?,Age of Buyer 1,Age of Buyer 2 or Person 2,Age of Person 3,Age of Person 4,Age of Person 5,Age of Person 6,Gender identity of Buyer 1,Gender identity of Buyer 2 or Person 2,Gender identity of Person 3,Gender identity of Person 4,Gender identity of Person 5,Gender identity of Person 6,Person 2's relationship to lead tenant,Person 3's relationship to lead tenant,Person 4's relationship to lead tenant,Person 5's relationship to lead tenant,Person 6's relationship to lead tenant,Working situation of Buyer 1,Working situation of Buyer 2 or Person 2,Working situation of Person 3,Working situation of Person 4,Working situation of Person 5,Working situation of Person 6,What is the buyer 1's ethnic group?,What is buyer 1's nationality?,What is buyer 1's gross annual income?,What is buyer 2's gross annual income?,Was buyer 1's income used for a mortgage application?,Was buyer 2's income used for a mortgage application?,"What is the total amount the buyers had in savings before they paid any deposit for the property?
+
+To the nearest £10",Have any of the buyers previously owned a property?,[BLANK],What was buyer 1's previous tenure?,What is the local authority of buyer 1's last settled home,Part 1 of postcode of buyer 1's last settled home,Part 2 of postcode of buyer 1's last settled home,Do you know the postcode of buyer 1's last settled home?,Was the buyer registered with their PRP (HA)?,Was the buyer registered with the local authority?,Was the buyer registered with a Help to Buy agent?,Was the buyer registered with another PRP (HA)?,Does anyone in the household consider themselves to have a disability?,Does anyone in the household use a wheelchair?,How many bedrooms does the property have?,What type of unit is the property?,Which type of building is the property?,What is the local authority of the property?,Part 1 of postcode of property,Part 2 of postcode of property,Is the property built or adapted to wheelchair-user standards?,What is the type of shared ownership sale?,"Is this a resale?
+
+Shared ownership","What is the day of the practical completion or handover date? - DD
+
+Shared ownership","What is the month of the practical completion or handover date? - MM
+
+Shared ownership","What is the year of the practical completion or handover date? - YY
+
+Shared ownership","What is the day of the exchange of contracts date? - DD
+
+Shared ownership","What is the month of the exchange of contracts date? - MM
+
+Shared ownership","What is the year of the exchange of contracts date? - YY
+
+Shared ownership","Was the household re-housed under a local authority nominations agreement?
+
+Shared ownership","How many bedrooms did the buyer's previous property have?
+
+Shared ownership","What was the type of the buyer's previous property?
+
+Shared ownership","What was the full purchase price?
+
+Shared ownership","What was the initial percentage equity stake purchased?
+
+Shared ownership","What is the mortgage amount?
+
+Shared ownership","Does this include any extra borrowing?
+
+Shared ownership","How much was the cash deposit paid on the property?
+
+Shared ownership","How much cash discount was given through Social Homebuy?
+
+Shared ownership","What is the basic monthly rent?
+
+Shared ownership","What are the total monthly leasehold charges for the property?
+
+Shared ownership",What is the type of discounted ownership sale?,"What was the full purchase price?
+
+Discounted ownership","What was the amount of any loan, grant, discount or subsidy given?
+
+Discounted ownership","What was the percentage discount?
+
+Discounted ownership","What is the mortgage amount?
+
+Discounted ownership","Does this include any extra borrowing?
+
+Discounted ownership","How much was the cash deposit paid on the property?
+
+Discounted ownership","What are the total monthly leasehold charges for the property?
+
+Discounted ownership",What is the type of outright sale?,"What is the 'other' type of outright sale?
+
+Outright sale",[BLANK],"What is the full purchase price?
+
+Outright sale","What is the mortgage amount?
+
+Outright sale","Does this include any extra borrowing?
+
+Outright sale","How much was the cash deposit paid on the property?
+
+Outright sale","What are the total monthly leasehold charges for the property?
+
+Outright sale","Which organisation owned this property before the sale?
+
+Organisation's CORE ID",Username,BLANK,Has the buyer ever served in the UK Armed Forces and for how long?,[BLANK],Are any of the buyers a spouse or civil partner of a UK Armed Forces regular who died in service within the last 2 years?,"What is the name of the mortgage lender?
+
+Shared ownership","What is the name of the 'other' mortgage lender?
+
+Shared ownership","What is the name of the mortgage lender?
+
+Discounted ownership","What is the name of the 'other' mortgage lender?
+
+Discounted ownership","What is the name of the mortgage lender?
+
+Outright sale","What is the name of the 'other' mortgage lender?
+
+Outright sale",Were the buyers receiving any of these housing-related benefits immediately before buying this property?,"What is the length of the mortgage in years?
+
+Shared ownership","What is the length of the mortgage in years?
+
+Discounted ownership","What is the length of the mortgage in years?
+
+Outright sale","How long have the buyers been living in the property before the purchase?
+
+Discounted ownership",Are there more than two joint purchasers of this property?,"How long have the buyers been living in the property before the purchase?
+
+Shared ownership",Is this a staircasing transaction?,Data Protection question,Was this purchase made through an ownership scheme?,"Is the buyer a company?
+
+Outright sale",Will the buyers live in the property?,Is this a joint purchase?,Will buyer 1 live in the property?,Will buyer 2 live in the property?,"Besides the buyers, how many people live in the property?","What percentage of the property has been bought in this staircasing transaction?
+
+Shared ownership","What percentage of the property does the buyer now own in total?
+
+Shared ownership","What was the rent type of the buyer's previous property?
+
+Shared ownership","Was a mortgage used for the purchase of this property?
+
+Shared ownership","Was a mortgage used for the purchase of this property?
+
+Discounted ownership","Was a mortgage used for the purchase of this property?
+
+Outright sale"
+Values,Max 9 digits,1 - 31,1 - 12,19 - 23,,1 or null,"15 - 110
+or R",1 - 110 or R,,,,,"M, F, X or R",,,,,,"P, C, X or R",,,,,0 - 10,,,,,,1 - 19,"12 -13, 17 -19",0 - 99999,,1 or 2,1 or 2,0 - 999990,1 - 3,,1 - 7 or 9,ONS CODE - E + 9 digits,XXX(X),XXX,1 or null,,,,,1 - 3,1 - 3,1 - 9,1 - 4 or 9,1 or 2,ONS CODE E + 9 digits,XXX(X),XXX,1 - 3,"2, 16, 18, 24, 28 or 30-31",1 or 2,1 - 31,1 - 12,19 - 23,1 - 31,1 - 12,19 - 23,1 - 3,1 - 9,1 - 4 or 9,0 - 999999,0 - 100,0 - 999999,1 - 3,0 - 999999,,0 - 999.99,,"8, 9, 14, 21, 22, 27 or 29",0 - 999999,,0 - 100,0 - 999999,1 - 3,0 - 999999,0 - 999.99,10 or 12,,,0 - 999999,,1-3,0 - 999999,0-999.99,Up to 7 digits,Username of CORE account this sales log should be assigned to,,3 - 8,,4 - 7,1 - 40,,1 - 40,,1 - 40,,1 - 4, Integer <=60, Integer <=60, Integer <=60, Integer <=80,1 - 3, Integer <=80,1 - 3,1,1 - 3,1 - 2,1 - 2,1 - 2,1 - 2,1 - 2,0 - 5,1 - 100,1 - 100,1-3 or 9-10,1 - 2,1 - 2,1 - 2
+Can be Null?,No,,,,,No,No,"If fields 14, 19 and 25 are all also null","If fields 15, 20 and 26 are all also null","If fields 16, 21 and 27 are all also null","If fields 17, 22 and 28 are all also null","If fields 18, 23 and 29 are all also null",No,"If fields 8, 19 and 25 are all also null","If fields 9, 20 and 26 are also null","If fields 10, 21 and 27 are all also null","If fields 11, 22 and 28 are all also null","If fields 12, 23 and 29 are all also null","If fields 8, 14 and 25 are all also null","If fields 9, 15 and 26 are all also null","If fields 10, 16 and 27 are all also null","If fields 11, 17 and 28 are all also null","If fields 12, 18 and 29 are all also null",If field 6 = 1,"If fields 8, 14 and 19 are all also null","If fields 9, 15 and 20 are all also null","If fields 10, 16 and 21 are all also null","If fields 11, 17 and 22 are all also null","If fields 12, 18 and 23 are all also null",If field 6 = 1,,,If field 116 = 2,If field 32 is null,If field 116 = 2,If field 6 = 1,,,If field 6 = 1,No,If field 43 = 1,,If fields 41 and 42 BOTH have valid entries,Yes,,,,If field 6 = 1,,No,,,,,,,If field 113 = 2 or 3,,,,,,,,,"If field 113 = 2 or 3
+OR
+field 39 = 3 - 7 or 9",,If field 113 = 2 or 3,,,,,"If field 57 is null, 2, 16, 24 or 28",If field 113 = 2 or 3,,If field 113 = 1 or 3,If field 76 is null,"If field 76 is null, 9 or 14","If field 76 is null, 8, 21 or 22",If field 113 = 1 or 3,,,,If field 113 = 1 or 2,If field 84 is null or 10,,If field 113 = 1 or 2,,,,,No,Yes,,No,,No,If field 113 = 2 or 3,"If field 113 = 2 or 3
+OR
+If field 98 is not 40",If field 113 = 1 or 3,"If field 113 = 1 or 3
+OR
+If field 100 is not 40",If field 113 = 1 or 2,"If field 113 = 1 or 2
+OR
+If field 102 is not 40",No,If field 113 = 2 or 3,If field 113 = 1 or 3,If field 113 = 1 or 2,If field 113 = 1 or 3,If field 116 = 2,If field 113 = 2 or 3,If field 113 = 2 or 3,No,No,If field 113 = 1 or 2,If field 113 = 1 or 2,No,No,If field 116 = 2,No,If field 113 = 2 or 3,If field 113 = 2 or 3,"If field 113 = 1 or 2
+OR
+If field 39 = 3 - 9",If field 113 = 2 or 3,If field 113 = 1 or 3,If field 113 = 1 or 2
+Bulk upload format and duplicate check,Yes,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
+Field number,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125
diff --git a/spec/fixtures/files/excel_as_csv.csv b/spec/fixtures/files/excel_as_csv.csv
new file mode 100644
index 000000000..f1b7de0bb
Binary files /dev/null and b/spec/fixtures/files/excel_as_csv.csv differ
diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb
index f9d03aa36..fe979ac27 100644
--- a/spec/models/form_handler_spec.rb
+++ b/spec/models/form_handler_spec.rb
@@ -153,4 +153,20 @@ RSpec.describe FormHandler do
expect(form.type).to eq("lettings")
expect(form.start_date.year).to eq(2022)
end
+
+ # rubocop:disable RSpec/PredicateMatcher
+ describe "#in_crossover_period?" do
+ context "when not in overlapping period" do
+ it "returns false" do
+ expect(form_handler.in_crossover_period?(now: Date.new(2022, 1, 1))).to be_falsey
+ end
+ end
+
+ context "when in overlapping period" do
+ it "returns true" do
+ expect(form_handler.in_crossover_period?(now: Date.new(2022, 6, 1))).to be_truthy
+ end
+ end
+ end
+ # rubocop:enable RSpec/PredicateMatcher
end
diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb
index a9a0c9b40..110a521e2 100644
--- a/spec/models/form_spec.rb
+++ b/spec/models/form_spec.rb
@@ -237,40 +237,6 @@ RSpec.describe Form, type: :model do
end
end
- describe "#in_crossover_period?" do
- context "when now not specified" do
- context "when after end period" do
- subject(:form) { described_class.new(nil, 2022, [], "sales") }
-
- it "returns false" do
- Timecop.freeze(2023, 8, 1) do
- expect(form).not_to be_in_crossover_period
- end
- end
- end
-
- context "when during crossover" do
- subject(:form) { described_class.new(nil, 2022, [], "sales") }
-
- it "returns true" do
- Timecop.freeze(2023, 6, 1) do
- expect(form).to be_in_crossover_period
- end
- end
- end
-
- context "when before crossover" do
- subject(:form) { described_class.new(nil, 2022, [], "sales") }
-
- it "returns false" do
- Timecop.freeze(2023, 1, 1) do
- expect(form).not_to be_in_crossover_period
- end
- end
- end
- end
- end
-
describe "when creating a lettings log", :aggregate_failures do
it "creates a valid lettings form" do
form = described_class.new("spec/fixtures/forms/2021_2022.json")
diff --git a/spec/models/forms/bulk_upload_lettings/upload_your_file_spec.rb b/spec/models/forms/bulk_upload_lettings/upload_your_file_spec.rb
new file mode 100644
index 000000000..1199201fa
--- /dev/null
+++ b/spec/models/forms/bulk_upload_lettings/upload_your_file_spec.rb
@@ -0,0 +1,47 @@
+require "rails_helper"
+
+RSpec.describe Forms::BulkUploadLettings::UploadYourFile do
+ subject(:form) { described_class.new(year:, file:, current_user:) }
+
+ let(:year) { 2022 }
+ let(:actual_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
+ let(:file) { ActionDispatch::Http::UploadedFile.new(tempfile: actual_file) }
+ let(:current_user) { create(:user) }
+ let(:mock_storage_service) { instance_double("S3Service") }
+
+ before do
+ vcap_services = { "aws-s3-bucket" => {} }
+
+ allow(ENV).to receive(:[])
+ allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services.to_json)
+
+ allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service)
+ allow(mock_storage_service).to receive(:write_file)
+ end
+
+ describe "#save" do
+ it "persists a BulkUpload" do
+ expect { form.save! }.to change(BulkUpload, :count).by(1)
+ end
+
+ it "persists a BulkUpload correctly" do
+ form.save!
+
+ bulk_upload = BulkUpload.last
+
+ expect(bulk_upload.user).to eql(current_user)
+ expect(bulk_upload.log_type).to eql("lettings")
+ expect(bulk_upload.year).to eql(year)
+ expect(bulk_upload.identifier).to be_present
+ end
+
+ it "uploads file via storage service" do
+ form.save!
+
+ bulk_upload = BulkUpload.last
+
+ expect(Storage::S3Service).to have_received(:new)
+ expect(mock_storage_service).to have_received(:write_file).with(bulk_upload.identifier, actual_file.read)
+ end
+ end
+end
diff --git a/spec/models/forms/bulk_upload_sales/upload_your_file_spec.rb b/spec/models/forms/bulk_upload_sales/upload_your_file_spec.rb
new file mode 100644
index 000000000..1b11b0f82
--- /dev/null
+++ b/spec/models/forms/bulk_upload_sales/upload_your_file_spec.rb
@@ -0,0 +1,47 @@
+require "rails_helper"
+
+RSpec.describe Forms::BulkUploadSales::UploadYourFile do
+ subject(:form) { described_class.new(year:, file:, current_user:) }
+
+ let(:year) { 2022 }
+ let(:actual_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
+ let(:file) { ActionDispatch::Http::UploadedFile.new(tempfile: actual_file) }
+ let(:current_user) { create(:user) }
+ let(:mock_storage_service) { instance_double("S3Service") }
+
+ before do
+ vcap_services = { "aws-s3-bucket" => {} }
+
+ allow(ENV).to receive(:[])
+ allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services.to_json)
+
+ allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service)
+ allow(mock_storage_service).to receive(:write_file)
+ end
+
+ describe "#save" do
+ it "persists a BulkUpload" do
+ expect { form.save! }.to change(BulkUpload, :count).by(1)
+ end
+
+ it "persists a BulkUpload correctly" do
+ form.save!
+
+ bulk_upload = BulkUpload.last
+
+ expect(bulk_upload.user).to eql(current_user)
+ expect(bulk_upload.log_type).to eql("sales")
+ expect(bulk_upload.year).to eql(year)
+ expect(bulk_upload.identifier).to be_present
+ end
+
+ it "uploads file via storage service" do
+ form.save!
+
+ bulk_upload = BulkUpload.last
+
+ expect(Storage::S3Service).to have_received(:new)
+ expect(mock_storage_service).to have_received(:write_file).with(bulk_upload.identifier, actual_file.read)
+ end
+ end
+end
diff --git a/spec/requests/bulk_upload_controller_spec.rb b/spec/requests/bulk_upload_controller_spec.rb
index bc75dbe84..fc3afd21c 100644
--- a/spec/requests/bulk_upload_controller_spec.rb
+++ b/spec/requests/bulk_upload_controller_spec.rb
@@ -45,7 +45,7 @@ RSpec.describe BulkUploadController, type: :request do
end
it "returns a page with a file upload form" do
- expect(response.body).to match(/