Skip to content

Conversation

@javiereguiluz
Copy link
Member

Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #26839
License MIT
Doc PR (I'll update docs if this is merged)

I know this won't work for 100% of our users ... but the goal is to make life easier to most of them. Thanks!

@javiereguiluz javiereguiluz added FrameworkBundle DX DX = Developer eXperience (anything that improves the experience of using Symfony) labels Aug 1, 2019
@javiereguiluz javiereguiluz added this to the next milestone Aug 1, 2019
$this->assertNotNull($client->getCookieJar()->get('MOCKSESSID'));
}

public function getUsers()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public function getUsers()
public function getUsers(): \Generator

Copy link
Contributor

@Koc Koc Aug 1, 2019

Choose a reason for hiding this comment

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

maybe even better : iterable

Copy link
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

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

Nice addition Javier 👍

{
public function loginUser(UserInterface $user, string $firewallContext = 'main'): self
{
$token = new UsernamePasswordToken($user, null, $firewallContext, $user->getRoles());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use a token specifically designed for testing here (perhaps an object as simple as possible)? The UsernamePasswordToken is specific to certain implementation. This also means that if you don't define any roles for your user, which is a very legit case, it would never consider you as authenticated, as this is a requirement for this particular token, but not Guard for example.

I've traced back the usages of this token in conjunction with "being authenticated", as it's injected into the session, and it should accept any token, regardless of authenticator

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review! Sadly, my knowledge of the Symfony Security parts is not as good as yours ... so I don't understand your comment. I'm afraid that I'll need the full copy+pasteable code to update this pull request. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

The issue is this line:

Meaning that whenever you want to authenticate, you are forced to create a user that has a minimum of 1 role, which isn't enforced in any other tokens. One option would be to use the PostAuthenticationGuardToken, the other would be to extends the AbstractToken and implement only what's not there yet.

There's also a ConcreteToken in security/Core/Tests/Authentication/Token/AbstractTokenTest.php, this is basically what I mean with a test specific token. Perhaps it's an idea to extract this token and make it a stub for multiple tests?

@wouterj
Copy link
Member

wouterj commented Mar 7, 2020

Hi @javiereguiluz! Thanks for starting this PR, I love how you implemented this feature with a very minimal changeset.

As I would like to have this in a next Symfony release, I've continued your work in #35997 .

@javiereguiluz
Copy link
Member Author

Closing in favor of #35997, which is much better! Thanks Wouter!

fabpot added a commit that referenced this pull request Mar 11, 2020
…cted resources (javiereguiluz, wouterj)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[DX][Testing] Added a loginUser() method to test protected resources

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #26839
| License       | MIT
| Doc PR        | tbd

This finishes #32850 original description:

> I know this won't work for 100% of our users ... but the goal is to make life easier to *most* of them. Thanks!

A custom `ConcreteToken` test-object is created as suggested by @linaori, to not bind this token to any specific implementation (as other implementations aren't fully compatible with eachother).

Commits
-------

2980a68 Added special test token and implemented 'real' functional tests
f516829 [DX][Testing] Added a loginUser() method to test protected resources
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Mar 11, 2020
…cted resources (javiereguiluz, wouterj)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[DX][Testing] Added a loginUser() method to test protected resources

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #26839
| License       | MIT
| Doc PR        | tbd

This finishes symfony/symfony#32850 original description:

> I know this won't work for 100% of our users ... but the goal is to make life easier to *most* of them. Thanks!

A custom `ConcreteToken` test-object is created as suggested by @linaori, to not bind this token to any specific implementation (as other implementations aren't fully compatible with eachother).

Commits
-------

2980a680d4 Added special test token and implemented 'real' functional tests
f516829d99 [DX][Testing] Added a loginUser() method to test protected resources
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Sep 22, 2022
… (artyuum)

This PR was submitted for the 5.3 branch but it was squashed and merged into the 5.4 branch instead.

Discussion
----------

[Security] Added a note regarding the loginUser() method

As `@javiereguiluz` [stated](symfony/symfony#32850), this method won't work for 100% of Symfony users. This is my case. I'm testing some protected API routes and the firewall is using a custom authenticator that relies on a custom header called "x-api-key". After spending hours trying to understand what I did wrong in my tests, I decided to dig into the code that defines the `loginUser()` method and noticed that [it can only work with session-based authentication](https://github.com/symfony/symfony/blob/18ab810a8d6d4c17497303df17e931261d542fce/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php#L139).

This little note could have saved me some time, so I believe it could help future users as well who are in the same case as me.

<!--

If your pull request fixes a BUG, use the oldest maintained branch that contains
the bug (see https://symfony.com/releases for the list of maintained branches).

If your pull request documents a NEW FEATURE, use the same Symfony branch where
the feature was introduced (and `5.x` for features of unreleased versions).

-->

Commits
-------

4d29c99 [Security] Added a note regarding the loginUser() method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature FrameworkBundle Status: Reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants