Browse Source

CLDC-2481: Add ability to disable organisations

pull/2293/head
Robert Sullivan 2 years ago committed by Kat
parent
commit
56203484e4
  1. 10
      app/controllers/organisations_controller.rb
  2. 2
      app/models/form/lettings/questions/stock_owner.rb
  3. 4
      app/views/organisations/toggle_active.html.erb
  4. 8
      db/migrate/20240305112507_add_default_value_to_organisation_active_field.rb
  5. 15
      spec/models/form/lettings/questions/managing_organisation_spec.rb
  6. 26
      spec/models/form/lettings/questions/stock_owner_spec.rb
  7. 7
      spec/models/form/sales/questions/managing_organisation_spec.rb
  8. 20
      spec/models/form/sales/questions/owning_organisation_id_spec.rb
  9. 5
      spec/models/location_spec.rb
  10. 41
      spec/models/organisation_spec.rb
  11. 5
      spec/models/scheme_spec.rb
  12. 40
      spec/policies/organisation_policy_spec.rb
  13. 28
      spec/requests/organisation_relationships_controller_spec.rb
  14. 112
      spec/views/locations/show.html.erb_spec.rb
  15. 14
      spec/views/organisations/show.html.erb_spec.rb
  16. 34
      spec/views/schemes/show.html.erb_spec.rb

10
app/controllers/organisations_controller.rb

@ -109,19 +109,21 @@ class OrganisationsController < ApplicationController
def update def update
if (current_user.data_coordinator? && org_params[:active].nil?) || current_user.support? if (current_user.data_coordinator? && org_params[:active].nil?) || current_user.support?
if @organisation.update(org_params) if @organisation.update(org_params)
if org_params[:active] == "false" case org_params[:active]
when "false"
@organisation.users.filter_by_active @organisation.users.filter_by_active
.update_all( .update_all(
active: false, active: false,
confirmed_at: nil, confirmed_at: nil,
sign_in_count: 0, sign_in_count: 0,
initial_confirmation_sent: false, initial_confirmation_sent: false,
reactivate_with_organisation: true) reactivate_with_organisation: true,
)
flash[:notice] = I18n.t("organisation.deactivated", organisation: @organisation.name) flash[:notice] = I18n.t("organisation.deactivated", organisation: @organisation.name)
elsif org_params[:active] == "true" when "true"
users_to_reactivate = @organisation.users.where(reactivate_with_organisation: true) users_to_reactivate = @organisation.users.where(reactivate_with_organisation: true)
users_to_reactivate.each do |user| users_to_reactivate.each do |user|
user.update(active: true, reactivate_with_organisation: false) user.update!(active: true, reactivate_with_organisation: false)
user.send_confirmation_instructions user.send_confirmation_instructions
end end
flash[:notice] = I18n.t("organisation.reactivated", organisation: @organisation.name) flash[:notice] = I18n.t("organisation.reactivated", organisation: @organisation.name)

2
app/models/form/lettings/questions/stock_owner.rb

@ -40,7 +40,7 @@ class Form::Lettings::Questions::StockOwner < ::Form::Question
end end
end end
else else
user.organisation.stock_owners.each do |stock_owner| user.organisation.stock_owners.filter_by_active.each do |stock_owner|
answer_opts[stock_owner.id] = stock_owner.name answer_opts[stock_owner.id] = stock_owner.name
end end
recently_absorbed_organisations.each do |absorbed_org| recently_absorbed_organisations.each do |absorbed_org|

4
app/views/organisations/toggle_active.html.erb

@ -20,6 +20,10 @@
<%= f.hidden_field :active, value: active_value %> <%= f.hidden_field :active, value: active_value %>
<%= f.govuk_submit "#{action.capitalize} this organisation" %> <%= f.govuk_submit "#{action.capitalize} this organisation" %>
<p class="govuk-body">
<%= govuk_link_to("Cancel", organisation_path(@organisation)) %>
</p>
</div> </div>
</div> </div>
<% end %> <% end %>

