Skip to content

test: replace allow_any_instance_of login stub with real OmniAuth login - #2768

Merged
mroderick merged 2 commits into
masterfrom
fix/rubocop-quick-wins
Aug 4, 2026
Merged

test: replace allow_any_instance_of login stub with real OmniAuth login#2768
mroderick merged 2 commits into
masterfrom
fix/rubocop-quick-wins

Conversation

@mroderick

@mroderick mroderick commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Problem

The RSpec/AnyInstance todo entry existed because the login test helper stubbed current_user with allow_any_instance_of(ApplicationController). This PR replaces the stub with a real OmniAuth login in feature specs (controller specs get a lightweight prepend-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/Debugger and Naming/PredicatePrefix PRs.

What the stub was masking

Switching to a real login surfaced three failures in specs that only ever passed because of the stub:

  1. Double login didn't switch users. A second login in the same test hit AuthServicesController's logged_in? branch and silently kept the old session, while the stub had simply replaced current_user. The helper now visits /logout first.
  2. Time stubs expired the session cookie. _planner_session is configured with expire_after: 24.hours, so any spec that stubs Time.now beyond that horizon loses the session mid-test — rack-test drops the "expired" cookie. The affected shared example now logs in again after stubbing.
  3. member_joining_spec passed via stale-error leakage. The stub made the controller reuse the test's own Member object, 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#update returns early when the how_you_found_us selection 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, LoginStub module prepended onto ApplicationController for controller specs, log out before logging in
  • spec/support/shared_examples/behaves_like_managing_workshop_attendance.rb — re-login after the Time.now stub
  • spec/features/member_joining_spec.rb — use toggle! so can_log_in actually persists (the previous update failed validation silently)
  • app/controllers/member/details_controller.rb — on an invalid how_you_found_us selection, assign attributes and run validations before rendering so all errors show at once

@mroderick
mroderick force-pushed the fix/rubocop-quick-wins branch 5 times, most recently from 763adf9 to 0d970b0 Compare July 31, 2026 18:53
@mroderick mroderick changed the title style(rubocop): resolve four quick-win todo entries test: replace allow_any_instance_of login stub with real OmniAuth login Jul 31, 2026
@mroderick
mroderick marked this pull request as ready for review July 31, 2026 20:18
@mroderick
mroderick requested a review from olleolleolle July 31, 2026 20:18
Comment thread spec/features/member_joining_spec.rb Outdated
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread spec/support/helpers/login_helpers.rb Outdated
module LoginHelpers
module LoginStub
class << self
attr_accessor :current_user

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: There exists a cattr_accessor. Blog post with interesting notes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat. Thank you for the link to the blog post, that helped me understand it!

I posted a fixup commit

@olleolleolle olleolleolle left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super-nice to use the real thing in tests, for this.

@mroderick
mroderick requested a review from olleolleolle August 4, 2026 08:55
@mroderick

Copy link
Copy Markdown
Collaborator Author

@olleolleolle, I've addressed both comments. Once approved, I'll rebase the fixups away and fix the conflict in .rubocop_todo.yml and we can merge it

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.
@mroderick
mroderick force-pushed the fix/rubocop-quick-wins branch from 2f5fbda to 1f5907f Compare August 4, 2026 09:03
@mroderick
mroderick enabled auto-merge August 4, 2026 09:06
@mroderick
mroderick merged commit 789b7ec into master Aug 4, 2026
9 checks passed
@mroderick
mroderick deleted the fix/rubocop-quick-wins branch August 4, 2026 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants