Skip to content

Commit 66d7270

Browse files
committed
Add test to verify Auth.PasswordRehash.authenticators config path
The new test explicitly verifies that the code correctly traverses: service->authenticators()->get('Form')->getIdentifier()->get('Password') This ensures the new configuration format is properly used for password rehashing.
1 parent f341c88 commit 66d7270

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tests/TestCase/Controller/Component/LoginComponentTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function setUp(): void
4040
$this->component->initialize([]);
4141
$this->user = new \CakeDC\Users\Model\Entity\User([
4242
'id' => 'd602b053-9d10-4a1c-b05d-5674f68a1f3a',
43+
'username' => 'testuser',
4344
'email' => 'test@example.com',
4445
'password' => 'password',
4546
]);
@@ -59,4 +60,89 @@ public function testLoginRehash()
5960
$this->controller->setRequest($this->request);
6061
$this->component->handleLogin(false, false);
6162
}
63+
64+
/**
65+
* Test that password rehash uses the new authenticators config path
66+
*
67+
* This test verifies that when Auth.PasswordRehash.authenticators is configured,
68+
* the code correctly traverses: service->authenticators()->get('Form')->getIdentifier()->get('Password')
69+
*
70+
* @return void
71+
*/
72+
public function testPasswordRehashUsesAuthenticatorsConfig()
73+
{
74+
// Configure the new authenticators path (clear any deprecated identifiers config)
75+
\Cake\Core\Configure::write('Auth.PasswordRehash', [
76+
'authenticators' => [
77+
'Form' => 'Password',
78+
],
79+
]);
80+
81+
// Create a password identifier that needs rehash
82+
$passwordIdentifier = $this->getMockBuilder(\Authentication\Identifier\PasswordIdentifier::class)
83+
->onlyMethods(['needsPasswordRehash'])
84+
->getMock();
85+
// This expectation verifies the code reaches the identifier through authenticators path
86+
$passwordIdentifier->expects($this->once())
87+
->method('needsPasswordRehash')
88+
->willReturn(false); // Return false to skip the actual save
89+
90+
$identifiers = new \Authentication\Identifier\IdentifierCollection([]);
91+
$identifiers->set('Password', $passwordIdentifier);
92+
93+
// Mock Form authenticator that returns our identifier collection
94+
$formAuthenticator = $this->getMockBuilder(\Authentication\Authenticator\FormAuthenticator::class)
95+
->disableOriginalConstructor()
96+
->onlyMethods(['getIdentifier'])
97+
->getMock();
98+
// This expectation verifies getIdentifier() is called on the Form authenticator
99+
$formAuthenticator->expects($this->atLeastOnce())
100+
->method('getIdentifier')
101+
->willReturn($identifiers);
102+
103+
// Mock authenticator collection
104+
$authenticators = $this->getMockBuilder(\Authentication\Authenticator\AuthenticatorCollection::class)
105+
->disableOriginalConstructor()
106+
->onlyMethods(['has', 'get'])
107+
->getMock();
108+
// This expectation verifies the code checks for 'Form' authenticator
109+
$authenticators->expects($this->atLeastOnce())
110+
->method('has')
111+
->with('Form')
112+
->willReturn(true);
113+
// This expectation verifies the code gets the 'Form' authenticator
114+
$authenticators->expects($this->atLeastOnce())
115+
->method('get')
116+
->with('Form')
117+
->willReturn($formAuthenticator);
118+
119+
// Mock authentication service
120+
$authenticationService = $this->getMockBuilder(AuthenticationService::class)
121+
->onlyMethods(['getResult', 'authenticators', 'identifiers'])
122+
->getMock();
123+
124+
$result = new Result($this->user->toArray(), Result::SUCCESS);
125+
$authenticationService->expects($this->once())->method('getResult')->willReturn($result);
126+
// This expectation verifies authenticators() is called (new config path)
127+
$authenticationService->expects($this->atLeastOnce())
128+
->method('authenticators')
129+
->willReturn($authenticators);
130+
// identifiers() should not be called for rehash since we cleared the deprecated config
131+
$authenticationService->expects($this->never())
132+
->method('identifiers');
133+
134+
$identity = $this->getMockBuilder(Identity::class)
135+
->disableOriginalConstructor()
136+
->getMock();
137+
$identity->expects($this->once())
138+
->method('getOriginalData')
139+
->willReturn($this->user);
140+
141+
$this->request = $this->request->withAttribute('authentication', $authenticationService);
142+
$this->request = $this->request->withAttribute('identity', $identity);
143+
$this->request = $this->request->withMethod('POST');
144+
$this->controller->setRequest($this->request);
145+
146+
$this->component->handleLogin(false, false);
147+
}
62148
}

0 commit comments

Comments
 (0)