Skip to content

Commit b2d84f0

Browse files
committed
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
1 parent 22bf566 commit b2d84f0

File tree

10 files changed

+42
-23
lines changed

10 files changed

+42
-23
lines changed

reporting-app/.rubocop.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
plugins:
22
- rubocop-rspec
3+
- rubocop-rspec_rails
34
inherit_gem:
45
pundit: config/rubocop-rspec.yml
56
rubocop-rails-omakase: rubocop.yml
67

8+
AllCops:
9+
NewCops: disable
10+
711
RSpec/IncludeExamples:
812
Enabled: true
913
RSpec/ExampleLength:
@@ -19,5 +23,14 @@ RSpec/NoExpectationExample:
1923
- assert_
2024
- is_expected_in_block
2125

26+
RSpecRails/HaveHttpStatus:
27+
Enabled: true
28+
RSpecRails/HttpStatusNameConsistency:
29+
Enabled: true
30+
RSpecRails/InferredSpecType:
31+
Enabled: false
32+
RSpecRails/NegationBeValid:
33+
Enabled: true
34+
2235
Style/FrozenStringLiteralComment:
2336
Enabled: true

reporting-app/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ group :development do
115115

116116
# Linting
117117
gem "rubocop-rails-omakase", require: false
118+
gem "rubocop-rspec_rails", require: false
118119
gem "rubocop-rspec", require: false
119120

120121
# Test notifications locally without sending the emails

reporting-app/Gemfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ GEM
501501
rubocop-rspec (3.7.0)
502502
lint_roller (~> 1.1)
503503
rubocop (~> 1.72, >= 1.72.1)
504+
rubocop-rspec_rails (2.32.0)
505+
lint_roller (~> 1.1)
506+
rubocop (~> 1.72, >= 1.72.1)
507+
rubocop-rspec (~> 3.5)
504508
ruby-graphviz (1.2.5)
505509
rexml
506510
ruby-lsp (0.26.1)
@@ -630,6 +634,7 @@ DEPENDENCIES
630634
rspec-rails (~> 8.0.0)
631635
rubocop-rails-omakase
632636
rubocop-rspec
637+
rubocop-rspec_rails
633638
ruby-lsp
634639
ruby-lsp-rails
635640
selenium-webdriver

reporting-app/app/controllers/staff/certification_batch_uploads_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def create
3838
else
3939
message = "Failed to upload file: #{@batch_upload.errors.full_messages.join(', ')}"
4040
format.html { redirect_to new_certification_batch_upload_path, alert: message }
41-
format.json { render json: { error: message }, status: :unprocessable_entity }
41+
format.json { render json: { error: message }, status: :unprocessable_content }
4242
end
4343
end
4444
end
@@ -66,7 +66,7 @@ def process_batch
6666
if @batch_upload.processable? == false
6767
message = "This batch cannot be processed. Current status: #{@batch_upload.status}."
6868
format.html { redirect_to certification_batch_upload_path(@batch_upload), alert: message }
69-
format.json { render json: { error: message }, status: :unprocessable_entity }
69+
format.json { render json: { error: message }, status: :unprocessable_content }
7070
elsif ProcessCertificationBatchUploadJob.perform_later(@batch_upload.id)
7171
format.html { redirect_to certification_batch_uploads_path, notice: "Processing started for #{@batch_upload.filename}. Results will be available shortly." }
7272
format.json { head :accepted }

reporting-app/spec/controllers/staff/certification_batch_uploads_controller_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@
111111
expect(flash[:alert]).to eq("Failed to upload file: Filename can't be blank, File must be attached")
112112
end
113113

114-
it "returns unprocessable_entity status for JSON requests" do
114+
it "returns unprocessable_content status for JSON requests" do
115115
post :create, params: { csv_file: csv_file, locale: "en", format: :json }
116116

117-
expect(response).to have_http_status(:unprocessable_entity)
117+
expect(response).to have_http_status(:unprocessable_content)
118118
end
119119

120120
it "returns error message in JSON for JSON requests" do
@@ -346,7 +346,7 @@
346346
it "returns unprocessable entity status" do
347347
post :process_batch, params: { id: batch_upload.id, locale: "en", format: :json }
348348

349-
expect(response).to have_http_status(:unprocessable_entity)
349+
expect(response).to have_http_status(:unprocessable_content)
350350
end
351351

352352
it "returns error message in JSON" do

reporting-app/spec/controllers/users/accounts_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
locale: "en"
6565
}
6666

67-
expect(response.status).to eq(422)
67+
expect(response).to have_http_status(:unprocessable_content)
6868
end
6969
end
7070
end

