Browse Source

CLDC-1337-permitted-user-can-add-location-when-creating-scheme (#704)

* added last step

* not finding postcode field

* testing being direct to add locations page after submitting support questions

* added route to locations controller

* location controller spec

* location controller

* added template for new

* next step in feature

* added postcode

* added name to locations

* removed total units from schemes

* moved total units to locations

* changed type of unit type in db

* using type of units

* add locations page is finished

* purged total units and added test for location create

* creating location

* testing creating location as data provider

* fixed factory

* refacotred update/create and added test creating location as a coord user

* testing returning back to create page if create another location is selected

* testing returning back to create page if create another location is selected - part 2

* thanks rubocop

* thanks rubocop

* testing back from new location

* returned tests back

* testing clickable locations link

* testing clickable locations link under correct context

* correctly looking table

* table with caption

* wip

* fixed failing exisiting tests

* fixed failing exisiting tests - part 2

* validating postcode case logs style

* validating postcode specs

* validating postcode specs debugger removed

* navigation when editing location

* spike creating new location after addition

* small refactoring

* added test to add another location from locations page

* added test to amend a location

* added test to amend a location - part 2

* testing location cell

* added wheelchair adaptions to the unit tyupe

* rebased

* testing postcode missing

* testing creating schem for a different org for coordinator

* testing new scheme for a different org for coordinator

* upcasing all postcodes before creation

* testing edge cases around postcodes and yes or no for another location

* create locations with support user

* details locations specs

* update locations specs

* rubocop

* checking raising error

* fixed failed test

* switched yes and no for wheelchairs

* routes refactoring

* fixed routing - WIP

* further chanegs

* feature tests passing

* correct page when errros

* redundant page

* moving viewing locations away from schemes controller

* new is fixed

* create fixed

* fixed locations specs

* fixed tab nav specs

* completed location specs

* lint
pull/711/head
J G 3 years ago committed by GitHub
parent
commit
5eac424384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 67
      app/controllers/locations_controller.rb
  2. 9
      app/controllers/schemes_controller.rb
  3. 5
      app/helpers/tab_nav_helper.rb
  4. 26
      app/models/location.rb
  5. 63
      app/views/locations/edit.html.erb
  6. 49
      app/views/locations/index.html.erb
  7. 63
      app/views/locations/new.html.erb
  8. 148
      app/views/schemes/check_answers.html.erb
  9. 46
      app/views/schemes/locations.html.erb
  10. 2
      config/routes.rb
  11. 5
      db/migrate/20220630114748_add_name_to_locations.rb
  12. 5
      db/migrate/20220630114917_remove_total_units_schemes.rb
  13. 6
      db/migrate/20220630120251_change_location_type_of_unit.rb
  14. 8
      db/schema.rb
  15. 9
      db/seeds.rb
  16. 2
      spec/factories/location.rb
  17. 1
      spec/factories/scheme.rb
  18. 317
      spec/features/schemes_spec.rb
  19. 12
      spec/helpers/tab_nav_helper_spec.rb
  20. 27
      spec/models/location_spec.rb
  21. 784
      spec/requests/locations_controller_spec.rb
  22. 246
      spec/requests/schemes_controller_spec.rb

67
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

9
app/controllers/schemes_controller.rb

@ -20,12 +20,6 @@ class SchemesController < ApplicationController
@scheme = Scheme.find_by(id: params[:id]) @scheme = Scheme.find_by(id: params[:id])
end end
def locations
@scheme = Scheme.find_by(id: params[:id])
@pagy, @locations = pagy(@scheme.locations)
@total_count = @scheme.locations.size
end
def new def new
@scheme = Scheme.new @scheme = Scheme.new
end end
@ -100,7 +94,7 @@ private
when "secondary-client-group" when "secondary-client-group"
scheme_support_path(@scheme) scheme_support_path(@scheme)
when "support" when "support"
scheme_check_answers_path(@scheme) new_location_path
when "details" when "details"
scheme_primary_client_group_path(@scheme) scheme_primary_client_group_path(@scheme)
end end
@ -113,7 +107,6 @@ private
:stock_owning_organisation_id, :stock_owning_organisation_id,
:scheme_type, :scheme_type,
:registered_under_care_act, :registered_under_care_act,
:total_units,
:id, :id,
:has_other_client_group, :has_other_client_group,
:primary_client_group, :primary_client_group,

5
app/helpers/tab_nav_helper.rb

@ -6,6 +6,11 @@ module TabNavHelper
[govuk_link_to(link_text, user), "<span class=\"govuk-visually-hidden\">User </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{user.email}</span>"].join("\n") [govuk_link_to(link_text, user), "<span class=\"govuk-visually-hidden\">User </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{user.email}</span>"].join("\n")
end end
def location_cell(location)
link_text = location.postcode
[govuk_link_to(link_text, "/schemes/#{location.scheme.id}/locations/#{location.id}/edit", method: :patch), "<span class=\"govuk-visually-hidden\">Location </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{location.name}</span>"].join("\n")
end
def scheme_cell(scheme) def scheme_cell(scheme)
link_text = scheme.service_name link_text = scheme.service_name
[govuk_link_to(link_text, scheme), "<span class=\"govuk-visually-hidden\">Scheme </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{scheme.primary_client_group}</span>"].join("\n") [govuk_link_to(link_text, scheme), "<span class=\"govuk-visually-hidden\">Scheme </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{scheme.primary_client_group}</span>"].join("\n")

26
app/models/location.rb

@ -1,13 +1,28 @@
class Location < ApplicationRecord class Location < ApplicationRecord
include Validations::PropertyValidations
validate :validate_postcode
belongs_to :scheme belongs_to :scheme
attr_accessor :add_another_location
WHEELCHAIR_ADAPTATIONS = { WHEELCHAIR_ADAPTATIONS = {
No: 0,
Yes: 1, Yes: 1,
No: 0,
}.freeze }.freeze
enum wheelchair_adaptation: WHEELCHAIR_ADAPTATIONS 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 def display_attributes
[ [
{ name: "Location code ", value: location_code, suffix: false }, { name: "Location code ", value: location_code, suffix: false },
@ -17,4 +32,13 @@ class Location < ApplicationRecord
{ name: "Wheelchair adaptation", value: wheelchair_adaptation, suffix: false }, { name: "Wheelchair adaptation", value: wheelchair_adaptation, suffix: false },
] ]
end 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 end

63
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| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= 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" %>
</div>
</div>
<% end %>

49
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)) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<%= govuk_table do |table| %>
<%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %>
<strong><%= @scheme.locations.count %></strong> <%= @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("<span>#{location.type_of_unit}</span>#{location.wheelchair_adaptation == 'Yes' ? "\n<span class=\"govuk-!-font-weight-regular app-!-colour-muted\">With wheelchair adaptations</span>" : ''}")) %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
</div>
<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %>

