You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.4 KiB
108 lines
3.4 KiB
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/location/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/location/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}/location/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 |
|
|
|
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}/location/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/location/create" |
|
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/location/create" |
|
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" } } } |
|
|
|
before do |
|
sign_in user |
|
post "/schemes/#{scheme.id}/location/create", params: params |
|
end |
|
|
|
it "creates a new location for scheme with valid params and redirects to correct page" do |
|
expect { post "/schemes/#{scheme.id}/location/create", 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.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
|
|
|