Browse Source

Deduplicate BU forms

pull/2873/head
Kat 8 months ago
parent
commit
f325687bcf
  1. 10
      app/controllers/bulk_upload_lettings_logs_controller.rb
  2. 10
      app/controllers/bulk_upload_sales_logs_controller.rb
  3. 9
      app/models/forms/bulk_upload_form/checking_file.rb
  4. 5
      app/models/forms/bulk_upload_form/guidance.rb
  5. 21
      app/models/forms/bulk_upload_form/prepare_your_file.rb
  6. 13
      app/models/forms/bulk_upload_form/upload_your_file.rb
  7. 17
      app/models/forms/bulk_upload_form/year.rb
  8. 60
      app/models/forms/bulk_upload_lettings/prepare_your_file.rb
  9. 32
      app/models/forms/bulk_upload_sales/checking_file.rb
  10. 51
      app/models/forms/bulk_upload_sales/guidance.rb
  11. 83
      app/models/forms/bulk_upload_sales/upload_your_file.rb
  12. 50
      app/models/forms/bulk_upload_sales/year.rb
  13. 117
      spec/models/forms/bulk_upload_form/guidance_spec.rb
  14. 100
      spec/models/forms/bulk_upload_form/upload_your_file_spec.rb
  15. 101
      spec/models/forms/bulk_upload_form/year_spec.rb
  16. 60
      spec/models/forms/bulk_upload_lettings/guidance_spec.rb
  17. 61
      spec/models/forms/bulk_upload_lettings/upload_your_file_spec.rb
  18. 47
      spec/models/forms/bulk_upload_lettings/year_spec.rb
  19. 60
      spec/models/forms/bulk_upload_sales/guidance_spec.rb
  20. 59
      spec/models/forms/bulk_upload_sales/upload_your_file_spec.rb
  21. 51
      spec/models/forms/bulk_upload_sales/year_spec.rb

10
app/controllers/bulk_upload_lettings_logs_controller.rb

@ -58,15 +58,15 @@ private
def form
@form ||= case params[:id]
when "year"
Forms::BulkUploadLettings::Year.new(form_params)
Forms::BulkUploadForm::Year.new(form_params.merge(log_type: "lettings"))
when "prepare-your-file"
Forms::BulkUploadLettings::PrepareYourFile.new(form_params)
Forms::BulkUploadForm::PrepareYourFile.new(form_params.merge(log_type: "lettings"))
when "guidance"
Forms::BulkUploadLettings::Guidance.new(form_params.merge(referrer: params[:referrer]))
Forms::BulkUploadForm::Guidance.new(form_params.merge(referrer: params[:referrer], log_type: "lettings"))
when "upload-your-file"
Forms::BulkUploadLettings::UploadYourFile.new(form_params.merge(current_user:))
Forms::BulkUploadForm::UploadYourFile.new(form_params.merge(current_user:, log_type: "lettings"))
when "checking-file"
Forms::BulkUploadLettings::CheckingFile.new(form_params)
Forms::BulkUploadForm::CheckingFile.new(form_params.merge(log_type: "lettings"))
else
raise "Page not found for path #{params[:id]}"
end

10
app/controllers/bulk_upload_sales_logs_controller.rb

@ -58,15 +58,15 @@ private
def form
@form ||= case params[:id]
when "year"
Forms::BulkUploadSales::Year.new(form_params)
Forms::BulkUploadForm::Year.new(form_params.merge(log_type: "sales"))
when "prepare-your-file"
Forms::BulkUploadSales::PrepareYourFile.new(form_params)
Forms::BulkUploadForm::PrepareYourFile.new(form_params.merge(log_type: "sales"))
when "guidance"
Forms::BulkUploadSales::Guidance.new(form_params.merge(referrer: params[:referrer]))
Forms::BulkUploadForm::Guidance.new(form_params.merge(referrer: params[:referrer], log_type: "sales"))
when "upload-your-file"
Forms::BulkUploadSales::UploadYourFile.new(form_params.merge(current_user:))
Forms::BulkUploadForm::UploadYourFile.new(form_params.merge(current_user:, log_type: "sales"))
when "checking-file"
Forms::BulkUploadSales::CheckingFile.new(form_params)
Forms::BulkUploadForm::CheckingFile.new(form_params.merge(log_type: "sales"))
else
raise "Page not found for path #{params[:id]}"
end

