Browse Source
* 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 * lintpull/711/head
J G
3 years ago
committed by
GitHub
22 changed files with 1449 additions and 451 deletions
@ -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 |
@ -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 %> |
@ -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" } %> |
@ -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 %> |
@ -1,65 +1,105 @@
|
||||
<% content_for :title, "Check your answers before creating this scheme" %> |
||||
|
||||
<%= render partial: "organisations/headings", locals: { main: "Check your changes before updating this scheme", sub: @scheme.service_name } %> |
||||
<%= render partial: "organisations/headings", locals: { main: "Check your changes before creating this scheme", sub: @scheme.service_name } %> |
||||
|
||||
<%= govuk_tabs(title: "Check your answers before creating this scheme") do |component| %> |
||||
<% component.tab(label: "Scheme") do %> |
||||
<%= govuk_summary_list do |summary_list| %> |
||||
<% @scheme.check_details_attributes.each do |attr| %> |
||||
<% next if current_user.data_coordinator? && attr[:name] == ("Managed by") %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_details_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% @scheme.check_primary_client_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_primary_client_group_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% @scheme.check_secondary_client_confirmation_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% if @scheme.has_other_client_group == "Yes" %> |
||||
<% @scheme.check_secondary_client_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% location_caption = @scheme.locations.count.eql?(1) ? "1 location" : "#{@scheme.locations.count} locations" %> |
||||
|
||||
<div class="govuk-grid-row"> |
||||
<div class="govuk-grid-column-three-quarters-from-desktop"> |
||||
<%= govuk_tabs(title: "Check your answers before creating this scheme") do |component| %> |
||||
<% component.tab(label: "Scheme") do %> |
||||
<%= govuk_summary_list do |summary_list| %> |
||||
<% @scheme.check_details_attributes.each do |attr| %> |
||||
<% next if current_user.data_coordinator? && attr[:name] == ("Managed by") %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_details_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% @scheme.check_primary_client_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_primary_client_group_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% @scheme.check_secondary_client_confirmation_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% if @scheme.has_other_client_group == "Yes" %> |
||||
<% @scheme.check_secondary_client_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% end %> |
||||
<% @scheme.check_support_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_support_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% end %> |
||||
<% end %> |
||||
<% end %> |
||||
<% end %> |
||||
<% @scheme.check_support_attributes.each do |attr| %> |
||||
<%= summary_list.row do |row| %> |
||||
<% row.key { attr[:name].to_s } %> |
||||
<% row.value { details_html(attr) } %> |
||||
<% row.action( |
||||
text: "Change", |
||||
href: scheme_support_path(scheme_id: @scheme.id, check_answers: true), |
||||
) %> |
||||
<% component.tab(label: "Locations") do %> |
||||
<%= govuk_table do |table| %> |
||||
<%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> |
||||
<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 %> |
||||
<% @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 %> |
||||
<%= govuk_button_link_to "Add a location", new_location_path(id: @scheme.id), secondary: true %> |
||||
<% end %> |
||||
<% end %> |
||||
<% end %> |
||||
<% end %> |
||||
</div> |
||||
</div> |
||||
|
||||
<%= govuk_button_link_to "Create scheme", schemes_path(scheme_id: @scheme.id), html: { method: :get } %> |
||||
|
@ -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" } %> |
@ -0,0 +1,5 @@
|
||||
class AddNameToLocations < ActiveRecord::Migration[7.0] |
||||
change_table :locations, bulk: true do |t| |
||||
t.integer :total_units |
||||
end |
||||
end |
@ -0,0 +1,5 @@
|
||||
class RemoveTotalUnitsSchemes < ActiveRecord::Migration[7.0] |
||||
def change |
||||
remove_column :schemes, :total_units, :integer |
||||
end |
||||
end |
@ -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 |
@ -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 |
@ -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 |
Loading…
Reference in new issue