From f7b42b4f401cd2ee57d51c7370ad3662cab0d070 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 14 Oct 2021 16:02:03 +0100 Subject: [PATCH] Spec PUT method --- app/views/form/check_answers.html.erb | 2 +- spec/requests/case_log_controller_spec.rb | 45 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb index f0433f30f..f5b0fe93e 100644 --- a/app/views/form/check_answers.html.erb +++ b/app/views/form/check_answers.html.erb @@ -5,7 +5,7 @@ <%= display_answered_questions_summary(subsection, @case_log, form) %> <% form.pages_for_subsection(subsection).each do |page, page_info| %> <% page_info["questions"].each do |question_title, question_info| %> - <% if total_questions(subsection, case_log, form).include?(question_title) %> + <% if total_questions(subsection, @case_log, form).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 %> diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index cb6672462..3b08a493c 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -137,4 +137,49 @@ RSpec.describe CaseLogsController, type: :request do end end end + + # We don't really have any meaningful distinction between PUT and PATCH here since you can update some or all + # fields in both cases, and both route to #Update. Rails generally recommends PATCH as it more closely matches + # what actually happens to an ActiveRecord object and what we're doing here, but either is allowed. + describe "PUT" do + let(:case_log) do + FactoryBot.create(:case_log, :in_progress, tenant_code: "Old Value", property_postcode: "Old Value") + end + let(:params) do + { tenant_code: "New Value" } + end + let(:id) { case_log.id } + + before do + put "/case_logs/#{id}", headers: headers, params: params.to_json + end + + it "returns http success" do + expect(response).to have_http_status(:success) + end + + it "updates the case log with the given fields and keeps original values where none are passed" do + case_log.reload + expect(case_log.tenant_code).to eq("New Value") + 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") + end + + it "returns 401" do + expect(response).to have_http_status(:unauthorized) + end + end + end end