PasswordableBehaviorTest.php 15 KB

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