8
db/migrate/20240305112507_add_default_value_to_organisation_active_field.rb

@ -1,8 +1,12 @@
class AddDefaultValueToOrganisationActiveField < ActiveRecord::Migration[7.0] class AddDefaultValueToOrganisationActiveField < ActiveRecord::Migration[7.0]
def change def up
change_column :organisations, :active, :boolean, :default => true change_column :organisations, :active, :boolean, default: true
execute "UPDATE organisations execute "UPDATE organisations
SET active = true;" SET active = true;"
end end
def down
change_column :organisations, :active, :boolean
end
end end

15
spec/models/form/lettings/questions/managing_organisation_spec.rb

@ -57,6 +57,7 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:managing_org1) { create(:organisation, name: "Managing org 1") } let(:managing_org1) { create(:organisation, name: "Managing org 1") }
let(:managing_org2) { create(:organisation, name: "Managing org 2") } let(:managing_org2) { create(:organisation, name: "Managing org 2") }
let(:managing_org3) { create(:organisation, name: "Managing org 3") } let(:managing_org3) { create(:organisation, name: "Managing org 3") }
let(:inactive_managing_org) { create(:organisation, name: "Inactive managing org", active: false) }
let(:log) { create(:lettings_log, managing_organisation: managing_org1) } let(:log) { create(:lettings_log, managing_organisation: managing_org1) }
let!(:org_rel1) do let!(:org_rel1) do
@ -76,7 +77,9 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
} }
end end
it "shows current managing agent at top, followed by user's org (with hint), followed by the managing agents of the user's org" do it "shows current managing agent at top, followed by user's org (with hint), followed by the active managing agents of the user's org" do
create(:organisation_relationship, parent_organisation: user.organisation, child_organisation: inactive_managing_org)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
end end
@ -100,6 +103,10 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
create(:organisation_relationship, parent_organisation: log_owning_org, child_organisation: managing_org3) create(:organisation_relationship, parent_organisation: log_owning_org, child_organisation: managing_org3)
end end
before do
create(:organisation, name: "Inactive managing org", active: false)
end
context "when org owns stock" do context "when org owns stock" do
let(:options) do let(:options) do
{ {
@ -111,7 +118,7 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
} }
end end
it "shows current managing agent at top, followed by the current owning organisation (with hint), followed by the managing agents of the current owning organisation" do it "shows current managing agent at top, followed by the current owning organisation (with hint), followed by the active managing agents of the current owning organisation" do
log_owning_org.update!(holds_own_stock: true) log_owning_org.update!(holds_own_stock: true)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
@ -133,7 +140,7 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
org_rel2.child_organisation.update!(merge_date: Time.zone.local(2023, 8, 2), absorbing_organisation_id: log_owning_org.id) org_rel2.child_organisation.update!(merge_date: Time.zone.local(2023, 8, 2), absorbing_organisation_id: log_owning_org.id)
end end
it "shows current managing agent at top, followed by the current owning organisation (with hint), followed by the managing agents of the current owning organisation" do it "shows current managing agent at top, followed by the current owning organisation (with hint), followed by the active managing agents of the current owning organisation" do
log_owning_org.update!(holds_own_stock: true) log_owning_org.update!(holds_own_stock: true)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
@ -149,7 +156,7 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
} }
end end
it "shows current managing agent at top, followed by the managing agents of the current owning organisation" do it "shows current managing agent at top, followed by the active managing agents of the current owning organisation" do
log_owning_org.update!(holds_own_stock: false) log_owning_org.update!(holds_own_stock: false)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end

26
spec/models/form/lettings/questions/stock_owner_spec.rb

