Browse Source

Rspec rubocop cleanup (#266)

* Devise token stubs

* Case logs controller stubs

* More devise token stubs

* And another one bites the dust

* 2 left

* All cops pass
pull/271/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
cb9c767dd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      .rubocop.yml
  2. 2
      app/controllers/users_controller.rb
  3. 3
      spec/features/form/page_routing_spec.rb
  4. 8
      spec/features/organisation_spec.rb
  5. 8
      spec/features/user_spec.rb
  6. 6
      spec/requests/auth/passwords_controller_spec.rb
  7. 4
      spec/requests/case_logs_controller_spec.rb
  8. 14
      spec/requests/form_controller_spec.rb
  9. 10
      spec/requests/users_controller_spec.rb

4
.rubocop.yml

@ -1,13 +1,13 @@
require: require:
- rubocop-performance - rubocop-performance
- rubocop-rails - rubocop-rails
# - rubocop-rspec - rubocop-rspec
inherit_gem: inherit_gem:
rubocop-govuk: rubocop-govuk:
- config/default.yml - config/default.yml
- config/rails.yml - config/rails.yml
# - config/rspec.yml - config/rspec.yml
AllCops: AllCops:
Exclude: Exclude:

2
app/controllers/users_controller.rb

@ -70,7 +70,7 @@ private
end end
def find_resource def find_resource
@user = User.find(params[:id]) @user = User.find_by(id: params[:id])
end end
def authenticate_scope! def authenticate_scope!

3
spec/features/form/page_routing_spec.rb

@ -14,10 +14,11 @@ RSpec.describe "Form Page Routing" do
) )
end end
let(:id) { case_log.id } let(:id) { case_log.id }
let(:validator) { case_log._validators[nil].first }
before do before do
RequestHelper.stub_http_requests RequestHelper.stub_http_requests
allow_any_instance_of(CaseLogValidator).to receive(:validate_pregnancy).and_return(true) allow(validator).to receive(:validate_pregnancy).and_return(true)
sign_in user sign_in user
end end

8
spec/features/organisation_spec.rb

@ -8,11 +8,13 @@ RSpec.describe "User Features" do
let(:set_password_template_id) { DeviseNotifyMailer::SET_PASSWORD_TEMPLATE_ID } let(:set_password_template_id) { DeviseNotifyMailer::SET_PASSWORD_TEMPLATE_ID }
let(:notify_client) { instance_double(Notifications::Client) } let(:notify_client) { instance_double(Notifications::Client) }
let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" } let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" }
let(:devise_notify_mailer) { DeviseNotifyMailer.new }
before do before do
allow_any_instance_of(DeviseNotifyMailer).to receive(:notify_client).and_return(notify_client) allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow_any_instance_of(DeviseNotifyMailer).to receive(:host).and_return("test.com") allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)
allow_any_instance_of(User).to receive(:set_reset_password_token).and_return(reset_password_token) allow(devise_notify_mailer).to receive(:host).and_return("test.com")
allow(Devise.token_generator).to receive(:generate).and_return(reset_password_token)
allow(notify_client).to receive(:send_email).and_return(true) allow(notify_client).to receive(:send_email).and_return(true)
sign_in user sign_in user
end end

8
spec/features/user_spec.rb

@ -6,12 +6,14 @@ RSpec.describe "User Features" do
let(:reset_password_template_id) { DeviseNotifyMailer::RESET_PASSWORD_TEMPLATE_ID } let(:reset_password_template_id) { DeviseNotifyMailer::RESET_PASSWORD_TEMPLATE_ID }
let(:notify_client) { instance_double(Notifications::Client) } let(:notify_client) { instance_double(Notifications::Client) }
let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" } let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" }
let(:devise_notify_mailer) { DeviseNotifyMailer.new }
before do before do
allow_any_instance_of(DeviseNotifyMailer).to receive(:notify_client).and_return(notify_client) allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow_any_instance_of(DeviseNotifyMailer).to receive(:host).and_return("test.com") allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)
allow(devise_notify_mailer).to receive(:host).and_return("test.com")
allow(notify_client).to receive(:send_email).and_return(true) allow(notify_client).to receive(:send_email).and_return(true)
allow_any_instance_of(User).to receive(:set_reset_password_token).and_return(reset_password_token) allow(Devise.token_generator).to receive(:generate).and_return(reset_password_token)
end end
context "when the user navigates to case logs" do context "when the user navigates to case logs" do

6
spec/requests/auth/passwords_controller_spec.rb

@ -5,9 +5,11 @@ RSpec.describe Auth::PasswordsController, type: :request do
let(:params) { { user: { email: email } } } let(:params) { { user: { email: email } } }
let(:page) { Capybara::Node::Simple.new(response.body) } let(:page) { Capybara::Node::Simple.new(response.body) }
let(:notify_client) { instance_double(Notifications::Client) } let(:notify_client) { instance_double(Notifications::Client) }
let(:devise_notify_mailer) { DeviseNotifyMailer.new }
before do before do
allow_any_instance_of(DeviseNotifyMailer).to receive(:notify_client).and_return(notify_client) allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)
allow(notify_client).to receive(:send_email).and_return(true) allow(notify_client).to receive(:send_email).and_return(true)
end end
@ -25,7 +27,7 @@ RSpec.describe Auth::PasswordsController, type: :request do
context "when a password reset is requested with an email that doesn't exist in the system" do context "when a password reset is requested with an email that doesn't exist in the system" do
before do before do
allow_any_instance_of(described_class).to receive(:is_navigational_format?).and_return(false) allow(Devise.navigational_formats).to receive(:include?).and_return(false)
end end
let(:email) { "madeup_email@test.com" } let(:email) { "madeup_email@test.com" }