9
app/models/forms/bulk_upload_lettings/checking_file.rb → app/models/forms/bulk_upload_form/checking_file.rb

@ -1,22 +1,23 @@
module Forms
module BulkUploadLettings
module BulkUploadForm
class CheckingFile
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :log_type
attribute :year, :integer
attribute :organisation_id, :integer
def view_path
"bulk_upload_lettings_logs/forms/checking_file"
"bulk_upload_#{log_type}_logs/forms/checking_file"
end
def back_path
if organisation_id.present?
lettings_logs_organisation_path(organisation_id)
send("#{log_type}_logs_organisation_path", organisation_id)
else
bulk_upload_lettings_log_path(id: "start")
send("bulk_upload_#{log_type}_log_path", id: "start")
end
end

5
app/models/forms/bulk_upload_lettings/guidance.rb → app/models/forms/bulk_upload_form/guidance.rb

@ -1,11 +1,12 @@
module Forms
module BulkUploadLettings
module BulkUploadForm
class Guidance
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
include CollectionTimeHelper
attribute :log_type
attribute :year, :integer
attribute :referrer
attribute :organisation_id, :integer
@ -23,7 +24,7 @@ module Forms
def back_path
case referrer
when "prepare-your-file"
bulk_upload_lettings_log_path(id: "prepare-your-file", form: { year:, organisation_id: }.compact)
send("bulk_upload_#{log_type}_log_path", id: "prepare-your-file", form: { year:, organisation_id: }.compact)
when "home"
root_path
else

21
app/models/forms/bulk_upload_sales/prepare_your_file.rb → app/models/forms/bulk_upload_form/prepare_your_file.rb

@ -1,42 +1,43 @@
module Forms
module BulkUploadSales
module BulkUploadForm
class PrepareYourFile
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :log_type
attribute :year, :integer
attribute :organisation_id, :integer
def view_path
case year
when 2023
"bulk_upload_sales_logs/forms/prepare_your_file_2023"
"bulk_upload_#{log_type}_logs/forms/prepare_your_file_2023"
when 2024
"bulk_upload_sales_logs/forms/prepare_your_file_2024"
"bulk_upload_#{log_type}_logs/forms/prepare_your_file_2024"
end
end
def back_path
if have_choice_of_year?
Rails.application.routes.url_helpers.bulk_upload_sales_log_path(id: "year", form: { year: }.compact)
Rails.application.routes.url_helpers.send("bulk_upload_#{log_type}_log_path", id: "year", form: { year: }.compact)
elsif organisation_id.present?
sales_logs_organisation_path(organisation_id)
send("#{log_type}_logs_organisation_path", organisation_id)
else
Rails.application.routes.url_helpers.sales_logs_path
Rails.application.routes.url_helpers.send("#{log_type}_logs_path")
end
end
def next_path
bulk_upload_sales_log_path(id: "upload-your-file", form: { year:, organisation_id: }.compact)
send("bulk_upload_#{log_type}_log_path", id: "upload-your-file", form: { year:, organisation_id: }.compact)
end
def template_path
download_mandatory_collection_resource_path(year:, log_type: "sales", resource_type: "bulk_upload_template")
download_mandatory_collection_resource_path(year:, log_type:, resource_type: "bulk_upload_template")
end
def specification_path
download_mandatory_collection_resource_path(year:, log_type: "sales", resource_type: "bulk_upload_specification")
download_mandatory_collection_resource_path(year:, log_type:, resource_type: "bulk_upload_specification")
end
def year_combo
@ -52,7 +53,7 @@ module Forms
def have_choice_of_year?
return true if FeatureToggle.allow_future_form_use?
FormHandler.instance.sales_in_crossover_period?
FormHandler.instance.send("#{log_type}_in_crossover_period?")
end
end
end

13
app/models/forms/bulk_upload_lettings/upload_your_file.rb → app/models/forms/bulk_upload_form/upload_your_file.rb

