PasswordableBehaviorTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <?php
  2. App::uses('ComponentCollection', 'Controller');
  3. App::uses('AuthComponent', 'Controller/Component');
  4. App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
  5. class PasswordableBehaviorTest extends CakeTestCase {
  6. public $fixtures = array(
  7. 'plugin.tools.tools_user', 'plugin.tools.role',
  8. );
  9. /**
  10. * SetUp method
  11. *
  12. * @return void
  13. */
  14. public function setUp() {
  15. parent::setUp();
  16. Configure::delete('Passwordable');
  17. Configure::write('Passwordable.auth', 'AuthTest');
  18. $this->User = ClassRegistry::init('Tools.ToolsUser');
  19. if (isset($this->User->validate['pwd'])) {
  20. unset($this->User->validate['pwd']);
  21. }
  22. if (isset($this->User->validate['pwd_repeat'])) {
  23. unset($this->User->validate['pwd_repeat']);
  24. }
  25. if (isset($this->User->validate['pwd_current'])) {
  26. unset($this->User->validate['pwd_current']);
  27. }
  28. if (isset($this->User->order)) {
  29. unset($this->User->order);
  30. }
  31. $this->User->create();
  32. $data = array(
  33. 'id' => '5',
  34. 'name' => 'admin',
  35. 'password' => Security::hash('somepwd', null, true),
  36. 'role_id' => '1'
  37. );
  38. $this->User->set($data);
  39. $result = $this->User->save();
  40. $this->assertTrue((bool)$result);
  41. Router::setRequestInfo(new CakeRequest(null, false));
  42. }
  43. /**
  44. * PasswordableBehaviorTest::testObject()
  45. *
  46. * @return void
  47. */
  48. public function testObject() {
  49. $this->User->Behaviors->load('Tools.Passwordable', array());
  50. $this->assertInstanceOf('PasswordableBehavior', $this->User->Behaviors->Passwordable);
  51. $result = $this->User->Behaviors->loaded('Passwordable');
  52. $this->assertTrue($result);
  53. }
  54. /**
  55. * Make sure validation is triggered correctly
  56. *
  57. * @return void
  58. */
  59. public function testValidate() {
  60. $this->User->Behaviors->load('Tools.Passwordable', array());
  61. $this->User->create();
  62. $data = array(
  63. 'pwd' => '123456',
  64. );
  65. $this->User->set($data);
  66. $is = $this->User->save();
  67. //debug($this->User->validationErrors);
  68. $this->assertFalse($is);
  69. $this->assertEquals(array('pwd_repeat'), array_keys($this->User->validationErrors));
  70. $this->User->create();
  71. $data = array(
  72. 'pwd' => '1234ab',
  73. 'pwd_repeat' => '123456'
  74. );
  75. $this->User->set($data);
  76. $is = $this->User->save();
  77. //debug($this->User->validationErrors);
  78. $this->assertFalse($is);
  79. $this->assertEquals(array(__('valErrPwdNotMatch')), $this->User->validationErrors['pwd_repeat']);
  80. $this->User->create();
  81. $data = array(
  82. 'pwd' => '123456',
  83. 'pwd_repeat' => '123456'
  84. );
  85. $this->User->set($data);
  86. //debug($this->User->validate);
  87. $is = $this->User->validates();
  88. $this->assertTrue(!empty($is));
  89. }
  90. /**
  91. * Test that confirm false does not require confirmation
  92. *
  93. * @return void
  94. */
  95. public function testValidateNoConfirm() {
  96. $this->User->Behaviors->load('Tools.Passwordable', array('confirm' => false));
  97. $this->User->create();
  98. $data = array(
  99. 'pwd' => '123456',
  100. );
  101. $this->User->set($data);
  102. $is = $this->User->save();
  103. //debug($is);
  104. $this->assertTrue(!empty($is));
  105. }
  106. /**
  107. * Trigger validation and update process if no values are entered but are required
  108. *
  109. * @return void
  110. */
  111. public function testValidateRequired() {
  112. $this->User->Behaviors->load('Tools.Passwordable');
  113. $this->User->create();
  114. $data = array(
  115. 'pwd' => '',
  116. 'pwd_repeat' => ''
  117. );
  118. $this->User->set($data);
  119. $is = $this->User->save();
  120. $this->assertFalse($is);
  121. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  122. }
  123. /**
  124. * Validation and update process gets skipped if no values are entered
  125. *
  126. * @return void
  127. */
  128. public function testValidateNotRequired() {
  129. $this->User->Behaviors->load('Tools.Passwordable', array('require' => false));
  130. $this->User->create();
  131. $data = array(
  132. 'name' => 'foo', // we need at least one field besides the password on CREATE
  133. 'pwd' => '',
  134. 'pwd_repeat' => ''
  135. );
  136. $this->User->set($data);
  137. $is = $this->User->save();
  138. $this->assertTrue((bool)$is);
  139. $this->assertEquals(array('name', 'id'), array_keys($is[$this->User->alias]));
  140. $id = $this->User->id;
  141. $data = array(
  142. 'id' => $id,
  143. 'pwd' => '',
  144. 'pwd_repeat' => ''
  145. );
  146. $this->User->set($data);
  147. $is = $this->User->save();
  148. $this->assertTrue((bool)$is);
  149. $this->assertEquals(array('id'), array_keys($is[$this->User->alias]));
  150. }
  151. /**
  152. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  153. *
  154. * @return void
  155. */
  156. public function testValidateEmptyWithCurrentPassword() {
  157. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  158. $this->User->create();
  159. $data = array(
  160. 'id' => '123',
  161. 'pwd' => '',
  162. 'pwd_repeat' => '',
  163. 'pwd_current' => '123456',
  164. );
  165. $this->User->set($data);
  166. $is = $this->User->save();
  167. //debug($this->User->validationErrors);
  168. $this->assertFalse($is);
  169. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  170. $this->tearDown();
  171. $this->setUp();
  172. $this->User->Behaviors->load('Tools.Passwordable', array('require' => false, 'current' => true));
  173. $this->User->create();
  174. $data = array(
  175. 'name' => 'foo',
  176. 'pwd' => '',
  177. 'pwd_repeat' => '',
  178. 'pwd_current' => '',
  179. );
  180. $is = $this->User->save($data);
  181. $this->assertTrue(!empty($is));
  182. }
  183. /**
  184. * Test aliases for field names
  185. */
  186. public function testDifferentFieldNames() {
  187. $this->User->Behaviors->load('Tools.Passwordable', array(
  188. 'formField' => 'passw',
  189. 'formFieldRepeat' => 'passw_repeat',
  190. 'formFieldCurrent' => 'passw_current',
  191. ));
  192. $this->User->create();
  193. $data = array(
  194. 'passw' => '123456',
  195. 'passw_repeat' => '123456'
  196. );
  197. $this->User->set($data);
  198. //debug($this->User->data);
  199. $is = $this->User->save();
  200. $this->assertTrue(!empty($is));
  201. }
  202. /**
  203. * Assert that allowSame false does not allow storing the same password as previously entered
  204. */
  205. public function testNotSame() {
  206. $this->User->Behaviors->load('Tools.Passwordable', array(
  207. 'formField' => 'passw',
  208. 'formFieldRepeat' => 'passw_repeat',
  209. 'formFieldCurrent' => 'passw_current',
  210. 'allowSame' => false,
  211. 'current' => true,
  212. //'userModel' => 'ToolsUser'
  213. ));
  214. $this->User->create();
  215. $data = array(
  216. 'id' => '5',
  217. 'passw_current' => 'something',
  218. 'passw' => 'somepwd',
  219. 'passw_repeat' => 'somepwd'
  220. );
  221. $this->User->set($data);
  222. $is = $this->User->save();
  223. //debug($this->User->validationErrors);
  224. $this->assertFalse($is);
  225. $this->User->create();
  226. $data = array(
  227. 'id' => '5',
  228. 'passw_current' => 'somepwd',
  229. 'passw' => 'newpwd',
  230. 'passw_repeat' => 'newpwd'
  231. );
  232. $this->User->set($data);
  233. $is = $this->User->save();
  234. $this->assertTrue(!empty($is));
  235. }
  236. /**
  237. * Assert that allowSame false does not allow storing the same password as previously entered
  238. */
  239. public function testNotSameWithoutCurrentField() {
  240. $this->User->Behaviors->load('Tools.Passwordable', array(
  241. 'formField' => 'passw',
  242. 'formFieldRepeat' => 'passw_repeat',
  243. 'allowSame' => false,
  244. 'current' => false
  245. ));
  246. $this->User->create();
  247. $data = array(
  248. 'passw' => 'somepwd',
  249. 'passw_repeat' => 'somepwd'
  250. );
  251. $this->User->set($data);
  252. $is = $this->User->save();
  253. $this->assertTrue((bool)$is);
  254. $id = $is[$this->User->alias]['id'];
  255. $this->User->create();
  256. $data = array(
  257. 'id' => $id,
  258. 'passw' => 'somepwd',
  259. 'passw_repeat' => 'somepwd'
  260. );
  261. $this->User->set($data);
  262. $is = $this->User->save();
  263. $this->assertFalse((bool)$is);
  264. $this->User->create();
  265. $data = array(
  266. 'id' => $id,
  267. 'passw' => 'newpwd',
  268. 'passw_repeat' => 'newpwd'
  269. );
  270. $this->User->set($data);
  271. $is = $this->User->save();
  272. $this->assertTrue((bool)$is);
  273. }
  274. /**
  275. * Assert that on edit it does not wrongly pass validation (require => false)
  276. */
  277. public function testRequireFalse() {
  278. $this->User->Behaviors->load('Tools.Passwordable', array(
  279. 'formField' => 'passw',
  280. 'formFieldRepeat' => 'passw_repeat',
  281. 'require' => false
  282. ));
  283. $this->User->create();
  284. $data = array(
  285. 'passw' => 'somepwd',
  286. 'passw_repeat' => 'somepwd'
  287. );
  288. $this->User->set($data);
  289. $is = $this->User->save();
  290. $this->assertTrue((bool)$is);
  291. $id = $is[$this->User->alias]['id'];
  292. $this->User->create();
  293. $data = array(
  294. 'id' => $id,
  295. 'passw' => 'somepwd2',
  296. 'passw_repeat' => ''
  297. );
  298. $this->User->set($data);
  299. $is = $this->User->save();
  300. $this->assertFalse((bool)$is);
  301. //debug($this->User->validationErrors);
  302. $this->User->create();
  303. $data = array(
  304. 'id' => $id,
  305. 'passw' => 'somepwd2',
  306. 'passw_repeat' => 'somepwd2'
  307. );
  308. $this->User->set($data);
  309. $is = $this->User->save();
  310. $this->assertTrue((bool)$is);
  311. }
  312. /**
  313. * Needs faking of pwd check...
  314. */
  315. public function testValidateCurrent() {
  316. $this->assertFalse($this->User->Behaviors->loaded('Passwordable'));
  317. $this->User->create();
  318. $data = array(
  319. 'name' => 'xyz',
  320. 'password' => Security::hash('somepwd', null, true));
  321. $result = $this->User->save($data);
  322. $this->assertTrue(!empty($result));
  323. $uid = (string)$this->User->id;
  324. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  325. $this->User->create();
  326. $data = array(
  327. 'id' => $uid,
  328. 'pwd' => '123456',
  329. 'pwd_repeat' => '12345678',
  330. //'pwd_current' => '',
  331. );
  332. $this->User->set($data);
  333. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  334. $is = $this->User->save();
  335. $this->assertFalse($is);
  336. $this->User->create();
  337. $data = array(
  338. 'id' => $uid,
  339. 'pwd_current' => 'somepwdx',
  340. 'pwd' => '123456',
  341. 'pwd_repeat' => '123456'
  342. );
  343. $this->User->set($data);
  344. $is = $this->User->save();
  345. $this->assertFalse($is);
  346. $this->User->create();
  347. $data = array(
  348. 'id' => $uid,
  349. 'name' => 'Yeah',
  350. 'pwd_current' => 'somepwd',
  351. 'pwd' => '123456',
  352. 'pwd_repeat' => '123456'
  353. );
  354. $this->User->set($data);
  355. // Test whitelist setting - only "password" needs to gets auto-added
  356. $options = array('validate' => true, 'fieldList' => array('id', 'pwd', 'pwd_repeat', 'pwd_current'));
  357. $is = $this->User->save(null, $options);
  358. $this->assertTrue(!empty($is));
  359. $user = $this->User->get($uid);
  360. // The password is updated, the name not
  361. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  362. $this->assertSame('xyz', $user['ToolsUser']['name']);
  363. // Proof that we manually need to add pwd, pwd_repeat etc due to a bug in CakePHP<=2.4 allowing behaviors to only modify saving,
  364. // not validating of additional whitelist fields. Validation for those will be just skipped, no matter what the behavior tries
  365. // to set.
  366. $this->User->create();
  367. $data = array(
  368. 'id' => $uid,
  369. 'name' => 'Yeah',
  370. 'pwd_current' => '123', // Obviously wrong
  371. 'pwd' => 'some', // Too short
  372. 'pwd_repeat' => 'somex' // Don't match
  373. );
  374. $this->User->set($data);
  375. // Test whitelist setting - only "password" gets auto-added, pwd, pwd_repeat etc need to be added manually
  376. // NOTE that I had to remove the code for adding those fields from the behavior (as it was not functional)
  377. // So of course, this won't work now as expected. But feel free to try to add them in the behavior. Results will be the same.
  378. $options = array('validate' => true, 'fieldList' => array('id', 'name'));
  379. $is = $this->User->save(null, $options);
  380. if ((float)Configure::version() >= 2.5) {
  381. // Validation errors triggered - as expected
  382. $this->assertFalse($is);
  383. $this->assertSame(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  384. return;
  385. }
  386. // Save is successful
  387. $this->assertTrue(!empty($is));
  388. $user = $this->User->get($uid);
  389. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  390. // The password is not updated, the name is
  391. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  392. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  393. }
  394. /**
  395. * Test cake2.4 passwordHasher feature
  396. *
  397. * @return void
  398. */
  399. public function testPasswordHasher() {
  400. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  401. $this->User->Behaviors->load('Tools.Passwordable', array(
  402. 'formField' => 'pwd',
  403. 'formFieldRepeat' => 'pwd_repeat',
  404. 'allowSame' => false,
  405. 'current' => false,
  406. 'passwordHasher' => 'Complex',
  407. ));
  408. $this->User->create();
  409. $data = array(
  410. 'pwd' => 'somepwd',
  411. 'pwd_repeat' => 'somepwd'
  412. );
  413. $this->User->set($data);
  414. $result = $this->User->save();
  415. $this->assertTrue((bool)$result);
  416. $uid = (string)$this->User->id;
  417. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  418. $this->User->create();
  419. $data = array(
  420. 'id' => $uid,
  421. 'pwd' => '123456',
  422. 'pwd_repeat' => '12345678',
  423. //'pwd_current' => '',
  424. );
  425. $this->User->set($data);
  426. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  427. $is = $this->User->save();
  428. $this->assertFalse($is);
  429. $this->User->create();
  430. $data = array(
  431. 'id' => $uid,
  432. 'pwd_current' => 'somepwdx',
  433. 'pwd' => '123456',
  434. 'pwd_repeat' => '123456'
  435. );
  436. $this->User->set($data);
  437. $is = $this->User->save();
  438. $this->assertFalse($is);
  439. $this->User->create();
  440. $data = array(
  441. 'id' => $uid,
  442. 'pwd_current' => 'somepwd',
  443. 'pwd' => '123456',
  444. 'pwd_repeat' => '123456'
  445. );
  446. $this->User->set($data);
  447. $is = $this->User->save();
  448. $this->assertTrue(!empty($is));
  449. }
  450. /**
  451. * PasswordableBehaviorTest::testBlowfish()
  452. *
  453. * @return void
  454. */
  455. public function testBlowfish() {
  456. $this->User->Behaviors->load('Tools.Passwordable', array(
  457. 'allowSame' => false,
  458. 'current' => false,
  459. 'authType' => 'Blowfish',
  460. ));
  461. $this->User->create();
  462. $data = array(
  463. 'pwd' => 'somepwd',
  464. 'pwd_repeat' => 'somepwd'
  465. );
  466. $this->User->set($data);
  467. $result = $this->User->save();
  468. $this->assertTrue((bool)$result);
  469. $uid = (string)$this->User->id;
  470. // Without the current password it will not continue
  471. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  472. $this->User->create();
  473. $data = array(
  474. 'id' => $uid,
  475. 'pwd' => '123456',
  476. 'pwd_repeat' => '12345678',
  477. );
  478. $this->User->set($data);
  479. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  480. $result = $this->User->save();
  481. $this->assertFalse($result);
  482. // Without the correct current password it will not continue
  483. $this->User->create();
  484. $data = array(
  485. 'id' => $uid,
  486. 'pwd_current' => 'somepwdx',
  487. 'pwd' => '123456',
  488. 'pwd_repeat' => '123456'
  489. );
  490. $this->User->set($data);
  491. $result = $this->User->save();
  492. $this->assertFalse($result);
  493. // Now it will
  494. $this->User->create();
  495. $data = array(
  496. 'id' => $uid,
  497. 'pwd_current' => 'somepwd',
  498. 'pwd' => '123456',
  499. 'pwd_repeat' => '123456'
  500. );
  501. $this->User->set($data);
  502. $result = $this->User->save();
  503. $this->assertTrue((bool)$result);
  504. }
  505. /**
  506. * PasswordableBehaviorTest::testSettings()
  507. *
  508. * @return void
  509. */
  510. public function testSettings() {
  511. // Pwd min and max length
  512. $this->User->Behaviors->load('Tools.Passwordable', array(
  513. 'allowSame' => false,
  514. 'current' => false,
  515. 'minLength' => 3,
  516. 'maxLength' => 6,
  517. ));
  518. $this->User->create();
  519. $data = array(
  520. 'pwd' => '123',
  521. 'pwd_repeat' => '123'
  522. );
  523. $this->User->set($data);
  524. $result = $this->User->save();
  525. $this->assertTrue((bool)$result);
  526. $this->User->create();
  527. $data = array(
  528. 'pwd' => '12345678',
  529. 'pwd_repeat' => '12345678'
  530. );
  531. $this->User->set($data);
  532. $result = $this->User->save();
  533. $this->assertFalse($result);
  534. $expected = array(
  535. 'pwd' => array(__('valErrBetweenCharacters %s %s', 3, 6)),
  536. 'pwd_repeat' => array(__('valErrBetweenCharacters %s %s', 3, 6))
  537. );
  538. $this->assertEquals($expected, $this->User->validationErrors);
  539. }
  540. /**
  541. * Test that validate false also works.
  542. *
  543. * @return void
  544. */
  545. public function testSaveWithValidateFalse() {
  546. $this->User->Behaviors->load('Tools.Passwordable');
  547. $this->User->create();
  548. $data = array(
  549. 'pwd' => '123',
  550. );
  551. $this->User->set($data);
  552. $result = $this->User->save(null, array('validate' => false));
  553. $this->assertTrue((bool)$result);
  554. $uid = (string)$this->User->id;
  555. $data = array(
  556. 'id' => $uid,
  557. 'pwd' => '1234'
  558. );
  559. $this->User->set($data);
  560. $result2 = $this->User->save(null, array('validate' => false));
  561. $this->assertTrue((bool)$result2);
  562. $this->assertTrue($result['ToolsUser']['password'] !== $result2['ToolsUser']['password']);
  563. }
  564. }
  565. /**
  566. * Test component
  567. */
  568. class AuthTestComponent extends AuthComponent {
  569. }
  570. if (!class_exists('SimplePasswordHasher')) {
  571. class SimplePasswordHasher {
  572. }
  573. }
  574. class ComplexPasswordHasher extends SimplePasswordHasher {
  575. }