Browse Source

test: add new #new tests

pull/1034/head
natdeanlewissoftwire 4 years ago
parent
commit
48acfbcc7b
  1. 8
      app/controllers/locations_controller.rb
  2. 2
      app/views/locations/index.html.erb
  3. 39
      spec/requests/locations_controller_spec.rb

8
app/controllers/locations_controller.rb

@ -22,6 +22,7 @@ class LocationsController < ApplicationController
def postcode def postcode
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
@location.postcode = PostcodeService.clean(params[:location][:postcode]) @location.postcode = PostcodeService.clean(params[:location][:postcode])
@location.location_admin_district = nil @location.location_admin_district = nil
@ -43,6 +44,7 @@ class LocationsController < ApplicationController
def local_authority def local_authority
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
@location.location_admin_district = params[:location][:location_admin_district] @location.location_admin_district = params[:location][:location_admin_district]
@location.location_code = Location.local_authorities.key(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 def name
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
@location.name = params[:location][:name] @location.name = params[:location][:name]
if @location.valid?(:name) if @location.valid?(:name)
@ -81,6 +84,7 @@ class LocationsController < ApplicationController
def units def units
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
@location.units = params[:location][:units] @location.units = params[:location][:units]
if @location.valid?(:units) if @location.valid?(:units)
@ -98,6 +102,7 @@ class LocationsController < ApplicationController
def type_of_unit def type_of_unit
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
@location.type_of_unit = params[:location][:type_of_unit] @location.type_of_unit = params[:location][:type_of_unit]
if @location.valid?(:type_of_unit) if @location.valid?(:type_of_unit)
@ -115,6 +120,7 @@ class LocationsController < ApplicationController
def mobility_standards def mobility_standards
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
@location.mobility_type = params[:location][:mobility_type] @location.mobility_type = params[:location][:mobility_type]
if @location.valid?(:mobility_type) if @location.valid?(:mobility_type)
@ -132,6 +138,7 @@ class LocationsController < ApplicationController
def availability def availability
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
day = params[:location]["startdate(3i)"] day = params[:location]["startdate(3i)"]
month = params[:location]["startdate(2i)"] month = params[:location]["startdate(2i)"]
@ -160,6 +167,7 @@ class LocationsController < ApplicationController
def check_answers def check_answers
render_not_found and return unless @location && @scheme render_not_found and return unless @location && @scheme
if params[:location].present? if params[:location].present?
@location.confirmed = true @location.confirmed = true
@location.save! @location.save!

2
app/views/locations/index.html.erb

@ -62,6 +62,6 @@
<% end %> <% end %>
<% end %> <% 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" } %> <%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %>

39
spec/requests/locations_controller_spec.rb

@ -41,13 +41,21 @@ RSpec.describe LocationsController, type: :request do
get "/schemes/#{scheme.id}/locations/new" get "/schemes/#{scheme.id}/locations/new"
end 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! follow_redirect!
expect(response).to have_http_status(:ok) 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 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) } let(:another_scheme) { FactoryBot.create(:scheme) }
it "displays the new page with an error message" do it "displays the new page with an error message" do
@ -58,16 +66,37 @@ RSpec.describe LocationsController, type: :request do
end end
context "when signed in as a support user" do 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 before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user sign_in user
get "/schemes/#{scheme.id}/locations/new" get "/schemes/#{scheme.id}/locations/new"
end 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! follow_redirect!
expect(response).to have_http_status(:ok) 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 end
end end

Loading…
Cancel
Save