63
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| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= 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" %>
</div>
</div>
<% end %>

148
app/views/schemes/check_answers.html.erb

@ -1,65 +1,105 @@
<% content_for :title, "Check your answers before creating this scheme" %> <% 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| %> <% location_caption = @scheme.locations.count.eql?(1) ? "1 location" : "#{@scheme.locations.count} locations" %>
<% component.tab(label: "Scheme") do %>
<%= govuk_summary_list do |summary_list| %> <div class="govuk-grid-row">
<% @scheme.check_details_attributes.each do |attr| %> <div class="govuk-grid-column-three-quarters-from-desktop">
<% next if current_user.data_coordinator? && attr[:name] == ("Managed by") %> <%= govuk_tabs(title: "Check your answers before creating this scheme") do |component| %>
<%= summary_list.row do |row| %> <% component.tab(label: "Scheme") do %>
<% row.key { attr[:name].to_s } %> <%= govuk_summary_list do |summary_list| %>
<% row.value { details_html(attr) } %> <% @scheme.check_details_attributes.each do |attr| %>
<% row.action( <% next if current_user.data_coordinator? && attr[:name] == ("Managed by") %>
text: "Change", <%= summary_list.row do |row| %>
href: scheme_details_path(scheme_id: @scheme.id, check_answers: true), <% row.key { attr[:name].to_s } %>
) %> <% row.value { details_html(attr) } %>
<% end %> <% row.action(
<% end %> text: "Change",
<% @scheme.check_primary_client_attributes.each do |attr| %> href: scheme_details_path(scheme_id: @scheme.id, check_answers: true),
<%= summary_list.row do |row| %> ) %>
<% row.key { attr[:name].to_s } %> <% end %>
<% row.value { details_html(attr) } %> <% end %>
<% row.action( <% @scheme.check_primary_client_attributes.each do |attr| %>
text: "Change", <%= summary_list.row do |row| %>
href: scheme_primary_client_group_path(scheme_id: @scheme.id, check_answers: true), <% row.key { attr[:name].to_s } %>
) %> <% row.value { details_html(attr) } %>
<% end %> <% row.action(
<% end %> text: "Change",
<% @scheme.check_secondary_client_confirmation_attributes.each do |attr| %> href: scheme_primary_client_group_path(scheme_id: @scheme.id, check_answers: true),
<%= summary_list.row do |row| %> ) %>
<% row.key { attr[:name].to_s } %> <% end %>
<% row.value { details_html(attr) } %> <% end %>
<% row.action( <% @scheme.check_secondary_client_confirmation_attributes.each do |attr| %>
text: "Change", <%= summary_list.row do |row| %>
href: scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), <% row.key { attr[:name].to_s } %>
) %> <% row.value { details_html(attr) } %>
<% end %> <% row.action(
<% end %> text: "Change",
<% if @scheme.has_other_client_group == "Yes" %> href: scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true),
<% @scheme.check_secondary_client_attributes.each do |attr| %> ) %>
<%= summary_list.row do |row| %> <% end %>
<% row.key { attr[:name].to_s } %> <% end %>
<% row.value { details_html(attr) } %> <% if @scheme.has_other_client_group == "Yes" %>
<% row.action( <% @scheme.check_secondary_client_attributes.each do |attr| %>
text: "Change", <%= summary_list.row do |row| %>
href: scheme_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), <% 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 %> <% end %>
<% end %> <% end %>
<% @scheme.check_support_attributes.each do |attr| %> <% component.tab(label: "Locations") do %>
<%= summary_list.row do |row| %> <%= govuk_table do |table| %>
<% row.key { attr[:name].to_s } %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %>
<% row.value { details_html(attr) } %> <strong><%= @scheme.locations.count %></strong> <%= @scheme.locations.count.eql?(1) ? "location" : "locations" %>
<% row.action( <% end %>
text: "Change", <%= table.head do |head| %>
href: scheme_support_path(scheme_id: @scheme.id, check_answers: true), <%= 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("<span>#{location.type_of_unit}</span>#{location.wheelchair_adaptation == 'Yes' ? "\n<span class=\"govuk-!-font-weight-regular app-!-colour-muted\">With wheelchair adaptations</span>" : ''}")) %>
<% end %>
<% end %>
<% end %>
<% end %> <% end %>
<%= govuk_button_link_to "Add a location", new_location_path(id: @scheme.id), secondary: true %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> </div>
<% end %> </div>
<%= govuk_button_link_to "Create scheme", schemes_path(scheme_id: @scheme.id), html: { method: :get } %> <%= govuk_button_link_to "Create scheme", schemes_path(scheme_id: @scheme.id), html: { method: :get } %>

46
app/views/schemes/locations.html.erb

@ -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")) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<% @locations.each do |location| %>
<section class="x-govuk-summary-card govuk-!-margin-bottom-6">
<header class="x-govuk-summary-card__header">
<h2 class="x-govuk-summary-card__title">
<%= location.name %>
</h2>
</header>
<div class="x-govuk-summary-card__body">
<dl class="govuk-summary-list">
<% location.display_attributes.each do |attribute| %>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
<%= attribute[:name] %>
</dt>
<dd class="govuk-summary-list__value app-!-font-tabular">
<span class="govuk-!-margin-right-4"><%= attribute[:value] %></span>
<% if attribute[:suffix] %>
<span class="govuk-!-font-weight-regular app-!-colour-muted"><%= attribute[:suffix] %></span>
<% end %>
</dd>
</div>
<% end %>
</dl>
</div>
</section>
<% end %>
</div>
</div>
<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %>

2
config/routes.rb

@ -44,7 +44,7 @@ Rails.application.routes.draw do
get "check-answers", to: "schemes#check_answers" get "check-answers", to: "schemes#check_answers"
member do member do
get "locations", to: "schemes#locations" resources :locations
end end
end end

5
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

5
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

6
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

8
db/schema.rb

@ -191,9 +191,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do
t.integer "joint" t.integer "joint"
t.bigint "created_by_id" t.bigint "created_by_id"
t.integer "illness_type_0" t.integer "illness_type_0"
t.integer "retirement_value_check"
t.integer "tshortfall_known" t.integer "tshortfall_known"
t.integer "sheltered" t.integer "sheltered"
t.integer "retirement_value_check"
t.integer "pregnancy_value_check" t.integer "pregnancy_value_check"
t.integer "hhtype" t.integer "hhtype"
t.integer "new_old" 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| create_table "locations", force: :cascade do |t|
t.string "location_code" t.string "location_code"
t.string "postcode" t.string "postcode"
t.string "type_of_unit"
t.string "type_of_building" t.string "type_of_building"
t.integer "wheelchair_adaptation" t.integer "wheelchair_adaptation"
t.bigint "scheme_id", null: false 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.string "county"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_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" t.index ["scheme_id"], name: "index_locations_on_scheme_id"
end end
create_table "logs_exports", force: :cascade do |t| create_table "logs_exports", force: :cascade do |t|
t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } 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 "base_number", default: 1, null: false
t.integer "increment_number", default: 1, null: false t.integer "increment_number", default: 1, null: false
t.boolean "empty_export", default: false, 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 "primary_client_group"
t.string "secondary_client_group" t.string "secondary_client_group"
t.integer "sensitive" t.integer "sensitive"
t.integer "total_units"
t.integer "scheme_type" t.integer "scheme_type"
t.integer "registered_under_care_act" t.integer "registered_under_care_act"
t.integer "support_type" t.integer "support_type"