4
spec/requests/case_logs_controller_spec.rb

@ -1,4 +1,5 @@
require "rails_helper" require "rails_helper"
require_relative "../request_helper"
RSpec.describe CaseLogsController, type: :request do RSpec.describe CaseLogsController, type: :request do
let(:owning_organisation) { FactoryBot.create(:organisation) } let(:owning_organisation) { FactoryBot.create(:organisation) }
@ -426,7 +427,8 @@ RSpec.describe CaseLogsController, type: :request do
context "when a case log deletion fails" do context "when a case log deletion fails" do
before do before do
allow_any_instance_of(CaseLog).to receive(:discard).and_return(false) allow(CaseLog).to receive(:find_by).and_return(case_log)
allow(case_log).to receive(:discard).and_return(false)
delete "/logs/#{id}", headers: headers delete "/logs/#{id}", headers: headers
end end

14
spec/requests/form_controller_spec.rb

@ -230,9 +230,10 @@ RSpec.describe FormController, type: :request do
Form::Question.new("tenant_code", { "type" => "text" }, nil), Form::Question.new("tenant_code", { "type" => "text" }, nil),
] ]
end end
let(:page) { case_log.form.get_page("accessibility_requirements") }
it "updates both question fields" do it "updates both question fields" do
allow_any_instance_of(Form::Page).to receive(:expected_responses).and_return(questions_for_page) allow(page).to receive(:expected_responses).and_return(questions_for_page)
post "/logs/#{case_log.id}/form", params: case_log_form_params post "/logs/#{case_log.id}/form", params: case_log_form_params
case_log.reload case_log.reload
@ -244,10 +245,7 @@ RSpec.describe FormController, type: :request do
end end
context "with conditional routing" do context "with conditional routing" do
before do let(:validator) { case_log._validators[nil].first }
allow_any_instance_of(CaseLogValidator).to receive(:validate_pregnancy).and_return(true)
end
let(:case_log_form_conditional_question_yes_params) do let(:case_log_form_conditional_question_yes_params) do
{ {
id: case_log.id, id: case_log.id,
@ -257,7 +255,6 @@ RSpec.describe FormController, type: :request do
}, },
} }
end end
let(:case_log_form_conditional_question_no_params) do let(:case_log_form_conditional_question_no_params) do
{ {
id: case_log.id, id: case_log.id,
@ -267,7 +264,6 @@ RSpec.describe FormController, type: :request do
}, },
} }
end end
let(:case_log_form_conditional_question_wchair_yes_params) do let(:case_log_form_conditional_question_wchair_yes_params) do
{ {
id: case_log.id, id: case_log.id,
@ -278,6 +274,10 @@ RSpec.describe FormController, type: :request do
} }
end end
before do
allow(validator).to receive(:validate_pregnancy).and_return(true)
end
it "routes to the appropriate conditional page based on the question answer of the current page" do it "routes to the appropriate conditional page based on the question answer of the current page" do
post "/logs/#{case_log.id}/form", params: case_log_form_conditional_question_yes_params post "/logs/#{case_log.id}/form", params: case_log_form_conditional_question_yes_params
expect(response).to redirect_to("/logs/#{case_log.id}/conditional-question-yes-page") expect(response).to redirect_to("/logs/#{case_log.id}/conditional-question-yes-page")

10
spec/requests/users_controller_spec.rb

@ -8,9 +8,11 @@ RSpec.describe UsersController, type: :request do
let(:new_value) { "new test name" } let(:new_value) { "new test name" }
let(:params) { { id: user.id, user: { name: new_value } } } let(:params) { { id: user.id, user: { name: new_value } } }
let(:notify_client) { instance_double(Notifications::Client) } let(:notify_client) { instance_double(Notifications::Client) }
let(:devise_notify_mailer) { DeviseNotifyMailer.new }
before do before do
allow_any_instance_of(DeviseNotifyMailer).to receive(:notify_client).and_return(notify_client) allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)
allow(notify_client).to receive(:send_email).and_return(true) allow(notify_client).to receive(:send_email).and_return(true)
end end
@ -84,7 +86,8 @@ RSpec.describe UsersController, type: :request do
end end
before do before do
allow_any_instance_of(User).to receive(:reset_password_sent_at).and_return(4.hours.ago) allow(User).to receive(:find_or_initialize_with_error_by).and_return(user)
allow(user).to receive(:reset_password_sent_at).and_return(4.hours.ago)
put "/users/password", headers: headers, params: params put "/users/password", headers: headers, params: params
end end
@ -197,8 +200,9 @@ RSpec.describe UsersController, type: :request do
context "when the update fails to persist" do context "when the update fails to persist" do
before do before do
allow_any_instance_of(User).to receive(:update).and_return(false)
sign_in user sign_in user
allow(User).to receive(:find_by).and_return(user)
allow(user).to receive(:update).and_return(false)
patch "/users/#{user.id}", headers: headers, params: params patch "/users/#{user.id}", headers: headers, params: params
end end

Loading…
Cancel
Save