21 changed files with 362 additions and 655 deletions
@ -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 |
||||
|
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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 |
@ -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…
Reference in new issue