diff --git a/spec/models/form/lettings/questions/stock_owner_spec.rb b/spec/models/form/lettings/questions/stock_owner_spec.rb index 3b7f6baec..cb600b986 100644 --- a/spec/models/form/lettings/questions/stock_owner_spec.rb +++ b/spec/models/form/lettings/questions/stock_owner_spec.rb @@ -206,13 +206,8 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do end context "when user is support" do - user = nil - log = nil - - before do - user = create(:user, :support) - log = create(:lettings_log) - end + let!(:user) { create(:user, :support) } + 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) diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 1111c7b65..98b082c56 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -231,7 +231,7 @@ RSpec.describe Organisation, type: :model do end context "when searching by active" do - it "returns case active records" 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") diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 53e830592..0bb1c3bb6 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -579,6 +579,32 @@ RSpec.describe OrganisationsController, type: :request do expect(whodunnit_actor).to be_a(User) expect(whodunnit_actor.id).to eq(user.id) end + + context "with active parameter true" do + let(:params) do + { + id: organisation.id, + organisation: { active: "true" }, + } + end + + it "redirects" do + expect(response).to have_http_status(:unauthorized) + end + end + + context "with active parameter false" do + let(:params) do + { + id: organisation.id, + organisation: { active: "false" }, + } + end + + it "redirects" do + expect(response).to have_http_status(:unauthorized) + end + end end context "with an organisation that the user does not belong to" do @@ -1407,6 +1433,98 @@ RSpec.describe OrganisationsController, type: :request do end end + describe "#update" do + context "with active parameter false" do + let(:params) { { id: organisation.id, organisation: { active: "false" } } } + + user_to_update = nil + + before do + user_to_update = create(:user, :data_coordinator, organisation:) + patch "/organisations/#{organisation.id}", headers:, params: + end + + it "deactivates associated users" do + user_to_update.reload + expect(user_to_update.active).to eq(false) + expect(user_to_update.reactivate_with_organisation).to eq(true) + end + end + + context "with active parameter true" do + user_to_reactivate = nil + user_not_to_reactivate = nil + + let(:params) do + { + id: organisation.id, + organisation: { active: "true" }, + } + end + let(:notify_client) { instance_double(Notifications::Client) } + let(:devise_notify_mailer) { DeviseNotifyMailer.new } + + let(:expected_personalisation) do + { + name: user_to_reactivate.name, + email: user_to_reactivate.email, + organisation: organisation.name, + link: include("/account/confirmation?confirmation_token="), + } + end + + before do + allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer) + allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client) + allow(notify_client).to receive(:send_email).and_return(true) + + user_to_reactivate = create(:user, :data_coordinator, organisation:, active: false, reactivate_with_organisation: true) + user_not_to_reactivate = create(:user, :data_coordinator, organisation:, active: false, reactivate_with_organisation: false) + patch "/organisations/#{organisation.id}", headers:, params: + end + + it "reactivates users deactivated with organisation" do + user_to_reactivate.reload + user_not_to_reactivate.reload + expect(user_to_reactivate.active).to eq(true) + expect(user_to_reactivate.reactivate_with_organisation).to eq(false) + expect(user_not_to_reactivate.active).to eq(false) + end + + it "sends invitation emails" do + expect(notify_client).to have_received(:send_email).with(email_address: user_to_reactivate.email, template_id: User::BETA_ONBOARDING_TEMPLATE_ID, personalisation: expected_personalisation).once + end + end + end + + describe "#deactivate" do + before do + get "/organisations/#{organisation.id}/deactivate", headers:, params: {} + end + + it "shows deactivation page with deactivate and cancel buttons for the organisation" do + expect(path).to include("/organisations/#{organisation.id}/deactivate") + expect(page).to have_content(organisation.name) + expect(page).to have_content("Are you sure you want to deactivate this organisation?") + expect(page).to have_button("Deactivate this organisation") + expect(page).to have_link("Cancel", href: "/organisations/#{organisation.id}") + end + end + + describe "#reactivate" do + before do + get "/organisations/#{organisation.id}/reactivate", headers:, params: {} + end + + it "shows reactivation page with reactivate and cancel buttons for the organisation" do + expect(path).to include("/organisations/#{organisation.id}/reactivate") + expect(page).to have_content(organisation.name) + expect(page).to have_content("Are you sure you want to reactivate this organisation?") + expect(page).to have_button("Reactivate this organisation") + expect(page).to have_link("Cancel", href: "/organisations/#{organisation.id}") + end + end + describe "#create" do let(:name) { " Unique new org name" } let(:address_line1) { "12 Random Street" }