@ -46,6 +46,7 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do
let(:owning_org_1) { create(:organisation, name: "Owning org 1") } let(:owning_org_1) { create(:organisation, name: "Owning org 1") }
let(:owning_org_2) { create(:organisation, name: "Owning org 2") } let(:owning_org_2) { create(:organisation, name: "Owning org 2") }
let(:inactive_owning_org) { create(:organisation, name: "Inactive owning org", active: false) }
let!(:org_rel) do let!(:org_rel) do
create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: owning_org_2) create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: owning_org_2)
end end
@ -61,7 +62,8 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do
} }
end end
it "shows current stock owner at top, followed by user's org (with hint), followed by the stock owners of the user's org" do it "shows current stock owner at top, followed by user's org (with hint), followed by the active stock owners of the user's org" do
create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: inactive_owning_org)
user.organisation.update!(holds_own_stock: true) user.organisation.update!(holds_own_stock: true)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
@ -93,7 +95,8 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do
} }
end end
it "shows current stock owner at top, followed by the stock owners of the user's org" do it "shows current stock owner at top, followed by the active stock active owners of the user's org" do
create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: inactive_owning_org)
user.organisation.update!(holds_own_stock: false) user.organisation.update!(holds_own_stock: false)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
@ -203,21 +206,26 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do
end end
context "when user is support" do context "when user is support" do
let(:user) { create(:user, :support) } user = nil
log = nil
before do
user = create(:user, :support)
log = create(:lettings_log)
end
let(:log) { create(:lettings_log) } it "shows active orgs where organisation holds own stock" do
non_stock_organisation = create(:organisation, name: "Non-stockholding org", holds_own_stock: false)
inactive_organisation = create(:organisation, name: "Inactive org", active: false)
let(:non_stock_organisation) { create(:organisation, holds_own_stock: false) } expected_opts = Organisation.filter_by_active.where(holds_own_stock: true).each_with_object(options) do |organisation, hsh|
let(:expected_opts) do
Organisation.where(holds_own_stock: true).each_with_object(options) do |organisation, hsh|
hsh[organisation.id] = organisation.name hsh[organisation.id] = organisation.name
hsh hsh
end end
end
it "shows orgs where organisation holds own stock" do
expect(question.displayed_answer_options(log, user)).to eq(expected_opts) expect(question.displayed_answer_options(log, user)).to eq(expected_opts)
expect(question.displayed_answer_options(log, user)).not_to include(non_stock_organisation.id) expect(question.displayed_answer_options(log, user)).not_to include(non_stock_organisation.id)
expect(question.displayed_answer_options(log, user)).not_to include(inactive_organisation.id)
end end
context "and org has recently absorbed other orgs and does not have available from date" do context "and org has recently absorbed other orgs and does not have available from date" do

7
spec/models/form/sales/questions/managing_organisation_spec.rb

@ -57,6 +57,7 @@ RSpec.describe Form::Sales::Questions::ManagingOrganisation, type: :model do
let(:managing_org1) { create(:organisation, name: "Managing org 1") } let(:managing_org1) { create(:organisation, name: "Managing org 1") }
let(:managing_org2) { create(:organisation, name: "Managing org 2") } let(:managing_org2) { create(:organisation, name: "Managing org 2") }
let(:managing_org3) { create(:organisation, name: "Managing org 3") } let(:managing_org3) { create(:organisation, name: "Managing org 3") }
let(:inactive_org) { create(:organisation, name: "Inactive org", active: false) }
let(:log) do let(:log) do
create(:lettings_log, owning_organisation: log_owning_org, managing_organisation: managing_org1, create(:lettings_log, owning_organisation: log_owning_org, managing_organisation: managing_org1,
@ -80,7 +81,8 @@ RSpec.describe Form::Sales::Questions::ManagingOrganisation, type: :model do
} }
end end
it "shows current managing agent at top, followed by the current owning organisation (with hint), followed by the managing agents of the current owning organisation" do it "shows current managing agent at top, followed by the current owning organisation (with hint), followed by the active managing agents of the current owning organisation" do
create(:organisation_relationship, parent_organisation: log_owning_org, child_organisation: inactive_org)
log_owning_org.update!(holds_own_stock: true) log_owning_org.update!(holds_own_stock: true)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
@ -96,7 +98,8 @@ RSpec.describe Form::Sales::Questions::ManagingOrganisation, type: :model do
} }
end end
it "shows current managing agent at top, followed by the managing agents of the current owning organisation" do it "shows current managing agent at top, followed by the active managing agents of the current owning organisation" do
create(:organisation_relationship, parent_organisation: log_owning_org, child_organisation: inactive_org)
log_owning_org.update!(holds_own_stock: false) log_owning_org.update!(holds_own_stock: false)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end