@ -1,14 +1,14 @@
require "shellwords"
module Forms
module BulkUploadLettings
module BulkUploadForm
class UploadYourFile
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :log_type
attribute :year, :integer
attribute :needstype, :integer
attribute :file
attribute :current_user
attribute :organisation_id, :integer
@ -18,11 +18,11 @@ module Forms
validate :validate_file_size
def view_path
"bulk_upload_lettings_logs/forms/upload_your_file"
"bulk_upload_#{log_type}_logs/forms/upload_your_file"
end
def back_path
bulk_upload_lettings_log_path(id: "prepare-your-file", form: { year:, needstype:, organisation_id: }.compact)
send("bulk_upload_#{log_type}_log_path", id: "prepare-your-file", form: { year:, organisation_id: }.compact)
end
def year_combo
@ -30,15 +30,14 @@ module Forms
end
def next_path
bulk_upload_lettings_log_path(id: "checking-file", form: { year:, organisation_id: }.compact)
send("bulk_upload_#{log_type}_log_path", id: "checking-file", form: { year:, organisation_id: }.compact)
end
def save!
bulk_upload = BulkUpload.create!(
user: current_user,
log_type: BulkUpload.log_types[:lettings],
log_type: BulkUpload.log_types[log_type.to_sym],
year:,
needstype:,
filename: file.original_filename,
organisation_id: (organisation_id if current_user.support?) || current_user.organisation_id,
)

17
app/models/forms/bulk_upload_lettings/year.rb → app/models/forms/bulk_upload_form/year.rb

@ -1,17 +1,18 @@
module Forms
module BulkUploadLettings
module BulkUploadForm
class Year
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :log_type
attribute :year, :integer
attribute :organisation_id, :integer
validates :year, presence: true
def view_path
"bulk_upload_lettings_logs/forms/year"
"bulk_upload_#{log_type}_logs/forms/year"
end
def options
@ -22,14 +23,14 @@ module Forms
def back_path
if organisation_id.present?
lettings_logs_organisation_path(organisation_id)
send("#{log_type}_logs_organisation_path", organisation_id)
else
lettings_logs_path
send("#{log_type}_logs_path")
end
end
def next_path
bulk_upload_lettings_log_path(id: "prepare-your-file", form: { year:, organisation_id: }.compact)
send("bulk_upload_#{log_type}_log_path", id: "prepare-your-file", form: { year:, organisation_id: }.compact)
end
def save!
@ -40,9 +41,9 @@ module Forms
def possible_years
[
FormHandler.instance.lettings_forms["current_lettings"].start_date.year,
(FormHandler.instance.previous_lettings_form.start_date.year if FormHandler.instance.lettings_in_crossover_period?),
(FormHandler.instance.next_lettings_form.start_date.year if FeatureToggle.allow_future_form_use?),
FormHandler.instance.send("#{log_type}_forms")["current_#{log_type}"].start_date.year,
(FormHandler.instance.send("previous_#{log_type}_form").start_date.year if FormHandler.instance.send("#{log_type}_in_crossover_period?")),
(FormHandler.instance.send("next_#{log_type}_form").start_date.year if FeatureToggle.allow_future_form_use?),
].compact
end
end

60
app/models/forms/bulk_upload_lettings/prepare_your_file.rb

@ -1,60 +0,0 @@
module Forms
module BulkUploadLettings
class PrepareYourFile
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :year, :integer
attribute :needstype, :integer
attribute :organisation_id, :integer
def view_path
case year
when 2023
"bulk_upload_lettings_logs/forms/prepare_your_file_2023"
when 2024
"bulk_upload_lettings_logs/forms/prepare_your_file_2024"
end
end
def back_path
if have_choice_of_year?
Rails.application.routes.url_helpers.bulk_upload_lettings_log_path(id: "year", form: { year:, organisation_id: }.compact)
elsif organisation_id.present?
lettings_logs_organisation_path(organisation_id)
else
Rails.application.routes.url_helpers.lettings_logs_path
end
end
def next_path
bulk_upload_lettings_log_path(id: "upload-your-file", form: { year:, needstype:, organisation_id: }.compact)
end
def template_path
download_mandatory_collection_resource_path(year:, log_type: "lettings", resource_type: "bulk_upload_template")
end
def specification_path
download_mandatory_collection_resource_path(year:, log_type: "lettings", resource_type: "bulk_upload_specification")
end
def year_combo
"#{year} to #{year + 1}"
end
def save!
true
end
private
def have_choice_of_year?
return true if FeatureToggle.allow_future_form_use?
FormHandler.instance.lettings_in_crossover_period?
end
end
end
end

