PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Acl/Test/Case/Controller/Component/Auth/TokenAuthenticateTest.php

https://github.com/kareypowell/croogo
PHP | 113 lines | 64 code | 12 blank | 37 comment | 0 complexity | a0bf71a2f24b13fd644aa252429f85c8 MD5 | raw file
  1. <?php
  2. /**
  3. * TokenAuthenticateTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Controller.Component.Auth
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('AuthComponent', 'Controller/Component');
  20. App::uses('TokenAuthenticate', 'Acl.Controller/Component/Auth');
  21. App::uses('AppModel', 'Model');
  22. App::uses('CakeRequest', 'Network');
  23. App::uses('CakeResponse', 'Network');
  24. App::uses('Controller', 'Controller');
  25. /**
  26. * Test case for FormAuthentication
  27. *
  28. * @package Cake.Test.Case.Controller.Component.Auth
  29. */
  30. class TokenAuthenticateTest extends CakeTestCase {
  31. public $fixtures = array('plugin.acl.multi_user');
  32. /**
  33. * setup
  34. *
  35. * @return void
  36. */
  37. public function setUp() {
  38. parent::setUp();
  39. $this->Collection = $this->getMock('ComponentCollection');
  40. $this->auth = new TokenAuthenticate($this->Collection, array(
  41. 'fields' => array(
  42. 'username' => 'user',
  43. 'password' => 'password',
  44. 'token' => 'token'
  45. ),
  46. 'userModel' => 'MultiUser',
  47. ));
  48. $password = Security::hash('password', null, true);
  49. $User = ClassRegistry::init('MultiUser');
  50. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  51. $this->response = $this->getMock('CakeResponse');
  52. }
  53. /**
  54. * test authenticate token as query parameter
  55. *
  56. * @return void
  57. */
  58. public function testAuthenticateTokenParameter() {
  59. $this->auth->settings['_parameter'] = 'token';
  60. $request = new CakeRequest('posts/index?_token=54321');
  61. $result = $this->auth->getUser($request, $this->response);
  62. $this->assertFalse($result);
  63. $expected = array(
  64. 'id' => '1',
  65. 'user' => 'mariano',
  66. 'email' => 'mariano@example.com',
  67. 'token' => '12345',
  68. 'created' => '2007-03-17 01:16:23',
  69. 'updated' => '2007-03-17 01:18:31'
  70. );
  71. $request = new CakeRequest('posts/index?_token=12345');
  72. $result = $this->auth->getUser($request, $this->response);
  73. $this->assertEquals($expected, $result);
  74. $this->auth->settings['parameter'] = 'tokenname';
  75. $request = new CakeRequest('posts/index?tokenname=12345');
  76. $result = $this->auth->getUser($request, $this->response);
  77. $this->assertEquals($expected, $result);
  78. }
  79. /**
  80. * test authenticate token as request header
  81. *
  82. * @return void
  83. */
  84. public function testAuthenticateTokenHeader() {
  85. $_SERVER['HTTP_X_APITOKEN'] = '54321';
  86. $request = new CakeRequest('posts/index', false);
  87. $result = $this->auth->getUser($request, $this->response);
  88. $this->assertFalse($result);
  89. $expected = array(
  90. 'id' => '1',
  91. 'user' => 'mariano',
  92. 'email' => 'mariano@example.com',
  93. 'token' => '12345',
  94. 'created' => '2007-03-17 01:16:23',
  95. 'updated' => '2007-03-17 01:18:31'
  96. );
  97. $_SERVER['HTTP_X_APITOKEN'] = '12345';
  98. $result = $this->auth->getUser($request, $this->response);
  99. $this->assertEquals($expected, $result);
  100. }
  101. }