ActionsAuthorizeTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. *
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package cake.tests.cases.libs.controller.components.auth
  13. * @since CakePHP(tm) v 2.0
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. App::uses('ActionsAuthorize', 'Controller/Component/Auth');
  17. App::uses('ComponentCollection', 'Controller');
  18. App::uses('AclComponent', 'Controller/Component');
  19. App::uses('CakeRequest', 'Network');
  20. App::uses('CakeResponse', 'Network');
  21. class ActionsAuthorizeTest extends CakeTestCase {
  22. /**
  23. * setup
  24. *
  25. * @return void
  26. */
  27. function setUp() {
  28. parent::setUp();
  29. $this->controller = $this->getMock('Controller', array(), array(), '', false);
  30. $this->Acl = $this->getMock('AclComponent', array(), array(), '', false);
  31. $this->Collection = $this->getMock('ComponentCollection');
  32. $this->auth = new ActionsAuthorize($this->Collection);
  33. $this->auth->settings['actionPath'] = '/controllers';
  34. }
  35. /**
  36. * setup the mock acl.
  37. *
  38. * @return void
  39. */
  40. protected function _mockAcl() {
  41. $this->Collection->expects($this->any())
  42. ->method('load')
  43. ->with('Acl')
  44. ->will($this->returnValue($this->Acl));
  45. }
  46. /**
  47. * test failure
  48. *
  49. * @return void
  50. */
  51. function testAuthorizeFailure() {
  52. $user = array(
  53. 'User' => array(
  54. 'id' => 1,
  55. 'user' => 'mariano'
  56. )
  57. );
  58. $request = new CakeRequest('/posts/index', false);
  59. $request->addParams(array(
  60. 'plugin' => null,
  61. 'controller' => 'posts',
  62. 'action' => 'index'
  63. ));
  64. $this->_mockAcl();
  65. $this->Acl->expects($this->once())
  66. ->method('check')
  67. ->with($user, '/controllers/Posts/index')
  68. ->will($this->returnValue(false));
  69. $this->assertFalse($this->auth->authorize($user, $request));
  70. }
  71. /**
  72. * test isAuthorized working.
  73. *
  74. * @return void
  75. */
  76. function testAuthorizeSuccess() {
  77. $user = array(
  78. 'User' => array(
  79. 'id' => 1,
  80. 'user' => 'mariano'
  81. )
  82. );
  83. $request = new CakeRequest('/posts/index', false);
  84. $request->addParams(array(
  85. 'plugin' => null,
  86. 'controller' => 'posts',
  87. 'action' => 'index'
  88. ));
  89. $this->_mockAcl();
  90. $this->Acl->expects($this->once())
  91. ->method('check')
  92. ->with($user, '/controllers/Posts/index')
  93. ->will($this->returnValue(true));
  94. $this->assertTrue($this->auth->authorize($user, $request));
  95. }
  96. /**
  97. * test action()
  98. *
  99. * @return void
  100. */
  101. function testActionMethod() {
  102. $request = new CakeRequest('/posts/index', false);
  103. $request->addParams(array(
  104. 'plugin' => null,
  105. 'controller' => 'posts',
  106. 'action' => 'index'
  107. ));
  108. $result = $this->auth->action($request);
  109. $this->assertEquals('/controllers/Posts/index', $result);
  110. }
  111. /**
  112. * test action() and plugins
  113. *
  114. * @return void
  115. */
  116. function testActionWithPlugin() {
  117. $request = new CakeRequest('/debug_kit/posts/index', false);
  118. $request->addParams(array(
  119. 'plugin' => 'debug_kit',
  120. 'controller' => 'posts',
  121. 'action' => 'index'
  122. ));
  123. $result = $this->auth->action($request);
  124. $this->assertEquals('/controllers/DebugKit/Posts/index', $result);
  125. }
  126. }