test: replace allow_any_instance_of login stub with real OmniAuth login - #2768
Conversation
763adf9 to
0d970b0
Compare
| scenario 'A visitor must fill in all mandatory fields in order to sign up' do | ||
| member = Fabricate(:member, name: nil, surname: nil, email: nil, about_you: nil, how_you_found_us: nil, how_you_found_us_other_reason: nil) | ||
| member.update(can_log_in: true) | ||
| member.toggle!(:can_log_in) |
There was a problem hiding this comment.
Do we want to "Flip true to false or false to true and save"? OR, directly set the value to a fixed one?
The ! is good, and update! is a thing, too.
There was a problem hiding this comment.
Thanks, good catch — you are right that toggle! was leaning on the false DB default rather than stating intent.
I briefly considered update!, but it would actually raise here: setting can_log_in to true triggers the if: :can_log_in? presence validations, and the Fabricate'd member has those fields blank (which is exactly why the original update(can_log_in: true) was silently failing — it returned false instead of raising).
So I went with update_attribute(:can_log_in, true) — it sets the value explicitly to true and saves without running validations, which matches the intent. Fixup commit is pushed.
| module LoginHelpers | ||
| module LoginStub | ||
| class << self | ||
| attr_accessor :current_user |
There was a problem hiding this comment.
Minor: There exists a cattr_accessor. Blog post with interesting notes.
There was a problem hiding this comment.
Neat. Thank you for the link to the blog post, that helped me understand it!
I posted a fixup commit
olleolleolle
left a comment
There was a problem hiding this comment.
Super-nice to use the real thing in tests, for this.
|
@olleolleolle, I've addressed both comments. Once approved, I'll rebase the fixups away and fix the conflict in |
The early return for an invalid how_you_found_us selection skipped the model validations, so members only saw the selection error and had to resubmit to discover the remaining blank fields. Assign the attributes and run validations before rendering so every error shows at once. Exposed by switching feature specs from an allow_any_instance_of stub to a real OmniAuth login: the old stub reused the test's member object, leaking its stale validation errors into the render and masking this.
2f5fbda to
1f5907f
Compare
Problem
The
RSpec/AnyInstancetodo entry existed because thelogintest helper stubbedcurrent_userwithallow_any_instance_of(ApplicationController). This PR replaces the stub with a real OmniAuth login in feature specs (controller specs get a lightweightprepend-based stub instead) and fixes a latent bug the stub had been hiding.Split out from #2768's original four-commit branch; independent of the
Lint/DebuggerandNaming/PredicatePrefixPRs.What the stub was masking
Switching to a real login surfaced three failures in specs that only ever passed because of the stub:
logindidn't switch users. A secondloginin the same test hitAuthServicesController'slogged_in?branch and silently kept the old session, while the stub had simply replacedcurrent_user. The helper now visits/logoutfirst._planner_sessionis configured withexpire_after: 24.hours, so any spec that stubsTime.nowbeyond that horizon loses the session mid-test — rack-test drops the "expired" cookie. The affected shared example now logs in again after stubbing.member_joining_specpassed via stale-error leakage. The stub made the controller reuse the test's ownMemberobject, including validation errors left over from the test's own (silently failing)member.update(can_log_in: true). With a fresh record loaded from the session, the spec exposed a genuine bug:Member::DetailsController#updatereturns early when thehow_you_found_usselection is invalid, so members only ever saw that one error and had to resubmit to discover the remaining blank fields.Changes
spec/support/helpers/login_helpers.rb— real OmniAuth login for feature specs,LoginStubmodule prepended ontoApplicationControllerfor controller specs, log out before logging inspec/support/shared_examples/behaves_like_managing_workshop_attendance.rb— re-login after theTime.nowstubspec/features/member_joining_spec.rb— usetoggle!socan_log_inactually persists (the previousupdatefailed validation silently)app/controllers/member/details_controller.rb— on an invalidhow_you_found_usselection, assign attributes and run validations before rendering so all errors show at once