9
db/seeds.rb

@ -76,7 +76,6 @@ unless Rails.env.test?
registered_under_care_act: 0, registered_under_care_act: 0,
support_type: 1, support_type: 1,
scheme_type: 4, scheme_type: 4,
total_units: 5,
intended_stay: "M", intended_stay: "M",
primary_client_group: "O", primary_client_group: "O",
secondary_client_group: "H", secondary_client_group: "H",
@ -90,7 +89,6 @@ unless Rails.env.test?
registered_under_care_act: 1, registered_under_care_act: 1,
support_type: 1, support_type: 1,
scheme_type: 5, scheme_type: 5,
total_units: 2,
intended_stay: "S", intended_stay: "S",
primary_client_group: "D", primary_client_group: "D",
secondary_client_group: "E", secondary_client_group: "E",
@ -104,7 +102,6 @@ unless Rails.env.test?
registered_under_care_act: 1, registered_under_care_act: 1,
support_type: 4, support_type: 4,
scheme_type: 7, scheme_type: 7,
total_units: 7,
intended_stay: "X", intended_stay: "X",
primary_client_group: "G", primary_client_group: "G",
secondary_client_group: "R", secondary_client_group: "R",
@ -117,7 +114,7 @@ unless Rails.env.test?
location_code: "S254-CU193AA", location_code: "S254-CU193AA",
postcode: "CU19 3AA", postcode: "CU19 3AA",
name: "Rectory Road", name: "Rectory Road",
type_of_unit: "Self-contained flat or bedsit", type_of_unit: 4,
type_of_building: "Purpose-built", type_of_building: "Purpose-built",
county: "Mid Sussex", county: "Mid Sussex",
wheelchair_adaptation: 0, wheelchair_adaptation: 0,
@ -128,7 +125,7 @@ unless Rails.env.test?
location_code: "S254-DM250DC", location_code: "S254-DM250DC",
postcode: "DM25 0DC", postcode: "DM25 0DC",
name: "Smithy Lane", 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", type_of_building: "Converted from previous residential or non-residential property",
county: "Fife", county: "Fife",
wheelchair_adaptation: 1, wheelchair_adaptation: 1,
@ -139,7 +136,7 @@ unless Rails.env.test?
location_code: "S254-YX130WP", location_code: "S254-YX130WP",
postcode: "YX13 0WP", postcode: "YX13 0WP",
name: "Smithy Lane", 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", type_of_building: "Converted from previous residential or non-residential property",
county: "Rochford", county: "Rochford",
wheelchair_adaptation: 1, wheelchair_adaptation: 1,