32
app/models/forms/bulk_upload_sales/checking_file.rb

@ -1,32 +0,0 @@
module Forms
module BulkUploadSales
class CheckingFile
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :year, :integer
attribute :organisation_id, :integer
def view_path
"bulk_upload_sales_logs/forms/checking_file"
end
def back_path
if organisation_id.present?
sales_logs_organisation_path(organisation_id)
else
bulk_upload_sales_log_path(id: "start")
end
end
def year_combo
"#{year} to #{year + 1}"
end
def save!
true
end
end
end
end

51
app/models/forms/bulk_upload_sales/guidance.rb

@ -1,51 +0,0 @@
module Forms
module BulkUploadSales
class Guidance
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
include CollectionTimeHelper
attribute :year, :integer
attribute :referrer
attribute :organisation_id, :integer
def initialize(params)
super(params)
self.year = current_collection_start_year if year.nil?
end
def view_path
"bulk_upload_shared/guidance"
end
def back_path
case referrer
when "prepare-your-file"
bulk_upload_sales_log_path(id: "prepare-your-file", form: { year:, organisation_id: }.compact)
when "home"
root_path
else
guidance_path
end
end
def lettings_template_path
Forms::BulkUploadLettings::PrepareYourFile.new(year:).template_path
end
def lettings_specification_path
Forms::BulkUploadLettings::PrepareYourFile.new(year:).specification_path
end
def sales_template_path
Forms::BulkUploadSales::PrepareYourFile.new(year:).template_path
end
def sales_specification_path
Forms::BulkUploadSales::PrepareYourFile.new(year:).specification_path
end
end
end
end

83
app/models/forms/bulk_upload_sales/upload_your_file.rb

