PasswordableBehaviorTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. $is = $this->User->save(null, true, array('id', 'pwd', 'pwd_repeat', 'pwd_current'));
  357. $this->assertTrue(!empty($is));
  358. $user = $this->User->get($uid);
  359. // The password is updated, the name not
  360. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  361. $this->assertSame('xyz', $user['ToolsUser']['name']);
  362. // 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,
  363. // not validating of additional whitelist fields. Validation for those will be just skipped, no matter what the behavior tries
  364. // to set.
  365. $this->User->create();
  366. $data = array(
  367. 'id' => $uid,
  368. 'name' => 'Yeah',
  369. 'pwd_current' => '123', // Obviously wrong
  370. 'pwd' => 'some', // Too short
  371. 'pwd_repeat' => 'somex' // Don't match
  372. );
  373. $this->User->set($data);
  374. // Test whitelist setting - only "password" gets auto-added, pwd, pwd_repeat etc need to be added manually
  375. // NOTE that I had to remove the code for adding those fields from the behavior (as it was not functional)
  376. // 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.
  377. $is = $this->User->save(null, true, array('id', 'name'));
  378. if ((float)Configure::version() >= 2.5) {
  379. // Validation errors triggered - as expected
  380. $this->assertFalse($is);
  381. $this->assertSame(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  382. return;
  383. }
  384. // Save is successful
  385. $this->assertTrue(!empty($is));
  386. $user = $this->User->get($uid);
  387. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  388. // The password is not updated, the name is
  389. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  390. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  391. }
  392. /**
  393. * Test cake2.4 passwordHasher feature
  394. *
  395. * @return void
  396. */
  397. public function testPasswordHasher() {
  398. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  399. $this->User->Behaviors->load('Tools.Passwordable', array(
  400. 'formField' => 'pwd',
  401. 'formFieldRepeat' => 'pwd_repeat',
  402. 'allowSame' => false,
  403. 'current' => false,
  404. 'passwordHasher' => 'Complex',
  405. ));
  406. $this->User->create();
  407. $data = array(
  408. 'pwd' => 'somepwd',
  409. 'pwd_repeat' => 'somepwd'
  410. );
  411. $this->User->set($data);
  412. $result = $this->User->save();
  413. $this->assertTrue((bool)$result);
  414. $uid = (string)$this->User->id;
  415. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  416. $this->User->create();
  417. $data = array(
  418. 'id' => $uid,
  419. 'pwd' => '123456',
  420. 'pwd_repeat' => '12345678',
  421. //'pwd_current' => '',
  422. );
  423. $this->User->set($data);
  424. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  425. $is = $this->User->save();
  426. $this->assertFalse($is);
  427. $this->User->create();
  428. $data = array(
  429. 'id' => $uid,
  430. 'pwd_current' => 'somepwdx',
  431. 'pwd' => '123456',
  432. 'pwd_repeat' => '123456'
  433. );
  434. $this->User->set($data);
  435. $is = $this->User->save();
  436. $this->assertFalse($is);
  437. $this->User->create();
  438. $data = array(
  439. 'id' => $uid,
  440. 'pwd_current' => 'somepwd',
  441. 'pwd' => '123456',
  442. 'pwd_repeat' => '123456'
  443. );
  444. $this->User->set($data);
  445. $is = $this->User->save();
  446. $this->assertTrue(!empty($is));
  447. }
  448. /**
  449. * PasswordableBehaviorTest::testBlowfish()
  450. *
  451. * @return void
  452. */
  453. public function testBlowfish() {
  454. $this->User->Behaviors->load('Tools.Passwordable', array(
  455. 'allowSame' => false,
  456. 'current' => false,
  457. 'authType' => 'Blowfish',
  458. ));
  459. $this->User->create();
  460. $data = array(
  461. 'pwd' => 'somepwd',
  462. 'pwd_repeat' => 'somepwd'
  463. );
  464. $this->User->set($data);
  465. $result = $this->User->save();
  466. $this->assertTrue((bool)$result);
  467. $uid = (string)$this->User->id;
  468. // Without the current password it will not continue
  469. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  470. $this->User->create();
  471. $data = array(
  472. 'id' => $uid,
  473. 'pwd' => '123456',
  474. 'pwd_repeat' => '12345678',
  475. );
  476. $this->User->set($data);
  477. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  478. $result = $this->User->save();
  479. $this->assertFalse($result);
  480. // Without the correct current password it will not continue
  481. $this->User->create();
  482. $data = array(
  483. 'id' => $uid,
  484. 'pwd_current' => 'somepwdx',
  485. 'pwd' => '123456',
  486. 'pwd_repeat' => '123456'
  487. );
  488. $this->User->set($data);
  489. $result = $this->User->save();
  490. $this->assertFalse($result);
  491. // Now it will
  492. $this->User->create();
  493. $data = array(
  494. 'id' => $uid,
  495. 'pwd_current' => 'somepwd',
  496. 'pwd' => '123456',
  497. 'pwd_repeat' => '123456'
  498. );
  499. $this->User->set($data);
  500. $result = $this->User->save();
  501. $this->assertTrue((bool)$result);
  502. }
  503. /**
  504. * PasswordableBehaviorTest::testSettings()
  505. *
  506. * @return void
  507. */
  508. public function testSettings() {
  509. // Pwd min and max length
  510. $this->User->Behaviors->load('Tools.Passwordable', array(
  511. 'allowSame' => false,
  512. 'current' => false,
  513. 'minLength' => 3,
  514. 'maxLength' => 6,
  515. ));
  516. $this->User->create();
  517. $data = array(
  518. 'pwd' => '123',
  519. 'pwd_repeat' => '123'
  520. );
  521. $this->User->set($data);
  522. $result = $this->User->save();
  523. $this->assertTrue((bool)$result);
  524. $uid = (string)$this->User->id;
  525. $this->User->create();
  526. $data = array(
  527. 'pwd' => '12345678',
  528. 'pwd_repeat' => '12345678'
  529. );
  530. $this->User->set($data);
  531. $result = $this->User->save();
  532. $this->assertFalse($result);
  533. $expected = array(
  534. 'pwd' => array(__('valErrBetweenCharacters %s %s', 3, 6)),
  535. 'pwd_repeat' => array(__('valErrBetweenCharacters %s %s', 3, 6))
  536. );
  537. $this->assertEquals($expected, $this->User->validationErrors);
  538. }
  539. }
  540. /**
  541. * Test component
  542. */
  543. class AuthTestComponent extends AuthComponent {
  544. }
  545. if (!class_exists('SimplePasswordHasher')) {
  546. class SimplePasswordHasher {
  547. }
  548. }
  549. class ComplexPasswordHasher extends SimplePasswordHasher {
  550. }