TinyAuthorizeTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * TinyAuthorizeTest file
  4. *
  5. * 2012-11-05 ms
  6. */
  7. //App::uses('AuthComponent', 'Controller/Component');
  8. App::uses('TinyAuthorize', 'Tools.Controller/Component/Auth');
  9. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  10. App::uses('Controller', 'Controller');
  11. App::uses('ComponentCollection', 'Controller');
  12. App::uses('CakeRequest', 'Network');
  13. /**
  14. * Test case for DirectAuthentication
  15. *
  16. * @package Cake.Test.Case.Controller.Component.Auth
  17. */
  18. class TinyAuthorizeTest extends MyCakeTestCase {
  19. public $fixtures = array('core.user', 'core.auth_user');
  20. public $Collection;
  21. public $request;
  22. /**
  23. * setup
  24. *
  25. * @return void
  26. */
  27. public function setUp() {
  28. parent::setUp();
  29. //$this->Controller = new Controller();
  30. $this->Collection = new ComponentCollection();
  31. //$this->auth = new TinyAuthorize($this->Collection, array());
  32. //$User = ClassRegistry::init('User');
  33. $this->request = new CakeRequest(null, false);
  34. $aclData = <<<INI
  35. [Users]
  36. ; add = public
  37. edit = user
  38. admin_index = admin
  39. [Comments]
  40. ; index is public
  41. add,edit,delete = user
  42. * = admin
  43. [Tags]
  44. add = *
  45. public_action = public
  46. INI;
  47. file_put_contents(TMP . 'acl.ini', $aclData);
  48. $this->assertTrue(file_exists(TMP . 'acl.ini'));
  49. Configure::write('Role', array('user' => 1, 'moderator' => 2, 'admin' => 3, 'public' => -1));
  50. }
  51. public function tearDown() {
  52. unlink(TMP . 'acl.ini');
  53. parent::tearDown();
  54. }
  55. /**
  56. * test applying settings in the constructor
  57. *
  58. * @return void
  59. */
  60. public function testConstructor() {
  61. $object = new TestTinyAuthorize($this->Collection, array(
  62. 'aclModel' => 'AuthRole',
  63. 'aclKey' => 'auth_role_id',
  64. 'autoClearCache' => true,
  65. ));
  66. $this->assertEquals('AuthRole', $object->settings['aclModel']);
  67. $this->assertEquals('auth_role_id', $object->settings['aclKey']);
  68. }
  69. /**
  70. * @return void
  71. */
  72. public function testGetAcl() {
  73. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  74. $res = $object->getAcl();
  75. $expected = array(
  76. 'users' => array(
  77. 'edit' => array(1),
  78. 'admin_index' => array(3)
  79. ),
  80. 'comments' => array(
  81. 'add' => array(1),
  82. 'edit' => array(1),
  83. 'delete' => array(1),
  84. '*' => array(3),
  85. ),
  86. 'tags' => array(
  87. 'add' => array(1, 2, 3, -1),
  88. 'public_action' => array(-1)
  89. ),
  90. );
  91. $this->debug($res);
  92. $this->assertEquals($expected, $res);
  93. }
  94. /**
  95. * @return void
  96. */
  97. public function testBasicUserMethodDisallowed() {
  98. $this->request->params['controller'] = 'users';
  99. $this->request->params['action'] = 'edit';
  100. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  101. $this->assertEquals('Role', $object->settings['aclModel']);
  102. $this->assertEquals('role_id', $object->settings['aclKey']);
  103. $user = array(
  104. 'role_id' => 4,
  105. );
  106. $res = $object->authorize($user, $this->request);
  107. $this->assertFalse($res);
  108. $user = array(
  109. 'role_id' => 3,
  110. );
  111. $res = $object->authorize($user, $this->request);
  112. $this->assertFalse($res);
  113. }
  114. /**
  115. * @return void
  116. */
  117. public function testBasicUserMethodAllowed() {
  118. $this->request->params['controller'] = 'users';
  119. $this->request->params['action'] = 'edit';
  120. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  121. // single role_id field in users table
  122. $user = array(
  123. 'role_id' => 1,
  124. );
  125. $res = $object->authorize($user, $this->request);
  126. $this->assertTrue($res);
  127. $this->request->params['action'] = 'admin_index';
  128. $user = array(
  129. 'role_id' => 3,
  130. );
  131. $res = $object->authorize($user, $this->request);
  132. $this->assertTrue($res);
  133. }
  134. /**
  135. * @return void
  136. */
  137. public function testBasicUserMethodAllowedMultiRole() {
  138. $this->request->params['controller'] = 'users';
  139. $this->request->params['action'] = 'admin_index';
  140. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  141. // flat list of roles
  142. $user = array(
  143. 'Role' => array(1, 3),
  144. );
  145. $res = $object->authorize($user, $this->request);
  146. $this->assertTrue($res);
  147. // verbose role defition using the new 2.x contain param for Auth
  148. $user = array(
  149. 'Role' => array(array('id' => 1, 'RoleUser' => array()), array('id' => 3, 'RoleUser' => array())),
  150. );
  151. $res = $object->authorize($user, $this->request);
  152. $this->assertTrue($res);
  153. }
  154. /**
  155. * @return void
  156. */
  157. public function testBasicUserMethodAllowedWildcard() {
  158. $this->request->params['controller'] = 'tags';
  159. $this->request->params['action'] = 'public_action';
  160. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  161. $user = array(
  162. 'role_id' => 6,
  163. );
  164. $res = $object->authorize($user, $this->request);
  165. $this->assertTrue($res);
  166. }
  167. /**
  168. * @return void
  169. */
  170. public function testUserMethodsAllowed() {
  171. $this->request->params['controller'] = 'users';
  172. $this->request->params['action'] = 'some_action';
  173. $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
  174. $user = array(
  175. 'role_id' => 1,
  176. );
  177. $res = $object->authorize($user, $this->request);
  178. $this->assertTrue($res);
  179. $this->request->params['controller'] = 'users';
  180. $this->request->params['action'] = 'admin_index';
  181. $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
  182. $user = array(
  183. 'role_id' => 1,
  184. );
  185. $res = $object->authorize($user, $this->request);
  186. $this->assertFalse($res);
  187. $user = array(
  188. 'role_id' => 3,
  189. );
  190. $res = $object->authorize($user, $this->request);
  191. $this->assertTrue($res);
  192. }
  193. /**
  194. * Should only be used in combination with Auth->allow() to mark those as public in the acl.ini, as well.
  195. * Not necessary and certainly not recommended as acl.ini only.
  196. *
  197. * @return void
  198. */
  199. public function testBasicUserMethodAllowedPublically() {
  200. $this->request->params['controller'] = 'tags';
  201. $this->request->params['action'] = 'add';
  202. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  203. $user = array(
  204. 'role_id' => 2,
  205. );
  206. $res = $object->authorize($user, $this->request);
  207. $this->assertTrue($res);
  208. $this->request->params['controller'] = 'comments';
  209. $this->request->params['action'] = 'foo';
  210. $user = array(
  211. 'role_id' => 3,
  212. );
  213. $res = $object->authorize($user, $this->request);
  214. $this->assertTrue($res);
  215. }
  216. }
  217. class TestTinyAuthorize extends TinyAuthorize {
  218. public function matchArray() {
  219. return $this->_matchArray;
  220. }
  221. public function getAcl() {
  222. return $this->_getAcl();
  223. }
  224. protected function _getAcl($path = TMP) {
  225. return parent::_getAcl($path);
  226. }
  227. }