require "rails_helper" RSpec.describe FormController, type: :request do let(:page) { Capybara::Node::Simple.new(response.body) } let(:user) { FactoryBot.create(:user) } let(:organisation) { user.organisation } let(:other_organisation) { FactoryBot.create(:organisation) } let!(:lettings_log) do FactoryBot.create( :lettings_log, owning_organisation: organisation, managing_organisation: organisation, ) end let!(:unauthorized_lettings_log) do FactoryBot.create( :lettings_log, owning_organisation: other_organisation, managing_organisation: other_organisation, ) end let(:setup_complete_lettings_log) do FactoryBot.create( :lettings_log, :about_completed, status: 1, startdate: Time.zone.local(2021, 10, 10), owning_organisation: organisation, managing_organisation: organisation, ) end let(:completed_lettings_log) do FactoryBot.create( :lettings_log, :completed, owning_organisation: organisation, managing_organisation: organisation, ) end let(:headers) { { "Accept" => "text/html" } } context "when a user is not signed in" do describe "GET" do it "does not let you get lettings logs pages you don't have access to" do get "/logs/#{lettings_log.id}/person-1-age", headers: headers, params: {} expect(response).to redirect_to("/account/sign-in") end it "does not let you get lettings log check answer pages you don't have access to" do get "/logs/#{lettings_log.id}/household-characteristics/check-answers", headers: headers, params: {} expect(response).to redirect_to("/account/sign-in") end end describe "POST" do it "does not let you post form answers to lettings logs you don't have access to" do post "/logs/#{lettings_log.id}/form", params: {} expect(response).to redirect_to("/account/sign-in") end end end context "when a user is signed in" do before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) sign_in user end describe "GET" do context "with form pages" do context "when forms exist for multiple years" do let(:lettings_log_year_1) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2021, 5, 1), owning_organisation: organisation, created_by: user) } let(:lettings_log_year_2) { FactoryBot.create(:lettings_log, :about_completed, startdate: Time.zone.local(2022, 5, 1), owning_organisation: organisation, created_by: user) } it "displays the correct question details for each lettings log based on form year" do get "/logs/#{lettings_log_year_1.id}/tenant-code-test", headers: headers, params: {} expect(response.body).to include("What is the tenant code?") get "/logs/#{lettings_log_year_2.id}/tenant-code-test", headers: headers, params: {} expect(response.body).to match("Different question header text for this year - 2023") end end context "when lettings logs are not owned or managed by your organisation" do it "does not show form pages for lettings logs you don't have access to" do get "/logs/#{unauthorized_lettings_log.id}/person-1-age", headers: headers, params: {} expect(response).to have_http_status(:not_found) end end context "with a form page that has custom guidance" do it "displays the correct partial" do get "/logs/#{lettings_log.id}/net-income", headers: headers, params: {} expect(response.body).to match("What counts as income?") end end context "when viewing the setup section schemes page" do context "when the user is support" do let(:user) { FactoryBot.create(:user, :support) } context "when organisation and user have not been selected yet" do let(:lettings_log) do FactoryBot.create( :lettings_log, owning_organisation: nil, managing_organisation: nil, created_by: nil, needstype: 2, ) end before do locations = FactoryBot.create_list(:location, 5) locations.each { |location| location.scheme.update!(arrangement_type: "The same organisation that owns the housing stock", managing_organisation_id: location.scheme.owning_organisation_id) } end it "returns an unfiltered list of schemes" do get "/logs/#{lettings_log.id}/scheme", headers: headers, params: {} expect(response.body.scan("