20
spec/models/form/sales/questions/owning_organisation_id_spec.rb

@ -57,6 +57,7 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do
let(:owning_org_1) { create(:organisation, name: "Owning org 1") } let(:owning_org_1) { create(:organisation, name: "Owning org 1") }
let(:owning_org_2) { create(:organisation, name: "Owning org 2") } let(:owning_org_2) { create(:organisation, name: "Owning org 2") }
let(:inactive_owning_org) { create(:organisation, name: "Inactive owning org", active: false) }
let(:non_stock_owner) { create(:organisation, name: "Non stock owner", holds_own_stock: false) } let(:non_stock_owner) { create(:organisation, name: "Non stock owner", holds_own_stock: false) }
let(:log) { create(:lettings_log, owning_organisation: owning_org_1) } let(:log) { create(:lettings_log, owning_organisation: owning_org_1) }
@ -75,7 +76,8 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do
} }
end end
it "shows user organisation, current owning organisation and the stock owners that hold their stock" do it "shows user organisation, current owning organisation and the activestock owners that hold their stock" do
create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: inactive_owning_org)
user.organisation.update!(holds_own_stock: true) user.organisation.update!(holds_own_stock: true)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
@ -95,7 +97,8 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do
create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: non_stock_owner) create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: non_stock_owner)
end end
it "shows current owning organisation and the stock owners that hold their stock" do it "shows current owning organisation and the active stock owners that hold their stock" do
create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: inactive_owning_org)
user.organisation.update!(holds_own_stock: false) user.organisation.update!(holds_own_stock: false)
expect(question.displayed_answer_options(log, user)).to eq(options) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
@ -204,20 +207,21 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do
context "when user is support" do context "when user is support" do
let(:user) { create(:user, :support, organisation: organisation_1) } let(:user) { create(:user, :support, organisation: organisation_1) }
let(:log) { create(:lettings_log, created_by: user) } let(:log) { create(:lettings_log, created_by: user) }
let(:non_stock_organisation) { create(:organisation, holds_own_stock: false) } it "shows active orgs where organisation holds own stock" do
let(:expected_opts) do non_stock_organisation = create(:organisation, holds_own_stock: false)
Organisation.where(holds_own_stock: true).each_with_object(options) do |organisation, hsh| inactive_org = create(:organisation, active: false)
expected_opts = Organisation.filter_by_active.where(holds_own_stock: true).each_with_object(options) do |organisation, hsh|
hsh[organisation.id] = organisation.name hsh[organisation.id] = organisation.name
hsh hsh
end end
end
it "shows orgs where organisation holds own stock" do
expect(question.displayed_answer_options(log, user)).to eq(expected_opts) expect(question.displayed_answer_options(log, user)).to eq(expected_opts)
expect(question.displayed_answer_options(log, user)).not_to include(non_stock_organisation.id) expect(question.displayed_answer_options(log, user)).not_to include(non_stock_organisation.id)
expect(question.displayed_answer_options(log, user)).not_to include(inactive_org.id)
expect(question.displayed_answer_options(log, user)).to include(organisation_1.id)
end end
context "when an org has recently absorbed other orgs" do context "when an org has recently absorbed other orgs" do

5
spec/models/location_spec.rb

@ -870,6 +870,11 @@ RSpec.describe Location, type: :model do
expect(location.status).to eq(:deactivating_soon) expect(location.status).to eq(:deactivating_soon)
end end
it "returns deactivated if the owning organisation is deactivated" do
location.scheme.owning_organisation.active = false
expect(location.status).to eq(:deactivated)
end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location:)
location.save! location.save!

