PasswordableBehaviorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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('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. $res = $this->User->save();
  39. $this->assertTrue((bool)$res);
  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. $res = $this->User->Behaviors->attached('Passwordable');
  51. $this->assertTrue($res);
  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. * needs faking of pwd check...
  275. */
  276. public function testValidateCurrent() {
  277. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  278. $this->User->create();
  279. $data = array(
  280. 'name' => 'xyz',
  281. 'password' => Security::hash('somepwd', null, true));
  282. $res = $this->User->save($data);
  283. $this->assertTrue(!empty($res));
  284. $uid = (String)$this->User->id;
  285. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  286. $this->User->create();
  287. $data = array(
  288. 'id' => $uid,
  289. 'pwd' => '123456',
  290. 'pwd_repeat' => '12345678',
  291. //'pwd_current' => '',
  292. );
  293. $this->User->set($data);
  294. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  295. $is = $this->User->save();
  296. $this->assertFalse($is);
  297. $this->User->create();
  298. $data = array(
  299. 'id' => $uid,
  300. 'pwd_current' => 'somepwdx',
  301. 'pwd' => '123456',
  302. 'pwd_repeat' => '123456'
  303. );
  304. $this->User->set($data);
  305. $is = $this->User->save();
  306. $this->assertFalse($is);
  307. $this->User->create();
  308. $data = array(
  309. 'id' => $uid,
  310. 'pwd_current' => 'somepwd',
  311. 'pwd' => '123456',
  312. 'pwd_repeat' => '123456'
  313. );
  314. $this->User->set($data);
  315. $is = $this->User->save();
  316. $this->assertTrue(!empty($is));
  317. }
  318. /**
  319. * Test cake2.4 passwordHasher feature
  320. *
  321. * @return void
  322. */
  323. public function testPasswordHasher() {
  324. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  325. $this->User->Behaviors->load('Tools.Passwordable', array(
  326. 'formField' => 'pwd',
  327. 'formFieldRepeat' => 'pwd_repeat',
  328. 'allowSame' => false,
  329. 'current' => false,
  330. 'passwordHasher' => 'Complex',
  331. ));
  332. $this->User->create();
  333. $data = array(
  334. 'pwd' => 'somepwd',
  335. 'pwd_repeat' => 'somepwd'
  336. );
  337. $this->User->set($data);
  338. $res = $this->User->save();
  339. $this->assertTrue((bool)$res);
  340. $uid = (String)$this->User->id;
  341. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  342. $this->User->create();
  343. $data = array(
  344. 'id' => $uid,
  345. 'pwd' => '123456',
  346. 'pwd_repeat' => '12345678',
  347. //'pwd_current' => '',
  348. );
  349. $this->User->set($data);
  350. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  351. $is = $this->User->save();
  352. $this->assertFalse($is);
  353. $this->User->create();
  354. $data = array(
  355. 'id' => $uid,
  356. 'pwd_current' => 'somepwdx',
  357. 'pwd' => '123456',
  358. 'pwd_repeat' => '123456'
  359. );
  360. $this->User->set($data);
  361. $is = $this->User->save();
  362. $this->assertFalse($is);
  363. $this->User->create();
  364. $data = array(
  365. 'id' => $uid,
  366. 'pwd_current' => 'somepwd',
  367. 'pwd' => '123456',
  368. 'pwd_repeat' => '123456'
  369. );
  370. $this->User->set($data);
  371. $is = $this->User->save();
  372. $this->assertTrue(!empty($is));
  373. }
  374. /**
  375. * PasswordableBehaviorTest::testBlowfish()
  376. *
  377. * @return void
  378. */
  379. public function testBlowfish() {
  380. $this->User->Behaviors->load('Tools.Passwordable', array(
  381. 'allowSame' => false,
  382. 'current' => false,
  383. 'authType' => 'Blowfish',
  384. ));
  385. $this->User->create();
  386. $data = array(
  387. 'pwd' => 'somepwd',
  388. 'pwd_repeat' => 'somepwd'
  389. );
  390. $this->User->set($data);
  391. $res = $this->User->save();
  392. $this->assertTrue((bool)$res);
  393. $uid = (String)$this->User->id;
  394. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  395. $this->User->create();
  396. $data = array(
  397. 'id' => $uid,
  398. 'pwd' => '123456',
  399. 'pwd_repeat' => '12345678',
  400. );
  401. $this->User->set($data);
  402. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  403. $is = $this->User->save();
  404. $this->assertFalse($is);
  405. $this->User->create();
  406. $data = array(
  407. 'id' => $uid,
  408. 'pwd_current' => 'somepwdx',
  409. 'pwd' => '123456',
  410. 'pwd_repeat' => '123456'
  411. );
  412. $this->User->set($data);
  413. $is = $this->User->save();
  414. $this->assertFalse($is);
  415. $this->User->create();
  416. $data = array(
  417. 'id' => $uid,
  418. 'pwd_current' => 'somepwd',
  419. 'pwd' => '123456',
  420. 'pwd_repeat' => '123456'
  421. );
  422. $this->User->set($data);
  423. $is = $this->User->save();
  424. $this->assertTrue(!empty($is));
  425. }
  426. /**
  427. * PasswordableBehaviorTest::testSettings()
  428. *
  429. * @return void
  430. */
  431. public function testSettings() {
  432. // Pwd min and max length
  433. $this->User->Behaviors->load('Tools.Passwordable', array(
  434. 'allowSame' => false,
  435. 'current' => false,
  436. 'minLength' => 3,
  437. 'maxLength' => 6,
  438. ));
  439. $this->User->create();
  440. $data = array(
  441. 'pwd' => '123',
  442. 'pwd_repeat' => '123'
  443. );
  444. $this->User->set($data);
  445. $res = $this->User->save();
  446. $this->assertTrue((bool)$res);
  447. $uid = (String)$this->User->id;
  448. $this->User->create();
  449. $data = array(
  450. 'pwd' => '12345678',
  451. 'pwd_repeat' => '12345678'
  452. );
  453. $this->User->set($data);
  454. $res = $this->User->save();
  455. $this->assertFalse($res);
  456. $expected = array(
  457. 'pwd' => array(__('valErrBetweenCharacters %s %s', 3, 6)),
  458. 'pwd_repeat' => array(__('valErrBetweenCharacters %s %s', 3, 6))
  459. );
  460. $this->assertEquals($expected, $this->User->validationErrors);
  461. }
  462. }
  463. /**
  464. * 2011-11-03 ms
  465. */
  466. class AuthTestComponent extends AuthComponent {
  467. }
  468. if (!class_exists('SimplePasswordHasher')) {
  469. class SimplePasswordHasher {
  470. }
  471. }
  472. class ComplexPasswordHasher extends SimplePasswordHasher {
  473. }