Problem/Motivation
This is a follow-up to #3569423: Convert expectation-less test mocks to stubs - User module. While I was updating tests to use stubs instead of mocks, I became concerned that UserAuthenticationTest::testAuthenticateWithZeroPassword() isn't testing things as intended:
public function testAuthenticateWithZeroPassword(): void {
$this->testUser->expects($this->once())
->method('id')
->willReturn(2);
$this->userStorage->expects($this->once())
->method('loadByProperties')
->with(['name' => $this->username])
->willReturn([$this->testUser]);
$this->passwordService->expects($this->once())
->method('check')
->with(0, 0)
->willReturn(TRUE);
$this->assertSame(2, $this->userAuth->authenticate($this->username, 0));
}
The ->with(0, 0) near the end is a red flag that something is going wrong. The check() function takes two parameters:
- The plain-text password as entered into a login form
- The user's hashed password as stored in their account
In other words, these two parameters should not be the same! My concern is that this test hasn't been testing what we think it's testing. The function needs to be audited to ensure we have proper test coverage.
Proposed resolution
Delete UserAuthenticationTest::testAuthenticateWithZeroPassword(). A deep dive on the history of the function and the justification for removing it is written in comment #7.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3570206
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3570206-userauthtest-password-test
changes, plain diff MR !16733
Comments
Comment #4
danielvezaWent through the report and the tests and question. I put some breakpoints in
UserAuthenticationwhile the tests were running and the report is correct. I've updated the tests so they're more accurate to what they're actually trying to test.This is NR assuming tests come back green, if not I'll revisit tomorrow.
Comment #5
danielvezaLooks like something to look into there in the pipeline
Comment #6
danielvezaTests green, ready for review
Comment #7
dcam commentedThank you for working on this and verifying my claims. Unfortunately, this issue requires more than simply setting up new mocks. Someone needs to do a deep dive on these tests, find out why they were created in the first place, and make certain they are still fulfilling that purpose.
For example, I did the deep dive on
testAuthenticateWithZeroPassword(). This test was originally created becauseUserAuth::authenticate()was responsible for doing user entity lookups. For performance reasons it avoided doing the lookup if the password wasempty(). But that meant a person could never log in with a falsey value, '0'. The function would short-circuit.Fast-forward ten years.
UserAuthno longer exists. The functions ofUserAuth::authenticate()were broken up and superseded byUserAuthentication::lookupAccount()andUserAuthentication::authenticateAccount. The test was relevant to the part of the process that was taken over bylookupAccount(). So there's two issues with the test now:authenticateAccount()which isn't the part of the login process that had the problem.lookupAccount()doesn't skip the entity query based on the password value. It doesn't receive the password at all.There's no code that matches what
UserAuth::authenticate()did anymore. Through all the changes over the years people have updatedtestAuthenticateWithZeroPassword()with new services and methods thinking that they were doing the right thing. No one realized that at some point they were were testing nothing at all.In my opinion we can simply delete
testAuthenticateWithZeroPassword(). If someone disagrees, that's fine, but they need to provide suggestions for improving the test to actually check something and make the test relevant again. It was never meant to testUserAuthentication::authenticateAccount. I won't get to the others tonight because it's getting late. Feel free to pick up where I left off.Comment #8
dcam commentedI removed
testAuthenticateWithIncorrectPassword()andtestAuthenticateWithCorrectPassword()from the scope of the issue. They've both changed since the time this issue was created and the worst of their problems were fixed. They pointlessly set up user storage just to calllookupAccount(), which is tested elsewhere. So the functions could be simplified, but it would require us to do major refactorings throughout the rest of the test. They're "fine" now, so I'll leave them alone.Given my analysis of
testAuthenticateWithZeroPassword()in #7 I am proposing that we delete it. I have changed the issue summary to reflect this.Comment #9
smustgrave commentedFrom the standpoint of just deleting seems pretty straight forward. Nice analysis!
Comment #10
dcam commentedI had to update the new baseline that resulted from increasing the PHPStan level to 3. No code was changed.
Comment #11
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #12
dcam commentedIt needed a rebase.
Comment #13
mstrelan commentedNeeds a rebase again. I pressed the button on the MR but it needs to be resolved manually.
Comment #14
dcam commentedRebased after #3629689: Fix usage of with*() without expects() in mocks - round 3 was committed.
Comment #17
catchCommitted/pushed to main and 12.0.x, thanks!