diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 31983cd2e..3fa10b351 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -22,11 +22,14 @@ class CaseLogsController < ApplicationController end def update - @case_log = CaseLog.find(params[:id]) - if @case_log.update(api_case_log_params) - render json: @case_log, status: :ok + if case_log = CaseLog.find_by(id: params[:id]) + if case_log.update(api_case_log_params) + render json: case_log, status: :ok + else + render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity + end else - render json: { errors: @case_log.errors.full_messages }, status: :unprocessable_entity + render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found end end @@ -61,7 +64,7 @@ class CaseLogsController < ApplicationController @case_log = CaseLog.find(params[:case_log_id]) current_url = request.env["PATH_INFO"] subsection = current_url.split("/")[-2] - render "form/check_answers", locals: { case_log: @case_log, subsection: subsection } + render "form/check_answers", locals: { subsection: subsection } end form = Form.new(2021, 2022) diff --git a/app/views/form/_check_answers_table.html.erb b/app/views/form/_check_answers_table.html.erb index 88567bc9c..3c5fb2d2c 100644 --- a/app/views/form/_check_answers_table.html.erb +++ b/app/views/form/_check_answers_table.html.erb @@ -4,10 +4,10 @@ <%= question_info["check_answer_label"].to_s %>
- <%= case_log[question_title] %> + <%= @case_log[question_title] %>
- <%= create_update_answer_link(case_log[question_title], case_log["id"], page)%> + <%= create_update_answer_link(@case_log[question_title], @case_log.id, page)%>
diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb index b6533b4c2..7f2d91456 100644 --- a/app/views/form/check_answers.html.erb +++ b/app/views/form/check_answers.html.erb @@ -2,11 +2,11 @@

Check the answers you gave for <%= subsection.humanize(capitalize: false) %>

- <%= display_answered_questions_summary(subsection, case_log) %> + <%= display_answered_questions_summary(subsection, @case_log) %> <% subsection_pages(subsection).each do |page, page_info| %> <% page_info["questions"].each do |question_title, question_info| %> - <% if total_questions(subsection, case_log).include?(question_title) %> - <%= render partial: 'form/check_answers_table', locals: { question_title: question_title, question_info: question_info, case_log: case_log, page: page } %> + <% if total_questions(subsection, @case_log).include?(question_title) %> + <%= render partial: 'form/check_answers_table', locals: { question_title: question_title, question_info: question_info, case_log: @case_log, page: page } %> <%end %> <%end %> <% end %> diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index 174f88de4..cb6672462 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -103,9 +103,10 @@ RSpec.describe CaseLogsController, type: :request do let(:params) do { tenant_code: "New Value" } end + let(:id) { case_log.id } before do - patch "/case_logs/#{case_log.id}", headers: headers, params: params.to_json + patch "/case_logs/#{id}", headers: headers, params: params.to_json end it "returns http success" do @@ -118,6 +119,14 @@ RSpec.describe CaseLogsController, type: :request do expect(case_log.property_postcode).to eq("Old Value") end + context "invalid case log id" do + let(:id) { (CaseLog.order(:id).last&.id || 0) + 1 } + + it "returns 404" do + expect(response).to have_http_status(:not_found) + end + end + context "request with invalid credentials" do let(:basic_credentials) do ActionController::HttpAuthentication::Basic.encode_credentials(api_username, "Oops")