41
spec/models/organisation_spec.rb

@ -212,8 +212,8 @@ RSpec.describe Organisation, type: :model do
describe "scopes" do describe "scopes" do
before do before do
create(:organisation, name: "Joe Bloggs") create(:organisation, name: "Joe Bloggs", active: false)
create(:organisation, name: "Tom Smith") create(:organisation, name: "Tom Smith", active: true)
end end
context "when searching by name" do context "when searching by name" do
@ -229,5 +229,42 @@ RSpec.describe Organisation, type: :model do
expect(described_class.search_by("joe").count).to eq(1) expect(described_class.search_by("joe").count).to eq(1)
end end
end end
context "when searching by active" do
it "returns case 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 end
end end

5
spec/models/scheme_spec.rb

@ -231,6 +231,11 @@ RSpec.describe Scheme, type: :model do
expect(scheme.status).to eq(:deactivating_soon) expect(scheme.status).to eq(:deactivating_soon)
end end
it "returns deactivated if the owning organisation is deactivated" do
scheme.owning_organisation.active = false
expect(scheme.status).to eq(:deactivated)
end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), scheme:)
scheme.reload scheme.reload

40
spec/policies/organisation_policy_spec.rb

@ -0,0 +1,40 @@
require "rails_helper"
# rubocop:disable RSpec/RepeatedExample
RSpec.describe OrganisationPolicy do
subject(:policy) { described_class }
let(:organisation) { FactoryBot.create(:organisation) }
let(:data_provider) { FactoryBot.create(:user, :data_provider) }
let(:data_coordinator) { FactoryBot.create(:user, :data_coordinator) }
let(:support) { FactoryBot.create(:user, :support) }
permissions :deactivate? do
it "does not permit data providers to deactivate an organisation" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not permit data coordinators to deactivate an organisation" do
expect(policy).not_to permit(data_coordinator, data_provider)
end
it "permits support users to deactivate an organisation" do
expect(policy).to permit(support, data_provider)
end
end
permissions :reactivate? do
it "does not permit data providers to reactivate an organisation" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not permit data coordinators to reactivate an organisation" do
expect(policy).not_to permit(data_coordinator, data_provider)
end
it "permits support users to reactivate an organisation" do
expect(policy).to permit(support, data_provider)
end
end
end
# rubocop:enable RSpec/RepeatedExample

28
spec/requests/organisation_relationships_controller_spec.rb