@ -1,83 +0,0 @@
require "shellwords"
module Forms
module BulkUploadSales
class UploadYourFile
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :year, :integer
attribute :file
attribute :current_user
attribute :organisation_id, :integer
validates :file, presence: true
validate :validate_file_is_csv
validate :validate_file_size
def view_path
"bulk_upload_sales_logs/forms/upload_your_file"
end
def back_path
bulk_upload_sales_log_path(id: "prepare-your-file", form: { year:, organisation_id: }.compact)
end
def year_combo
"#{year} to #{year + 1}"
end
def next_path
bulk_upload_sales_log_path(id: "checking-file", form: { year:, organisation_id: }.compact)
end
def save!
bulk_upload = BulkUpload.create!(
user: current_user,
log_type: BulkUpload.log_types[:sales],
year:,
filename: file.original_filename,
organisation_id: (organisation_id if current_user.support?) || current_user.organisation_id,
)
storage_service.write_file(bulk_upload.identifier, File.read(file.path))
ProcessBulkUploadJob.perform_later(bulk_upload:)
true
end
private
def storage_service
@storage_service ||= if FeatureToggle.upload_enabled?
Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["BULK_UPLOAD_BUCKET"])
else
Storage::LocalDiskService.new
end
end
def validate_file_is_csv
return unless file
argv = %W[file --brief --mime-type -- #{file.path}]
output = `#{argv.shelljoin}`
unless output.match?(/text\/csv|text\/plain/)
errors.add(:file, :not_csv)
end
end
MAX_FILE_SIZE = 10.megabytes
def validate_file_size
return unless file
if file.size > MAX_FILE_SIZE
errors.add(:file, :file_too_large)
end
end
end
end
end

50
app/models/forms/bulk_upload_sales/year.rb

@ -1,50 +0,0 @@
module Forms
module BulkUploadSales
class Year
include ActiveModel::Model
include ActiveModel::Attributes
include Rails.application.routes.url_helpers
attribute :year, :integer
attribute :organisation_id, :integer
validates :year, presence: true
def view_path
"bulk_upload_sales_logs/forms/year"
end
def options
possible_years.map do |year|
OpenStruct.new(id: year, name: "#{year} to #{year + 1}")
end
end
def back_path
if organisation_id.present?
sales_logs_organisation_path(organisation_id)
else
sales_logs_path
end
end
def next_path
bulk_upload_sales_log_path(id: "prepare-your-file", form: { year:, organisation_id: }.compact)
end
def save!
true
end
private
def possible_years
[
FormHandler.instance.current_sales_form.start_date.year,
(FormHandler.instance.previous_sales_form.start_date.year if FormHandler.instance.sales_in_crossover_period?),
(FormHandler.instance.next_sales_form.start_date.year if FeatureToggle.allow_future_form_use?),
].compact
end
end
end
end

117
spec/models/forms/bulk_upload_form/guidance_spec.rb

@ -0,0 +1,117 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadForm::Guidance do
include Rails.application.routes.url_helpers
subject(:bu_guidance) { described_class.new(year:, referrer:, log_type:) }
let(:year) { 2024 }
let(:log_type) { "sales" }
let(:referrer) { nil }
describe "sales" do
describe "#back_path" do
context "when referrer is prepare-your-file" do
let(:referrer) { "prepare-your-file" }
it "returns the prepare your file path" do
expect(bu_guidance.back_path).to eq bulk_upload_sales_log_path(id: "prepare-your-file", form: { year: })
end
end
context "when referrer is home" do
let(:referrer) { "home" }
it "returns the root path" do
expect(bu_guidance.back_path).to eq root_path
end
end
context "when referrer is guidance" do
let(:referrer) { "guidance" }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
context "when referrer is absent" do
let(:referrer) { nil }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
end
describe "year" do
context "when year is not provided" do
let(:year) { nil }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionTimeHelper).to receive(:current_collection_start_year).and_return(2030)
# rubocop:enable RSpec/AnyInstance
end
it "is set to the current collection start year" do
expect(bu_guidance.year).to eq(2030)
end
end
end
end
describe "lettings" do
let(:log_type) { "lettings" }
describe "#back_path" do
context "when referrer is prepare-your-file" do
let(:referrer) { "prepare-your-file" }
it "returns the prepare your file path" do
expect(bu_guidance.back_path).to eq bulk_upload_lettings_log_path(id: "prepare-your-file", form: { year: })
end
end
context "when referrer is home" do
let(:referrer) { "home" }
it "returns the root path" do
expect(bu_guidance.back_path).to eq root_path
end
end
context "when referrer is guidance" do
let(:referrer) { "guidance" }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
context "when referrer is absent" do
let(:referrer) { nil }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
end
describe "year" do
context "when year is not provided" do
let(:year) { nil }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionTimeHelper).to receive(:current_collection_start_year).and_return(2030)
# rubocop:enable RSpec/AnyInstance
end
it "is set to the current collection start year" do
expect(bu_guidance.year).to eq(2030)
end
end
end
end
end

100
spec/models/forms/bulk_upload_form/upload_your_file_spec.rb

@ -0,0 +1,100 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadForm::UploadYourFile do
subject(:form) { described_class.new(year:, file:, current_user:, log_type:) }
let(:year) { 2022 }
let(:actual_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
let(:file) do
ActionDispatch::Http::UploadedFile.new(
tempfile: actual_file,
filename: "my-file.csv",
)
end
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 "lettings" do
let(:log_type) { "lettings" }
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.filename).to eql("my-file.csv")
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
it "enqueues job to process bulk upload" do
expect {
form.save!
}.to have_enqueued_job(ProcessBulkUploadJob)
end
end
end
describe "sales" do
let(:log_type) { "sales" }
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.filename).to eql("my-file.csv")
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
it "enqueues job to process bulk upload" do
expect {
form.save!
}.to have_enqueued_job(ProcessBulkUploadJob)
end
end
end
end

101
spec/models/forms/bulk_upload_form/year_spec.rb

