Submit social housing lettings and sales data (CORE)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
11 KiB

require "rails_helper"
RSpec.describe Organisation, type: :model do
describe "#new" do
let(:user) { create(:user) }
let!(:organisation) { user.organisation }
let!(:scheme) { create(:scheme, owning_organisation: organisation) }
it "has expected fields" do
expect(organisation.attribute_names).to include("name", "phone", "provider_type")
end
CLDC-1290 Permitted user can create a new scheme (#671) * added test to find link to create a new scheme * added button to create a new scheme * testing arriving to the new scheme form * non exsiting link to controller new action * SPIKEEEE * first page complete * posting to create * refactored scheme to enums * refactored new scheme page to use enums as well * views * SPIKE WIP * SPIKE WIP * SPIKE WIP * drawing list of scheme details * expanded on feature seeing more fields to fill in * expanded on feature seeing more fields to fill in 2nd page * refactored * working back buttons * working flash * more change in wip * default value for org * working spike * some spacing * filling answers * spike finished * correct name for details * testing fill in details * removed gem and further tests * Add has other client group field to schemes. Display it in the check answers. Fix tests and routing * remove details view and path Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com> * WIP change update paths * Implement changing answers (except the details one) * Add details page back for editing scheme details * added missing test for support questions and nested check answers under correct context * checking the back functionality * rubo and lint * checking the back functionality for primary gourp * checking the back functionality for confirm secondary * checking the back functionality for secondary group * checking the back functionality for support answers * checking the back functionality for returning to check answers from details page * checking the back functionality for returning to check answers from primary group page * checking the back functionality for secondary group confirm yes * checking the back functionality for secondary group confirm yes - fixed * checking the back functionality for secondary group * checking the back functionality for secondary group -fixed * checking the back functionality for returning from support page * Do not display secondary client group if the scheme doesn't have one * Add details path to schemes controller update method * Add more tests for back button * Add tests for new controller method * Add tests for creating schemes as data coordinator * fixed schema * added test for missing params when creating scheme * create for support user with or withotu required param * code to get controller render errors when required param is missing * code to implement correct validation * code to implement correct validation - part 2 * highlight missing field * doing silly dance to get correct field highlighted on the error * testing patch for schemes * testing patch for schemes - correct path * small refactoring * testing primary client group update via regular path * testing primary client group update via check answers page * testing confirm secondary group update with YES NO and returning from check answers page * testing updating secondary client group update and returning from check answers page * testing support answers and returning from check answers page * testing details and returning from check answers page * weird path when no names supplied * rubocop * lost in rebasing * started id to code refactoring * model specs remastered * fixed scheme controller specs * further refactorings * fixing feature schemes * final touches * removed code from db * remaining code purged * rubocop * included check for owning org field * checking for stock owning org selection * added stock owning org * added stock owning org on new page * added stock owning org to details * removed total units * managing related schems properly via Org * managing related schems properly via Org - rubocop * small refactoring * small refactoring - 2 * small refactoring - 3 * tests for owned_schemes and managed_schemes * rubocop * added tests for support user creating scheme * rubocop -a * tests for a primary-client-group * tests for a secondary-client-group * tests for a confirm-secondary-client-group * tests for a check-answers * tests for a details * rubocop * redundant action in controller * Trigger WF * switched to int for confirm * flashing test * flashing test for support user * flashing test rubocop Co-authored-by: Kat <katrina@madetech.com> Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com>
3 years ago
it "has owned_schemes" do
expect(organisation.owned_schemes.first).to eq(scheme)
Cldc 1227 supported housing index (#648) * Added test file for supported housing schemes * Added factory bot for supported housing schemes * swapped managing agent to org for scheme * created migration for schemes * created model for scheme * added migration to add foreign key to the schemes table * missing spec and log spec for supported housing * fixed failing specs * added schemes migration * next step feature * added route * added controller * added index * added all schemes * correct test for scheme * added view * route alias for schemes * spec for index schemes * failing scheme controller spec * added simple view, scheme seed and authentication * spec for index schemes list * rubocop - thanks * better seed * added Heading to org * added feature flag to hide supported services on prod * added feature flag testing nav items * testing coordinator user can see the link to supported housing * moved toggle to a different place * moved toggle to PrimaryNavigationComponent * testing not being signed in on support pages * testing showing search bar * added search bar * testing subset of schemes for coordinator user * rubocop * failing test for title in page * code to expose title * pagination with tests without searching * partial for schemes * scoping out all but support and coord users * searching schemes code and test * searching via code and org tests * searching by org name tests * searching by org name code * search_by tests * search_by code * search_by woops must search by service * searching schemes feature * tests for data coordinator user * redirect for data coordinator user * testing org schemes for coordiantor user * schemes in org controller for coordiantor user * refactored specs moved into orgs what belongs there * view for org schemes * rubocop * accebility field * accebility field on org page * correct return when on org schemes * passing search test on the orgs page * highlight nav tab * navs helper done * rubocop * fixed failing tests for support user * correct view * how did I manage to delete this file? * checking you cant access schems unathorized * moved test * renamed service name * correct title for sup user schemes org * testing not being able to view any other orgs supported housing for coordinator user * Trigger WF * last fix * aded has many to org for schemes * rubocop Co-authored-by: Ted <ted.booton@madetech.com>
3 years ago
end
it "validates provider_type presence" do
expect { create(:organisation, provider_type: nil) }
.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}")
end
context "with parent/child associations", :aggregate_failures do
let!(:child_organisation) { create(:organisation, name: "MHCLG Child") }
let!(:grandchild_organisation) { create(:organisation, name: "MHCLG Grandchild") }
before do
create(
:organisation_relationship,
child_organisation:,
parent_organisation: organisation,
)
create(
:organisation_relationship,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)
end
it "has correct child_organisations" do
expect(organisation.child_organisations).to eq([child_organisation])
expect(child_organisation.child_organisations).to eq([grandchild_organisation])
end
it "has correct parent_organisations" do
expect(child_organisation.parent_organisations).to eq([organisation])
expect(grandchild_organisation.parent_organisations).to eq([child_organisation])
end
end
context "with owning association", :aggregate_failures do
let!(:child_organisation) { create(:organisation, name: "MHCLG Child") }
let!(:grandchild_organisation) { create(:organisation, name: "MHCLG Grandchild") }
before do
create(
:organisation_relationship,
child_organisation:,
parent_organisation: organisation,
)
create(
:organisation_relationship,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)
end
it "has correct stock_owners" do
expect(child_organisation.stock_owners).to eq([organisation])
expect(grandchild_organisation.stock_owners).to eq([child_organisation])
end
end
context "with managing association", :aggregate_failures do
let!(:child_organisation) { create(:organisation, name: "MHCLG Child") }
let!(:grandchild_organisation) { create(:organisation, name: "MHCLG Grandchild") }
before do
create(
:organisation_relationship,
child_organisation:,
parent_organisation: organisation,
)
create(
:organisation_relationship,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)
end
it "has correct managing_agents" do
expect(organisation.managing_agents).to eq([child_organisation])
expect(child_organisation.managing_agents).to eq([grandchild_organisation])
expect(grandchild_organisation.managing_agents).to eq([])
end
end
context "with data protection confirmations" do
before do
create(:data_protection_confirmation, organisation:, confirmed: false, created_at: Time.utc(2018, 0o6, 0o5, 10, 36, 49))
create(:data_protection_confirmation, organisation:, created_at: Time.utc(2019, 0o6, 0o5, 10, 36, 49))
end
it "takes the most recently created" do
expect(organisation.data_protection_confirmed?).to be true
end
end
Cldc 3114 enable support control of rent periods per organisation (#2442) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
context "with associated rent periods" do
let(:organisation) { create(:organisation) }
Cldc 3114 enable support control of rent periods per organisation (#2442) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
let(:period_1_label) { "Every minute" }
let(:fake_rent_periods) do
{
"1" => { "value" => period_1_label },
"2" => { "value" => "Every decade" },
}
end
3 years ago
before do
Cldc 3114 enable support control of rent periods per organisation (#2442) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
create(:organisation_rent_period, organisation:, rent_period: 1)
allow(RentPeriod).to receive(:rent_period_mappings).and_return(fake_rent_periods)
3 years ago
end
Cldc 3114 enable support control of rent periods per organisation (#2442) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
context "when the org does not use all rent periods" do
it "#rent_periods returns the correct ids" do
expect(organisation.rent_periods).to eq [1]
end
it "#rent_period_labels returns the correct labels" do
expect(organisation.rent_period_labels).to eq [period_1_label]
end
Cldc 3114 enable support control of rent periods per organisation (#2389) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
Cldc 3114 enable support control of rent periods per organisation (#2442) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
context "and has organisation rent periods associated for rent periods that no longer appear in the form" do
before do
create(:organisation_rent_period, organisation:, rent_period: 3)
end
it "#rent_period_labels returns the correct labels" do
expect(organisation.rent_period_labels).to eq [period_1_label]
end
end
end
Cldc 3114 enable support control of rent periods per organisation (#2389) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
Cldc 3114 enable support control of rent periods per organisation (#2442) * write helper method to support having the correct rent period checkboxes checked * update new and create in organisations controller and view to enable creation of relevant organisation rent periods write tests for this * small changes to models * enable editing rent period in the UI display change button on org details page display rent periods question on edit page * enable updating org rent periods update logic in #update related tests * alter redirect after support user creates an organisation * adjust various UI elements: ordering of rows, copy changes, label size and associated tests * rework the #rent_period_labels method to return All under the correct conditions, rework tests related to that. + fix assorted tests that were either flakey or breaking due to addition of rent periods logic to create and update * amend failing tests and resolve linting complaints * changes following review * disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid hint text added to the question to explain this I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method * update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods update tests adding both cases and removing unnecessary additional db additions * rake task to create rent period associations for orgs that have none * revert mistaken copy changes in designs * create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods * stub api call for factory value, update csv tests and fixtures accordingly * extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests * remove before(:context) and associated patterns. use assign_attributes in various places for cleaner code * escape . in regex for API call stubs to satisfy codeQL remove destroy_all call at the start of a test that was dealing with leftover modesl in the test db * further refactoring of various tests to reduce database interactions and improve speed * remove outdated distinction between unitletas mappings from before 23/24 * remove tests that seem to be testing active record and/or ruby Date class
7 months ago
context "when the org uses all rent periods" do
before do
create(:organisation_rent_period, organisation:, rent_period: 2)
end
it "#rent_periods returns the correct ids" do
expect(organisation.rent_periods).to eq [1, 2]
end
it "#rent_period_labels returns All" do
expect(organisation.rent_period_labels).to eq %w[All]
end
context "and has organisation rent periods associated for rent periods that no longer appear in the form" do
before do
create(:organisation_rent_period, organisation:, rent_period: 3)
end
it "#rent_period_labels returns All" do
expect(organisation.rent_period_labels).to eq %w[All]
end
end
end
end
context "with lettings logs" do
let(:other_organisation) { create(:organisation) }
let!(:owned_lettings_log) do
create(
:lettings_log,
:completed,
managing_organisation: other_organisation,
assigned_to: user,
)
end
let!(:managed_lettings_log) do
create(
:lettings_log,
assigned_to: user,
)
end
it "has owned lettings logs" do
expect(organisation.owned_lettings_logs.first).to eq(owned_lettings_log)
end
it "has managed lettings logs" do
expect(organisation.managed_lettings_logs.first).to eq(managed_lettings_log)
end
it "has lettings logs" do
expect(organisation.lettings_logs.to_a).to match_array([owned_lettings_log, managed_lettings_log])
end
end
end
describe "paper trail" do
let(:organisation) { create(:organisation) }
it "creates a record of changes to a log" do
expect { organisation.update!(name: "new test name") }.to change(organisation.versions, :count).by(1)
end
it "allows lettings logs to be restored to a previous version" do
organisation.update!(name: "new test name")
expect(organisation.paper_trail.previous_version.name).to eq("MHCLG")
end
end
describe "delete cascade" do
context "when the organisation is deleted" do
let!(:organisation) { create(:organisation) }
let!(:user) { create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) }
let!(:scheme_to_delete) { create(:scheme, owning_organisation: user.organisation) }
let!(:log_to_delete) { create(:lettings_log, owning_organisation: user.organisation) }
let!(:sales_log_to_delete) { create(:sales_log, owning_organisation: user.organisation) }
context "when organisation is deleted" do
it "child relationships ie logs, schemes and users are deleted too - application" do
organisation.destroy!
expect { organisation.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { LettingsLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { SalesLog.find(sales_log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
it "child relationships ie logs, schemes and users are deleted too - database" do
ActiveRecord::Base.connection.exec_query("DELETE FROM organisations WHERE id = #{organisation.id};")
expect { LettingsLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { SalesLog.find(sales_log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
describe "scopes" do
before do
create(:organisation, name: "Joe Bloggs", active: false)
create(:organisation, name: "Tom Smith", active: true)
end
context "when searching by name" do
it "returns case insensitive matching records" do
expect(described_class.search_by_name("Joe").count).to eq(1)
expect(described_class.search_by_name("joe").count).to eq(1)
end
end
context "when searching by all searchable field" do
it "returns case insensitive matching records" do
expect(described_class.search_by("Joe").count).to eq(1)
expect(described_class.search_by("joe").count).to eq(1)
end
end
context "when searching by active" do
it "returns only active records" do
results = described_class.filter_by_active
expect(results.count).to eq(1)
expect(results[0].name).to eq("Tom Smith")
end
end
end
describe "status" do
let!(:organisation) { create(:organisation) }
it "returns inactive when organisation inactive" do
organisation.active = false
expect(organisation.status).to be(:deactivated)
end
it "returns active when organisation active" do
organisation.active = true
expect(organisation.status).to be(:active)
end
it "returns merged when organisation merged in the past" do
organisation.merge_date = 1.month.ago
expect(organisation.status).to be(:merged)
end
it "does not return merged when organisation merges in the future" do
organisation.active = true
organisation.merge_date = Time.zone.now + 1.month
expect(organisation.status).to be(:active)
end
end
describe "discard" do
let(:organisation) { create(:organisation) }
let!(:user) { create(:user, organisation:) }
let!(:scheme) { create(:scheme, owning_organisation: organisation) }
context "when merged organisation is discarded" do
before do
organisation.merge_date = Time.zone.yesterday
organisation.absorbing_organisation_id = create(:organisation).id
organisation.save!
end
it "discards all of the organisation resources" do
organisation.discard!
expect(organisation.status).to eq(:deleted)
expect(user.reload.status).to eq(:deleted)
expect(scheme.reload.status).to eq(:deleted)
end
end
end
end