From 47293acf7899d6b631c7e134fb69a2cd9d3fdf0a Mon Sep 17 00:00:00 2001 From: Mike Giver Date: Fri, 14 Nov 2025 15:57:31 -0500 Subject: [PATCH] Add rubocop-rspec_rails gem and fix RSpec lint violations - Add rubocop-rspec_rails gem and configure in .rubocop.yml - Enable RSpecRails cops for HTTP status and validation matchers - Update HTTP status codes from :unprocessable_entity to :unprocessable_content - Replace numeric status checks (422) with semantic matcher have_http_status - Replace be_invalid with not_to be_valid for better RSpec idioms --- reporting-app/.rubocop.yml | 6 ++++++ reporting-app/Gemfile.lock | 2 +- .../staff/certification_batch_uploads_controller.rb | 4 ++-- .../certification_batch_uploads_controller_spec.rb | 6 +++--- .../controllers/users/accounts_controller_spec.rb | 2 +- .../controllers/users/passwords_controller_spec.rb | 12 ++++++------ .../users/registrations_controller_spec.rb | 10 +++++----- .../controllers/users/sessions_controller_spec.rb | 6 +++--- .../spec/forms/users/verify_account_form_spec.rb | 6 +++--- 9 files changed, 30 insertions(+), 24 deletions(-) diff --git a/reporting-app/.rubocop.yml b/reporting-app/.rubocop.yml index baaa0127..4faa1842 100644 --- a/reporting-app/.rubocop.yml +++ b/reporting-app/.rubocop.yml @@ -23,8 +23,14 @@ RSpec/NoExpectationExample: - assert_ - is_expected_in_block +RSpecRails/HaveHttpStatus: + Enabled: true RSpecRails/HttpStatusNameConsistency: Enabled: true +RSpecRails/InferredSpecType: + Enabled: false +RSpecRails/NegationBeValid: + Enabled: true Style/FrozenStringLiteralComment: Enabled: true diff --git a/reporting-app/Gemfile.lock b/reporting-app/Gemfile.lock index cd702278..c7327e66 100644 --- a/reporting-app/Gemfile.lock +++ b/reporting-app/Gemfile.lock @@ -635,9 +635,9 @@ DEPENDENCIES rspec-rails (~> 8.0.0) rubocop-rails-omakase rubocop-rspec + rubocop-rspec_rails ruby-lsp ruby-lsp-rails - rubocop-rspec_rails selenium-webdriver simplecov sprockets-rails diff --git a/reporting-app/app/controllers/staff/certification_batch_uploads_controller.rb b/reporting-app/app/controllers/staff/certification_batch_uploads_controller.rb index 282c1af3..823cbf9b 100644 --- a/reporting-app/app/controllers/staff/certification_batch_uploads_controller.rb +++ b/reporting-app/app/controllers/staff/certification_batch_uploads_controller.rb @@ -38,7 +38,7 @@ def create else message = "Failed to upload file: #{@batch_upload.errors.full_messages.join(', ')}" format.html { redirect_to new_certification_batch_upload_path, alert: message } - format.json { render json: { error: message }, status: :unprocessable_entity } + format.json { render json: { error: message }, status: :unprocessable_content } end end end @@ -74,7 +74,7 @@ def process_batch if @batch_upload.processable? == false message = "This batch cannot be processed. Current status: #{@batch_upload.status}." format.html { redirect_to certification_batch_upload_path(@batch_upload), alert: message } - format.json { render json: { error: message }, status: :unprocessable_entity } + format.json { render json: { error: message }, status: :unprocessable_content } elsif ProcessCertificationBatchUploadJob.perform_later(@batch_upload.id) format.html { redirect_to certification_batch_uploads_path, notice: "Processing started for #{@batch_upload.filename}. Results will be available shortly." } format.json { head :accepted } diff --git a/reporting-app/spec/controllers/staff/certification_batch_uploads_controller_spec.rb b/reporting-app/spec/controllers/staff/certification_batch_uploads_controller_spec.rb index a662cbd4..7814c809 100644 --- a/reporting-app/spec/controllers/staff/certification_batch_uploads_controller_spec.rb +++ b/reporting-app/spec/controllers/staff/certification_batch_uploads_controller_spec.rb @@ -111,10 +111,10 @@ expect(flash[:alert]).to eq("Failed to upload file: Filename can't be blank, File must be attached") end - it "returns unprocessable_entity status for JSON requests" do + it "returns unprocessable_content status for JSON requests" do post :create, params: { csv_file: csv_file, locale: "en", format: :json } - expect(response).to have_http_status(:unprocessable_entity) + expect(response).to have_http_status(:unprocessable_content) end it "returns error message in JSON for JSON requests" do @@ -363,7 +363,7 @@ it "returns unprocessable entity status" do post :process_batch, params: { id: batch_upload.id, locale: "en", format: :json } - expect(response).to have_http_status(:unprocessable_entity) + expect(response).to have_http_status(:unprocessable_content) end it "returns error message in JSON" do diff --git a/reporting-app/spec/controllers/users/accounts_controller_spec.rb b/reporting-app/spec/controllers/users/accounts_controller_spec.rb index b00b75e3..7c2be030 100644 --- a/reporting-app/spec/controllers/users/accounts_controller_spec.rb +++ b/reporting-app/spec/controllers/users/accounts_controller_spec.rb @@ -64,7 +64,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end end end diff --git a/reporting-app/spec/controllers/users/passwords_controller_spec.rb b/reporting-app/spec/controllers/users/passwords_controller_spec.rb index 06c8d363..9e4140c1 100644 --- a/reporting-app/spec/controllers/users/passwords_controller_spec.rb +++ b/reporting-app/spec/controllers/users/passwords_controller_spec.rb @@ -35,7 +35,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end it "handles auth provider errors" do @@ -44,7 +44,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end it "handles submission by bots" do @@ -53,7 +53,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end end @@ -91,7 +91,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end it "handles auth provider errors" do @@ -104,7 +104,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end it "handles submission by bots" do @@ -118,7 +118,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end end end diff --git a/reporting-app/spec/controllers/users/registrations_controller_spec.rb b/reporting-app/spec/controllers/users/registrations_controller_spec.rb index 240c1612..94a27dd0 100644 --- a/reporting-app/spec/controllers/users/registrations_controller_spec.rb +++ b/reporting-app/spec/controllers/users/registrations_controller_spec.rb @@ -45,7 +45,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end it "handles auth provider errors" do @@ -57,7 +57,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end it "handles submission by bots" do @@ -73,7 +73,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end end @@ -107,7 +107,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end it "handles auth provider errors" do @@ -119,7 +119,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end end end diff --git a/reporting-app/spec/controllers/users/sessions_controller_spec.rb b/reporting-app/spec/controllers/users/sessions_controller_spec.rb index b0aae3f1..ee3e48d6 100644 --- a/reporting-app/spec/controllers/users/sessions_controller_spec.rb +++ b/reporting-app/spec/controllers/users/sessions_controller_spec.rb @@ -37,7 +37,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) expect(response.body).to have_selector("h1", text: /sign in/i) expect(response.body).to have_selector(".usa-alert--error") end @@ -108,7 +108,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) end end @@ -141,7 +141,7 @@ locale: "en" } - expect(response.status).to eq(422) + expect(response).to have_http_status(:unprocessable_content) expect(response.body).to have_selector(".usa-alert--error") end diff --git a/reporting-app/spec/forms/users/verify_account_form_spec.rb b/reporting-app/spec/forms/users/verify_account_form_spec.rb index 8bc5c328..17270dbc 100644 --- a/reporting-app/spec/forms/users/verify_account_form_spec.rb +++ b/reporting-app/spec/forms/users/verify_account_form_spec.rb @@ -12,7 +12,7 @@ it "requires email and code" do form = described_class.new(email: nil, code: nil) - expect(form).to be_invalid + expect(form).not_to be_valid expect(form.errors).to be_of_kind(:email, :blank) expect(form.errors).to be_of_kind(:code, :blank) end @@ -20,14 +20,14 @@ it "requires email to be a valid email" do form = described_class.new(email: "invalid-email", code: "123456") - expect(form).to be_invalid + expect(form).not_to be_valid expect(form.errors).to be_of_kind(:email, :invalid) end it "requires code to be 6 characters" do form = described_class.new(email: "test@example.com", code: "12345") - expect(form).to be_invalid + expect(form).not_to be_valid expect(form.errors).to be_of_kind(:code, :wrong_length) end end