@ -0,0 +1,101 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadForm::Year do
subject(:form) { described_class.new(log_type:) }
describe "lettings" do
let(:log_type) { "lettings" }
describe "#options" do
before do
allow(FormHandler.instance).to receive(:lettings_forms).and_return({ "current_lettings" => instance_double(Form, start_date: Time.zone.local(2024, 4, 1)) })
allow(FormHandler.instance).to receive(:previous_lettings_form).and_return(instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))
allow(FormHandler.instance).to receive(:next_lettings_form).and_return(instance_double(Form, start_date: Time.zone.local(2025, 4, 1)))
end
context "when in a crossover period" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
end
it "returns current and previous years" do
expect(form.options.map(&:id)).to eql([2024, 2023])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2023 to 2024"])
end
end
context "when not in a crossover period" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
end
it "returns the current year" do
expect(form.options.map(&:id)).to eql([2024])
expect(form.options.map(&:name)).to eql(["2024 to 2025"])
end
end
context "when allow_future_form_use is toggled on" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true)
end
it "returns current and next years" do
expect(form.options.map(&:id)).to eql([2024, 2025])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2025 to 2026"])
end
end
end
end
describe "sales" do
let(:log_type) { "sales" }
describe "#options" do
before do
allow(FormHandler.instance).to receive(:sales_forms).and_return({ "current_sales" => instance_double(Form, start_date: Time.zone.local(2024, 4, 1)) })
allow(FormHandler.instance).to receive(:previous_sales_form).and_return(instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))
allow(FormHandler.instance).to receive(:next_sales_form).and_return(instance_double(Form, start_date: Time.zone.local(2025, 4, 1)))
end
context "when in a crossover period" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true)
end
it "returns current and previous years" do
expect(form.options.map(&:id)).to eql([2024, 2023])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2023 to 2024"])
end
end
context "when not in a crossover period" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
end
it "returns the current year" do
expect(form.options.map(&:id)).to eql([2024])
expect(form.options.map(&:name)).to eql(["2024 to 2025"])
end
end
context "when allow_future_form_use is toggled on" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true)
end
after do
Timecop.return
end
it "returns current and next years" do
expect(form.options.map(&:id)).to eql([2024, 2025])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2025 to 2026"])
end
end
end
end
end

60
spec/models/forms/bulk_upload_lettings/guidance_spec.rb

@ -1,60 +0,0 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadLettings::Guidance do
include Rails.application.routes.url_helpers
subject(:bu_guidance) { described_class.new(year:, referrer:) }
let(:year) { 2024 }
let(:referrer) { nil }
describe "#back_path" do
context "when referrer is prepare-your-file" do
let(:referrer) { "prepare-your-file" }
it "returns the prepare your file path" do
expect(bu_guidance.back_path).to eq bulk_upload_lettings_log_path(id: "prepare-your-file", form: { year: })
end
end
context "when referrer is home" do
let(:referrer) { "home" }
it "returns the root path" do
expect(bu_guidance.back_path).to eq root_path
end
end
context "when referrer is guidance" do
let(:referrer) { "guidance" }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
context "when referrer is absent" do
let(:referrer) { nil }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
end
describe "year" do
context "when year is not provided" do
let(:year) { nil }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionTimeHelper).to receive(:current_collection_start_year).and_return(2030)
# rubocop:enable RSpec/AnyInstance
end
it "is set to the current collection start year" do
expect(bu_guidance.year).to eq(2030)
end
end
end
end

61
spec/models/forms/bulk_upload_lettings/upload_your_file_spec.rb

@ -1,61 +0,0 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadLettings::UploadYourFile do
subject(:form) { described_class.new(year:, needstype:, file:, current_user:) }
let(:year) { 2022 }
let(:needstype) { 2 }
let(:actual_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
let(:file) do
ActionDispatch::Http::UploadedFile.new(
tempfile: actual_file,
filename: "my-file.csv",
)
end
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.needstype).to eql(needstype)
expect(bulk_upload.filename).to eql("my-file.csv")
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
it "enqueues job to process bulk upload" do
expect {
form.save!
}.to have_enqueued_job(ProcessBulkUploadJob)
end
end
end

47
spec/models/forms/bulk_upload_lettings/year_spec.rb

