diff --git a/.github/workflows/staging_pipeline.yml b/.github/workflows/staging_pipeline.yml index e97247d53..0f1352ffb 100644 --- a/.github/workflows/staging_pipeline.yml +++ b/.github/workflows/staging_pipeline.yml @@ -72,7 +72,7 @@ jobs: - name: Run tests run: | - bundle exec rake parallel:spec['spec\/(?!features)'] + bundle exec rake parallel:spec['spec\/(?!features|models|requests)'] feature_test: name: Feature Tests @@ -132,6 +132,123 @@ jobs: run: | bundle exec rspec spec/features --fail-fast + model_test: + name: Model tests + runs-on: ubuntu-latest + + services: + postgres: + image: postgres:13.5 + env: + POSTGRES_PASSWORD: password + POSTGRES_USER: postgres + POSTGRES_DB: data_collector + ports: + - 5432:5432 + # Needed because the Postgres container does not provide a health check + # tmpfs makes database faster by using RAM + options: >- + --mount type=tmpfs,destination=/var/lib/postgresql/data + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + env: + RAILS_ENV: test + GEMFILE_RUBY_VERSION: 3.1.1 + DB_HOST: localhost + DB_DATABASE: data_collector + DB_USERNAME: postgres + DB_PASSWORD: password + RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }} + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: 20 + + - name: Create database + run: | + bundle exec rake db:prepare + + - name: Compile assets + run: | + bundle exec rake assets:precompile + + - name: Run tests + run: | + bundle exec rspec spec/models --fail-fast + + requests_test: + name: Requests tests + runs-on: ubuntu-latest + + services: + postgres: + image: postgres:13.5 + env: + POSTGRES_PASSWORD: password + POSTGRES_USER: postgres + POSTGRES_DB: data_collector + ports: + - 5432:5432 + # Needed because the Postgres container does not provide a health check + # tmpfs makes database faster by using RAM + options: >- + --mount type=tmpfs,destination=/var/lib/postgresql/data + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + env: + RAILS_ENV: test + GEMFILE_RUBY_VERSION: 3.1.1 + DB_HOST: localhost + DB_DATABASE: data_collector + DB_USERNAME: postgres + DB_PASSWORD: password + RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }} + PARALLEL_TEST_PROCESSORS: 4 + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: 20 + + - name: Create database + run: | + bundle exec rake parallel:setup + + - name: Compile assets + run: | + bundle exec rake assets:precompile + + - name: Run tests + run: | + bundle exec rake parallel:spec['spec/requests'] + lint: name: Lint runs-on: ubuntu-latest @@ -179,7 +296,7 @@ jobs: aws_deploy: name: AWS Deploy if: github.ref == 'refs/heads/main' - needs: [lint, test, feature_test, audit] + needs: [lint, test, feature_test, requests_test, model_test, audit] uses: ./.github/workflows/aws_deploy.yml with: aws_account_id: 107155005276 diff --git a/db/seeds.rb b/db/seeds.rb index e478055d9..38125d8d1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -255,7 +255,7 @@ unless Rails.env.test? assigned_to: support_user, owning_organisation: org, managing_organisation: org, - saledate: Date.new(2023, 4, 1), + saledate: Time.zone.today, purchid: "1", ownershipsch: 1, type: 2, @@ -267,7 +267,7 @@ unless Rails.env.test? assigned_to: support_user, owning_organisation: org, managing_organisation: org, - saledate: Date.new(2023, 4, 1), + saledate: Time.zone.today, purchid: "1", ownershipsch: 2, type: 9, @@ -279,7 +279,7 @@ unless Rails.env.test? assigned_to: support_user, owning_organisation: org, managing_organisation: org, - saledate: Date.new(2023, 4, 1), + saledate: Time.zone.today, purchid: "1", ownershipsch: 3, type: 10, diff --git a/docs/testing.md b/docs/testing.md index 57e40f747..0c7e62571 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -31,3 +31,52 @@ bundle exec rake parallel:setup ```sh RAILS_ENV=test bundle exec rake parallel:spec ``` + +## Factories for Lettings Log, Sales Log, Organisation, and User +Each of these factories has nested relationships and callbacks that ensure associated objects are created and linked properly. For instance, creating a `lettings_log` involves creating or associating with a `user`, which in turn is linked to an `organisation`, potentially leading to creating `organisation_rent_periods` and a `data_protection_confirmation`. + +This documentation outlines the objects that are created and/or persisted to the database when using FactoryBot to create or build models for LettingsLog, SalesLog, Organisation, and User. There are other factories, but they are simpler, less frequently used and don't have as much resource hierarchy. + +### Lettings Log +Objects Created/Persisted: +- **User**: The `assigned_to` user is created. + - **Organisation**: The `assigned_to` user’s organisation created by `User` factory. +- **DataProtectionConfirmation**: If `organisation` does not have DSA signed, `DataProtectionConfirmation` gets created with `assigned_to` user as a `data_protection_officer` +- **OrganisationRentPeriod**: If `log.period` is present and the `managing_organisation` does not have an `OrganisationRentPeriod` for that period, a new `OrganisationRentPeriod` is created and associated with `managing_organisation`. + +Example Usage: +``` +let(:lettings_log) { create(:lettings_log) } +``` + +### Sales Log +Objects Created/Persisted: +- **User**: The `assigned_to` user is created. + - **Organisation**: The `assigned_to` user’s organisation created by `User` factory. +- **DataProtectionConfirmation**: If `organisation` does not have DSA signed, `DataProtectionConfirmation` gets created with `assigned_to` user as a `data_protection_officer` + +Example Usage: +``` +let(:sales_log) { create(:sales_log) } +``` + +### Organisation +Objects Created/Persisted: +- **OrganisationRentPeriod**: For each rent period in transient attribute `rent_periods`, an `OrganisationRentPeriod` is created. +- **DataProtectionConfirmation**: If `with_dsa` is `true` (default), a `DataProtectionConfirmation` is created with a `data_protection_officer` +- **User**: Data protection officer that signs the data protection confirmation + +Example Usage: +``` +let(:organisation) { create(:organisation, rent_periods: [1, 2])} +``` + +### User +Objects Created/Persisted: +- **Organisation**: User’s organisation. +- **DataProtectionConfirmation**: If `organisation` does not have DSA signed, `DataProtectionConfirmation` gets created with this user as a `data_protection_officer` + +Example Usage: +``` +let(:user) { create(:user) } +``` \ No newline at end of file diff --git a/lib/tasks/fix_nil_letting_allocation_values.rake b/lib/tasks/fix_nil_letting_allocation_values.rake deleted file mode 100644 index 1136ed684..000000000 --- a/lib/tasks/fix_nil_letting_allocation_values.rake +++ /dev/null @@ -1,25 +0,0 @@ -desc "Infer nil letting allocation values as no" -task fix_nil_letting_allocation_values: :environment do - LettingsLog.where(cbl: nil) - .or(LettingsLog.where(chr: nil)) - .or(LettingsLog.where(cap: nil)) - .or(LettingsLog.filter_by_year(2024).where(accessible_register: nil)) - .find_each do |log| - next unless log.cbl.present? || log.chr.present? || log.cap.present? || log.accessible_register.present? || log.letting_allocation_unknown.present? - - log.cbl = 0 if log.cbl.blank? - log.chr = 0 if log.chr.blank? - log.cap = 0 if log.cap.blank? - log.accessible_register = 0 if log.form.start_year_after_2024? && log.accessible_register.blank? - - log.letting_allocation_unknown = if log.cbl == 1 || log.chr == 1 || log.cap == 1 || log.accessible_register == 1 - 0 - else - 1 - end - - next if log.save - - Rails.logger.info("NilLettingsAllocationValues: Unable to save changes to log #{log.id}") - end -end diff --git a/spec/components/create_log_actions_component_spec.rb b/spec/components/create_log_actions_component_spec.rb index 8444b939e..b1caa1443 100644 --- a/spec/components/create_log_actions_component_spec.rb +++ b/spec/components/create_log_actions_component_spec.rb @@ -66,7 +66,7 @@ RSpec.describe CreateLogActionsComponent, type: :component do context "when not support user" do context "without data sharing agreement" do - let(:user) { create(:user, organisation: create(:organisation, :without_dpc)) } + let(:user) { create(:user, organisation: create(:organisation, :without_dpc), with_dsa: false) } it "does not render actions" do expect(component).not_to be_display_actions diff --git a/spec/components/data_protection_confirmation_banner_component_spec.rb b/spec/components/data_protection_confirmation_banner_component_spec.rb index 3064a1b8e..608628093 100644 --- a/spec/components/data_protection_confirmation_banner_component_spec.rb +++ b/spec/components/data_protection_confirmation_banner_component_spec.rb @@ -5,11 +5,11 @@ RSpec.describe DataProtectionConfirmationBannerComponent, type: :component do let(:component) { described_class.new(user:, organisation:) } let(:render) { render_inline(component) } - let(:user) { create(:user) } + let(:user) { create(:user, with_dsa: false) } let(:organisation) { user.organisation } context "when user is support and organisation is blank" do - let(:user) { create(:user, :support) } + let(:user) { create(:user, :support, with_dsa: false) } let(:organisation) { nil } it "does not display banner" do @@ -37,8 +37,8 @@ RSpec.describe DataProtectionConfirmationBannerComponent, type: :component do context "when org does not have a signed data sharing agreement" do context "when user is not a DPO" do let(:organisation) { create(:organisation, :without_dpc) } - let(:user) { create(:user, organisation:) } - let!(:dpo) { create(:user, :data_protection_officer, organisation:) } + let(:user) { create(:user, organisation:, with_dsa: false) } + let!(:dpo) { create(:user, :data_protection_officer, organisation:, with_dsa: false) } it "displays the banner and shows DPOs" do expect(component.display_banner?).to eq(true) @@ -50,7 +50,7 @@ RSpec.describe DataProtectionConfirmationBannerComponent, type: :component do context "when user is a DPO" do let(:organisation) { create(:organisation, :without_dpc) } - let(:user) { create(:user, :data_protection_officer, organisation:) } + let(:user) { create(:user, :data_protection_officer, organisation:, with_dsa: false) } it "displays the banner and asks to sign" do expect(component.display_banner?).to eq(true) @@ -141,8 +141,8 @@ RSpec.describe DataProtectionConfirmationBannerComponent, type: :component do context "when org does not have a signed data sharing agreement" do context "when user is not a DPO" do let(:organisation) { create(:organisation, :without_dpc) } - let(:user) { create(:user, organisation:) } - let!(:dpo) { create(:user, :data_protection_officer, organisation:) } + let(:user) { create(:user, organisation:, with_dsa: false) } + let!(:dpo) { create(:user, :data_protection_officer, organisation:, with_dsa: false) } it "displays the banner and shows DPOs" do expect(component.display_banner?).to eq(true) @@ -168,7 +168,7 @@ RSpec.describe DataProtectionConfirmationBannerComponent, type: :component do context "when user is a DPO" do let(:organisation) { create(:organisation, :without_dpc) } - let(:user) { create(:user, :data_protection_officer, organisation:) } + let(:user) { create(:user, :data_protection_officer, organisation:, with_dsa: false) } it "displays the banner and asks to sign" do expect(component.display_banner?).to eq(true) diff --git a/spec/factories/lettings_log.rb b/spec/factories/lettings_log.rb index 62f0cb514..8ebe44875 100644 --- a/spec/factories/lettings_log.rb +++ b/spec/factories/lettings_log.rb @@ -216,6 +216,7 @@ FactoryBot.define do trait :ignore_validation_errors do to_create do |instance| instance.valid? + instance.errors.clear instance.save!(validate: false) end end diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb index c42a9d6a2..f498632cd 100644 --- a/spec/factories/organisation.rb +++ b/spec/factories/organisation.rb @@ -25,11 +25,11 @@ FactoryBot.define do end after(:create) do |org, evaluator| - if evaluator.with_dsa + if evaluator.with_dsa && !org.data_protection_confirmed? create( :data_protection_confirmation, organisation: org, - data_protection_officer: org.users.any? ? org.users.first : create(:user, :data_protection_officer, organisation: org), + data_protection_officer: org.users.any? ? org.users.first : create(:user, :data_protection_officer, organisation: org, with_dsa: false), ) end end diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 22b93d2d6..956d88332 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -3,7 +3,7 @@ FactoryBot.define do sequence(:email) { "test#{SecureRandom.hex}@example.com" } name { "Danny Rojas" } password { "pAssword1" } - organisation + organisation { association :organisation, with_dsa: is_dpo ? false : true } role { "data_provider" } phone { "1234512345123" } trait :data_provider do @@ -27,10 +27,18 @@ FactoryBot.define do old_user_id { SecureRandom.uuid } end - after(:create) do |user, evaluator| - FactoryBot.create(:legacy_user, old_user_id: evaluator.old_user_id, user:) + transient do + with_dsa { true } + end - user.reload + after(:create) do |user, evaluator| + if evaluator.with_dsa && !user.organisation.data_protection_confirmed? + create( + :data_protection_confirmation, + organisation: user.organisation, + data_protection_officer: user, + ) + end end end end diff --git a/spec/features/user_spec.rb b/spec/features/user_spec.rb index e3335c762..169465cb1 100644 --- a/spec/features/user_spec.rb +++ b/spec/features/user_spec.rb @@ -594,7 +594,7 @@ RSpec.describe "User Features" do end before do - other_user.update!(initial_confirmation_sent: true, last_sign_in_at: nil) + other_user.update!(initial_confirmation_sent: true, last_sign_in_at: nil, old_user_id: "old-user-id") allow(user).to receive(:need_two_factor_authentication?).and_return(false) sign_in(user) visit(user_path(other_user)) diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index aba7d7c98..5b3a77e61 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -1,6 +1,8 @@ require "rails_helper" RSpec.describe LocationsHelper do + include CollectionTimeHelper + describe "mobility type selection" do expected_selection = [OpenStruct.new(id: "Wheelchair-user standard", name: "Wheelchair-user standard", description: "Suitable for someone who uses a wheelchair and offers the full use of all rooms and facilities."), OpenStruct.new(id: "Fitted with equipment and adaptations", name: "Fitted with equipment and adaptations", description: "Fitted with stairlifts, ramps, level access showers or grab rails."), @@ -201,18 +203,26 @@ RSpec.describe LocationsHelper do context "when viewing availability" do context "with no deactivations" do - it "displays current collection start date as availability date if created_at is later than collection start date" do - location.update!(startdate: nil, created_at: Time.zone.local(2024, 1, 16)) + before do + allow(Time).to receive(:now).and_call_original + end + + it "displays current collection start date as availability date if created_at is later than collection start date and not in a crossover period" do + allow(FormHandler.instance).to receive(:in_crossover_period?).with(anything).and_return(false) + + location.update!(startdate: nil, created_at: current_collection_start_date + 6.months) availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value] - expect(availability_attribute).to eq("Active from 1 April 2023") + expect(availability_attribute).to eq("Active from 1 April #{current_collection_start_date.year}") end it "displays previous collection start date as availability date if created_at is later than collection start date and in crossover" do - location.update!(startdate: nil, created_at: Time.zone.local(2023, 4, 16)) + allow(FormHandler.instance).to receive(:in_crossover_period?).with(anything).and_return(true) + + location.update!(startdate: nil, created_at: current_collection_start_date + 1.week) availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value] - expect(availability_attribute).to eq("Active from 1 April 2022") + expect(availability_attribute).to eq("Active from 1 April #{previous_collection_start_date.year}") end context "when location was merged" do diff --git a/spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb b/spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb deleted file mode 100644 index 8db478eb8..000000000 --- a/spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -require "rails_helper" -require "rake" - -RSpec.describe "fix_nil_letting_allocation_values" do - describe ":fix_nil_letting_allocation_values", type: :task do - subject(:task) { Rake::Task["fix_nil_letting_allocation_values"] } - - before do - Rake.application.rake_require("tasks/fix_nil_letting_allocation_values") - Rake::Task.define_task(:environment) - task.reenable - end - - it "sets nil values to 0 when one allocation type value is non-nil" do - log = create(:lettings_log, :setup_completed, :startdate_today, cbl: nil, chr: nil, cap: 1, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be 0 - expect(log.chr).to be 0 - expect(log.cap).to be 1 - expect(log.accessible_register).to be 0 - expect(log.letting_allocation_unknown).to be 0 - end - - it "sets nil values to 0 and letting_allocation_unknown to 1 when non-nil allocation type values are 0" do - log = create(:lettings_log, :setup_completed, :startdate_today, cbl: 0, chr: 0, cap: nil, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be 0 - expect(log.chr).to be 0 - expect(log.cap).to be 0 - expect(log.accessible_register).to be 0 - expect(log.letting_allocation_unknown).to be 1 - end - - it "does not set anything when question has not been answered at all" do - log = create(:lettings_log, :setup_completed, :startdate_today, cbl: nil, chr: nil, cap: nil, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be_nil - expect(log.chr).to be_nil - expect(log.cap).to be_nil - expect(log.accessible_register).to be_nil - expect(log.letting_allocation_unknown).to be_nil - end - - it "does not set accessible_register for logs before 2024" do - log = create(:lettings_log, :setup_completed, startdate: Time.zone.local(2023, 5, 1), cbl: 1, chr: nil, cap: nil, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be 1 - expect(log.chr).to be 0 - expect(log.cap).to be 0 - expect(log.accessible_register).to be_nil - expect(log.letting_allocation_unknown).to be 0 - end - - it "logs the log id if the change cannot be saved" do - log = create(:lettings_log, :ignore_validation_errors, :setup_completed, startdate: Time.zone.local(2022, 4, 1), cbl: 1, chr: nil, cap: nil, letting_allocation_unknown: nil) - - expect(Rails.logger).to receive(:info).with(match(/log #{log.id}/)) - task.invoke - end - end -end diff --git a/spec/mailers/resend_invitation_mailer_spec.rb b/spec/mailers/resend_invitation_mailer_spec.rb index a5eadad20..2b76484c3 100644 --- a/spec/mailers/resend_invitation_mailer_spec.rb +++ b/spec/mailers/resend_invitation_mailer_spec.rb @@ -41,6 +41,7 @@ RSpec.describe ResendInvitationMailer do end it "sends an initial invitation" do + FactoryBot.create(:legacy_user, old_user_id: new_active_migrated_user.old_user_id, user: new_active_migrated_user) expect(notify_client).to receive(:send_email).with(email_address: "new_active_migrated_user@example.com", template_id: User::BETA_ONBOARDING_TEMPLATE_ID, personalisation:).once described_class.new.resend_invitation_email(new_active_migrated_user) end diff --git a/spec/models/form/lettings/pages/address_matcher_spec.rb b/spec/models/form/lettings/pages/address_matcher_spec.rb index 8cc98f480..16f571754 100644 --- a/spec/models/form/lettings/pages/address_matcher_spec.rb +++ b/spec/models/form/lettings/pages/address_matcher_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Lettings::Pages::AddressMatcher, type: :model do let(:page_id) { nil } let(:page_definition) { nil } let(:subsection) { instance_double(Form::Subsection) } - let(:log) { create(:lettings_log) } + let(:log) { build(:lettings_log) } it "has correct subsection" do expect(page.subsection).to eq(subsection) diff --git a/spec/models/form/lettings/pages/uprn_selection_spec.rb b/spec/models/form/lettings/pages/uprn_selection_spec.rb index 5cbb08e93..a2e8086d0 100644 --- a/spec/models/form/lettings/pages/uprn_selection_spec.rb +++ b/spec/models/form/lettings/pages/uprn_selection_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Lettings::Pages::UprnSelection, type: :model do let(:page_id) { nil } let(:page_definition) { nil } let(:subsection) { instance_double(Form::Subsection) } - let(:log) { create(:lettings_log) } + let(:log) { build(:lettings_log) } it "has correct subsection" do expect(page.subsection).to eq(subsection) diff --git a/spec/models/form/lettings/pages/uprn_spec.rb b/spec/models/form/lettings/pages/uprn_spec.rb index 089daf4bc..c41de5f6a 100644 --- a/spec/models/form/lettings/pages/uprn_spec.rb +++ b/spec/models/form/lettings/pages/uprn_spec.rb @@ -45,7 +45,7 @@ RSpec.describe Form::Lettings::Pages::Uprn, type: :model do end context "when log is present" do - let(:log) { create(:lettings_log) } + let(:log) { build(:lettings_log) } context "with 2023/24 form" do it "points to address page" do diff --git a/spec/models/form/lettings/questions/property_reference_spec.rb b/spec/models/form/lettings/questions/property_reference_spec.rb index 2f0c21b75..970bead96 100644 --- a/spec/models/form/lettings/questions/property_reference_spec.rb +++ b/spec/models/form/lettings/questions/property_reference_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Lettings::Questions::PropertyReference, type: :model do let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page, subsection: instance_double(Form::Subsection, form: instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))) } - let(:lettings_log) { FactoryBot.create(:lettings_log) } + let(:lettings_log) { FactoryBot.build(:lettings_log) } it "has correct page" do expect(question.page).to eq(page) diff --git a/spec/models/form/lettings/questions/uprn_confirmation_spec.rb b/spec/models/form/lettings/questions/uprn_confirmation_spec.rb index ee0235c54..c0b6ef8a4 100644 --- a/spec/models/form/lettings/questions/uprn_confirmation_spec.rb +++ b/spec/models/form/lettings/questions/uprn_confirmation_spec.rb @@ -42,7 +42,7 @@ RSpec.describe Form::Lettings::Questions::UprnConfirmation, type: :model do describe "notification_banner" do context "when address is not present" do it "returns nil" do - log = create(:lettings_log) + log = build(:lettings_log) expect(question.notification_banner(log)).to be_nil end @@ -50,7 +50,7 @@ RSpec.describe Form::Lettings::Questions::UprnConfirmation, type: :model do context "when address is present" do it "returns formatted value" do - log = create(:lettings_log, :setup_completed, address_line1: "1, Test Street", town_or_city: "Test Town", postcode_full: "AA1 1AA", uprn: "1", uprn_known: 1) + log = build(:lettings_log, :setup_completed, address_line1: "1, Test Street", town_or_city: "Test Town", postcode_full: "AA1 1AA", uprn: "1", uprn_known: 1) expect(question.notification_banner(log)).to eq( { @@ -64,7 +64,7 @@ RSpec.describe Form::Lettings::Questions::UprnConfirmation, type: :model do describe "has the correct hidden_in_check_answers" do context "when uprn_known != 1 && uprn_confirmed == nil" do - let(:log) { create(:lettings_log, uprn_known: 0, uprn_confirmed: nil) } + let(:log) { build(:lettings_log, uprn_known: 0, uprn_confirmed: nil) } it "returns true" do expect(question.hidden_in_check_answers?(log)).to eq(true) @@ -72,7 +72,7 @@ RSpec.describe Form::Lettings::Questions::UprnConfirmation, type: :model do end context "when uprn_known == 1 && uprn_confirmed == nil" do - let(:log) { create(:lettings_log, :completed, uprn_known: 1, uprn: 1, uprn_confirmed: nil) } + let(:log) { build(:lettings_log, :completed, uprn_known: 1, uprn: 1, uprn_confirmed: nil) } it "returns false" do expect(question.hidden_in_check_answers?(log)).to eq(false) @@ -80,7 +80,7 @@ RSpec.describe Form::Lettings::Questions::UprnConfirmation, type: :model do end context "when uprn_known != 1 && uprn_confirmed == 1" do - let(:log) { create(:lettings_log) } + let(:log) { build(:lettings_log) } it "returns true" do log.uprn_known = 1 diff --git a/spec/models/form/sales/pages/address_matcher_spec.rb b/spec/models/form/sales/pages/address_matcher_spec.rb index e2eea6fa0..6d6a4174f 100644 --- a/spec/models/form/sales/pages/address_matcher_spec.rb +++ b/spec/models/form/sales/pages/address_matcher_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Sales::Pages::AddressMatcher, type: :model do let(:page_id) { nil } let(:page_definition) { nil } let(:subsection) { instance_double(Form::Subsection) } - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } it "has correct subsection" do expect(page.subsection).to eq(subsection) diff --git a/spec/models/form/sales/pages/uprn_confirmation_spec.rb b/spec/models/form/sales/pages/uprn_confirmation_spec.rb index a09cb2f08..bd52233b6 100644 --- a/spec/models/form/sales/pages/uprn_confirmation_spec.rb +++ b/spec/models/form/sales/pages/uprn_confirmation_spec.rb @@ -33,7 +33,7 @@ RSpec.describe Form::Sales::Pages::UprnConfirmation, type: :model do describe "has correct routed_to?" do context "when uprn present && uprn_known == 1 " do - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } it "returns true" do log.uprn_known = 1 @@ -43,7 +43,7 @@ RSpec.describe Form::Sales::Pages::UprnConfirmation, type: :model do end context "when uprn = nil" do - let(:log) { create(:sales_log, uprn_known: 1, uprn: nil) } + let(:log) { build(:sales_log, uprn_known: 1, uprn: nil) } it "returns false" do expect(page.routed_to?(log)).to eq(false) @@ -51,7 +51,7 @@ RSpec.describe Form::Sales::Pages::UprnConfirmation, type: :model do end context "when uprn_known == 0" do - let(:log) { create(:sales_log, uprn_known: 0, uprn: "123456789") } + let(:log) { build(:sales_log, uprn_known: 0, uprn: "123456789") } it "returns false" do expect(page.routed_to?(log)).to eq(false) diff --git a/spec/models/form/sales/pages/uprn_selection_spec.rb b/spec/models/form/sales/pages/uprn_selection_spec.rb index 6013774d3..2e3f3e8f6 100644 --- a/spec/models/form/sales/pages/uprn_selection_spec.rb +++ b/spec/models/form/sales/pages/uprn_selection_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Sales::Pages::UprnSelection, type: :model do let(:page_id) { nil } let(:page_definition) { nil } let(:subsection) { instance_double(Form::Subsection) } - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } it "has correct subsection" do expect(page.subsection).to eq(subsection) diff --git a/spec/models/form/sales/pages/uprn_spec.rb b/spec/models/form/sales/pages/uprn_spec.rb index ef1c9848f..8511af372 100644 --- a/spec/models/form/sales/pages/uprn_spec.rb +++ b/spec/models/form/sales/pages/uprn_spec.rb @@ -45,7 +45,7 @@ RSpec.describe Form::Sales::Pages::Uprn, type: :model do end context "when log is present" do - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } context "with 2023/24 form" do it "points to address page" do diff --git a/spec/models/form/sales/questions/address_line1_for_address_matcher_spec.rb b/spec/models/form/sales/questions/address_line1_for_address_matcher_spec.rb index 213fddaf3..0272d1329 100644 --- a/spec/models/form/sales/questions/address_line1_for_address_matcher_spec.rb +++ b/spec/models/form/sales/questions/address_line1_for_address_matcher_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Sales::Questions::AddressLine1ForAddressMatcher, type: :mod let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page) } - let(:log) { create(:sales_log, :in_progress, address_line1_input: "Address line 1", postcode_full_input: "AA1 1AA") } + let(:log) { build(:sales_log, :in_progress, address_line1_input: "Address line 1", postcode_full_input: "AA1 1AA") } it "has correct page" do expect(question.page).to eq(page) diff --git a/spec/models/form/sales/questions/buyer1_mortgage_spec.rb b/spec/models/form/sales/questions/buyer1_mortgage_spec.rb index 94d78eafa..a0790c7f8 100644 --- a/spec/models/form/sales/questions/buyer1_mortgage_spec.rb +++ b/spec/models/form/sales/questions/buyer1_mortgage_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Sales::Questions::Buyer1Mortgage, type: :model do let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page, subsection: instance_double(Form::Subsection, form: instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))) } - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } it "has correct page" do expect(question.page).to eq(page) diff --git a/spec/models/form/sales/questions/buyer2_mortgage_spec.rb b/spec/models/form/sales/questions/buyer2_mortgage_spec.rb index 6b297423c..d96f094e2 100644 --- a/spec/models/form/sales/questions/buyer2_mortgage_spec.rb +++ b/spec/models/form/sales/questions/buyer2_mortgage_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Sales::Questions::Buyer2Mortgage, type: :model do let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page, subsection: instance_double(Form::Subsection, form: instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))) } - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } it "has correct page" do expect(question.page).to eq(page) diff --git a/spec/models/form/sales/questions/buyers_organisations_spec.rb b/spec/models/form/sales/questions/buyers_organisations_spec.rb index 0c28cff6f..c997c5883 100644 --- a/spec/models/form/sales/questions/buyers_organisations_spec.rb +++ b/spec/models/form/sales/questions/buyers_organisations_spec.rb @@ -48,7 +48,7 @@ RSpec.describe Form::Sales::Questions::BuyersOrganisations, type: :model do end it "has the correct displayed answer_options" do - expect(question.displayed_answer_options(FactoryBot.create(:sales_log))).to eq( + expect(question.displayed_answer_options(FactoryBot.build(:sales_log))).to eq( { "pregyrha" => { "value" => "Their private registered provider (PRP) - housing association" }, "pregother" => { "value" => "Other private registered provider (PRP) - housing association" }, diff --git a/spec/models/form/sales/questions/deposit_amount_spec.rb b/spec/models/form/sales/questions/deposit_amount_spec.rb index cb32a7ee1..acfab8af7 100644 --- a/spec/models/form/sales/questions/deposit_amount_spec.rb +++ b/spec/models/form/sales/questions/deposit_amount_spec.rb @@ -1,34 +1,15 @@ require "rails_helper" RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do - subject(:question) { described_class.new(question_id, question_definition, page, ownershipsch: 1, optional: false) } + subject(:question) { described_class.new(question_id, question_definition, page, ownershipsch: 1, optional:) } + let(:optional) { false } let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page, subsection: instance_double(Form::Subsection, form: instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))) } - it "has correct page" do - expect(question.page).to eq(page) - end - - it "has the correct id" do - expect(question.id).to eq("deposit") - end - - it "has the correct header" do - expect(question.header).to eq("How much cash deposit was paid on the property?") - end - - it "has the correct check_answer_label" do - expect(question.check_answer_label).to eq("Cash deposit") - end - - it "has the correct type" do - expect(question.type).to eq("numeric") - end - context "when the ownership type is shared" do - let(:log) { create(:sales_log, :completed, ownershipsch: 1, mortgageused: 2) } + let(:log) { build(:sales_log, :completed, ownershipsch: 1, mortgageused: 2) } it "is not marked as derived" do expect(question.derived?(log)).to be false @@ -36,7 +17,7 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do end context "when the ownership type is discounted for 2023" do - let(:log) { create(:sales_log, :completed, ownershipsch: 2, mortgageused: 2, saledate: Time.zone.local(2024, 3, 1)) } + let(:log) { build(:sales_log, :completed, ownershipsch: 2, mortgageused: 2, saledate: Time.zone.local(2024, 3, 1)) } it "is not marked as derived" do expect(question.derived?(log)).to be false @@ -44,49 +25,48 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do end context "when the ownership type is outright" do - let(:log) { create(:sales_log, :completed, ownershipsch: 3, mortgageused: 2) } + let(:log) { build(:sales_log, :outright_sale_setup_complete, mortgageused:) } - it "is not marked as derived when a mortgage is used" do - log.mortgageused = 1 - expect(question.derived?(log)).to be false - end + context "when a mortgage is used" do + let(:mortgageused) { 1 } - it "is marked as derived when a mortgage is not used" do - log.mortgageused = 2 - expect(question.derived?(log)).to be true + it "is not marked as derived " do + expect(question.derived?(log)).to be false + end end - it "is marked as derived when the mortgage use is unknown" do - log.mortgageused = 3 - expect(question.derived?(log)).to be true - end - end + context "when a mortgage is not used" do + let(:mortgageused) { 2 } - it "has the correct hint" do - expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan") - end + it "is marked as derived " do + expect(question.derived?(log)).to be true + end + end - it "has correct width" do - expect(question.width).to eq(5) - end + context "when the mortgage use is unknown" do + let(:mortgageused) { 3 } - it "has correct prefix" do - expect(question.prefix).to eq("£") + it "is marked as derived " do + expect(question.derived?(log)).to be true + end + end end - it "has correct min" do - expect(question.min).to eq(0) - end + describe "hint text" do + context "when optional is false" do + let(:optional) { false } - it "has correct max" do - expect(question.max).to eq(999_999) - end + it "has the correct hint" do + expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan") + end + end - context "when optional iis true" do - subject(:question) { described_class.new(question_id, question_definition, page, ownershipsch: 1, optional: true) } + context "when optional is true" do + let(:optional) { true } - it "has a correct hint_text" do - expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan. As this is a fully staircased sale this question is optional. If you do not have the information available click save and continue") + it "has the correct hint" do + expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan. As this is a fully staircased sale this question is optional. If you do not have the information available click save and continue") + end end end end diff --git a/spec/models/form/sales/questions/postcode_for_address_matcher_spec.rb b/spec/models/form/sales/questions/postcode_for_address_matcher_spec.rb index e2a2931a9..5ba7e8505 100644 --- a/spec/models/form/sales/questions/postcode_for_address_matcher_spec.rb +++ b/spec/models/form/sales/questions/postcode_for_address_matcher_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Sales::Questions::PostcodeForAddressMatcher, type: :model d let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page) } - let(:log) { create(:sales_log, :in_progress, address_line1_input: "Address line 1", postcode_full_input: "AA1 1AA") } + let(:log) { build(:sales_log, :in_progress, address_line1_input: "Address line 1", postcode_full_input: "AA1 1AA") } it "has correct page" do expect(question.page).to eq(page) diff --git a/spec/models/form/sales/questions/uprn_confirmation_spec.rb b/spec/models/form/sales/questions/uprn_confirmation_spec.rb index b6e9195e0..bb5688944 100644 --- a/spec/models/form/sales/questions/uprn_confirmation_spec.rb +++ b/spec/models/form/sales/questions/uprn_confirmation_spec.rb @@ -42,7 +42,7 @@ RSpec.describe Form::Sales::Questions::UprnConfirmation, type: :model do describe "notification_banner" do context "when address is not present" do it "returns nil" do - log = create(:sales_log) + log = build(:sales_log) expect(question.notification_banner(log)).to be_nil end @@ -64,7 +64,7 @@ RSpec.describe Form::Sales::Questions::UprnConfirmation, type: :model do describe "has the correct hidden_in_check_answers" do context "when uprn_known != 1 && uprn_confirmed == nil" do - let(:log) { create(:sales_log, uprn_known: 0, uprn_confirmed: nil) } + let(:log) { build(:sales_log, uprn_known: 0, uprn_confirmed: nil) } it "returns true" do expect(question.hidden_in_check_answers?(log)).to eq(true) @@ -72,7 +72,7 @@ RSpec.describe Form::Sales::Questions::UprnConfirmation, type: :model do end context "when uprn_known == 1 && uprn_confirmed == nil" do - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } it "returns false" do log.uprn_known = 1 @@ -83,7 +83,7 @@ RSpec.describe Form::Sales::Questions::UprnConfirmation, type: :model do end context "when uprn_known != 1 && uprn_confirmed == 1" do - let(:log) { create(:sales_log, uprn_known: 1, uprn: "12345", uprn_confirmed: 1) } + let(:log) { build(:sales_log, uprn_known: 1, uprn: "12345", uprn_confirmed: 1) } it "returns true" do expect(question.hidden_in_check_answers?(log)).to eq(true) diff --git a/spec/models/form/sales/questions/uprn_selection_spec.rb b/spec/models/form/sales/questions/uprn_selection_spec.rb index 5f7951c3d..cd23a8e9f 100644 --- a/spec/models/form/sales/questions/uprn_selection_spec.rb +++ b/spec/models/form/sales/questions/uprn_selection_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Form::Sales::Questions::UprnSelection, type: :model do let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page, skip_href: "skip_href") } - let(:log) { create(:sales_log, :in_progress, address_line1_input: "Address line 1", postcode_full_input: "AA1 1AA") } + let(:log) { build(:sales_log, :in_progress, address_line1_input: "Address line 1", postcode_full_input: "AA1 1AA") } let(:address_client_instance) { AddressClient.new(log.address_string) } before do diff --git a/spec/models/form/sales/questions/uprn_spec.rb b/spec/models/form/sales/questions/uprn_spec.rb index fc0c5caa7..afc4e4a24 100644 --- a/spec/models/form/sales/questions/uprn_spec.rb +++ b/spec/models/form/sales/questions/uprn_spec.rb @@ -45,7 +45,7 @@ RSpec.describe Form::Sales::Questions::Uprn, type: :model do describe "get_extra_check_answer_value" do context "when address is not present" do - let(:log) { create(:sales_log) } + let(:log) { build(:sales_log) } it "returns nil" do expect(question.get_extra_check_answer_value(log)).to be_nil diff --git a/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb b/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb index 99e861b6d..5f20cbb91 100644 --- a/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb +++ b/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb @@ -67,7 +67,7 @@ RSpec.describe Form::Sales::Subsections::DiscountedOwnershipScheme, type: :model end context "when it is a discounted ownership scheme" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 2) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 2) } it "is displayed in tasklist" do expect(discounted_ownership_scheme.displayed_in_tasklist?(log)).to eq(true) @@ -75,7 +75,7 @@ RSpec.describe Form::Sales::Subsections::DiscountedOwnershipScheme, type: :model end context "when it is not a discounted ownership scheme" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 1) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 1) } it "is displayed in tasklist" do expect(discounted_ownership_scheme.displayed_in_tasklist?(log)).to eq(false) diff --git a/spec/models/form/sales/subsections/household_characteristics_spec.rb b/spec/models/form/sales/subsections/household_characteristics_spec.rb index 2557f33c6..be78f91d3 100644 --- a/spec/models/form/sales/subsections/household_characteristics_spec.rb +++ b/spec/models/form/sales/subsections/household_characteristics_spec.rb @@ -378,7 +378,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model end context "when the sale is to a company buyer" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 3, companybuy: 1) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 3, companybuy: 1) } it "is not displayed in tasklist" do expect(household_characteristics.displayed_in_tasklist?(log)).to eq(false) @@ -386,7 +386,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model end context "when the sale is not to a company buyer" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 3, companybuy: 2) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 3, companybuy: 2) } it "is displayed in tasklist" do expect(household_characteristics.displayed_in_tasklist?(log)).to eq(true) diff --git a/spec/models/form/sales/subsections/outright_sale_spec.rb b/spec/models/form/sales/subsections/outright_sale_spec.rb index cb0f9e7b3..efb2aaad6 100644 --- a/spec/models/form/sales/subsections/outright_sale_spec.rb +++ b/spec/models/form/sales/subsections/outright_sale_spec.rb @@ -122,7 +122,7 @@ RSpec.describe Form::Sales::Subsections::OutrightSale, type: :model do end context "when it is a outright sale" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 3) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 3) } it "is displayed in tasklist" do expect(outright_sale.displayed_in_tasklist?(log)).to eq(true) @@ -130,7 +130,7 @@ RSpec.describe Form::Sales::Subsections::OutrightSale, type: :model do end context "when it is not a outright sale" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 2) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 2) } it "is displayed in tasklist" do expect(outright_sale.displayed_in_tasklist?(log)).to eq(false) diff --git a/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb b/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb index 0935ce394..59e7bfbfb 100644 --- a/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb +++ b/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb @@ -77,7 +77,7 @@ RSpec.describe Form::Sales::Subsections::SharedOwnershipScheme, type: :model do end context "when it is a shared ownership scheme" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 1) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 1) } it "is displayed in tasklist" do expect(shared_ownership_scheme.displayed_in_tasklist?(log)).to eq(true) @@ -85,7 +85,7 @@ RSpec.describe Form::Sales::Subsections::SharedOwnershipScheme, type: :model do end context "when it is not a shared ownership scheme" do - let(:log) { FactoryBot.create(:sales_log, ownershipsch: 2) } + let(:log) { FactoryBot.build(:sales_log, ownershipsch: 2) } it "is displayed in tasklist" do expect(shared_ownership_scheme.displayed_in_tasklist?(log)).to eq(false) diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index 75fe66833..a04e7d465 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -305,7 +305,7 @@ RSpec.describe Scheme, type: :model do end context "when scheme has discarded_at value" do - let(:scheme) { FactoryBot.create(:scheme, discarded_at: Time.zone.now) } + let(:scheme) { FactoryBot.build(:scheme, discarded_at: Time.zone.now) } it "returns deleted" do expect(scheme.status).to eq(:deleted) @@ -368,7 +368,7 @@ RSpec.describe Scheme, type: :model do describe "owning organisation" do let(:stock_owning_org) { FactoryBot.create(:organisation, holds_own_stock: true) } let(:non_stock_owning_org) { FactoryBot.create(:organisation, holds_own_stock: false) } - let(:scheme) { FactoryBot.create(:scheme, owning_organisation_id: stock_owning_org.id) } + let(:scheme) { FactoryBot.build(:scheme, owning_organisation_id: stock_owning_org.id) } context "when the owning organisation is set as a non-stock-owning organisation" do it "throws the correct validation error" do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 05a581ee8..edb998ac3 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -114,6 +114,7 @@ RSpec.describe User, type: :model do end it "can have one or more legacy users" do + FactoryBot.create(:legacy_user, old_user_id: user.old_user_id, user:) expect(user.legacy_users.size).to eq(1) end diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 75907b5bc..59be2537e 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Validations::DateValidations do subject(:date_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::DateValidations } } - let(:record) { create(:lettings_log) } + let(:record) { build(:lettings_log) } let(:scheme) { create(:scheme, end_date: Time.zone.today - 5.days) } let(:scheme_no_end_date) { create(:scheme, end_date: nil) } diff --git a/spec/models/validations/local_authority_validations_spec.rb b/spec/models/validations/local_authority_validations_spec.rb index 3b903e4dc..6344bf427 100644 --- a/spec/models/validations/local_authority_validations_spec.rb +++ b/spec/models/validations/local_authority_validations_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Validations::LocalAuthorityValidations do subject(:local_auth_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::LocalAuthorityValidations } } - let(:log) { create(:lettings_log) } + let(:log) { build(:lettings_log) } describe "#validate_previous_accommodation_postcode" do it "does not add an error if the log ppostcode_full is missing" do diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index 8ce602d36..c8e8618fc 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe Validations::Sales::SoftValidations do - let(:record) { create(:sales_log) } + let(:record) { build(:sales_log) } describe "income1 min validations" do context "when validating soft min" do diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 5e7705c6d..7fc4dd982 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Validations::SetupValidations do subject(:setup_validator) { setup_validator_class.new } let(:setup_validator_class) { Class.new { include Validations::SetupValidations } } - let(:record) { create(:lettings_log) } + let(:record) { build(:lettings_log) } describe "tenancy start date" do context "when in 22/23 collection" do @@ -853,7 +853,7 @@ RSpec.describe Validations::SetupValidations do end context "when updating" do - let(:log) { create(:lettings_log, :in_progress) } + let(:log) { build(:lettings_log, :in_progress) } let(:org_with_dpc) { create(:organisation) } let(:org_without_dpc) { create(:organisation, :without_dpc) } diff --git a/spec/models/validations/tenancy_validations_spec.rb b/spec/models/validations/tenancy_validations_spec.rb index f5ffc05b0..a41752616 100644 --- a/spec/models/validations/tenancy_validations_spec.rb +++ b/spec/models/validations/tenancy_validations_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Validations::TenancyValidations do let(:validator_class) { Class.new { include Validations::TenancyValidations } } describe "tenancy length validations" do - let(:record) { FactoryBot.create(:lettings_log, :setup_completed) } + let(:record) { FactoryBot.build(:lettings_log, :setup_completed) } shared_examples "adds expected errors based on the tenancy length" do |tenancy_type_case, error_fields, min_tenancy_length| context "and tenancy type is #{tenancy_type_case[:name]}" do @@ -276,7 +276,7 @@ RSpec.describe Validations::TenancyValidations do end describe "tenancy type validations" do - let(:record) { FactoryBot.create(:lettings_log, :setup_completed) } + let(:record) { FactoryBot.build(:lettings_log, :setup_completed) } let(:field) { "validations.other_field_missing" } let(:main_field_label) { "tenancy type" } let(:other_field) { "tenancyother" } @@ -320,7 +320,7 @@ RSpec.describe Validations::TenancyValidations do describe "joint tenancy validation" do context "when the data inputter has said that there is only one member in the household" do - let(:record) { FactoryBot.create(:lettings_log, :setup_completed, hhmemb: 1) } + let(:record) { FactoryBot.build(:lettings_log, :setup_completed, hhmemb: 1) } let(:expected_error) { I18n.t("validations.tenancy.not_joint") } let(:hhmemb_expected_error) { I18n.t("validations.tenancy.joint_more_than_one_member") } diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb index e35e078e4..e2266cb48 100644 --- a/spec/policies/user_policy_spec.rb +++ b/spec/policies/user_policy_spec.rb @@ -201,7 +201,7 @@ RSpec.describe UserPolicy do end context "and user is the DPO that hasn't signed the agreement" do - let(:user) { create(:user, active: false, is_dpo: true) } + let(:user) { create(:user, active: false, is_dpo: true, with_dsa: false) } it "does not allow deleting a user as a provider" do expect(policy).not_to permit(data_provider, user) diff --git a/spec/requests/bulk_upload_lettings_logs_controller_spec.rb b/spec/requests/bulk_upload_lettings_logs_controller_spec.rb index d9a9a508d..18b208e74 100644 --- a/spec/requests/bulk_upload_lettings_logs_controller_spec.rb +++ b/spec/requests/bulk_upload_lettings_logs_controller_spec.rb @@ -13,7 +13,7 @@ RSpec.describe BulkUploadLettingsLogsController, type: :request do describe "GET /lettings-logs/bulk-upload-logs/start" do context "when data protection confirmation not signed" do let(:organisation) { create(:organisation, :without_dpc) } - let(:user) { create(:user, organisation:) } + let(:user) { create(:user, organisation:, with_dsa: false) } it "redirects to lettings index page" do get "/lettings-logs/bulk-upload-logs/start", params: {} diff --git a/spec/requests/bulk_upload_sales_logs_controller_spec.rb b/spec/requests/bulk_upload_sales_logs_controller_spec.rb index 6fcf7c15d..7cd6d4be8 100644 --- a/spec/requests/bulk_upload_sales_logs_controller_spec.rb +++ b/spec/requests/bulk_upload_sales_logs_controller_spec.rb @@ -13,7 +13,7 @@ RSpec.describe BulkUploadSalesLogsController, type: :request do describe "GET /sales-logs/bulk-upload-logs/start" do context "when data protection confirmation not signed" do let(:organisation) { create(:organisation, :without_dpc) } - let(:user) { create(:user, organisation:) } + let(:user) { create(:user, organisation:, with_dsa: false) } it "redirects to sales index page" do get "/sales-logs/bulk-upload-logs/start", params: {} diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 68f69d9f2..e6fb20ac7 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -1648,6 +1648,7 @@ RSpec.describe OrganisationsController, type: :request do allow(notify_client).to receive(:send_email).and_return(true) user_to_reactivate = create(:user, :data_coordinator, organisation:, active: false, reactivate_with_organisation: true) + FactoryBot.create(:legacy_user, old_user_id: user_to_reactivate.old_user_id, user: user_to_reactivate) user_not_to_reactivate = create(:user, :data_coordinator, organisation:, active: false, reactivate_with_organisation: false) patch "/organisations/#{organisation.id}", headers:, params: end @@ -2144,7 +2145,7 @@ RSpec.describe OrganisationsController, type: :request do Timecop.unfreeze end - let(:user) { create(:user, is_dpo: true, organisation:) } + let(:user) { create(:user, is_dpo: true, organisation:, with_dsa: false) } it "returns redirects to details page" do post "/organisations/#{organisation.id}/data-sharing-agreement", headers: headers diff --git a/spec/requests/start_controller_spec.rb b/spec/requests/start_controller_spec.rb index 017ab32e7..699bdfa9c 100644 --- a/spec/requests/start_controller_spec.rb +++ b/spec/requests/start_controller_spec.rb @@ -203,7 +203,7 @@ RSpec.describe StartController, type: :request do it "shows the correct counts of logs created by them" do last_year_in_progress_count = 2 this_year_in_progress_count = 3 - create_list(:lettings_log, last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + create_list(:lettings_log, last_year_in_progress_count, :in_progress, :ignore_validation_errors, assigned_to: provider_1, startdate: Time.zone.today - 1.year) create_list(:lettings_log, this_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today) get root_path @@ -217,7 +217,7 @@ RSpec.describe StartController, type: :request do end it "does not include logs created by other users in the count, whether in their organisation or not" do - create(:lettings_log, :in_progress, assigned_to: coordinator, startdate: Time.zone.today - 1.year) + create(:lettings_log, :in_progress, :ignore_validation_errors, assigned_to: coordinator, startdate: Time.zone.today - 1.year) create(:lettings_log, :in_progress, assigned_to: provider_2, startdate: Time.zone.today) get root_path @@ -237,7 +237,7 @@ RSpec.describe StartController, type: :request do it "shows the correct counts of logs created by all users in their organisation" do last_year_in_progress_count = 2 this_year_in_progress_count = 3 - create_list(:lettings_log, last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + create_list(:lettings_log, last_year_in_progress_count, :in_progress, :ignore_validation_errors, assigned_to: provider_1, startdate: Time.zone.today - 1.year) create_list(:lettings_log, this_year_in_progress_count, :in_progress, assigned_to: coordinator, startdate: Time.zone.today) get root_path @@ -288,9 +288,9 @@ RSpec.describe StartController, type: :request do coordinator_lettings_this_year_in_progress_count = 3 provider_2_lettings_last_year_in_progress_count = 2 provider_2_sales_this_year_in_progress_count = 3 - create_list(:lettings_log, provider_1_lettings_last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + create_list(:lettings_log, provider_1_lettings_last_year_in_progress_count, :in_progress, :ignore_validation_errors, assigned_to: provider_1, startdate: Time.zone.today - 1.year) create_list(:lettings_log, coordinator_lettings_this_year_in_progress_count, :in_progress, assigned_to: coordinator, startdate: Time.zone.today) - create_list(:lettings_log, provider_2_lettings_last_year_in_progress_count, :in_progress, assigned_to: provider_2, startdate: Time.zone.today - 1.year) + create_list(:lettings_log, provider_2_lettings_last_year_in_progress_count, :in_progress, :ignore_validation_errors, assigned_to: provider_2, startdate: Time.zone.today - 1.year) create_list(:sales_log, provider_2_sales_this_year_in_progress_count, :in_progress, assigned_to: provider_2, saledate: Time.zone.today) get root_path diff --git a/spec/services/csv/sales_log_csv_service_spec.rb b/spec/services/csv/sales_log_csv_service_spec.rb index 7f6ad8c2a..aee659ae5 100644 --- a/spec/services/csv/sales_log_csv_service_spec.rb +++ b/spec/services/csv/sales_log_csv_service_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe Csv::SalesLogCsvService do let(:form_handler_mock) { instance_double(FormHandler) } let(:organisation) { create(:organisation) } - let(:fixed_time) { Time.zone.local(2023, 12, 8) } + let(:fixed_time) { now } let(:now) { Time.zone.now } let(:user) { create(:user, :support, email: "billyboy@eyeKLAUD.com") } let(:log) do @@ -134,6 +134,8 @@ RSpec.describe Csv::SalesLogCsvService do context "when exporting with human readable labels" do let(:year) { 2023 } + let(:fixed_time) { Time.zone.local(2023, 12, 8) } + let(:now) { fixed_time } it "gives answers to radio questions as their labels" do national_column_index = csv.first.index("NATIONAL") @@ -220,6 +222,8 @@ RSpec.describe Csv::SalesLogCsvService do context "when exporting values as codes" do let(:service) { described_class.new(user:, export_type: "codes", year:) } let(:year) { 2023 } + let(:fixed_time) { Time.zone.local(2023, 12, 8) } + let(:now) { fixed_time } it "gives answers to radio questions as their codes" do national_column_index = csv.first.index("NATIONAL") diff --git a/spec/views/logs/_create_for_org_actions.html.erb_spec.rb b/spec/views/logs/_create_for_org_actions.html.erb_spec.rb index 1f2918218..e82cb8d27 100644 --- a/spec/views/logs/_create_for_org_actions.html.erb_spec.rb +++ b/spec/views/logs/_create_for_org_actions.html.erb_spec.rb @@ -20,7 +20,7 @@ RSpec.describe "logs/_create_for_org_actions.html.erb" do end context "without data sharing agreement" do - let(:user) { create(:user, organisation: create(:organisation, :without_dpc)) } + let(:user) { create(:user, organisation: create(:organisation, :without_dpc), with_dsa: false) } it "does not include create log buttons" do render diff --git a/spec/views/organisations/data_sharing_agreement.html.erb_spec.rb b/spec/views/organisations/data_sharing_agreement.html.erb_spec.rb index 3c57dcf8a..8bcb29c9d 100644 --- a/spec/views/organisations/data_sharing_agreement.html.erb_spec.rb +++ b/spec/views/organisations/data_sharing_agreement.html.erb_spec.rb @@ -17,7 +17,7 @@ RSpec.describe "organisations/data_sharing_agreement.html.erb", :aggregate_failu let(:data_protection_confirmation) { nil } context "when dpo" do - let(:user) { create(:user, is_dpo: true, organisation: create(:organisation, :without_dpc)) } + let(:user) { create(:user, is_dpo: true, organisation: create(:organisation, :without_dpc), with_dsa: false) } it "renders dynamic content" do render diff --git a/spec/views/organisations/show.html.erb_spec.rb b/spec/views/organisations/show.html.erb_spec.rb index 4314360dc..6c9990317 100644 --- a/spec/views/organisations/show.html.erb_spec.rb +++ b/spec/views/organisations/show.html.erb_spec.rb @@ -11,7 +11,7 @@ RSpec.describe "organisations/show.html.erb" do let(:organisation_with_dsa) { create(:organisation) } context "when dpo" do - let(:user) { create(:user, is_dpo: true, organisation: organisation_without_dpc) } + let(:user) { create(:user, is_dpo: true, organisation: organisation_without_dpc, with_dsa: false) } it "includes data sharing agreement row" do render @@ -32,7 +32,7 @@ RSpec.describe "organisations/show.html.erb" do end context "when accepted" do - let(:user) { create(:user, organisation: organisation_with_dsa) } + let(:user) { create(:user, organisation: organisation_with_dsa, with_dsa: false) } it "includes data sharing agreement row" do render @@ -55,7 +55,7 @@ RSpec.describe "organisations/show.html.erb" do end context "when support user" do - let(:user) { create(:user, :support, organisation: organisation_without_dpc) } + let(:user) { create(:user, :support, organisation: organisation_without_dpc, with_dsa: false) } it "includes data sharing agreement row" do render @@ -82,7 +82,7 @@ RSpec.describe "organisations/show.html.erb" do end context "when accepted" do - let(:user) { create(:user, :support, organisation: organisation_with_dsa) } + let(:user) { create(:user, :support, organisation: organisation_with_dsa, with_dsa: false) } it "includes data sharing agreement row" do render @@ -125,7 +125,7 @@ RSpec.describe "organisations/show.html.erb" do end context "when not dpo" do - let(:user) { create(:user, organisation: organisation_without_dpc) } + let(:user) { create(:user, organisation: organisation_without_dpc, with_dsa: false) } it "includes data sharing agreement row" do render @@ -149,7 +149,7 @@ RSpec.describe "organisations/show.html.erb" do end context "when accepted" do - let(:user) { create(:user, organisation: organisation_with_dsa) } + let(:user) { create(:user, organisation: organisation_with_dsa, with_dsa: false) } it "includes data sharing agreement row" do render