@ -18,11 +18,13 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
context "with an organisation that the user belongs to" do context "with an organisation that the user belongs to" do
let!(:stock_owner) { FactoryBot.create(:organisation) } let!(:stock_owner) { FactoryBot.create(:organisation) }
let!(:other_org_stock_owner) { FactoryBot.create(:organisation, name: "Foobar LTD") } let!(:other_org_stock_owner) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:inactive_stock_owner) { FactoryBot.create(:organisation, name: "Inactive LTD", active: false) }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: stock_owner) FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: stock_owner)
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_stock_owner) FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_stock_owner)
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: inactive_stock_owner)
get "/organisations/#{organisation.id}/stock-owners", headers:, params: {} get "/organisations/#{organisation.id}/stock-owners", headers:, params: {}
end end
@ -46,11 +48,18 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
expect(page).not_to have_content(other_org_stock_owner.name) expect(page).not_to have_content(other_org_stock_owner.name)
end end
it "does not show inactive stock owners" do
expect(page).not_to have_content(inactive_stock_owner.name)
end
it "shows the pagination count" do it "shows the pagination count" do
expect(page).to have_content("1 total stock owners") expect(page).to have_content("1 total stock owners")
end end
context "when adding a stock owner" do context "when adding a stock owner" do
let!(:active_organisation) { FactoryBot.create(:organisation, name: "Active Org", active: true) }
let!(:inactive_organisation) { FactoryBot.create(:organisation, name: "Inactive LTD", active: false) }
before do before do
get "/organisations/#{organisation.id}/stock-owners/add", headers:, params: {} get "/organisations/#{organisation.id}/stock-owners/add", headers:, params: {}
end end
@ -66,6 +75,11 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "shows a cancel button" do it "shows a cancel button" do
expect(page).to have_link("Cancel", href: "/organisations/#{organisation.id}/stock-owners") expect(page).to have_link("Cancel", href: "/organisations/#{organisation.id}/stock-owners")
end end
it "includes only active organisations as options" do
expect(response.body).to include(active_organisation.name)
expect(response.body).not_to include(inactive_organisation.name)
end
end end
end end
@ -84,11 +98,13 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
context "with an organisation that the user belongs to" do context "with an organisation that the user belongs to" do
let!(:managing_agent) { FactoryBot.create(:organisation) } let!(:managing_agent) { FactoryBot.create(:organisation) }
let!(:other_org_managing_agent) { FactoryBot.create(:organisation, name: "Foobar LTD") } let!(:other_org_managing_agent) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:inactive_managing_agent) { FactoryBot.create(:organisation, name: "Inactive LTD", active: false) }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent) FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent) FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: inactive_managing_agent)
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {} get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end end
@ -112,12 +128,19 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
expect(page).not_to have_content(other_org_managing_agent.name) expect(page).not_to have_content(other_org_managing_agent.name)
end end
it "does not show inactive managing-agents" do
expect(page).not_to have_content(inactive_managing_agent.name)
end
it "shows the pagination count" do it "shows the pagination count" do
expect(page).to have_content("1 total managing agents") expect(page).to have_content("1 total managing agents")
end end
end end
context "when adding a managing agent" do context "when adding a managing agent" do
let!(:active_organisation) { FactoryBot.create(:organisation, name: "Active Org", active: true) }
let!(:inactive_organisation) { FactoryBot.create(:organisation, name: "Inactive LTD", active: false) }
before do before do
get "/organisations/#{organisation.id}/managing-agents/add", headers:, params: {} get "/organisations/#{organisation.id}/managing-agents/add", headers:, params: {}
end end
@ -125,6 +148,11 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "has the correct header" do it "has the correct header" do
expect(response.body).to include("What is the name of your managing agent?") expect(response.body).to include("What is the name of your managing agent?")
end end
it "includes only active organisations as options" do
expect(response.body).to include(active_organisation.name)
expect(response.body).not_to include(inactive_organisation.name)
end
end end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do

112
spec/views/locations/show.html.erb_spec.rb

@ -1,50 +1,50 @@
require "rails_helper" require "rails_helper"
RSpec.describe "locations/show.html.erb" do RSpec.describe "locations/show.html.erb" do
context "when a data provider" do let(:scheme) do
let(:user) { create(:user) } instance_double(
Scheme,
owning_organisation: user.organisation,
id: 1,
service_name: "some name",
id_to_display: "S1",
sensitive: false,
scheme_type: "some type",
registered_under_care_act: false,
arrangement_type: "some other type",
primary_client_group: false,
has_other_client_group: false,
secondary_client_group: false,
support_type: "some support type",
intended_stay: "some intended stay",
available_from: 1.week.ago,
scheme_deactivation_periods: [],
status: :active,
)
end
let(:scheme) do let(:location) do
instance_double( instance_double(
Scheme, Location,
owning_organisation: user.organisation, id: 5,
id: 1, name: "some location",
service_name: "some name", postcode: "EC1N 2TD",
id_to_display: "S1", linked_local_authorities: [],
sensitive: false, units: "",
scheme_type: "some type", type_of_unit: "",
registered_under_care_act: false, mobility_type: "",
arrangement_type: "some other type", available_from: 1.week.ago,
primary_client_group: false, location_deactivation_periods: [],
has_other_client_group: false, status: :active,
secondary_client_group: false, active?: true,
support_type: "some support type", scheme:,
intended_stay: "some intended stay", deactivates_in_a_long_time?: false,
available_from: 1.week.ago, is_la_inferred: nil,
scheme_deactivation_periods: [], )
status: :active, end
)
end
let(:location) do context "when a data provider" do
instance_double( let(:user) { create(:user) }
Location,
id: 5,
name: "some location",
postcode: "EC1N 2TD",
linked_local_authorities: [],
units: "",
type_of_unit: "",
mobility_type: "",
available_from: 1.week.ago,
location_deactivation_periods: [],
status: :active,
active?: true,
scheme:,
deactivates_in_a_long_time?: false,
is_la_inferred: nil,
)
end
it "does not see add a location button" do it "does not see add a location button" do
assign(:scheme, scheme) assign(:scheme, scheme)
@ -70,4 +70,32 @@ RSpec.describe "locations/show.html.erb" do
expect(rendered).not_to have_content("Change") expect(rendered).not_to have_content("Change")
end end
end end
context "when a support user" do
let(:user) { create(:user, role: "support") }
it "sees deactivate scheme location button" do
assign(:scheme, scheme)
assign(:location, location)
allow(view).to receive(:current_user).and_return(user)
render
expect(rendered).to have_content("Deactivate this location")
end
it "does not see deactivate scheme location button when organisation is deactivated" do
user.organisation.active = false
assign(:scheme, scheme)
assign(:location, location)
allow(view).to receive(:current_user).and_return(user)
render
expect(rendered).not_to have_content("Deactivate this location")
end
end
end end

