require "rails_helper" RSpec.describe CaseLogsController, type: :request do let(:owning_organisation) { FactoryBot.create(:organisation) } let(:managing_organisation) { owning_organisation } let(:user) { FactoryBot.create(:user) } let(:api_username) { "test_user" } let(:api_password) { "test_password" } let(:basic_credentials) do ActionController::HttpAuthentication::Basic .encode_credentials(api_username, api_password) end let(:headers) do { "Content-Type" => "application/json", "Accept" => "application/json", "Authorization" => basic_credentials, } end before do allow(ENV).to receive(:[]) allow(ENV).to receive(:[]).with("API_USER").and_return(api_username) allow(ENV).to receive(:[]).with("API_KEY").and_return(api_password) end describe "POST #create" do let(:tenant_code) { "T365" } let(:age1) { 35 } let(:offered) { 12 } let(:period) { 2 } let(:postcode_full) { "SE116TY" } let(:in_progress) { "in_progress" } let(:completed) { "completed" } context "when API" do let(:params) do { "owning_organisation_id": owning_organisation.id, "managing_organisation_id": managing_organisation.id, "created_by_id": user.id, "tenant_code": tenant_code, "age1": age1, "postcode_full": postcode_full, "offered": offered, "period": period, } end before do post "/logs", headers:, params: params.to_json end it "returns http success" do expect(response).to have_http_status(:success) end it "returns a serialized Case Log" do json_response = JSON.parse(response.body) expect(json_response.keys).to match_array(CaseLog.new.attributes.keys) end it "creates a case log with the values passed" do json_response = JSON.parse(response.body) expect(json_response["tenant_code"]).to eq(tenant_code) expect(json_response["age1"]).to eq(age1) expect(json_response["postcode_full"]).to eq(postcode_full) end context "with invalid json parameters" do let(:age1) { 2000 } let(:offered) { 21 } it "validates case log parameters" do json_response = JSON.parse(response.body) expect(response).to have_http_status(:unprocessable_entity) expect(json_response["errors"]).to match_array([["offered", [I18n.t("validations.property.offered.relet_number")]], ["age1", [I18n.t("validations.numeric.valid", field: "Lead tenant’s age", min: 16, max: 120)]]]) end end context "with a partial case log submission" do it "marks the record as in_progress" do json_response = JSON.parse(response.body) expect(json_response["status"]).to eq(in_progress) end end context "with a complete case log submission" do let(:org_params) do { "case_log" => { "owning_organisation_id" => owning_organisation.id, "managing_organisation_id" => managing_organisation.id, "created_by_id" => user.id, }, } end let(:case_log_params) { JSON.parse(File.open("spec/fixtures/complete_case_log.json").read) } let(:params) do case_log_params.merge(org_params) { |_k, a_val, b_val| a_val.merge(b_val) } end it "marks the record as completed" do json_response = JSON.parse(response.body) expect(json_response).not_to have_key("errors") expect(json_response["status"]).to eq(completed) end end context "with a request containing invalid credentials" do let(:basic_credentials) do ActionController::HttpAuthentication::Basic.encode_credentials(api_username, "Oops") end it "returns 401" do expect(response).to have_http_status(:unauthorized) end end end context "when UI" do let(:organisation) { FactoryBot.create(:organisation) } let(:user) { FactoryBot.create(:user) } let(:support_user) { FactoryBot.create(:user, :support) } let(:headers) { { "Accept" => "text/html" } } let(:params) { { "organisation_id" => organisation.id } } context("and created by a user from the same organisation") do before do RequestHelper.stub_http_requests sign_in user post "/logs", headers: end it "tracks who created the record" do created_id = response.location.match(/[0-9]+/)[0] case_log = CaseLog.find_by(id: created_id) whodunnit_actor = case_log.versions.last.actor expect(whodunnit_actor).to be_a(User) expect(whodunnit_actor.id).to eq(user.id) expect(case_log.created_by_id).to eq(user.id) end end context("and created by a support user") do let(:created_id) do response.location.match(/[0-9]+/)[0] end before do RequestHelper.stub_http_requests allow(support_user).to receive(:need_two_factor_authentication?).and_return(false) sign_in support_user end context "when organisaition params are provided" do before do post "/logs", headers:, params: params end it "tracks who created the record" do whodunnit_actor = CaseLog.find_by(id: created_id).versions.last.actor expect(whodunnit_actor).to be_a(User) expect(whodunnit_actor.id).to eq(support_user.id) end it "creates the record for the correct organisation" do case_log = CaseLog.find_by(id: created_id) expect(case_log.owning_organisation_id).to eq(organisation.id) expect(case_log.managing_organisation_id).to eq(organisation.id) end end context "with no organisation params" do before do post "/logs", headers: end it "created the record with the support user's organisation" do case_log = CaseLog.find_by(id: created_id) expect(case_log.owning_organisation_id).to eq(support_user.organisation.id) expect(case_log.managing_organisation_id).to eq(support_user.organisation.id) end end end end end describe "GET" 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!(:case_log) do FactoryBot.create( :case_log, owning_organisation: organisation, managing_organisation: organisation, tenant_code: "LC783", ) end let!(:unauthorized_case_log) do FactoryBot.create( :case_log, owning_organisation: other_organisation, managing_organisation: other_organisation, tenant_code: "UA984", ) end context "when displaying a collection of logs" do let(:headers) { { "Accept" => "text/html" } } context "when the user is a customer support user" do let(:user) { FactoryBot.create(:user, :support) } before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) sign_in user end it "does have organisation columns" do get "/logs", headers: headers, params: {} expect(page).to have_content("Owning organisation") expect(page).to have_content("Managing organisation") end it "shows case logs for all organisations" do get "/logs", headers: headers, params: {} expect(page).to have_content("LC783") expect(page).to have_content("UA984") end context "when there are no logs in the database" do before do CaseLog.destroy_all end it "page has correct title" do get "/logs", headers: headers, params: {} expect(page).to have_title("Your organisation (Logs) - Submit social housing lettings and sales data (CORE) - GOV.UK") end end context "when filtering" do context "with status filter" do let(:organisation_2) { FactoryBot.create(:organisation) } let!(:in_progress_case_log) do FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation, managing_organisation: organisation) end let!(:completed_case_log) do FactoryBot.create(:case_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation) end it "shows case logs for multiple selected statuses" do get "/logs?status[]=in_progress&status[]=completed", headers: headers, params: {} expect(page).to have_link(in_progress_case_log.id.to_s) expect(page).to have_link(completed_case_log.id.to_s) end it "shows case logs for one selected status" do get "/logs?status[]=in_progress", headers: headers, params: {} expect(page).to have_link(in_progress_case_log.id.to_s) expect(page).not_to have_link(completed_case_log.id.to_s) end it "filters on organisation" do get "/logs?organisation[]=#{organisation_2.id}", headers: headers, params: {} expect(page).to have_link(completed_case_log.id.to_s) expect(page).not_to have_link(in_progress_case_log.id.to_s) end it "does not reset the filters" do get "/logs?status[]=in_progress", headers: headers, params: {} expect(page).to have_link(in_progress_case_log.id.to_s) expect(page).not_to have_link(completed_case_log.id.to_s) get "/logs", headers: headers, params: {} expect(page).to have_link(in_progress_case_log.id.to_s) expect(page).not_to have_link(completed_case_log.id.to_s) end end context "with year filter" do let!(:case_log_2021) do FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation, startdate: Time.zone.local(2022, 3, 1), managing_organisation: organisation) end let!(:case_log_2022) do FactoryBot.create(:case_log, :completed, owning_organisation: organisation, mrcdate: Time.zone.local(2022, 2, 1), startdate: Time.zone.local(2022, 12, 1), tenancy: 6, managing_organisation: organisation) end it "shows case logs for multiple selected years" do get "/logs?years[]=2021&years[]=2022", headers: headers, params: {} expect(page).to have_link(case_log_2021.id.to_s) expect(page).to have_link(case_log_2022.id.to_s) end it "shows case logs for one selected year" do get "/logs?years[]=2021", headers: headers, params: {} expect(page).to have_link(case_log_2021.id.to_s) expect(page).not_to have_link(case_log_2022.id.to_s) end end context "with year and status filter" do let!(:case_log_2021) do FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation, startdate: Time.zone.local(2022, 3, 1), managing_organisation: organisation) end let!(:case_log_2022) do FactoryBot.create(:case_log, :completed, owning_organisation: organisation, mrcdate: Time.zone.local(2022, 2, 1), startdate: Time.zone.local(2022, 12, 1), tenancy: 6, managing_organisation: organisation) end let!(:case_log_2022_in_progress) do FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation, mrcdate: Time.zone.local(2022, 2, 1), startdate: Time.zone.local(2022, 12, 1), tenancy: 6, managing_organisation: organisation) end it "shows case logs for multiple selected statuses and years" do get "/logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed", headers: headers, params: {} expect(page).to have_link(case_log_2021.id.to_s) expect(page).to have_link(case_log_2022.id.to_s) expect(page).to have_link(case_log_2022_in_progress.id.to_s) end it "shows case logs for one selected status" do get "/logs?years[]=2022&status[]=in_progress", headers: headers, params: {} expect(page).to have_link(case_log_2022_in_progress.id.to_s) expect(page).not_to have_link(case_log_2021.id.to_s) expect(page).not_to have_link(case_log_2022.id.to_s) end end end end context "when the user is not a customer support user" do before do sign_in user end it "does not have organisation columns" do get "/logs", headers: headers, params: {} expect(page).not_to have_content("Owning organisation") expect(page).not_to have_content("Managing organisation") end context "when using a search query" do let(:logs) { FactoryBot.create_list(:case_log, 3, :completed, owning_organisation: user.organisation) } let(:log_to_search) { FactoryBot.create(:case_log, :completed, owning_organisation: user.organisation) } let(:log_total_count) { CaseLog.where(owning_organisation: user.organisation).count } it "has search results in the title" do get "/logs?search=#{log_to_search.id}", headers: headers, params: {} expect(page).to have_title("Your organisation (1 log matching ‘#{log_to_search.id}’ of #{log_total_count} total logs) - Submit social housing lettings and sales data (CORE) - GOV.UK") end it "shows case logs matching the id" do get "/logs?search=#{log_to_search.id}", headers: headers, params: {} expect(page).to have_link(log_to_search.id.to_s) logs.each do |log| expect(page).not_to have_link(log.id.to_s) end end it "shows case logs matching the tenant code" do get "/logs?search=#{log_to_search.tenant_code}", headers: headers, params: {} expect(page).to have_link(log_to_search.id.to_s) logs.each do |log| expect(page).not_to have_link(log.id.to_s) end end it "shows case logs matching the property reference" do get "/logs?search=#{log_to_search.propcode}", headers: headers, params: {} expect(page).to have_link(log_to_search.id.to_s) logs.each do |log| expect(page).not_to have_link(log.id.to_s) end end it "shows case logs matching the property postcode" do get "/logs?search=#{log_to_search.postcode_full}", headers: headers, params: {} expect(page).to have_link(log_to_search.id.to_s) logs.each do |log| expect(page).not_to have_link(log.id.to_s) end end context "when more than one results with matching postcode" do let!(:matching_postcode_log) { FactoryBot.create(:case_log, :completed, owning_organisation: user.organisation, postcode_full: log_to_search.postcode_full) } it "displays all matching logs" do get "/logs?search=#{log_to_search.postcode_full}", headers: headers, params: {} expect(page).to have_link(log_to_search.id.to_s) expect(page).to have_link(matching_postcode_log.id.to_s) logs.each do |log| expect(page).not_to have_link(log.id.to_s) end end end context "when there are more than 1 page of search results" do let(:postcode) { "XX11YY" } let(:logs) { FactoryBot.create_list(:case_log, 30, :completed, owning_organisation: user.organisation, postcode_full: postcode) } let(:log_total_count) { CaseLog.where(owning_organisation: user.organisation).count } it "has title with pagination details for page 1" do get "/logs?search=#{logs[0].postcode_full}", headers: headers, params: {} expect(page).to have_title("Your organisation (#{logs.count} logs matching ‘#{postcode}’ of #{log_total_count} total logs) (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") end it "has title with pagination details for page 2" do get "/logs?search=#{logs[0].postcode_full}&page=2", headers: headers, params: {} expect(page).to have_title("Your organisation (#{logs.count} logs matching ‘#{postcode}’ of #{log_total_count} total logs) (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") end end context "when search query doesn't match any logs" do it "doesn't display any logs" do get "/logs?search=foobar", headers:, params: {} logs.each do |log| expect(page).not_to have_link(log.id.to_s) end expect(page).not_to have_link(log_to_search.id.to_s) end end context "when search query is empty" do it "doesn't display any logs" do get "/logs?search=", headers:, params: {} logs.each do |log| expect(page).not_to have_link(log.id.to_s) end expect(page).not_to have_link(log_to_search.id.to_s) end end context "when search and filter is present" do let(:matching_postcode) { log_to_search.postcode_full } let(:matching_status) { "in_progress" } let!(:log_matching_filter_and_search) { FactoryBot.create(:case_log, :in_progress, owning_organisation: user.organisation, postcode_full: matching_postcode) } it "shows only logs matching both search and filters" do get "/logs?search=#{matching_postcode}&status[]=#{matching_status}", headers: headers, params: {} expect(page).to have_link(log_matching_filter_and_search.id.to_s) expect(page).not_to have_link(log_to_search.id.to_s) logs.each do |log| expect(page).not_to have_link(log.id.to_s) end end end end context "when there are less than 20 logs" do before do get "/logs", headers:, params: {} end it "shows a table of logs" do expect(CGI.unescape_html(response.body)).to match(/