ActionsAuthorizeTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * ActionsAuthorizeTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Controller\Component\Auth;
  18. use Cake\Controller\Component\Auth\ActionsAuthorize;
  19. use Cake\Network\Request;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Class ActionsAuthorizeTest
  23. *
  24. */
  25. class ActionsAuthorizeTest extends TestCase {
  26. /**
  27. * setUp
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $this->controller = $this->getMock('Cake\Controller\Controller', array(), array(), '', false);
  34. $this->Acl = $this->getMock('Cake\Controller\Component\AclComponent', array(), array(), '', false);
  35. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  36. $this->auth = new ActionsAuthorize($this->Collection);
  37. $this->auth->config('actionPath', '/controllers');
  38. }
  39. /**
  40. * setup the mock acl.
  41. *
  42. * @return void
  43. */
  44. protected function _mockAcl() {
  45. $this->Collection->expects($this->any())
  46. ->method('load')
  47. ->with('Acl')
  48. ->will($this->returnValue($this->Acl));
  49. }
  50. /**
  51. * test failure
  52. *
  53. * @return void
  54. */
  55. public function testAuthorizeFailure() {
  56. $user = array(
  57. 'Users' => array(
  58. 'id' => 1,
  59. 'user' => 'mariano'
  60. )
  61. );
  62. $request = new Request('/posts/index');
  63. $request->addParams(array(
  64. 'plugin' => null,
  65. 'controller' => 'posts',
  66. 'action' => 'index'
  67. ));
  68. $this->_mockAcl();
  69. $this->Acl->expects($this->once())
  70. ->method('check')
  71. ->with($user, 'controllers/Posts/index')
  72. ->will($this->returnValue(false));
  73. $this->assertFalse($this->auth->authorize($user['Users'], $request));
  74. }
  75. /**
  76. * test isAuthorized working.
  77. *
  78. * @return void
  79. */
  80. public function testAuthorizeSuccess() {
  81. $user = array(
  82. 'Users' => array(
  83. 'id' => 1,
  84. 'user' => 'mariano'
  85. )
  86. );
  87. $request = new Request('/posts/index');
  88. $request->addParams(array(
  89. 'plugin' => null,
  90. 'controller' => 'posts',
  91. 'action' => 'index'
  92. ));
  93. $this->_mockAcl();
  94. $this->Acl->expects($this->once())
  95. ->method('check')
  96. ->with($user, 'controllers/Posts/index')
  97. ->will($this->returnValue(true));
  98. $this->assertTrue($this->auth->authorize($user['Users'], $request));
  99. }
  100. /**
  101. * testAuthorizeSettings
  102. *
  103. * @return void
  104. */
  105. public function testAuthorizeSettings() {
  106. $request = new Request('/posts/index');
  107. $request->addParams(array(
  108. 'plugin' => null,
  109. 'controller' => 'posts',
  110. 'action' => 'index'
  111. ));
  112. $this->_mockAcl();
  113. $this->auth->config('userModel', 'TestPlugin.AuthUser');
  114. $user = array(
  115. 'id' => 1,
  116. 'username' => 'mariano'
  117. );
  118. $expected = array('TestPlugin.AuthUser' => array('id' => 1, 'username' => 'mariano'));
  119. $this->Acl->expects($this->once())
  120. ->method('check')
  121. ->with($expected, 'controllers/Posts/index')
  122. ->will($this->returnValue(true));
  123. $this->assertTrue($this->auth->authorize($user, $request));
  124. }
  125. /**
  126. * test action()
  127. *
  128. * @return void
  129. */
  130. public function testActionMethod() {
  131. $request = new Request('/posts/index');
  132. $request->addParams(array(
  133. 'plugin' => null,
  134. 'controller' => 'posts',
  135. 'action' => 'index'
  136. ));
  137. $result = $this->auth->action($request);
  138. $this->assertEquals('controllers/Posts/index', $result);
  139. }
  140. /**
  141. * Make sure that action() doesn't create double slashes anywhere.
  142. *
  143. * @return void
  144. */
  145. public function testActionNoDoubleSlash() {
  146. $this->auth->config('actionPath', '/controllers/');
  147. $request = new Request('/posts/index', false);
  148. $request->addParams(array(
  149. 'plugin' => null,
  150. 'controller' => 'posts',
  151. 'action' => 'index'
  152. ));
  153. $result = $this->auth->action($request);
  154. $this->assertEquals('controllers/Posts/index', $result);
  155. }
  156. /**
  157. * test action() and plugins
  158. *
  159. * @return void
  160. */
  161. public function testActionWithPlugin() {
  162. $request = new Request('/debug_kit/posts/index');
  163. $request->addParams(array(
  164. 'plugin' => 'debug_kit',
  165. 'controller' => 'posts',
  166. 'action' => 'index'
  167. ));
  168. $result = $this->auth->action($request);
  169. $this->assertEquals('controllers/DebugKit/Posts/index', $result);
  170. }
  171. }