X Tutup
The Wayback Machine - https://web.archive.org/web/20220708022906/https://github.com/github/view_component/pull/1212
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow component tests to define the current_user #1212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cpjmcquillan
Copy link
Contributor

@cpjmcquillan cpjmcquillan commented Dec 16, 2021

Summary

Authentication gems like clearance and devise define a current_user helper
method that can be used in controllers and views to access the currently signed
in user.

When building components that depend on the currently signed in user and
delegating :current_user to the Rails helpers, it is useful to be able to
define current_user in tests.

The with_current_user helper method defines a current_user method on the
controller so that these components can be tested easily.

Other Information

Discussion a while back indicated there could be appetite for a test helper like this
in ViewComponent::TestHelpers.

@cpjmcquillan cpjmcquillan requested a review from as a code owner Dec 16, 2021
def with_current_user(user)
controller.define_singleton_method(:current_user) { user }
yield
ensure
controller.define_singleton_method(:current_user) { nil }
end
Copy link
Contributor Author

@cpjmcquillan cpjmcquillan Dec 16, 2021

Choose a reason for hiding this comment

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

I'm not convinced this is the best approach, but I'm haven't been able to think of other approaches yet that would work and don't depend on a particular testing framework.

Copy link
Member

@BlakeWilliams BlakeWilliams Jan 27, 2022

Choose a reason for hiding this comment

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

I feel like this is a great application-level helper, but I'm not sure how it fits into the framework. In the GitHub codebase we've defined our own custom method for this that is specific to our application.

Do you know if/how authentication library helper methods like Devise's sign_in helper work with ViewComponent? I think that could provide some guidance on how to move forward here.

@@ -3,6 +3,8 @@
class IntegrationExamplesController < ActionController::Base
layout false

helper_method :current_user
Copy link
Contributor Author

@cpjmcquillan cpjmcquillan Dec 16, 2021

Choose a reason for hiding this comment

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

I assumed the default test controller was an ok place to define this, and I didn't want to obfuscate the intention of the test by creating a specific controller.

Authentication gems like [clearance] and [devise] define a `current_user` helper
method that can be used in controllers and views to access the currently signed
in user.

When building components that depend on the currently signed in user and
delegating `:current_user` to the Rails helpers, it is useful to be able to
define `current_user` in tests.

The `with_current_user` helper method defines a current_user method on the
controller so that these components can be tested easily.

[clearance]: https://github.com/thoughtbot/clearance
[devise]: https://github.com/heartcombo/devise
@joelhawksley
Copy link
Contributor

@joelhawksley joelhawksley commented Dec 16, 2021

@cpjmcquillan doesn't devise already have a sign_in test helper? Is there any way we could adapt it to work with ViewComponent and avoid having to write our own test helper?

@github github deleted a comment from primer-css May 23, 2022
@cpjmcquillan
Copy link
Contributor Author

@cpjmcquillan cpjmcquillan commented May 24, 2022

Sorry I haven't found time to pick this up again yet.

Devise and Clearance both have test helpers that differ slightly in implementation. I think it would be nice if a ViewComponent test helper was authentication-gem-agnostic, but I'm not sure if that is achievable.

Ultimately some guidance in the docs with clear examples for common libraries might be sufficient.

I'll try and pick this up in the next couple of weeks.

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.

None yet

3 participants
X Tutup