@ -1,47 +0,0 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadLettings::Year do
subject(:form) { described_class.new }
describe "#options" do
before do
allow(FormHandler.instance).to receive(:lettings_forms).and_return({ "current_lettings" => instance_double(Form, start_date: Time.zone.local(2024, 4, 1)) })
allow(FormHandler.instance).to receive(:previous_lettings_form).and_return(instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))
allow(FormHandler.instance).to receive(:next_lettings_form).and_return(instance_double(Form, start_date: Time.zone.local(2025, 4, 1)))
end
context "when in a crossover period" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
end
it "returns current and previous years" do
expect(form.options.map(&:id)).to eql([2024, 2023])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2023 to 2024"])
end
end
context "when not in a crossover period" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
end
it "returns the current year" do
expect(form.options.map(&:id)).to eql([2024])
expect(form.options.map(&:name)).to eql(["2024 to 2025"])
end
end
context "when allow_future_form_use is toggled on" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true)
end
it "returns current and next years" do
expect(form.options.map(&:id)).to eql([2024, 2025])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2025 to 2026"])
end
end
end
end

60
spec/models/forms/bulk_upload_sales/guidance_spec.rb

@ -1,60 +0,0 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadSales::Guidance do
include Rails.application.routes.url_helpers
subject(:bu_guidance) { described_class.new(year:, referrer:) }
let(:year) { 2024 }
let(:referrer) { nil }
describe "#back_path" do
context "when referrer is prepare-your-file" do
let(:referrer) { "prepare-your-file" }
it "returns the prepare your file path" do
expect(bu_guidance.back_path).to eq bulk_upload_sales_log_path(id: "prepare-your-file", form: { year: })
end
end
context "when referrer is home" do
let(:referrer) { "home" }
it "returns the root path" do
expect(bu_guidance.back_path).to eq root_path
end
end
context "when referrer is guidance" do
let(:referrer) { "guidance" }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
context "when referrer is absent" do
let(:referrer) { nil }
it "returns the main guidance page path" do
expect(bu_guidance.back_path).to eq guidance_path
end
end
end
describe "year" do
context "when year is not provided" do
let(:year) { nil }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionTimeHelper).to receive(:current_collection_start_year).and_return(2030)
# rubocop:enable RSpec/AnyInstance
end
it "is set to the current collection start year" do
expect(bu_guidance.year).to eq(2030)
end
end
end
end

59
spec/models/forms/bulk_upload_sales/upload_your_file_spec.rb

@ -1,59 +0,0 @@
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) do
ActionDispatch::Http::UploadedFile.new(
tempfile: actual_file,
filename: "my-file.csv",
)
end
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.filename).to eql("my-file.csv")
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
it "enqueues job to process bulk upload" do
expect {
form.save!
}.to have_enqueued_job(ProcessBulkUploadJob)
end
end
end

51
spec/models/forms/bulk_upload_sales/year_spec.rb

@ -1,51 +0,0 @@
require "rails_helper"
RSpec.describe Forms::BulkUploadSales::Year do
subject(:form) { described_class.new }
describe "#options" do
before do
allow(FormHandler.instance).to receive(:sales_forms).and_return({ "current_sales" => instance_double(Form, start_date: Time.zone.local(2024, 4, 1)) })
allow(FormHandler.instance).to receive(:previous_sales_form).and_return(instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))
allow(FormHandler.instance).to receive(:next_sales_form).and_return(instance_double(Form, start_date: Time.zone.local(2025, 4, 1)))
end
context "when in a crossover period" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true)
end
it "returns current and previous years" do
expect(form.options.map(&:id)).to eql([2024, 2023])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2023 to 2024"])
end
end
context "when not in a crossover period" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
end
it "returns the current year" do
expect(form.options.map(&:id)).to eql([2024])
expect(form.options.map(&:name)).to eql(["2024 to 2025"])
end
end
context "when allow_future_form_use is toggled on" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true)
end
after do
Timecop.return
end
it "returns current and next years" do
expect(form.options.map(&:id)).to eql([2024, 2025])
expect(form.options.map(&:name)).to eql(["2024 to 2025", "2025 to 2026"])
end
end
end
end
Loading…
Cancel
Save