PasswordableBehaviorTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. public function setUp() {
  13. parent::setUp();
  14. Configure::write('Passwordable.auth', 'AuthTest');
  15. $this->User = ClassRegistry::init('ToolsUser');
  16. if (isset($this->User->validate['pwd'])) {
  17. unset($this->User->validate['pwd']);
  18. }
  19. if (isset($this->User->validate['pwd_repeat'])) {
  20. unset($this->User->validate['pwd_repeat']);
  21. }
  22. if (isset($this->User->validate['pwd_current'])) {
  23. unset($this->User->validate['pwd_current']);
  24. }
  25. if (isset($this->User->order)) {
  26. unset($this->User->order);
  27. }
  28. $this->User->create();
  29. $data = array(
  30. 'id' => '5',
  31. 'name' => 'admin',
  32. 'password' => Security::hash('some', null, true),
  33. 'role_id' => '1'
  34. );
  35. $this->User->set($data);
  36. $res = $this->User->save();
  37. $this->assertTrue((bool)$res);
  38. Router::setRequestInfo(new CakeRequest(null, false));
  39. }
  40. /**
  41. * Tear-down method. Resets environment state.
  42. */
  43. public function tearDown() {
  44. unset($this->User);
  45. parent::tearDown();
  46. ClassRegistry::flush();
  47. }
  48. public function testObject() {
  49. $this->User->Behaviors->load('Tools.Passwordable', array());
  50. $this->assertInstanceOf('PasswordableBehavior', $this->User->Behaviors->Passwordable);
  51. $res = $this->User->Behaviors->attached('Passwordable');
  52. $this->assertTrue($res);
  53. }
  54. /**
  55. * make sure validation is triggered correctly
  56. */
  57. public function testValidate() {
  58. $this->User->Behaviors->load('Tools.Passwordable', array());
  59. $this->User->create();
  60. $data = array(
  61. 'pwd' => '123456',
  62. );
  63. $this->User->set($data);
  64. $is = $this->User->save();
  65. //debug($this->User->validationErrors);
  66. $this->assertFalse($is);
  67. $this->assertEquals(array('pwd_repeat'), array_keys($this->User->validationErrors));
  68. $this->User->create();
  69. $data = array(
  70. 'pwd' => '1234ab',
  71. 'pwd_repeat' => '123456'
  72. );
  73. $this->User->set($data);
  74. $is = $this->User->save();
  75. //debug($this->User->validationErrors);
  76. $this->assertFalse($is);
  77. $this->assertEquals(array(__('valErrPwdNotMatch')), $this->User->validationErrors['pwd_repeat']);
  78. $this->User->create();
  79. $data = array(
  80. 'pwd' => '123456',
  81. 'pwd_repeat' => '123456'
  82. );
  83. $this->User->set($data);
  84. //debug($this->User->validate);
  85. $is = $this->User->validates();
  86. $this->assertTrue(!empty($is));
  87. }
  88. /**
  89. * test that confirm false does not require confirmation
  90. */
  91. public function testValidateNoConfirm() {
  92. $this->User->Behaviors->load('Tools.Passwordable', array('confirm'=>false));
  93. $this->User->create();
  94. $data = array(
  95. 'pwd' => '123456',
  96. );
  97. $this->User->set($data);
  98. $is = $this->User->save();
  99. //debug($is);
  100. $this->assertTrue(!empty($is));
  101. }
  102. /**
  103. * validation and update process gets skipped if no values are entered
  104. */
  105. public function testValidateEmpty() {
  106. $this->User->Behaviors->load('Tools.Passwordable');
  107. $this->User->create();
  108. $data = array(
  109. 'pwd' => '',
  110. 'pwd_repeat' => ''
  111. );
  112. $this->User->set($data);
  113. $is = $this->User->save();
  114. //debug($this->User->validationErrors);
  115. $this->assertFalse($is);
  116. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  117. }
  118. /**
  119. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  120. *
  121. * @return void
  122. */
  123. public function testValidateEmptyWithCurrentPassword() {
  124. $this->User->Behaviors->load('Tools.Passwordable', array('current'=>true));
  125. $this->User->create();
  126. $data = array(
  127. 'id' => '123',
  128. 'pwd' => '',
  129. 'pwd_repeat' => '',
  130. 'pwd_current' => '123',
  131. );
  132. $this->User->set($data);
  133. $is = $this->User->save();
  134. //debug($this->User->validationErrors);
  135. $this->assertFalse($is);
  136. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  137. $this->tearDown();
  138. $this->setUp();
  139. $this->User->Behaviors->load('Tools.Passwordable', array('allowEmpty'=>true, 'current'=>true));
  140. $this->User->create();
  141. $data = array(
  142. 'name' => 'foo',
  143. 'pwd' => '',
  144. 'pwd_repeat' => '',
  145. 'pwd_current' => '',
  146. );
  147. $is = $this->User->save($data);
  148. $this->assertTrue(!empty($is));
  149. }
  150. /**
  151. * test aliases for field names
  152. */
  153. public function testDifferentFieldNames() {
  154. $this->User->Behaviors->load('Tools.Passwordable', array(
  155. 'formField' => 'passw',
  156. 'formFieldRepeat' => 'passw_repeat',
  157. 'formFieldCurrent' => 'passw_current',
  158. ));
  159. $this->User->create();
  160. $data = array(
  161. 'passw' => '123456',
  162. 'passw_repeat' => '123456'
  163. );
  164. $this->User->set($data);
  165. //debug($this->User->data);
  166. $is = $this->User->save();
  167. $this->assertTrue(!empty($is));
  168. }
  169. /**
  170. * assert that allowSame false does not allow storing the same password as previously entered
  171. */
  172. public function testNotSame() {
  173. $this->User->Behaviors->load('Tools.Passwordable', array(
  174. 'formField' => 'passw',
  175. 'formFieldRepeat' => 'passw_repeat',
  176. 'formFieldCurrent' => 'passw_current',
  177. 'allowSame' => false,
  178. 'current' => true,
  179. //'userModel' => 'ToolsUser'
  180. ));
  181. $this->User->create();
  182. $data = array(
  183. 'id' => '5',
  184. 'passw_current' => 'some',
  185. 'passw' => 'some',
  186. 'passw_repeat' => 'some'
  187. );
  188. $this->User->set($data);
  189. $is = $this->User->save();
  190. //debug($this->User->validationErrors);
  191. $this->assertFalse($is);
  192. $this->User->create();
  193. $data = array(
  194. 'id' => '5',
  195. 'passw_current' => 'some',
  196. 'passw' => 'new',
  197. 'passw_repeat' => 'new'
  198. );
  199. $this->User->set($data);
  200. $is = $this->User->save();
  201. $this->assertTrue(!empty($is));
  202. }
  203. /**
  204. * assert that allowSame false does not allow storing the same password as previously entered
  205. */
  206. public function testNotSameWithoutCurrentField() {
  207. $this->User->Behaviors->load('Tools.Passwordable', array(
  208. 'formField' => 'passw',
  209. 'formFieldRepeat' => 'passw_repeat',
  210. 'allowSame' => false,
  211. 'current' => false
  212. ));
  213. $this->User->create();
  214. $data = array(
  215. 'passw' => 'some',
  216. 'passw_repeat' => 'some'
  217. );
  218. $this->User->set($data);
  219. $is = $this->User->save();
  220. $this->assertTrue((bool)$is);
  221. $id = $is[$this->User->alias]['id'];
  222. $this->User->create();
  223. $data = array(
  224. 'id' => $id,
  225. 'passw' => 'some',
  226. 'passw_repeat' => 'some'
  227. );
  228. $this->User->set($data);
  229. $is = $this->User->save();
  230. $this->assertFalse((bool)$is);
  231. $this->User->create();
  232. $data = array(
  233. 'id' => $id,
  234. 'passw' => 'new',
  235. 'passw_repeat' => 'new'
  236. );
  237. $this->User->set($data);
  238. $is = $this->User->save();
  239. $this->assertTrue((bool)$is);
  240. }
  241. /**
  242. * needs faking of pwd check...
  243. */
  244. public function testValidateCurrent() {
  245. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  246. $this->User->create();
  247. $data = array(
  248. 'name' => 'xyz',
  249. 'password' => Security::hash('some', null, true));
  250. $res = $this->User->save($data);
  251. $this->assertTrue(!empty($res));
  252. $uid = (String)$this->User->id;
  253. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  254. $this->User->create();
  255. $data = array(
  256. 'id' => $uid,
  257. 'pwd' => '1234',
  258. 'pwd_repeat' => '123456',
  259. //'pwd_current' => '',
  260. );
  261. $this->User->set($data);
  262. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  263. $is = $this->User->save();
  264. $this->assertFalse($is);
  265. $this->User->create();
  266. $data = array(
  267. 'id' => $uid,
  268. 'pwd_current' => 'somex',
  269. 'pwd' => '123456',
  270. 'pwd_repeat' => '123456'
  271. );
  272. $this->User->set($data);
  273. $is = $this->User->save();
  274. $this->assertFalse($is);
  275. $this->User->create();
  276. $data = array(
  277. 'id' => $uid,
  278. 'pwd_current' => 'some',
  279. 'pwd' => '123456',
  280. 'pwd_repeat' => '123456'
  281. );
  282. $this->User->set($data);
  283. $is = $this->User->save();
  284. $this->assertTrue(!empty($is));
  285. }
  286. /**
  287. * Test cake2.4 passwordHasher feature
  288. *
  289. * @return void
  290. */
  291. public function testPasswordHasher() {
  292. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  293. $this->User->Behaviors->load('Tools.Passwordable', array(
  294. 'formField' => 'pwd',
  295. 'formFieldRepeat' => 'pwd_repeat',
  296. 'allowSame' => false,
  297. 'current' => false,
  298. 'passwordHasher' => 'Complex',
  299. ));
  300. $this->User->create();
  301. $data = array(
  302. 'pwd' => 'some',
  303. 'pwd_repeat' => 'some'
  304. );
  305. $this->User->set($data);
  306. $res = $this->User->save();
  307. $this->assertTrue((bool)$res);
  308. $uid = (String)$this->User->id;
  309. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  310. $this->User->create();
  311. $data = array(
  312. 'id' => $uid,
  313. 'pwd' => '1234',
  314. 'pwd_repeat' => '123456',
  315. //'pwd_current' => '',
  316. );
  317. $this->User->set($data);
  318. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  319. $is = $this->User->save();
  320. $this->assertFalse($is);
  321. $this->User->create();
  322. $data = array(
  323. 'id' => $uid,
  324. 'pwd_current' => 'somex',
  325. 'pwd' => '123456',
  326. 'pwd_repeat' => '123456'
  327. );
  328. $this->User->set($data);
  329. $is = $this->User->save();
  330. $this->assertFalse($is);
  331. $this->User->create();
  332. $data = array(
  333. 'id' => $uid,
  334. 'pwd_current' => 'some',
  335. 'pwd' => '123456',
  336. 'pwd_repeat' => '123456'
  337. );
  338. $this->User->set($data);
  339. $is = $this->User->save();
  340. $this->assertTrue(!empty($is));
  341. }
  342. /**
  343. * PasswordableBehaviorTest::testBlowfish()
  344. *
  345. * @return void
  346. */
  347. public function testBlowfish() {
  348. //Configure::write('Security.salt', 'Cf1f11ePArKlBJomM0F6aJ');
  349. /*
  350. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  351. $this->User->create();
  352. $data = array(
  353. 'name' => 'xyz',
  354. 'password' => Security::hash('some', 'blowfish'));
  355. $res = $this->User->save($data);
  356. $this->assertTrue(!empty($res));
  357. $uid = (String)$this->User->id;
  358. */
  359. $this->User->Behaviors->load('Tools.Passwordable', array(
  360. 'formField' => 'pwd',
  361. 'formFieldRepeat' => 'pwd_repeat',
  362. 'allowSame' => false,
  363. 'current' => false,
  364. //'userModel' => 'ToolsUser',
  365. 'authType' => 'Blowfish',
  366. ));
  367. $this->User->create();
  368. $data = array(
  369. 'pwd' => 'some',
  370. 'pwd_repeat' => 'some'
  371. );
  372. $this->User->set($data);
  373. $res = $this->User->save();
  374. $this->assertTrue((bool)$res);
  375. $uid = (String)$this->User->id;
  376. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  377. $this->User->create();
  378. $data = array(
  379. 'id' => $uid,
  380. 'pwd' => '1234',
  381. 'pwd_repeat' => '123456',
  382. //'pwd_current' => '',
  383. );
  384. $this->User->set($data);
  385. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  386. $is = $this->User->save();
  387. $this->assertFalse($is);
  388. $this->User->create();
  389. $data = array(
  390. 'id' => $uid,
  391. 'pwd_current' => 'somex',
  392. 'pwd' => '123456',
  393. 'pwd_repeat' => '123456'
  394. );
  395. $this->User->set($data);
  396. $is = $this->User->save();
  397. $this->assertFalse($is);
  398. $this->User->create();
  399. $data = array(
  400. 'id' => $uid,
  401. 'pwd_current' => 'some',
  402. 'pwd' => '123456',
  403. 'pwd_repeat' => '123456'
  404. );
  405. $this->User->set($data);
  406. $is = $this->User->save();
  407. $this->assertTrue(!empty($is));
  408. }
  409. }
  410. /**
  411. * 2011-11-03 ms
  412. */
  413. class AuthTestComponent extends AuthComponent {
  414. }
  415. if (!class_exists('SimplePasswordHasher')) {
  416. class SimplePasswordHasher {
  417. }
  418. }
  419. class ComplexPasswordHasher extends SimplePasswordHasher {
  420. }