diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index f2920771e..b3d85bdc1 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -22,6 +22,7 @@ class LocationsController < ApplicationController def postcode render_not_found and return unless @location && @scheme + if params[:location].present? @location.postcode = PostcodeService.clean(params[:location][:postcode]) @location.location_admin_district = nil @@ -43,6 +44,7 @@ class LocationsController < ApplicationController def local_authority render_not_found and return unless @location && @scheme + if params[:location].present? @location.location_admin_district = params[:location][:location_admin_district] @location.location_code = Location.local_authorities.key(params[:location][:location_admin_district]) @@ -61,6 +63,7 @@ class LocationsController < ApplicationController def name render_not_found and return unless @location && @scheme + if params[:location].present? @location.name = params[:location][:name] if @location.valid?(:name) @@ -81,6 +84,7 @@ class LocationsController < ApplicationController def units render_not_found and return unless @location && @scheme + if params[:location].present? @location.units = params[:location][:units] if @location.valid?(:units) @@ -98,6 +102,7 @@ class LocationsController < ApplicationController def type_of_unit render_not_found and return unless @location && @scheme + if params[:location].present? @location.type_of_unit = params[:location][:type_of_unit] if @location.valid?(:type_of_unit) @@ -115,6 +120,7 @@ class LocationsController < ApplicationController def mobility_standards render_not_found and return unless @location && @scheme + if params[:location].present? @location.mobility_type = params[:location][:mobility_type] if @location.valid?(:mobility_type) @@ -132,6 +138,7 @@ class LocationsController < ApplicationController def availability render_not_found and return unless @location && @scheme + if params[:location].present? day = params[:location]["startdate(3i)"] month = params[:location]["startdate(2i)"] @@ -160,6 +167,7 @@ class LocationsController < ApplicationController def check_answers render_not_found and return unless @location && @scheme + if params[:location].present? @location.confirmed = true @location.save! diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb index cfe42716a..b205b699d 100644 --- a/app/views/locations/index.html.erb +++ b/app/views/locations/index.html.erb @@ -62,6 +62,6 @@ <% end %> <% end %> <% end %> -<%= govuk_button_link_to "Add a location", new_scheme_location_path(scheme_id: @scheme.id), secondary: true %> +<%= govuk_button_link_to "Add a location", new_scheme_location_path(@scheme), secondary: true %> <%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %> diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 6c28a5223..3f18854f8 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -41,13 +41,21 @@ RSpec.describe LocationsController, type: :request do get "/schemes/#{scheme.id}/locations/new" end - it "returns a template for a new location" do + it "creates a new location for scheme and redirects to correct page" do + expect { get "/schemes/#{scheme.id}/locations/new" }.to change(Location, :count).by(1) + end + + it "redirects to the postcode page" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Add a location to this scheme") + expect(page).to have_content("What is the postcode?") end - context "when trying to new location to a scheme that belongs to another organisation" do + it "creates a new location for scheme with the right owning organisation" do + expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) + end + + context "when trying to add a 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 @@ -58,16 +66,37 @@ RSpec.describe LocationsController, type: :request do end context "when signed in as a support user" do + + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } + 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 + it "creates a new location for scheme and redirects to correct page" do + expect { get "/schemes/#{scheme.id}/locations/new" }.to change(Location, :count).by(1) + end + + it "redirects to the postcode page" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("Add a location to this scheme") + expect(page).to have_content("What is the postcode?") + end + + it "creates a new location for scheme with the right owning organisation" do + expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) + end + + context "when trying to add a 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 end