14
spec/views/organisations/show.html.erb_spec.rb

@ -113,6 +113,20 @@ RSpec.describe "organisations/show.html.erb" do
expect(fragment).to have_link(text: "View agreement", href: "/organisations/#{organisation_with_dsa.id}/data-sharing-agreement") expect(fragment).to have_link(text: "View agreement", href: "/organisations/#{organisation_with_dsa.id}/data-sharing-agreement")
end end
end end
it "shows deactivate button when organisation is active" do
user.organisation.active = true
render
expect(fragment).to have_content("Deactivate this organisation")
expect(fragment).not_to have_content("Reactivate this organisation")
end
it "shows reactivate button when organisation is inactive" do
user.organisation.active = false
render
expect(fragment).not_to have_content("Deactivate this organisation")
expect(fragment).to have_content("Reactivate this organisation")
end
end end
context "when not dpo" do context "when not dpo" do

34
spec/views/schemes/show.html.erb_spec.rb

@ -1,10 +1,15 @@
require "rails_helper" require "rails_helper"
RSpec.describe "schemes/show.html.erb" do RSpec.describe "schemes/show.html.erb" do
let(:organisation) { create(:organisation, holds_own_stock: true) }
let(:scheme) { create(:scheme, owning_organisation: organisation, confirmed: true) }
before do
create(:location, scheme:, confirmed: true)
end
context "when data provider" do context "when data provider" do
let(:organisation) { create(:organisation, holds_own_stock: true) }
let(:user) { build(:user, organisation:) } let(:user) { build(:user, organisation:) }
let(:scheme) { create(:scheme, owning_organisation: user.organisation) }
it "does not render button to deactivate schemes" do it "does not render button to deactivate schemes" do
assign(:scheme, scheme) assign(:scheme, scheme)
@ -26,4 +31,29 @@ RSpec.describe "schemes/show.html.erb" do
expect(rendered).not_to have_content("Change") expect(rendered).not_to have_content("Change")
end end
end end
context "when support" do
let(:user) { build(:user, organisation:, role: "support") }
it "renders button to deactivate scheme" do
assign(:scheme, scheme)
allow(view).to receive(:current_user).and_return(user)
render
expect(rendered).to have_content("Deactivate this scheme")
end
it "does not render button to deactivate scheme if organisation is deactivated" do
organisation.active = false
assign(:scheme, scheme)
allow(view).to receive(:current_user).and_return(user)
render
expect(rendered).not_to have_content("Deactivate this scheme")
end
end
end end

Loading…
Cancel
Save