From 0a8a2f312f3b5101fa2d647c354ec8a0b00dc3ac Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 5 Jul 2022 10:50:08 +0100 Subject: [PATCH] details locations specs --- spec/factories/location.rb | 2 - spec/requests/locations_controller_spec.rb | 65 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/spec/factories/location.rb b/spec/factories/location.rb index 7696ec4a6..53f8d2cd0 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -3,8 +3,6 @@ FactoryBot.define do location_code { Faker::Name.initials(number: 10) } postcode { Faker::Address.postcode.delete(" ") } name { Faker::Address.street_name } - address_line1 { Faker::Address.street_name } - address_line2 { Faker::Address.city } type_of_unit { Faker::Number.within(range: 1..6) } type_of_building { Faker::Lorem.word } wheelchair_adaptation { 0 } diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 3cec722a6..4115fdfb6 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -298,4 +298,69 @@ RSpec.describe LocationsController, type: :request do end end end + + describe "#details" do + context "when not signed in" do + it "redirects to the sign in page" do + get "/schemes/1/location" + 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/location" + 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: scheme) } + + before do + sign_in user + get "/schemes/#{location.id}/location?scheme_id=#{scheme.id}" + 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/#{scheme.id}/location" + 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: scheme) } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + get "/schemes/#{location.id}/location?scheme_id=#{scheme.id}" + 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 end