reporting-app/spec/controllers/users/passwords_controller_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
locale: "en"
3636
}
3737

38-
expect(response.status).to eq(422)
38+
expect(response).to have_http_status(:unprocessable_content)
3939
end
4040

4141
it "handles auth provider errors" do
@@ -44,7 +44,7 @@
4444
locale: "en"
4545
}
4646

47-
expect(response.status).to eq(422)
47+
expect(response).to have_http_status(:unprocessable_content)
4848
end
4949

5050
it "handles submission by bots" do
@@ -53,7 +53,7 @@
5353
locale: "en"
5454
}
5555

56-
expect(response.status).to eq(422)
56+
expect(response).to have_http_status(:unprocessable_content)
5757
end
5858
end
5959

@@ -91,7 +91,7 @@
9191
locale: "en"
9292
}
9393

94-
expect(response.status).to eq(422)
94+
expect(response).to have_http_status(:unprocessable_content)
9595
end
9696

9797
it "handles auth provider errors" do
@@ -104,7 +104,7 @@
104104
locale: "en"
105105
}
106106

107-
expect(response.status).to eq(422)
107+
expect(response).to have_http_status(:unprocessable_content)
108108
end
109109

110110
it "handles submission by bots" do
@@ -118,7 +118,7 @@
118118
locale: "en"
119119
}
120120

121-
expect(response.status).to eq(422)
121+
expect(response).to have_http_status(:unprocessable_content)
122122
end
123123
end
124124
end

reporting-app/spec/controllers/users/registrations_controller_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
locale: "en"
4646
}
4747

48-
expect(response.status).to eq(422)
48+
expect(response).to have_http_status(:unprocessable_content)
4949
end
5050

5151
it "handles auth provider errors" do
@@ -57,7 +57,7 @@
5757
locale: "en"
5858
}
5959

60-
expect(response.status).to eq(422)
60+
expect(response).to have_http_status(:unprocessable_content)
6161
end
6262

6363
it "handles submission by bots" do
@@ -73,7 +73,7 @@
7373
locale: "en"
7474
}
7575

76-
expect(response.status).to eq(422)
76+
expect(response).to have_http_status(:unprocessable_content)
7777
end
7878
end
7979

@@ -107,7 +107,7 @@
107107
locale: "en"
108108
}
109109

110-
expect(response.status).to eq(422)
110+
expect(response).to have_http_status(:unprocessable_content)
111111
end
112112

113113
it "handles auth provider errors" do
@@ -119,7 +119,7 @@
119119
locale: "en"
120120
}
121121

122-
expect(response.status).to eq(422)
122+
expect(response).to have_http_status(:unprocessable_content)
123123
end
124124
end
125125
end

reporting-app/spec/controllers/users/sessions_controller_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
locale: "en"
3838
}
3939

40-
expect(response.status).to eq(422)
40+
expect(response).to have_http_status(:unprocessable_content)
4141
expect(response.body).to have_selector("h1", text: /sign in/i)
4242
expect(response.body).to have_selector(".usa-alert--error")
4343
end
@@ -108,7 +108,7 @@
108108
locale: "en"
109109
}
110110

111-
expect(response.status).to eq(422)
111+
expect(response).to have_http_status(:unprocessable_content)
112112
end
113113
end
114114

@@ -141,7 +141,7 @@
141141
locale: "en"
142142
}
143143

144-
expect(response.status).to eq(422)
144+
expect(response).to have_http_status(:unprocessable_content)
145145
expect(response.body).to have_selector(".usa-alert--error")
146146
end
147147

reporting-app/spec/forms/users/verify_account_form_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
it "requires email and code" do
1313
form = described_class.new(email: nil, code: nil)
1414

15-
expect(form).to be_invalid
15+
expect(form).not_to be_valid
1616
expect(form.errors).to be_of_kind(:email, :blank)
1717
expect(form.errors).to be_of_kind(:code, :blank)
1818
end
1919

2020
it "requires email to be a valid email" do
2121
form = described_class.new(email: "invalid-email", code: "123456")
2222

23-
expect(form).to be_invalid
23+
expect(form).not_to be_valid
2424
expect(form.errors).to be_of_kind(:email, :invalid)
2525
end
2626

2727
it "requires code to be 6 characters" do
2828
form = described_class.new(email: "[email protected]", code: "12345")
2929

30-
expect(form).to be_invalid
30+
expect(form).not_to be_valid
3131
expect(form.errors).to be_of_kind(:code, :wrong_length)
3232
end
3333
end

0 commit comments

Comments
 (0)