2
spec/factories/location.rb

@ -3,7 +3,7 @@ FactoryBot.define do
location_code { Faker::Name.initials(number: 10) } location_code { Faker::Name.initials(number: 10) }
postcode { Faker::Address.postcode.delete(" ") } postcode { Faker::Address.postcode.delete(" ") }
name { Faker::Address.street_name } 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 } type_of_building { Faker::Lorem.word }
wheelchair_adaptation { 0 } wheelchair_adaptation { 0 }
county { Faker::Address.state } county { Faker::Address.state }

1
spec/factories/scheme.rb

@ -5,7 +5,6 @@ FactoryBot.define do
registered_under_care_act { Faker::Number.within(range: 0..1) } registered_under_care_act { Faker::Number.within(range: 0..1) }
support_type { Faker::Number.within(range: 0..6) } support_type { Faker::Number.within(range: 0..6) }
scheme_type { 0 } scheme_type { 0 }
total_units { Faker::Number.number(digits: 2) }
intended_stay { %w[M P S V X].sample } 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 } 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 } secondary_client_group { %w[O H M L A G F B D E I S N R Q P X].sample }

317
spec/features/schemes_spec.rb

@ -208,7 +208,7 @@ RSpec.describe "Schemes scheme Features" do
it "shows details of those locations" do it "shows details of those locations" do
locations.each do |location| locations.each do |location|
expect(page).to have_content(location.location_code) expect(page).to have_content(location.id)
end end
end end
end end
@ -375,173 +375,286 @@ RSpec.describe "Schemes scheme Features" do
click_button "Save and continue" click_button "Save and continue"
end end
it "lets me check my answers" do it "lets me add location" do
expect(page).to have_content "Check your changes before updating this scheme" expect(page).to have_content "Add a location to this scheme"
end end
context "when changing answers" do context "when I press the back button" do
it "displays change links" do before do
assert_selector "a", text: "Change", count: 12 click_link "Back"
end end
context "when changing details" do it "lets me select the secondary group" do
before do expect(page).to have_current_path("/schemes/#{scheme.id}/support")
click_link("Change", href: "/schemes/#{scheme.id}/details?check_answers=true", match: :first) expect(page).to have_content "What support does this scheme provide?"
end end
it "allows changing details questions" do
expect(page).to have_current_path("/schemes/#{scheme.id}/details?check_answers=true")
fill_in "Scheme name", with: "Example" context "when I amend support" do
choose "Direct access hostel" it "returns to the add location page" do
choose "Yes – registered care home providing nursing care"
click_button "Save and continue" click_button "Save and continue"
expect(page).to have_current_path("/schemes/#{scheme.id}/locations/new")
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 end
end
end
context "when I press the back button" do context "when I add location to the scheme" do
before do before do
click_link "Back" fill_in "Postcode", with: "SW1P 4DF"
end 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 it "lets me check my 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"
expect(page).to have_content "Check your changes before updating this scheme"
end
end
end end
context "when changing primary client group" do context "when I select to view locations" do
before do before do
click_link("Change", href: "/schemes/#{scheme.id}/primary-client-group?check_answers=true") click_link "Locations"
end end
it "allows changing primary-client-group question" do it "displays information about locations" do
expect(page).to have_current_path("/schemes/#{scheme.id}/primary-client-group?check_answers=true") 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" click_button "Save and continue"
end
expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") it "lets me check my answers" do
expect(page).to have_content "Older people with support needs" expect(page).to have_content "Check your changes before creating this scheme"
end end
context "when I press the back button" do context "when I select to view locations" do
before do before do
click_link "Back" click_link "Locations"
end end
it "lets me select the support answers" do it "displays information about another location" do
expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") expect(page).to have_content "Locations"
expect(page).to have_content "Check your changes before updating this scheme" 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 end
end end
context "when changing confirm secondary group answer" do context "when changing answers" do
before do it "displays change links" do
click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") assert_selector "a", text: "Change", count: 12
end end
it "allows changing confirm-secondary-client-group question to yes" do context "when changing details" do
expect(page).to have_current_path("/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") before do
click_link("Change", href: "/schemes/#{scheme.id}/details?check_answers=true", match: :first)
end
choose "Yes" it "allows changing details questions" do
click_button "Save and continue" 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" expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers")
click_button "Save and continue" 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") context "when I press the back button" do
expect(page).to have_content "People at risk of domestic violence" 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 "when I press the back button" do context "when changing primary client group" do
before do before do
click_link "Back" click_link("Change", href: "/schemes/#{scheme.id}/primary-client-group?check_answers=true")
end 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_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
end
context "when allows changing confirm-secondary-client-group question to no" do context "when I press the back button" do
before do before do
click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") 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
it "allows changing confirm-secondary-client-group question to no" do context "when changing confirm secondary group answer" do
expect(page).to have_current_path("/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true") before do
click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true")
end
choose "No" it "allows changing confirm-secondary-client-group question to yes" do
click_button "Save and continue" 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") choose "Yes"
expect(page).not_to have_content "Secondary client group" click_button "Save and continue"
end
end
context "when changing secondary-client-group question" do expect(page).to have_current_path("/schemes/#{scheme.id}/secondary-client-group?check_answers=true")
before do
click_link("Change", href: "/schemes/#{scheme.id}/secondary-client-group?check_answers=true")
end
it "allows changing secondary-client-group question" do choose "People at risk of domestic violence"
expect(page).to have_current_path("/schemes/#{scheme.id}/secondary-client-group?check_answers=true") click_button "Save and continue"
choose "People at risk of domestic violence" expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers")
click_button "Save and continue" 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") it "lets me select the support answers" do
expect(page).to have_content "People at risk of domestic violence" 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 "when I press the back button" do context "when allows changing confirm-secondary-client-group question to no" do
before do before do
click_link "Back" click_link("Change", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?check_answers=true")
end 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_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 end
end
context "when changing support questions" do context "when changing secondary-client-group question" do
before do before do
click_link("Change", href: "/schemes/#{scheme.id}/support?check_answers=true", match: :first) click_link("Change", href: "/schemes/#{scheme.id}/secondary-client-group?check_answers=true")
end end
it "allows changing support questions" do it "allows changing secondary-client-group question" do
expect(page).to have_current_path("/schemes/#{scheme.id}/support?check_answers=true") expect(page).to have_current_path("/schemes/#{scheme.id}/secondary-client-group?check_answers=true")
choose "Resettlement support" choose "People at risk of domestic violence"
choose "Medium stay" click_button "Save and continue"
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") context "when I press the back button" do
expect(page).to have_content "Resettlement support" before do
expect(page).to have_content "Medium stay" 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 "when I press the back button" do context "when changing support questions" do
before do before do
click_link "Back" click_link("Change", href: "/schemes/#{scheme.id}/support?check_answers=true", match: :first)
end end
it "lets me select the support answers" do it "allows changing support questions" do
expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") expect(page).to have_current_path("/schemes/#{scheme.id}/support?check_answers=true")
expect(page).to have_content "Check your changes before updating this scheme"
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 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 end
end end

12
spec/helpers/tab_nav_helper_spec.rb

@ -3,7 +3,8 @@ require "rails_helper"
RSpec.describe TabNavHelper do RSpec.describe TabNavHelper do
let(:organisation) { FactoryBot.create(:organisation) } let(:organisation) { FactoryBot.create(:organisation) }
let(:user) { FactoryBot.build(:user, 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 describe "#user_cell" do
it "returns user link and email separated by a newline character" do it "returns user link and email separated by a newline character" do
@ -19,9 +20,16 @@ RSpec.describe TabNavHelper do
end end
end end
describe "#location_cell" do
it "returns the location link to the postcode with optional name" do
expected_html = "<a class=\"govuk-link\" rel=\"nofollow\" data-method=\"patch\" href=\"/schemes/#{scheme.id}/locations/#{location.id}/edit\">#{location.postcode}</a>\n<span class=\"govuk-visually-hidden\">Location </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{location.name}</span>"
expect(location_cell(location)).to match(expected_html)
end
end
describe "#scheme_cell" do describe "#scheme_cell" do
it "returns the scheme link service name and primary user group separated by a newline character" do it "returns the scheme link service name and primary user group separated by a newline character" do
expected_html = "<a class=\"govuk-link\" href=\"/schemes\">#{scheme.service_name}</a>\n<span class=\"govuk-visually-hidden\">Scheme </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{scheme.primary_client_group}</span>" expected_html = "<a class=\"govuk-link\" href=\"/schemes/#{scheme.id}\">#{scheme.service_name}</a>\n<span class=\"govuk-visually-hidden\">Scheme </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{scheme.primary_client_group}</span>"
expect(scheme_cell(scheme)).to match(expected_html) expect(scheme_cell(scheme)).to match(expected_html)
end end
end end

27
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

784
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 <b>1</b> to <b>20</b> of <b>#{locations.count}</b> 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 <b>21</b> to <b>25</b> of <b>#{locations.count}</b> 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 <b>1</b> to <b>20</b> of <b>#{locations.count}</b> 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 <b>21</b> to <b>25</b> of <b>#{locations.count}</b> 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

246
spec/requests/schemes_controller_spec.rb

@ -261,205 +261,6 @@ RSpec.describe SchemesController, type: :request do
end end
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 <b>1</b> to <b>20</b> of <b>#{locations.count}</b> 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 <b>21</b> to <b>25</b> of <b>#{locations.count}</b> 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 <b>1</b> to <b>20</b> of <b>#{locations.count}</b> 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 <b>21</b> to <b>25</b> of <b>#{locations.count}</b> 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 describe "#new" do
context "when not signed in" do context "when not signed in" do
it "redirects to the sign in page" 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 context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) } 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 before do
sign_in user sign_in user
@ -569,7 +370,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a support user" do context "when signed in as a support user" do
let(:organisation) { FactoryBot.create(:organisation) } let(:organisation) { FactoryBot.create(:organisation) }
let(:user) { FactoryBot.create(:user, :support) } 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 before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
@ -600,7 +401,7 @@ RSpec.describe SchemesController, type: :request do
end end
context "when required organisation id param is missing" do 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 it "displays the new page with an error message" do
post "/schemes", params: params post "/schemes", params: params
@ -662,7 +463,7 @@ RSpec.describe SchemesController, type: :request do
it "renders check answers page after successful update" do it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do it "updates a scheme with valid params" do
@ -750,10 +551,10 @@ RSpec.describe SchemesController, type: :request do
context "when updating support" do context "when updating support" do
let(:params) { { scheme: { intended_stay: "Medium stay", support_type: "Resettlement support", page: "support" } } } 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! follow_redirect!
expect(response).to have_http_status(:ok) 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 end
it "updates a scheme with valid params" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do it "updates a scheme with valid params" do
@ -780,7 +581,7 @@ RSpec.describe SchemesController, type: :request do
end end
context "when updating details" do 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 it "renders confirm secondary group after successful update" do
follow_redirect! 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.scheme_type).to eq("Foyer")
expect(scheme_to_update.reload.sensitive).to eq("Yes") 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.registered_under_care_act).to eq("No")
expect(scheme_to_update.reload.total_units).to eq(1)
end end
context "when updating from check answers page" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do 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.scheme_type).to eq("Foyer")
expect(scheme_to_update.reload.sensitive).to eq("Yes") 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.registered_under_care_act).to eq("No")
expect(scheme_to_update.reload.total_units).to eq(1)
end end
end end
end end
@ -848,7 +647,7 @@ RSpec.describe SchemesController, type: :request do
it "renders check answers page after successful update" do it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do 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 it "renders confirm secondary group after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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 end
it "updates a scheme with valid params" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do it "updates a scheme with valid params" do
@ -972,7 +771,6 @@ RSpec.describe SchemesController, type: :request do
sensitive: "1", sensitive: "1",
scheme_type: "Foyer", scheme_type: "Foyer",
registered_under_care_act: "No", registered_under_care_act: "No",
total_units: "1",
page: "details", page: "details",
organisation_id: another_organisation.id, organisation_id: another_organisation.id,
stock_owning_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.scheme_type).to eq("Foyer")
expect(scheme_to_update.reload.sensitive).to eq("Yes") 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.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.organisation_id).to eq(another_organisation.id)
expect(scheme_to_update.reload.stock_owning_organisation_id).to eq(another_organisation.id) expect(scheme_to_update.reload.stock_owning_organisation_id).to eq(another_organisation.id)
end end
context "when updating from check answers page" do 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 it "renders check answers page after successful update" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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
it "updates a scheme with valid params" do 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.scheme_type).to eq("Foyer")
expect(scheme_to_update.reload.sensitive).to eq("Yes") 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.registered_under_care_act).to eq("No")
expect(scheme_to_update.reload.total_units).to eq(1)
end end
end end
end end
@ -1315,7 +1111,7 @@ RSpec.describe SchemesController, type: :request do
it "returns a template for a support" do it "returns a template for a support" do
expect(response).to have_http_status(:ok) 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
context "when attempting to access check-answers scheme page for another organisation" do 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 it "returns a template for a support" do
expect(response).to have_http_status(:ok) 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 end
end end

Loading…
Cancel
Save