Skip to content

Commit 0740d33

Browse files
author
Pedro F Steimbruch
committed
Refs CakeDC#376 redirect when anonym try to freely access change password
1 parent bebcc71 commit 0740d33

3 files changed

Lines changed: 60 additions & 10 deletions

File tree

src/Controller/Traits/PasswordManagementTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function changePassword()
4343
} else {
4444
$user->id = $this->request->session()->read(Configure::read('Users.Key.Session.resetPasswordUserId'));
4545
$validatePassword = false;
46+
if (!$user->id) {
47+
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
48+
$this->redirect($this->Auth->config('loginAction'));
49+
return;
50+
}
4651
//@todo add to the documentation: list of routes used
4752
$redirect = $this->Auth->config('loginAction');
4853
}

tests/TestCase/Controller/Traits/BaseTraitTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,40 @@ public function tearDown()
8787
parent::tearDown();
8888
}
8989

90+
/**
91+
* Mock session and mock session attributes
92+
*
93+
* @return void
94+
*/
95+
protected function _mockSession($attributes)
96+
{
97+
$session = new \Cake\Network\Session();
98+
99+
foreach ($attributes as $field => $value) {
100+
$session->write($field, $value);
101+
}
102+
103+
$this->Trait->request
104+
->expects($this->any())
105+
->method('session')
106+
->willReturn($session);
107+
}
108+
90109
/**
91110
* mock request for GET
92111
*
93112
* @return void
94113
*/
95-
protected function _mockRequestGet()
114+
protected function _mockRequestGet($withSession = false)
96115
{
116+
$methods = ['is', 'referer', 'data'];
117+
118+
if ($withSession) {
119+
$methods[] = 'session';
120+
}
121+
97122
$this->Trait->request = $this->getMockBuilder('Cake\Network\Request')
98-
->setMethods(['is', 'referer', 'data'])
123+
->setMethods($methods)
99124
->getMock();
100125
$this->Trait->request->expects($this->any())
101126
->method('is')

tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,37 @@ public function testChangePasswordGetLoggedIn()
215215
*
216216
* @return void
217217
*/
218-
public function testChangePasswordGetNotLoggedIn()
218+
public function testChangePasswordGetNotLoggedInInsideResetPasswordFlow()
219219
{
220-
$this->_mockRequestGet();
220+
$this->_mockRequestGet(true);
221221
$this->_mockAuth();
222+
$this->_mockFlash();
223+
$this->_mockSession([
224+
Configure::read('Users.Key.Session.resetPasswordUserId') => '00000000-0000-0000-0000-000000000001'
225+
]);
222226
$this->Trait->expects($this->any())
223-
->method('set')
224-
->will($this->returnCallback(function ($param1, $param2 = null) {
225-
if ($param1 === 'validatePassword') {
226-
TestCase::assertEquals($param2, false);
227-
}
228-
}));
227+
->method('set')
228+
->will($this->returnCallback(function ($param1, $param2 = null) {
229+
if ($param1 === 'validatePassword') {
230+
TestCase::assertEquals($param2, false);
231+
}
232+
}));
233+
$this->Trait->changePassword();
234+
}
235+
236+
/**
237+
* test
238+
*
239+
* @return void
240+
*/
241+
public function testChangePasswordGetNotLoggedInOutsideResetPasswordFlow()
242+
{
243+
$this->_mockRequestGet();
244+
$this->_mockAuth();
245+
$this->_mockFlash();
246+
$this->Trait->Flash->expects($this->once())
247+
->method('error')
248+
->with('User was not found');
229249
$this->Trait->changePassword();
230250
}
231251

0 commit comments

Comments
 (0)