|
32 | 32 | use Cake\Http\ServerRequest; |
33 | 33 | use Cake\Http\ServerRequestFactory; |
34 | 34 | use Cake\Http\Uri; |
| 35 | +use Cake\I18n\FrozenTime; |
35 | 36 | use Psr\Http\Message\RequestInterface; |
36 | 37 | use Psr\Http\Message\ResponseInterface; |
37 | 38 | use Psr\Http\Message\ServerRequestInterface; |
@@ -135,6 +136,58 @@ public function testAuthenticateWithChallengeDisabled() |
135 | 136 | $this->assertFalse($result->isValid()); |
136 | 137 | } |
137 | 138 |
|
| 139 | + /** |
| 140 | + * Integration test for session auth + identify always getting a fresh user record. |
| 141 | + * |
| 142 | + * @return void |
| 143 | + */ |
| 144 | + public function testAuthenticationWithSessionIdentify() |
| 145 | + { |
| 146 | + $users = $this->fetchTable('Users'); |
| 147 | + $user = $users->get(1); |
| 148 | + |
| 149 | + $request = ServerRequestFactory::fromGlobals([ |
| 150 | + 'SERVER_NAME' => 'example.com', |
| 151 | + 'REQUEST_URI' => '/testpath', |
| 152 | + ]); |
| 153 | + $request->getSession()->write('Auth', [ |
| 154 | + 'username' => $user->username, |
| 155 | + 'password' => $user->password, |
| 156 | + ]); |
| 157 | + |
| 158 | + $factory = function () { |
| 159 | + return new AuthenticationService([ |
| 160 | + 'identifiers' => [ |
| 161 | + 'Authentication.Password', |
| 162 | + ], |
| 163 | + 'authenticators' => [ |
| 164 | + 'Authentication.Session' => [ |
| 165 | + 'identify' => true, |
| 166 | + ], |
| 167 | + ], |
| 168 | + ]); |
| 169 | + }; |
| 170 | + $service = $factory(); |
| 171 | + $result = $service->authenticate($request); |
| 172 | + $this->assertTrue($result->isValid()); |
| 173 | + |
| 174 | + $dateValue = new FrozenTime('2022-01-01 10:11:12'); |
| 175 | + $identity = $result->getData(); |
| 176 | + $this->assertEquals($identity->username, $user->username); |
| 177 | + $this->assertNotEquals($identity->created, $dateValue); |
| 178 | + |
| 179 | + // Update the user so that we can ensure session is reading from the db. |
| 180 | + $user->created = $dateValue; |
| 181 | + $users->saveOrFail($user); |
| 182 | + |
| 183 | + $service = $factory(); |
| 184 | + $result = $service->authenticate($request); |
| 185 | + $this->assertTrue($result->isValid()); |
| 186 | + $identity = $result->getData(); |
| 187 | + $this->assertEquals($identity->username, $user->username); |
| 188 | + $this->assertEquals($identity->created, $dateValue); |
| 189 | + } |
| 190 | + |
138 | 191 | /** |
139 | 192 | * testLoadAuthenticatorException |
140 | 193 | */ |
|
0 commit comments