diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb new file mode 100644 index 000000000..568a2fa60 --- /dev/null +++ b/app/controllers/locations_controller.rb @@ -0,0 +1,67 @@ +class LocationsController < ApplicationController + include Pagy::Backend + before_action :authenticate_user! + before_action :authenticate_scope! + before_action :find_location, except: %i[new create index] + before_action :find_scheme + before_action :authenticate_action! + + def index + @pagy, @locations = pagy(@scheme.locations) + @total_count = @scheme.locations.size + end + + def new + @location = Location.new + end + + def create + @location = Location.new(location_params) + + if @location.save + location_params[:add_another_location] == "Yes" ? redirect_to(new_location_path(id: @scheme.id)) : redirect_to(scheme_check_answers_path(scheme_id: @scheme.id)) + else + render :new, status: :unprocessable_entity + end + end + + def edit; end + + def update + if @location.update(location_params) + location_params[:add_another_location] == "Yes" ? redirect_to(new_location_path(@location.scheme)) : redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) + else + render :edit, status: :unprocessable_entity + end + end + +private + + def find_scheme + @scheme = if %w[new create index].include?(action_name) + Scheme.find(params[:id]) + else + @location.scheme + end + end + + def find_location + @location = Location.find(params[:id]) + end + + def authenticate_scope! + head :unauthorized and return unless current_user.data_coordinator? || current_user.support? + end + + def authenticate_action! + if %w[new edit update create index].include?(action_name) && !((current_user.organisation == @scheme.organisation) || current_user.support?) + render_not_found and return + end + end + + def location_params + required_params = params.require(:location).permit(:postcode, :name, :total_units, :type_of_unit, :wheelchair_adaptation, :add_another_location).merge(scheme_id: @scheme.id) + required_params[:postcode] = required_params[:postcode].delete(" ").upcase.encode("ASCII", "UTF-8", invalid: :replace, undef: :replace, replace: "") if required_params[:postcode] + required_params + end +end diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 4dd657534..c6e18db29 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -20,12 +20,6 @@ class SchemesController < ApplicationController @scheme = Scheme.find_by(id: params[:id]) end - def locations - @scheme = Scheme.find_by(id: params[:id]) - @pagy, @locations = pagy(@scheme.locations) - @total_count = @scheme.locations.size - end - def new @scheme = Scheme.new end @@ -100,7 +94,7 @@ private when "secondary-client-group" scheme_support_path(@scheme) when "support" - scheme_check_answers_path(@scheme) + new_location_path when "details" scheme_primary_client_group_path(@scheme) end @@ -113,7 +107,6 @@ private :stock_owning_organisation_id, :scheme_type, :registered_under_care_act, - :total_units, :id, :has_other_client_group, :primary_client_group, diff --git a/app/helpers/tab_nav_helper.rb b/app/helpers/tab_nav_helper.rb index f316d5ab2..3ef211401 100644 --- a/app/helpers/tab_nav_helper.rb +++ b/app/helpers/tab_nav_helper.rb @@ -6,6 +6,11 @@ module TabNavHelper [govuk_link_to(link_text, user), "User #{user.email}"].join("\n") end + def location_cell(location) + link_text = location.postcode + [govuk_link_to(link_text, "/schemes/#{location.scheme.id}/locations/#{location.id}/edit", method: :patch), "Location #{location.name}"].join("\n") + end + def scheme_cell(scheme) link_text = scheme.service_name [govuk_link_to(link_text, scheme), "Scheme #{scheme.primary_client_group}"].join("\n") diff --git a/app/models/location.rb b/app/models/location.rb index d3cc23f82..c8be513eb 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,13 +1,28 @@ class Location < ApplicationRecord + include Validations::PropertyValidations + validate :validate_postcode belongs_to :scheme + attr_accessor :add_another_location + WHEELCHAIR_ADAPTATIONS = { - No: 0, Yes: 1, + No: 0, }.freeze enum wheelchair_adaptation: WHEELCHAIR_ADAPTATIONS + TYPE_OF_UNIT = { + "Self-contained flat or bedsit": 1, + "Self-contained flat or bedsit with common facilities": 2, + "Shared flat": 3, + "Shared house or hostel": 4, + "Bungalow": 5, + "Self-contained house": 6, + }.freeze + + enum type_of_unit: TYPE_OF_UNIT + def display_attributes [ { name: "Location code ", value: location_code, suffix: false }, @@ -17,4 +32,13 @@ class Location < ApplicationRecord { name: "Wheelchair adaptation", value: wheelchair_adaptation, suffix: false }, ] end + +private + + def validate_postcode + if postcode.nil? || !postcode&.match(Validations::PropertyValidations::POSTCODE_REGEXP) + error_message = I18n.t("validations.postcode") + errors.add :postcode, error_message + end + end end diff --git a/app/views/locations/edit.html.erb b/app/views/locations/edit.html.erb new file mode 100644 index 000000000..bbcccdef6 --- /dev/null +++ b/app/views/locations/edit.html.erb @@ -0,0 +1,63 @@ +<% content_for :title, "Add a location to this scheme" %> + +<% content_for :before_content do %> + <%= govuk_back_link( + text: "Back", + href: "/schemes/#{@scheme.id}/support", + ) %> +<% end %> + +<%= render partial: "organisations/headings", locals: { main: "Add a location to this scheme", sub: @scheme.service_name } %> + +<%= form_for(@location, method: :patch, url: location_path) do |f| %> +
+
+ <%= f.govuk_error_summary %> + + <%= f.govuk_text_field :postcode, + label: { size: "m" }, + hint: { text: "For example, SW1P 4DF." }, + width: 5 %> + + <%= f.govuk_text_field :name, + label: { text: "Name (optional)", size: "m" }, + hint: { text: "This is how you refer to this location within your organisation" } %> + + <%= f.govuk_number_field :total_units, + label: { text: "Total number of units at this location", size: "m" }, + width: 2, + hint: { text: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff.s" }, + autofocus: true %> + + <% type_of_units_selection = Location.type_of_units.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :type_of_unit, + type_of_units_selection, + :id, + :name, + legend: { text: "What is this type of scheme?", size: "m" } %> + + <% wheelchair_user_selection = Location.wheelchair_adaptations.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :wheelchair_adaptation, + wheelchair_user_selection, + :id, + :name, + hint: { text: "This includes stairlifts, ramps, level-access showers or grab rails" }, + legend: { text: "Are the majority of units in this location built or adapted to wheelchair-user standards?", size: "m" } %> + + <%= govuk_section_break(visible: true, size: "m") %> + + <% another_location_selection = %w[Yes no].map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :add_another_location, + another_location_selection, + :id, + :name, + inline: true, + legend: { text: "Do you want to add another location?", size: "m" } %> + + <%= f.govuk_submit "Save and continue" %> +
+
+<% end %> diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb new file mode 100644 index 000000000..6d3cc3de3 --- /dev/null +++ b/app/views/locations/index.html.erb @@ -0,0 +1,49 @@ +<% title = @scheme.service_name %> +<% content_for :title, title %> +<% content_for :before_content do %> + <%= govuk_back_link( + text: "Back", + href: "/schemes/#{@scheme.id}", + ) %> +<% end %> +<%= render partial: "organisations/headings", locals: { main: @scheme.service_name, sub: nil } %> +<% location_caption = @scheme.locations.count.eql?(1) ? "1 location" : "#{@scheme.locations.count} locations" %> +<%= render SubNavigationComponent.new(items: scheme_items(request.path, @scheme.id, location_caption)) %> + +
+
+ <%= govuk_table do |table| %> + <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> + <%= @scheme.locations.count %> <%= @scheme.locations.count.eql?(1) ? "location" : "locations" %>. + <% end %> + <%= table.head do |head| %> + <%= head.row do |row| %> + <% row.cell(header: true, text: "Code", html_attributes: { + scope: "col", + }) %> + <% row.cell(header: true, text: "Postcode", html_attributes: { + scope: "col", + }) %> + <% row.cell(header: true, text: "Units", html_attributes: { + scope: "col", + }) %> + <% row.cell(header: true, text: "Common unit type", html_attributes: { + scope: "col", + }) %> + <% end %> + <% end %> + <% @locations.each do |location| %> + <%= table.body do |body| %> + <%= body.row do |row| %> + <% row.cell(text: location.id) %> + <% row.cell(text: location.postcode) %> + <% row.cell(text: location.total_units) %> + <% row.cell(text: simple_format("#{location.type_of_unit}#{location.wheelchair_adaptation == 'Yes' ? "\nWith wheelchair adaptations" : ''}")) %> + <% end %> + <% end %> + <% end %> + <% end %> +
+
+ +<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %> diff --git a/app/views/locations/new.html.erb b/app/views/locations/new.html.erb new file mode 100644 index 000000000..8c8ff23c5 --- /dev/null +++ b/app/views/locations/new.html.erb @@ -0,0 +1,63 @@ +<% content_for :title, "Add a location to this scheme" %> + +<% content_for :before_content do %> + <%= govuk_back_link( + text: "Back", + href: "/schemes/#{@scheme.id}/support", + ) %> +<% end %> + +<%= render partial: "organisations/headings", locals: { main: "Add a location to this scheme", sub: @scheme.service_name } %> + +<%= form_for(@location, method: :post, url: locations_path) do |f| %> +
+
+ <%= f.govuk_error_summary %> + + <%= f.govuk_text_field :postcode, + label: { size: "m" }, + hint: { text: "For example, SW1P 4DF." }, + width: 5 %> + + <%= f.govuk_text_field :name, + label: { text: "Name (optional)", size: "m" }, + hint: { text: "This is how you refer to this location within your organisation" } %> + + <%= f.govuk_number_field :total_units, + label: { text: "Total number of units at this location", size: "m" }, + width: 2, + hint: { text: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff.s" }, + autofocus: true %> + + <% type_of_units_selection = Location.type_of_units.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :type_of_unit, + type_of_units_selection, + :id, + :name, + legend: { text: "What is this type of scheme?", size: "m" } %> + + <% wheelchair_user_selection = Location.wheelchair_adaptations.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :wheelchair_adaptation, + wheelchair_user_selection, + :id, + :name, + hint: { text: "This includes stairlifts, ramps, level-access showers or grab rails" }, + legend: { text: "Are the majority of units in this location built or adapted to wheelchair-user standards?", size: "m" } %> + + <%= govuk_section_break(visible: true, size: "m") %> + + <% another_location_selection = %w[Yes No].map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :add_another_location, + another_location_selection, + :id, + :name, + inline: true, + legend: { text: "Do you want to add another location?", size: "m" } %> + + <%= f.govuk_submit "Save and continue" %> +
+
+<% end %> diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index 78fff8674..da0db6b15 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -1,65 +1,105 @@ <% content_for :title, "Check your answers before creating this scheme" %> -<%= render partial: "organisations/headings", locals: { main: "Check your changes before updating this scheme", sub: @scheme.service_name } %> +<%= render partial: "organisations/headings", locals: { main: "Check your changes before creating this scheme", sub: @scheme.service_name } %> -<%= govuk_tabs(title: "Check your answers before creating this scheme") do |component| %> - <% component.tab(label: "Scheme") do %> - <%= govuk_summary_list do |summary_list| %> - <% @scheme.check_details_attributes.each do |attr| %> - <% next if current_user.data_coordinator? && attr[:name] == ("Managed by") %> - <%= summary_list.row do |row| %> - <% row.key { attr[:name].to_s } %> - <% row.value { details_html(attr) } %> - <% row.action( - text: "Change", - href: scheme_details_path(scheme_id: @scheme.id, check_answers: true), - ) %> - <% end %> - <% end %> - <% @scheme.check_primary_client_attributes.each do |attr| %> - <%= summary_list.row do |row| %> - <% row.key { attr[:name].to_s } %> - <% row.value { details_html(attr) } %> - <% row.action( - text: "Change", - href: scheme_primary_client_group_path(scheme_id: @scheme.id, check_answers: true), - ) %> - <% end %> - <% end %> - <% @scheme.check_secondary_client_confirmation_attributes.each do |attr| %> - <%= summary_list.row do |row| %> - <% row.key { attr[:name].to_s } %> - <% row.value { details_html(attr) } %> - <% row.action( - text: "Change", - href: scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), - ) %> - <% end %> - <% end %> - <% if @scheme.has_other_client_group == "Yes" %> - <% @scheme.check_secondary_client_attributes.each do |attr| %> - <%= summary_list.row do |row| %> - <% row.key { attr[:name].to_s } %> - <% row.value { details_html(attr) } %> - <% row.action( - text: "Change", - href: scheme_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), - ) %> +<% location_caption = @scheme.locations.count.eql?(1) ? "1 location" : "#{@scheme.locations.count} locations" %> + +
+
+ <%= govuk_tabs(title: "Check your answers before creating this scheme") do |component| %> + <% component.tab(label: "Scheme") do %> + <%= govuk_summary_list do |summary_list| %> + <% @scheme.check_details_attributes.each do |attr| %> + <% next if current_user.data_coordinator? && attr[:name] == ("Managed by") %> + <%= summary_list.row do |row| %> + <% row.key { attr[:name].to_s } %> + <% row.value { details_html(attr) } %> + <% row.action( + text: "Change", + href: scheme_details_path(scheme_id: @scheme.id, check_answers: true), + ) %> + <% end %> + <% end %> + <% @scheme.check_primary_client_attributes.each do |attr| %> + <%= summary_list.row do |row| %> + <% row.key { attr[:name].to_s } %> + <% row.value { details_html(attr) } %> + <% row.action( + text: "Change", + href: scheme_primary_client_group_path(scheme_id: @scheme.id, check_answers: true), + ) %> + <% end %> + <% end %> + <% @scheme.check_secondary_client_confirmation_attributes.each do |attr| %> + <%= summary_list.row do |row| %> + <% row.key { attr[:name].to_s } %> + <% row.value { details_html(attr) } %> + <% row.action( + text: "Change", + href: scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), + ) %> + <% end %> + <% end %> + <% if @scheme.has_other_client_group == "Yes" %> + <% @scheme.check_secondary_client_attributes.each do |attr| %> + <%= summary_list.row do |row| %> + <% row.key { attr[:name].to_s } %> + <% row.value { details_html(attr) } %> + <% row.action( + text: "Change", + href: scheme_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), + ) %> + <% end %> + <% end %> + <% end %> + <% @scheme.check_support_attributes.each do |attr| %> + <%= summary_list.row do |row| %> + <% row.key { attr[:name].to_s } %> + <% row.value { details_html(attr) } %> + <% row.action( + text: "Change", + href: scheme_support_path(scheme_id: @scheme.id, check_answers: true), + ) %> + <% end %> <% end %> <% end %> <% end %> - <% @scheme.check_support_attributes.each do |attr| %> - <%= summary_list.row do |row| %> - <% row.key { attr[:name].to_s } %> - <% row.value { details_html(attr) } %> - <% row.action( - text: "Change", - href: scheme_support_path(scheme_id: @scheme.id, check_answers: true), - ) %> + <% component.tab(label: "Locations") do %> + <%= govuk_table do |table| %> + <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> + <%= @scheme.locations.count %> <%= @scheme.locations.count.eql?(1) ? "location" : "locations" %> + <% end %> + <%= table.head do |head| %> + <%= head.row do |row| %> + <% row.cell(header: true, text: "Code", html_attributes: { + scope: "col", + }) %> + <% row.cell(header: true, text: "Postcode", html_attributes: { + scope: "col", + }) %> + <% row.cell(header: true, text: "Units", html_attributes: { + scope: "col", + }) %> + <% row.cell(header: true, text: "Common unit type", html_attributes: { + scope: "col", + }) %> + <% end %> + <% end %> + <% @scheme.locations.each do |location| %> + <%= table.body do |body| %> + <%= body.row do |row| %> + <% row.cell(text: location.id) %> + <% row.cell(text: simple_format(location_cell(location), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %> + <% row.cell(text: location.total_units) %> + <% row.cell(text: simple_format("#{location.type_of_unit}#{location.wheelchair_adaptation == 'Yes' ? "\nWith wheelchair adaptations" : ''}")) %> + <% end %> + <% end %> + <% end %> <% end %> + <%= govuk_button_link_to "Add a location", new_location_path(id: @scheme.id), secondary: true %> <% end %> <% end %> - <% end %> -<% end %> +
+
<%= govuk_button_link_to "Create scheme", schemes_path(scheme_id: @scheme.id), html: { method: :get } %> diff --git a/app/views/schemes/locations.html.erb b/app/views/schemes/locations.html.erb deleted file mode 100644 index 0b29024c7..000000000 --- a/app/views/schemes/locations.html.erb +++ /dev/null @@ -1,46 +0,0 @@ -<% title = @scheme.service_name %> -<% content_for :title, title %> - -<% content_for :before_content do %> - <%= govuk_back_link( - text: "Back", - href: "/schemes/#{@scheme.id}", - ) %> -<% end %> - -<%= render partial: "organisations/headings", locals: { main: @scheme.service_name, sub: nil } %> - -<%= render SubNavigationComponent.new(items: scheme_items(request.path, @scheme.id, @scheme.locations.count.eql?(1) ? "1 location" : "#{@scheme.locations.count} locations")) %> - -
-
- <% @locations.each do |location| %> -
-
-

- <%= location.name %> -

-
-
-
- <% location.display_attributes.each do |attribute| %> -
-
- <%= attribute[:name] %> -
-
- <%= attribute[:value] %> - <% if attribute[:suffix] %> - <%= attribute[:suffix] %> - <% end %> -
-
- <% end %> -
-
-
- <% end %> -
-
- -<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %> diff --git a/config/routes.rb b/config/routes.rb index 0cbaa82c2..a643f2ab4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -44,7 +44,7 @@ Rails.application.routes.draw do get "check-answers", to: "schemes#check_answers" member do - get "locations", to: "schemes#locations" + resources :locations end end diff --git a/db/migrate/20220630114748_add_name_to_locations.rb b/db/migrate/20220630114748_add_name_to_locations.rb new file mode 100644 index 000000000..8da11bb69 --- /dev/null +++ b/db/migrate/20220630114748_add_name_to_locations.rb @@ -0,0 +1,5 @@ +class AddNameToLocations < ActiveRecord::Migration[7.0] + change_table :locations, bulk: true do |t| + t.integer :total_units + end +end diff --git a/db/migrate/20220630114917_remove_total_units_schemes.rb b/db/migrate/20220630114917_remove_total_units_schemes.rb new file mode 100644 index 000000000..77c022757 --- /dev/null +++ b/db/migrate/20220630114917_remove_total_units_schemes.rb @@ -0,0 +1,5 @@ +class RemoveTotalUnitsSchemes < ActiveRecord::Migration[7.0] + def change + remove_column :schemes, :total_units, :integer + end +end diff --git a/db/migrate/20220630120251_change_location_type_of_unit.rb b/db/migrate/20220630120251_change_location_type_of_unit.rb new file mode 100644 index 000000000..47d1e17b6 --- /dev/null +++ b/db/migrate/20220630120251_change_location_type_of_unit.rb @@ -0,0 +1,6 @@ +class ChangeLocationTypeOfUnit < ActiveRecord::Migration[7.0] + change_table :locations, bulk: true do |t| + t.remove :type_of_unit + t.integer :type_of_unit + end +end diff --git a/db/schema.rb b/db/schema.rb index b38a76b1a..62044426f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -191,9 +191,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do t.integer "joint" t.bigint "created_by_id" t.integer "illness_type_0" + t.integer "retirement_value_check" t.integer "tshortfall_known" t.integer "sheltered" - t.integer "retirement_value_check" t.integer "pregnancy_value_check" t.integer "hhtype" t.integer "new_old" @@ -239,7 +239,6 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do create_table "locations", force: :cascade do |t| t.string "location_code" t.string "postcode" - t.string "type_of_unit" t.string "type_of_building" t.integer "wheelchair_adaptation" t.bigint "scheme_id", null: false @@ -247,12 +246,14 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do t.string "county" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "total_units" + t.integer "type_of_unit" t.index ["scheme_id"], name: "index_locations_on_scheme_id" end create_table "logs_exports", force: :cascade do |t| t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } - t.datetime "started_at", precision: nil, null: false + t.datetime "started_at", null: false t.integer "base_number", default: 1, null: false t.integer "increment_number", default: 1, null: false t.boolean "empty_export", default: false, null: false @@ -305,7 +306,6 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do t.string "primary_client_group" t.string "secondary_client_group" t.integer "sensitive" - t.integer "total_units" t.integer "scheme_type" t.integer "registered_under_care_act" t.integer "support_type" diff --git a/db/seeds.rb b/db/seeds.rb index 0da986c48..175922921 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -76,7 +76,6 @@ unless Rails.env.test? registered_under_care_act: 0, support_type: 1, scheme_type: 4, - total_units: 5, intended_stay: "M", primary_client_group: "O", secondary_client_group: "H", @@ -90,7 +89,6 @@ unless Rails.env.test? registered_under_care_act: 1, support_type: 1, scheme_type: 5, - total_units: 2, intended_stay: "S", primary_client_group: "D", secondary_client_group: "E", @@ -104,7 +102,6 @@ unless Rails.env.test? registered_under_care_act: 1, support_type: 4, scheme_type: 7, - total_units: 7, intended_stay: "X", primary_client_group: "G", secondary_client_group: "R", @@ -117,7 +114,7 @@ unless Rails.env.test? location_code: "S254-CU193AA", postcode: "CU19 3AA", name: "Rectory Road", - type_of_unit: "Self-contained flat or bedsit", + type_of_unit: 4, type_of_building: "Purpose-built", county: "Mid Sussex", wheelchair_adaptation: 0, @@ -128,7 +125,7 @@ unless Rails.env.test? location_code: "S254-DM250DC", postcode: "DM25 0DC", name: "Smithy Lane", - type_of_unit: "Self-contained flat or bedsit with common facilities", + type_of_unit: 1, type_of_building: "Converted from previous residential or non-residential property", county: "Fife", wheelchair_adaptation: 1, @@ -139,7 +136,7 @@ unless Rails.env.test? location_code: "S254-YX130WP", postcode: "YX13 0WP", name: "Smithy Lane", - type_of_unit: "Shared house or hostel", + type_of_unit: 2, type_of_building: "Converted from previous residential or non-residential property", county: "Rochford", wheelchair_adaptation: 1, diff --git a/spec/factories/location.rb b/spec/factories/location.rb index e547e0bd2..53f8d2cd0 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -3,7 +3,7 @@ FactoryBot.define do location_code { Faker::Name.initials(number: 10) } postcode { Faker::Address.postcode.delete(" ") } name { Faker::Address.street_name } - type_of_unit { Faker::Lorem.word } + type_of_unit { Faker::Number.within(range: 1..6) } type_of_building { Faker::Lorem.word } wheelchair_adaptation { 0 } county { Faker::Address.state } diff --git a/spec/factories/scheme.rb b/spec/factories/scheme.rb index b16cc75cf..6ed28631b 100644 --- a/spec/factories/scheme.rb +++ b/spec/factories/scheme.rb @@ -5,7 +5,6 @@ FactoryBot.define do registered_under_care_act { Faker::Number.within(range: 0..1) } support_type { Faker::Number.within(range: 0..6) } scheme_type { 0 } - total_units { Faker::Number.number(digits: 2) } intended_stay { %w[M P S V X].sample } primary_client_group { %w[O H M L A G F B D E I S N R Q P X].sample } secondary_client_group { %w[O H M L A G F B D E I S N R Q P X].sample } diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index 62107aaac..98647a193 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -208,7 +208,7 @@ RSpec.describe "Schemes scheme Features" do it "shows details of those locations" do locations.each do |location| - expect(page).to have_content(location.location_code) + expect(page).to have_content(location.id) end end end @@ -375,173 +375,286 @@ RSpec.describe "Schemes scheme Features" do click_button "Save and continue" end - it "lets me check my answers" do - expect(page).to have_content "Check your changes before updating this scheme" + it "lets me add location" do + expect(page).to have_content "Add a location to this scheme" end - context "when changing answers" do - it "displays change links" do - assert_selector "a", text: "Change", count: 12 + context "when I press the back button" do + before do + click_link "Back" end - context "when changing details" do - before do - click_link("Change", href: "/schemes/#{scheme.id}/details?check_answers=true", match: :first) - end - - it "allows changing details questions" do - expect(page).to have_current_path("/schemes/#{scheme.id}/details?check_answers=true") + it "lets me select the secondary group" do + expect(page).to have_current_path("/schemes/#{scheme.id}/support") + expect(page).to have_content "What support does this scheme provide?" + end - fill_in "Scheme name", with: "Example" - choose "Direct access hostel" - choose "Yes – registered care home providing nursing care" + context "when I amend support" do + it "returns to the add location page" do click_button "Save and continue" - - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Example" - expect(page).to have_content "Yes – registered care home providing nursing care" + expect(page).to have_current_path("/schemes/#{scheme.id}/locations/new") end + end + end - context "when I press the back button" do - before do - click_link "Back" - end + context "when I add location to the scheme" do + before do + fill_in "Postcode", with: "SW1P 4DF" + fill_in "Name (optional)", with: "Some name" + fill_in "Total number of units at this location", with: 1 + choose "Self-contained house" + choose "location-wheelchair-adaptation-no-field" + choose "location-add-another-location-no-field" + click_button "Save and continue" + end - it "lets me select the support answers" do - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Check your changes before updating this scheme" - end - end + it "lets me check my answers" do + expect(page).to have_content "Check your changes before creating this scheme" end - context "when changing primary client group" do + context "when I select to view locations" do before do - click_link("Change", href: "/schemes/#{scheme.id}/primary-client-group?check_answers=true") + click_link "Locations" end - it "allows changing primary-client-group question" do - expect(page).to have_current_path("/schemes/#{scheme.id}/primary-client-group?check_answers=true") + it "displays information about locations" do + expect(page).to have_content "Locations" + expect(page).to have_content "#{scheme.locations.count} location" + end - choose "Older people with support needs" + it "displays information about newly created location" do + expect(page).to have_content "SW1P4DF" + expect(page).to have_content "Some name" + expect(page).to have_content "Self-contained house" + end + end + + context "and I select to add another location a scheme" do + before do + click_link "Add a location" + fill_in "Postcode", with: "XX1 1XX" + fill_in "Name (optional)", with: "Other name" + fill_in "Total number of units at this location", with: 2 + choose "Self-contained house" + choose "location-wheelchair-adaptation-no-field" + choose "location-add-another-location-no-field" click_button "Save and continue" + end - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Older people with support needs" + it "lets me check my answers" do + expect(page).to have_content "Check your changes before creating this scheme" end - context "when I press the back button" do + context "when I select to view locations" do before do - click_link "Back" + click_link "Locations" end - it "lets me select the support answers" do - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Check your changes before updating this scheme" + it "displays information about another location" do + expect(page).to have_content "Locations" + expect(page).to have_content "#{scheme.locations.count} location" + end + + it "displays information about newly created location" do + expect(page).to have_content "XX11XX" + expect(page).to have_content "Other name" + expect(page).to have_content "Self-contained house" + end + + context "when changing location details" do + before do + click_link "XX11XX" + fill_in "Postcode", with: "ZZ1 1ZZ" + click_button "Save and continue" + end + + it "displays changed location" do + expect(page).to have_content "Locations" + expect(page).to have_content "#{scheme.locations.count} location" + expect(page).to have_content "ZZ11ZZ" + end end end end - context "when changing confirm secondary group answer" do - before do - click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") + context "when changing answers" do + it "displays change links" do + assert_selector "a", text: "Change", count: 12 end - it "allows changing confirm-secondary-client-group question to yes" do - expect(page).to have_current_path("/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") + context "when changing details" do + before do + click_link("Change", href: "/schemes/#{scheme.id}/details?check_answers=true", match: :first) + end - choose "Yes" - click_button "Save and continue" + it "allows changing details questions" do + expect(page).to have_current_path("/schemes/#{scheme.id}/details?check_answers=true") - expect(page).to have_current_path("/schemes/#{scheme.id}/secondary-client-group?check_answers=true") + fill_in "Scheme name", with: "Example" + choose "Direct access hostel" + choose "Yes – registered care home providing nursing care" + click_button "Save and continue" - choose "People at risk of domestic violence" - click_button "Save and continue" + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "Example" + expect(page).to have_content "Yes – registered care home providing nursing care" + end - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "People at risk of domestic violence" + context "when I press the back button" do + before do + click_link "Back" + end + + it "lets me select the support answers" do + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "Check your changes before creating this scheme" + end + end end - context "when I press the back button" do + context "when changing primary client group" do before do - click_link "Back" + click_link("Change", href: "/schemes/#{scheme.id}/primary-client-group?check_answers=true") end - it "lets me select the support answers" do + it "allows changing primary-client-group question" do + expect(page).to have_current_path("/schemes/#{scheme.id}/primary-client-group?check_answers=true") + + choose "Older people with support needs" + click_button "Save and continue" + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Check your changes before updating this scheme" + expect(page).to have_content "Older people with support needs" end - end - end - context "when allows changing confirm-secondary-client-group question to no" do - before do - click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") + context "when I press the back button" do + before do + click_link "Back" + end + + it "lets me select the support answers" do + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "Check your changes before creating this scheme" + end + end end - it "allows changing confirm-secondary-client-group question to no" do - expect(page).to have_current_path("/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") + context "when changing confirm secondary group answer" do + before do + click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") + end - choose "No" - click_button "Save and continue" + it "allows changing confirm-secondary-client-group question to yes" do + expect(page).to have_current_path("/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).not_to have_content "Secondary client group" - end - end + choose "Yes" + click_button "Save and continue" - context "when changing secondary-client-group question" do - before do - click_link("Change", href: "/schemes/#{scheme.id}/secondary-client-group?check_answers=true") - end + expect(page).to have_current_path("/schemes/#{scheme.id}/secondary-client-group?check_answers=true") - it "allows changing secondary-client-group question" do - expect(page).to have_current_path("/schemes/#{scheme.id}/secondary-client-group?check_answers=true") + choose "People at risk of domestic violence" + click_button "Save and continue" - choose "People at risk of domestic violence" - click_button "Save and continue" + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "People at risk of domestic violence" + end + + context "when I press the back button" do + before do + click_link "Back" + end - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "People at risk of domestic violence" + it "lets me select the support answers" do + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "Check your changes before creating this scheme" + end + end end - context "when I press the back button" do + context "when allows changing confirm-secondary-client-group question to no" do before do - click_link "Back" + click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") end - it "lets me select the support answers" do + it "allows changing confirm-secondary-client-group question to no" do + expect(page).to have_current_path("/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") + + choose "No" + click_button "Save and continue" + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Check your changes before updating this scheme" + expect(page).not_to have_content "Secondary client group" end end - end - context "when changing support questions" do - before do - click_link("Change", href: "/schemes/#{scheme.id}/support?check_answers=true", match: :first) - end + context "when changing secondary-client-group question" do + before do + click_link("Change", href: "/schemes/#{scheme.id}/secondary-client-group?check_answers=true") + end - it "allows changing support questions" do - expect(page).to have_current_path("/schemes/#{scheme.id}/support?check_answers=true") + it "allows changing secondary-client-group question" do + expect(page).to have_current_path("/schemes/#{scheme.id}/secondary-client-group?check_answers=true") - choose "Resettlement support" - choose "Medium stay" - click_button "Save and continue" + choose "People at risk of domestic violence" + click_button "Save and continue" + + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "People at risk of domestic violence" + end - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Resettlement support" - expect(page).to have_content "Medium stay" + context "when I press the back button" do + before do + click_link "Back" + end + + it "lets me select the support answers" do + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "Check your changes before creating this scheme" + end + end end - context "when I press the back button" do + context "when changing support questions" do before do - click_link "Back" + click_link("Change", href: "/schemes/#{scheme.id}/support?check_answers=true", match: :first) end - it "lets me select the support answers" do - expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_content "Check your changes before updating this scheme" + it "allows changing support questions" do + expect(page).to have_current_path("/schemes/#{scheme.id}/support?check_answers=true") + + choose "Resettlement support" + choose "Medium stay" + click_button "Save and continue" + + expect(page).to have_current_path("/schemes/#{scheme.id}/locations/new") + expect(page).to have_content "Add a location to this scheme" end + + context "when I press the back button" do + before do + click_link "Back" + end + + it "lets me select the support answers" do + expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") + expect(page).to have_content "Check your changes before creating this scheme" + end + end + end + end + + context "and I select to create a scheme" do + before do + click_link "Create scheme" + end + + it "adds scheme to the list of schemes" do + expect(page).to have_content "Supported housing schemes" + expect(page).to have_content scheme.id_to_display + expect(page).to have_content scheme.service_name + expect(page).to have_content scheme.organisation.name + expect(page).to have_content scheme.stock_owning_organisation.name + expect(page).to have_content "#{scheme.organisation.name} has been created." end end end diff --git a/spec/helpers/tab_nav_helper_spec.rb b/spec/helpers/tab_nav_helper_spec.rb index f1e483fd1..3403ce490 100644 --- a/spec/helpers/tab_nav_helper_spec.rb +++ b/spec/helpers/tab_nav_helper_spec.rb @@ -3,7 +3,8 @@ require "rails_helper" RSpec.describe TabNavHelper do let(:organisation) { FactoryBot.create(:organisation) } let(:user) { FactoryBot.build(:user, organisation:) } - let(:scheme) { FactoryBot.build(:scheme, service_name: "Some name") } + let(:scheme) { FactoryBot.create(:scheme, service_name: "Some name") } + let(:location) { FactoryBot.create(:location, scheme:) } describe "#user_cell" do it "returns user link and email separated by a newline character" do @@ -19,9 +20,16 @@ RSpec.describe TabNavHelper do end end + describe "#location_cell" do + it "returns the location link to the postcode with optional name" do + expected_html = "#{location.postcode}\nLocation #{location.name}" + expect(location_cell(location)).to match(expected_html) + end + end + describe "#scheme_cell" do it "returns the scheme link service name and primary user group separated by a newline character" do - expected_html = "#{scheme.service_name}\nScheme #{scheme.primary_client_group}" + expected_html = "#{scheme.service_name}\nScheme #{scheme.primary_client_group}" expect(scheme_cell(scheme)).to match(expected_html) end end diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb new file mode 100644 index 000000000..a886c6034 --- /dev/null +++ b/spec/models/location_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +RSpec.describe Location, type: :model do + describe "#new" do + let(:location) { FactoryBot.build(:location) } + + it "belongs to an organisation" do + expect(location.scheme).to be_a(Scheme) + end + end + + describe "#validate_postcode" do + let(:location) { FactoryBot.build(:location) } + + it "does not add an error if postcode is valid" do + location.postcode = "M1 1AE" + location.save! + expect(location.errors).to be_empty + end + + it "does add an error when the postcode is invalid" do + location.postcode = "invalid" + expect { location.save! } + .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Postcode Enter a postcode in the correct format, for example AA1 1AA") + end + end +end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb new file mode 100644 index 000000000..36025a306 --- /dev/null +++ b/spec/requests/locations_controller_spec.rb @@ -0,0 +1,784 @@ +require "rails_helper" + +RSpec.describe LocationsController, type: :request do + let(:page) { Capybara::Node::Simple.new(response.body) } + let(:user) { FactoryBot.create(:user, :support) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + + describe "#new" do + context "when not signed in" do + it "redirects to the sign in page" do + get "/schemes/1/locations/new" + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when signed in as a data provider" do + let(:user) { FactoryBot.create(:user) } + + before do + sign_in user + get "/schemes/1/locations/new" + end + + it "returns 401 unauthorized" do + request + expect(response).to have_http_status(:unauthorized) + end + end + + context "when signed in as a data coordinator" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + + before do + sign_in user + get "/schemes/#{scheme.id}/locations/new" + end + + it "returns a template for a new location" do + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + + context "when trying to new location to a scheme that belongs to another organisation" do + let(:another_scheme) { FactoryBot.create(:scheme) } + + it "displays the new page with an error message" do + get "/schemes/#{another_scheme.id}/locations/new" + expect(response).to have_http_status(:not_found) + end + end + end + + context "when signed in as a support user" do + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + get "/schemes/#{scheme.id}/locations/new" + end + + it "returns a template for a new location" do + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + end + end + + describe "#create" do + context "when not signed in" do + it "redirects to the sign in page" do + post "/schemes/1/locations" + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when signed in as a data provider" do + let(:user) { FactoryBot.create(:user) } + + before do + sign_in user + post "/schemes/1/locations" + end + + it "returns 401 unauthorized" do + request + expect(response).to have_http_status(:unauthorized) + end + end + + context "when signed in as a data coordinator" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + before do + sign_in user + post "/schemes/#{scheme.id}/locations", params: params + end + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your answers before creating this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.postcode).to eq("ZZ11ZZ") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + + context "when postcode is submitted with lower case" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + + it "creates a new location for scheme with postcode " do + expect(Location.last.postcode).to eq("ZZ11ZZ") + end + end + + context "when trying to add location to a scheme that belongs to another organisation" do + let(:another_scheme) { FactoryBot.create(:scheme) } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + it "displays the new page with an error message" do + post "/schemes/#{another_scheme.id}/locations", params: params + expect(response).to have_http_status(:not_found) + end + end + + context "when required postcode param is missing" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.postcode")) + end + end + + context "when do you want to add another location is selected as yes" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is selected as no" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is not selected" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + end + + context "when signed in as a support user" do + let(:user) { FactoryBot.create(:user, :support) } + let!(:scheme) { FactoryBot.create(:scheme) } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + post "/schemes/#{scheme.id}/locations", params: params + end + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your answers before creating this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.postcode).to eq("ZZ11ZZ") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + + context "when postcode is submitted with lower case" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + + it "creates a new location for scheme with postcode " do + expect(Location.last.postcode).to eq("ZZ11ZZ") + end + end + + context "when required postcode param is missing" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No" } } } + + it "displays the new page with an error message" do + post "/schemes/#{scheme.id}/locations", params: params + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.postcode")) + end + end + + context "when do you want to add another location is selected as yes" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is selected as no" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is not selected" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + + it "creates a new location for scheme with valid params and redirects to correct page" do + expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "creates a new location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + end + end + + describe "#edit" do + context "when not signed in" do + it "redirects to the sign in page" do + get "/schemes/1/locations/1/edit" + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when signed in as a data provider" do + let(:user) { FactoryBot.create(:user) } + + before do + sign_in user + get "/schemes/1/locations/1/edit" + end + + it "returns 401 unauthorized" do + request + expect(response).to have_http_status(:unauthorized) + end + end + + context "when signed in as a data coordinator" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let!(:location) { FactoryBot.create(:location, scheme:) } + + before do + sign_in user + get "/schemes/#{scheme.id}/locations/#{location.id}/edit" + end + + it "returns a template for a new location" do + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + + context "when trying to new location to a scheme that belongs to another organisation" do + let(:another_scheme) { FactoryBot.create(:scheme) } + let(:another_location) { FactoryBot.create(:location, scheme: another_scheme) } + + it "displays the new page with an error message" do + get "/schemes/#{another_scheme.id}/locations/#{another_location.id}/edit" + expect(response).to have_http_status(:not_found) + end + end + end + + context "when signed in as a support user" do + let(:user) { FactoryBot.create(:user, :support) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let!(:location) { FactoryBot.create(:location, scheme:) } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + get "/schemes/#{scheme.id}/locations/#{location.id}/edit" + end + + it "returns a template for a new location" do + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + end + end + + describe "#update" do + context "when not signed in" do + it "redirects to the sign in page" do + patch "/schemes/1/locations/1" + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when signed in as a data provider" do + let(:user) { FactoryBot.create(:user) } + + before do + sign_in user + patch "/schemes/1/locations/1" + end + + it "returns 401 unauthorized" do + request + expect(response).to have_http_status(:unauthorized) + end + end + + context "when signed in as a data coordinator" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let!(:location) { FactoryBot.create(:location, scheme:) } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + before do + sign_in user + patch "/schemes/#{scheme.id}/locations/#{location.id}", params: params + end + + it "updates existing location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your answers before creating this scheme") + end + + it "updates existing location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.postcode).to eq("ZZ11ZZ") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + + context "when postcode is submitted with lower case" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + + it "updates existing location for scheme with postcode " do + expect(Location.last.postcode).to eq("ZZ11ZZ") + end + end + + context "when trying to update location for a scheme that belongs to another organisation" do + let(:another_scheme) { FactoryBot.create(:scheme) } + let(:another_location) { FactoryBot.create(:location) } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + it "displays the new page with an error message" do + patch "/schemes/#{another_scheme.id}/locations/#{another_location.id}", params: params + expect(response).to have_http_status(:not_found) + end + end + + context "when required postcode param is invalid" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.postcode")) + end + end + + context "when do you want to add another location is selected as yes" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + + it "updates existing location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + + it "updates existing location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is selected as no" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + it "updates existing location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "updates existing location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is not selected" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + + it "updates existing location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "updates existing location for scheme with valid params" do + expect(Location.last.scheme.organisation_id).to eq(user.organisation_id) + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + end + + context "when signed in as a support user" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let!(:location) { FactoryBot.create(:location, scheme:) } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + patch "/schemes/#{scheme.id}/locations/#{location.id}", params: params + end + + it "updates a location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your answers before creating this scheme") + end + + it "updates existing location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.postcode).to eq("ZZ11ZZ") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + + context "when postcode is submitted with lower case" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + + it "updates a location for scheme with postcode " do + expect(Location.last.postcode).to eq("ZZ11ZZ") + end + end + + context "when required postcode param is missing" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.postcode")) + end + end + + context "when do you want to add another location is selected as yes" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + + it "updates location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Add a location to this scheme") + end + + it "updates existing location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is selected as no" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + + it "updates a location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "updates existing location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + + context "when do you want to add another location is not selected" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + + it "updates a location for scheme with valid params and redirects to correct page" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("Check your changes before creating this scheme") + end + + it "updates a location for scheme with valid params" do + expect(Location.last.name).to eq("Test") + expect(Location.last.total_units).to eq(5) + expect(Location.last.type_of_unit).to eq("Bungalow") + expect(Location.last.wheelchair_adaptation).to eq("No") + end + end + end + end + + describe "#index" do + context "when not signed in" do + it "redirects to the sign in page" do + get "/schemes/#{scheme.id}/locations" + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when signed in as a data provider user" do + let(:user) { FactoryBot.create(:user) } + + before do + sign_in user + get "/schemes/#{scheme.id}/locations" + end + + it "returns 401 unauthorized" do + request + expect(response).to have_http_status(:unauthorized) + end + end + + context "when signed in as a data coordinator user" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } + let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) } + + before do + sign_in user + get "/schemes/#{scheme.id}/locations" + end + + context "when coordinator attempts to see scheme belonging to a different organisation" do + let!(:another_scheme) { FactoryBot.create(:scheme) } + + before do + FactoryBot.create(:location, scheme:) + end + + it "returns 404 not found" do + get "/schemes/#{another_scheme.id}/locations" + expect(response).to have_http_status(:not_found) + end + end + + it "shows scheme" do + locations.each do |location| + expect(page).to have_content(location.id) + expect(page).to have_content(location.postcode) + expect(page).to have_content(location.type_of_unit) + expect(page).to have_content(location.wheelchair_adaptation) + end + end + + it "has page heading" do + expect(page).to have_content(scheme.service_name) + end + + it "has correct title" do + expected_title = CGI.escapeHTML("#{scheme.service_name} - Submit social housing lettings and sales data (CORE) - GOV.UK") + expect(page).to have_title(expected_title) + end + + context "when paginating over 20 results" do + let!(:locations) { FactoryBot.create_list(:location, 25, scheme:) } + + context "when on the first page" do + before do + get "/schemes/#{scheme.id}/locations" + end + + it "shows which schemes are being shown on the current page" do + expect(CGI.unescape_html(response.body)).to match("Showing 1 to 20 of #{locations.count} locations") + end + + it "has correct page 1 of 2 title" do + expected_title = CGI.escapeHTML("#{scheme.service_name} (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") + expect(page).to have_title(expected_title) + end + + it "has pagination links" do + expect(page).not_to have_content("Previous") + expect(page).not_to have_link("Previous") + expect(page).to have_content("Next") + expect(page).to have_link("Next") + end + end + + context "when on the second page" do + before do + get "/schemes/#{scheme.id}/locations?page=2" + end + + it "shows which schemes are being shown on the current page" do + expect(CGI.unescape_html(response.body)).to match("Showing 21 to 25 of #{locations.count} locations") + end + + it "has correct page 2 of 2 title" do + expected_title = CGI.escapeHTML("#{scheme.service_name} (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") + expect(page).to have_title(expected_title) + end + + it "has pagination links" do + expect(page).to have_content("Previous") + expect(page).to have_link("Previous") + expect(page).not_to have_content("Next") + expect(page).not_to have_link("Next") + end + end + end + end + + context "when signed in as a support user" do + let(:user) { FactoryBot.create(:user, :support) } + let!(:scheme) { FactoryBot.create(:scheme) } + let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + get "/schemes/#{scheme.id}/locations" + end + + it "shows scheme" do + locations.each do |location| + expect(page).to have_content(location.id) + expect(page).to have_content(location.postcode) + expect(page).to have_content(location.type_of_unit) + expect(page).to have_content(location.wheelchair_adaptation) + end + end + + it "has page heading" do + expect(page).to have_content(scheme.service_name) + end + + it "has correct title" do + expected_title = CGI.escapeHTML("#{scheme.service_name} - Submit social housing lettings and sales data (CORE) - GOV.UK") + expect(page).to have_title(expected_title) + end + + context "when paginating over 20 results" do + let!(:locations) { FactoryBot.create_list(:location, 25, scheme:) } + + context "when on the first page" do + before do + get "/schemes/#{scheme.id}/locations" + end + + it "shows which schemes are being shown on the current page" do + expect(CGI.unescape_html(response.body)).to match("Showing 1 to 20 of #{locations.count} locations") + end + + it "has correct page 1 of 2 title" do + expected_title = CGI.escapeHTML("#{scheme.service_name} (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") + expect(page).to have_title(expected_title) + end + + it "has pagination links" do + expect(page).not_to have_content("Previous") + expect(page).not_to have_link("Previous") + expect(page).to have_content("Next") + expect(page).to have_link("Next") + end + end + + context "when on the second page" do + before do + get "/schemes/#{scheme.id}/locations?page=2" + end + + it "shows which schemes are being shown on the current page" do + expect(CGI.unescape_html(response.body)).to match("Showing 21 to 25 of #{locations.count} locations") + end + + it "has correct page 1 of 2 title" do + expected_title = CGI.escapeHTML("#{scheme.service_name} (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") + expect(page).to have_title(expected_title) + end + + it "has pagination links" do + expect(page).to have_content("Previous") + expect(page).to have_link("Previous") + expect(page).not_to have_content("Next") + expect(page).not_to have_link("Next") + end + end + end + end + end +end diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 4dc88fdcb..2c2238f34 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -261,205 +261,6 @@ RSpec.describe SchemesController, type: :request do end end - describe "#locations" do - let(:specific_scheme) { schemes.first } - - context "when not signed in" do - it "redirects to the sign in page" do - get "/schemes/#{specific_scheme.id}/locations" - expect(response).to redirect_to("/account/sign-in") - end - end - - context "when signed in as a data provider user" do - let(:user) { FactoryBot.create(:user) } - - before do - sign_in user - get "/schemes/#{specific_scheme.id}/locations" - end - - it "returns 401 unauthorized" do - request - expect(response).to have_http_status(:unauthorized) - end - end - - context "when signed in as a data coordinator user" do - let(:user) { FactoryBot.create(:user, :data_coordinator) } - let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) } - let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) } - - before do - sign_in user - get "/schemes/#{scheme.id}/locations" - end - - context "when coordinator attempts to see scheme belonging to a different organisation" do - let!(:specific_scheme) { FactoryBot.create(:scheme) } - - before do - FactoryBot.create(:location, scheme: specific_scheme) - end - - it "returns 404 not found" do - get "/schemes/#{specific_scheme.id}/locations" - expect(response).to have_http_status(:not_found) - end - end - - it "shows scheme" do - locations.each do |location| - expect(page).to have_content(location.location_code) - expect(page).to have_content(location.postcode) - expect(page).to have_content(location.county) - expect(page).to have_content(location.type_of_unit) - expect(page).to have_content(location.type_of_building) - expect(page).to have_content(location.wheelchair_adaptation) - expect(page).to have_content(location.name) - end - end - - it "has page heading" do - expect(page).to have_content(scheme.service_name) - end - - it "has correct title" do - expected_title = CGI.escapeHTML("#{scheme.service_name} - Submit social housing lettings and sales data (CORE) - GOV.UK") - expect(page).to have_title(expected_title) - end - - context "when paginating over 20 results" do - let!(:locations) { FactoryBot.create_list(:location, 25, scheme:) } - - context "when on the first page" do - before do - get "/schemes/#{scheme.id}/locations" - end - - it "shows which schemes are being shown on the current page" do - expect(CGI.unescape_html(response.body)).to match("Showing 1 to 20 of #{locations.count} locations") - end - - it "has correct page 1 of 2 title" do - expected_title = CGI.escapeHTML("#{scheme.service_name} (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") - expect(page).to have_title(expected_title) - end - - it "has pagination links" do - expect(page).not_to have_content("Previous") - expect(page).not_to have_link("Previous") - expect(page).to have_content("Next") - expect(page).to have_link("Next") - end - end - - context "when on the second page" do - before do - get "/schemes/#{scheme.id}/locations?page=2" - end - - it "shows which schemes are being shown on the current page" do - expect(CGI.unescape_html(response.body)).to match("Showing 21 to 25 of #{locations.count} locations") - end - - it "has correct page 2 of 2 title" do - expected_title = CGI.escapeHTML("#{scheme.service_name} (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") - expect(page).to have_title(expected_title) - end - - it "has pagination links" do - expect(page).to have_content("Previous") - expect(page).to have_link("Previous") - expect(page).not_to have_content("Next") - expect(page).not_to have_link("Next") - end - end - end - end - - context "when signed in as a support user" do - let(:user) { FactoryBot.create(:user, :support) } - let!(:scheme) { FactoryBot.create(:scheme) } - let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) } - - before do - allow(user).to receive(:need_two_factor_authentication?).and_return(false) - sign_in user - get "/schemes/#{scheme.id}/locations" - end - - it "shows scheme" do - locations.each do |location| - expect(page).to have_content(location.location_code) - expect(page).to have_content(location.postcode) - expect(page).to have_content(location.county) - expect(page).to have_content(location.type_of_unit) - expect(page).to have_content(location.type_of_building) - expect(page).to have_content(location.wheelchair_adaptation) - expect(page).to have_content(location.name) - end - end - - it "has page heading" do - expect(page).to have_content(scheme.service_name) - end - - it "has correct title" do - expected_title = CGI.escapeHTML("#{scheme.service_name} - Submit social housing lettings and sales data (CORE) - GOV.UK") - expect(page).to have_title(expected_title) - end - - context "when paginating over 20 results" do - let!(:locations) { FactoryBot.create_list(:location, 25, scheme:) } - - context "when on the first page" do - before do - get "/schemes/#{scheme.id}/locations" - end - - it "shows which schemes are being shown on the current page" do - expect(CGI.unescape_html(response.body)).to match("Showing 1 to 20 of #{locations.count} locations") - end - - it "has correct page 1 of 2 title" do - expected_title = CGI.escapeHTML("#{scheme.service_name} (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") - expect(page).to have_title(expected_title) - end - - it "has pagination links" do - expect(page).not_to have_content("Previous") - expect(page).not_to have_link("Previous") - expect(page).to have_content("Next") - expect(page).to have_link("Next") - end - end - - context "when on the second page" do - before do - get "/schemes/#{scheme.id}/locations?page=2" - end - - it "shows which schemes are being shown on the current page" do - expect(CGI.unescape_html(response.body)).to match("Showing 21 to 25 of #{locations.count} locations") - end - - it "has correct page 1 of 2 title" do - expected_title = CGI.escapeHTML("#{scheme.service_name} (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") - expect(page).to have_title(expected_title) - end - - it "has pagination links" do - expect(page).to have_content("Previous") - expect(page).to have_link("Previous") - expect(page).not_to have_content("Next") - expect(page).not_to have_link("Next") - end - end - end - end - end - describe "#new" do context "when not signed in" do it "redirects to the sign in page" do @@ -536,7 +337,7 @@ RSpec.describe SchemesController, type: :request do context "when signed in as a data coordinator" do let(:user) { FactoryBot.create(:user, :data_coordinator) } - let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", total_units: "1" } } } + let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No" } } } before do sign_in user @@ -569,7 +370,7 @@ RSpec.describe SchemesController, type: :request do context "when signed in as a support user" do let(:organisation) { FactoryBot.create(:organisation) } let(:user) { FactoryBot.create(:user, :support) } - let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", total_units: "1", organisation_id: organisation.id } } } + let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", organisation_id: organisation.id } } } before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) @@ -600,7 +401,7 @@ RSpec.describe SchemesController, type: :request do end context "when required organisation id param is missing" do - let(:params) { { "scheme" => { "service_name" => "qweqwer", "sensitive" => "Yes", "organisation_id" => "", "scheme_type" => "Foyer", "registered_under_care_act" => "Yes – part registered as a care home", "total_units" => "1" } } } + let(:params) { { "scheme" => { "service_name" => "qweqwer", "sensitive" => "Yes", "organisation_id" => "", "scheme_type" => "Foyer", "registered_under_care_act" => "Yes – part registered as a care home" } } } it "displays the new page with an error message" do post "/schemes", params: params @@ -662,7 +463,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -707,7 +508,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -737,7 +538,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -750,10 +551,10 @@ RSpec.describe SchemesController, type: :request do context "when updating support" do let(:params) { { scheme: { intended_stay: "Medium stay", support_type: "Resettlement support", page: "support" } } } - it "renders confirm secondary group after successful update" do + it "renders add location to this scheme successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Add a location to this scheme") end it "updates a scheme with valid params" do @@ -768,7 +569,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -780,7 +581,7 @@ RSpec.describe SchemesController, type: :request do end context "when updating details" do - let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", total_units: "1", page: "details" } } } + let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", page: "details" } } } it "renders confirm secondary group after successful update" do follow_redirect! @@ -794,16 +595,15 @@ RSpec.describe SchemesController, type: :request do expect(scheme_to_update.reload.scheme_type).to eq("Foyer") expect(scheme_to_update.reload.sensitive).to eq("Yes") expect(scheme_to_update.reload.registered_under_care_act).to eq("No") - expect(scheme_to_update.reload.total_units).to eq(1) end context "when updating from check answers page" do - let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", total_units: "1", page: "details", check_answers: "true" } } } + let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", page: "details", check_answers: "true" } } } it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -812,7 +612,6 @@ RSpec.describe SchemesController, type: :request do expect(scheme_to_update.reload.scheme_type).to eq("Foyer") expect(scheme_to_update.reload.sensitive).to eq("Yes") expect(scheme_to_update.reload.registered_under_care_act).to eq("No") - expect(scheme_to_update.reload.total_units).to eq(1) end end end @@ -848,7 +647,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -893,7 +692,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -923,7 +722,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -939,7 +738,7 @@ RSpec.describe SchemesController, type: :request do it "renders confirm secondary group after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Add a location to this scheme") end it "updates a scheme with valid params" do @@ -954,7 +753,7 @@ RSpec.describe SchemesController, type: :request do it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -972,7 +771,6 @@ RSpec.describe SchemesController, type: :request do sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", - total_units: "1", page: "details", organisation_id: another_organisation.id, stock_owning_organisation_id: another_organisation.id } } @@ -990,18 +788,17 @@ RSpec.describe SchemesController, type: :request do expect(scheme_to_update.reload.scheme_type).to eq("Foyer") expect(scheme_to_update.reload.sensitive).to eq("Yes") expect(scheme_to_update.reload.registered_under_care_act).to eq("No") - expect(scheme_to_update.reload.total_units).to eq(1) expect(scheme_to_update.reload.organisation_id).to eq(another_organisation.id) expect(scheme_to_update.reload.stock_owning_organisation_id).to eq(another_organisation.id) end context "when updating from check answers page" do - let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", total_units: "1", page: "details", check_answers: "true" } } } + let(:params) { { scheme: { service_name: "testy", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", page: "details", check_answers: "true" } } } it "renders check answers page after successful update" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end it "updates a scheme with valid params" do @@ -1010,7 +807,6 @@ RSpec.describe SchemesController, type: :request do expect(scheme_to_update.reload.scheme_type).to eq("Foyer") expect(scheme_to_update.reload.sensitive).to eq("Yes") expect(scheme_to_update.reload.registered_under_care_act).to eq("No") - expect(scheme_to_update.reload.total_units).to eq(1) end end end @@ -1315,7 +1111,7 @@ RSpec.describe SchemesController, type: :request do it "returns a template for a support" do expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end context "when attempting to access check-answers scheme page for another organisation" do @@ -1342,7 +1138,7 @@ RSpec.describe SchemesController, type: :request do it "returns a template for a support" do expect(response).to have_http_status(:ok) - expect(page).to have_content("Check your changes before updating this scheme") + expect(page).to have_content("Check your changes before creating this scheme") end end end